Skip to content

Commit dceec3e

Browse files
bors[bot]Veykril
andauthored
Merge #9136
9136: feat: Add function references hover action r=Veykril a=Veykril ![image](https://user-images.githubusercontent.com/3757771/120811670-8422ed80-c54c-11eb-87f5-dd65c1f8ef7d.png) This is off by default as this can slow down hover messages significantly for very big projects I believe. Fixes #6590 bors r+ Co-authored-by: Lukas Wirth <[email protected]>
2 parents cd46255 + 0739431 commit dceec3e

File tree

6 files changed

+75
-1
lines changed

6 files changed

+75
-1
lines changed

crates/ide/src/hover.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use crate::{
2828
#[derive(Clone, Debug, PartialEq, Eq)]
2929
pub struct HoverConfig {
3030
pub implementations: bool,
31+
pub references: bool,
3132
pub run: bool,
3233
pub debug: bool,
3334
pub goto_type_def: bool,
@@ -38,6 +39,7 @@ pub struct HoverConfig {
3839
impl HoverConfig {
3940
pub const NO_ACTIONS: Self = Self {
4041
implementations: false,
42+
references: false,
4143
run: false,
4244
debug: false,
4345
goto_type_def: false,
@@ -46,7 +48,7 @@ impl HoverConfig {
4648
};
4749

4850
pub fn any(&self) -> bool {
49-
self.implementations || self.runnable() || self.goto_type_def
51+
self.implementations || self.references || self.runnable() || self.goto_type_def
5052
}
5153

5254
pub fn none(&self) -> bool {
@@ -62,6 +64,7 @@ impl HoverConfig {
6264
pub enum HoverAction {
6365
Runnable(Runnable),
6466
Implementation(FilePosition),
67+
Reference(FilePosition),
6568
GoToType(Vec<HoverGotoTypeData>),
6669
}
6770

@@ -148,6 +151,10 @@ pub(crate) fn hover(
148151
res.actions.push(action);
149152
}
150153

154+
if let Some(action) = show_fn_references_action(db, definition) {
155+
res.actions.push(action);
156+
}
157+
151158
if let Some(action) = runnable_action(&sema, definition, position.file_id) {
152159
res.actions.push(action);
153160
}
@@ -211,6 +218,18 @@ fn show_implementations_action(db: &RootDatabase, def: Definition) -> Option<Hov
211218
adt.try_to_nav(db).map(to_action)
212219
}
213220

221+
fn show_fn_references_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
222+
match def {
223+
Definition::ModuleDef(ModuleDef::Function(it)) => it.try_to_nav(db).map(|nav_target| {
224+
HoverAction::Reference(FilePosition {
225+
file_id: nav_target.file_id,
226+
offset: nav_target.focus_or_full_range().start(),
227+
})
228+
}),
229+
_ => None,
230+
}
231+
}
232+
214233
fn runnable_action(
215234
sema: &Semantics<RootDatabase>,
216235
def: Definition,
@@ -2377,6 +2396,14 @@ fn foo_$0test() {}
23772396
"#,
23782397
expect![[r#"
23792398
[
2399+
Reference(
2400+
FilePosition {
2401+
file_id: FileId(
2402+
0,
2403+
),
2404+
offset: 11,
2405+
},
2406+
),
23802407
Runnable(
23812408
Runnable {
23822409
nav: NavigationTarget {

crates/rust-analyzer/src/config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ config_data! {
152152
/// Whether to show `Implementations` action. Only applies when
153153
/// `#rust-analyzer.hoverActions.enable#` is set.
154154
hoverActions_implementations: bool = "true",
155+
/// Whether to show `References` action. Only applies when
156+
/// `#rust-analyzer.hoverActions.enable#` is set.
157+
hoverActions_references: bool = "false",
155158
/// Whether to show `Run` action. Only applies when
156159
/// `#rust-analyzer.hoverActions.enable#` is set.
157160
hoverActions_run: bool = "true",
@@ -719,6 +722,7 @@ impl Config {
719722
HoverConfig {
720723
implementations: self.data.hoverActions_enable
721724
&& self.data.hoverActions_implementations,
725+
references: self.data.hoverActions_enable && self.data.hoverActions_references,
722726
run: self.data.hoverActions_enable && self.data.hoverActions_run,
723727
debug: self.data.hoverActions_enable && self.data.hoverActions_debug,
724728
goto_type_def: self.data.hoverActions_enable && self.data.hoverActions_gotoTypeDef,

crates/rust-analyzer/src/handlers.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,6 +1506,36 @@ fn show_impl_command_link(
15061506
None
15071507
}
15081508

1509+
fn show_ref_command_link(
1510+
snap: &GlobalStateSnapshot,
1511+
position: &FilePosition,
1512+
) -> Option<lsp_ext::CommandLinkGroup> {
1513+
if snap.config.hover().implementations {
1514+
if let Some(ref_search_res) = snap.analysis.find_all_refs(*position, None).unwrap_or(None) {
1515+
let uri = to_proto::url(snap, position.file_id);
1516+
let line_index = snap.file_line_index(position.file_id).ok()?;
1517+
let position = to_proto::position(&line_index, position.offset);
1518+
let locations: Vec<_> = ref_search_res
1519+
.references
1520+
.into_iter()
1521+
.flat_map(|(file_id, ranges)| {
1522+
ranges.into_iter().filter_map(move |(range, _)| {
1523+
to_proto::location(snap, FileRange { file_id, range }).ok()
1524+
})
1525+
})
1526+
.collect();
1527+
let title = to_proto::reference_title(locations.len());
1528+
let command = to_proto::command::show_references(title, &uri, position, locations);
1529+
1530+
return Some(lsp_ext::CommandLinkGroup {
1531+
commands: vec![to_command_link(command, "Go to references".into())],
1532+
..Default::default()
1533+
});
1534+
}
1535+
}
1536+
None
1537+
}
1538+
15091539
fn runnable_action_links(
15101540
snap: &GlobalStateSnapshot,
15111541
runnable: Runnable,
@@ -1566,6 +1596,7 @@ fn prepare_hover_actions(
15661596
.iter()
15671597
.filter_map(|it| match it {
15681598
HoverAction::Implementation(position) => show_impl_command_link(snap, position),
1599+
HoverAction::Reference(position) => show_ref_command_link(snap, position),
15691600
HoverAction::Runnable(r) => runnable_action_links(snap, r.clone()),
15701601
HoverAction::GoToType(targets) => goto_type_action_links(snap, targets),
15711602
})

docs/user/generated_config.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,12 @@ Whether to show `Go to Type Definition` action. Only applies when
228228
Whether to show `Implementations` action. Only applies when
229229
`#rust-analyzer.hoverActions.enable#` is set.
230230
--
231+
[[rust-analyzer.hoverActions.references]]rust-analyzer.hoverActions.references (default: `false`)::
232+
+
233+
--
234+
Whether to show `References` action. Only applies when
235+
`#rust-analyzer.hoverActions.enable#` is set.
236+
--
231237
[[rust-analyzer.hoverActions.run]]rust-analyzer.hoverActions.run (default: `true`)::
232238
+
233239
--

editors/code/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,11 @@
660660
"default": true,
661661
"type": "boolean"
662662
},
663+
"rust-analyzer.hoverActions.references": {
664+
"markdownDescription": "Whether to show `References` action. Only applies when\n`#rust-analyzer.hoverActions.enable#` is set.",
665+
"default": false,
666+
"type": "boolean"
667+
},
663668
"rust-analyzer.hoverActions.run": {
664669
"markdownDescription": "Whether to show `Run` action. Only applies when\n`#rust-analyzer.hoverActions.enable#` is set.",
665670
"default": true,

editors/code/src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ export class Config {
165165
return {
166166
enable: this.get<boolean>("hoverActions.enable"),
167167
implementations: this.get<boolean>("hoverActions.implementations"),
168+
references: this.get<boolean>("hoverActions.references"),
168169
run: this.get<boolean>("hoverActions.run"),
169170
debug: this.get<boolean>("hoverActions.debug"),
170171
gotoTypeDef: this.get<boolean>("hoverActions.gotoTypeDef"),

0 commit comments

Comments
 (0)