Skip to content

Commit eeb40db

Browse files
committed
Add method references CodeLens
1 parent e813de6 commit eeb40db

File tree

4 files changed

+74
-8
lines changed

4 files changed

+74
-8
lines changed

crates/rust-analyzer/src/config.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,18 @@ pub struct LensConfig {
7474
pub run: bool,
7575
pub debug: bool,
7676
pub implementations: bool,
77+
pub method_refs: bool,
7778
}
7879

7980
impl Default for LensConfig {
8081
fn default() -> Self {
81-
Self { run: true, debug: true, implementations: true }
82+
Self { run: true, debug: true, implementations: true, method_refs: true }
8283
}
8384
}
8485

8586
impl LensConfig {
86-
pub const NO_LENS: LensConfig = Self { run: false, debug: false, implementations: false };
87-
8887
pub fn any(&self) -> bool {
89-
self.implementations || self.runnable()
88+
self.implementations || self.runnable() || self.references()
9089
}
9190

9291
pub fn none(&self) -> bool {
@@ -96,6 +95,10 @@ impl LensConfig {
9695
pub fn runnable(&self) -> bool {
9796
self.run || self.debug
9897
}
98+
99+
pub fn references(&self) -> bool {
100+
self.method_refs
101+
}
99102
}
100103

101104
#[derive(Debug, Clone)]
@@ -278,6 +281,7 @@ impl Config {
278281
run: data.lens_enable && data.lens_run,
279282
debug: data.lens_enable && data.lens_debug,
280283
implementations: data.lens_enable && data.lens_implementations,
284+
method_refs: data.lens_enable && data.lens_methodReferences,
281285
};
282286

283287
if !data.linkedProjects.is_empty() {
@@ -459,10 +463,11 @@ config_data! {
459463
inlayHints_parameterHints: bool = true,
460464
inlayHints_typeHints: bool = true,
461465

462-
lens_debug: bool = true,
463-
lens_enable: bool = true,
464-
lens_implementations: bool = true,
465-
lens_run: bool = true,
466+
lens_debug: bool = true,
467+
lens_enable: bool = true,
468+
lens_implementations: bool = true,
469+
lens_run: bool = true,
470+
lens_methodReferences: bool = true,
466471

467472
linkedProjects: Vec<ManifestOrProjectJson> = Vec::new(),
468473
lruCapacity: Option<usize> = None,

crates/rust-analyzer/src/handlers.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use ide::{
1111
FileId, FilePosition, FileRange, HoverAction, HoverGotoTypeData, NavigationTarget, Query,
1212
RangeInfo, Runnable, RunnableKind, SearchScope, TextEdit,
1313
};
14+
use itertools::Itertools;
1415
use lsp_server::ErrorCode;
1516
use lsp_types::{
1617
CallHierarchyIncomingCall, CallHierarchyIncomingCallsParams, CallHierarchyItem,
@@ -952,6 +953,52 @@ pub(crate) fn handle_code_lens(
952953
}),
953954
);
954955
}
956+
957+
if snap.config.lens.references() {
958+
let ref_lenses = snap
959+
.analysis
960+
.file_structure(file_id)?
961+
.into_iter()
962+
.filter(|it| match it.kind {
963+
SyntaxKind::FN => true,
964+
_ => false,
965+
})
966+
.filter_map(|it| {
967+
let position = FilePosition { file_id, offset: it.navigation_range.start() };
968+
let scope = None; // all references
969+
970+
snap.analysis.find_all_refs(position, scope).unwrap_or(None).map(|r| {
971+
let mut lenses = Vec::new();
972+
if r.len() == 1 {
973+
// Only a declaration
974+
return lenses;
975+
}
976+
977+
let uri = to_proto::url(&snap, file_id);
978+
let range = to_proto::range(&line_index, it.node_range);
979+
let position = to_proto::position(&line_index, position.offset);
980+
981+
if snap.config.lens.method_refs {
982+
let all_locations: Vec<_> = r
983+
.references()
984+
.iter()
985+
.filter_map(|it| to_proto::location(&snap, it.file_range).ok())
986+
.collect();
987+
let title = reference_title(all_locations.len());
988+
let all_refs =
989+
show_references_command(title, &uri, position, all_locations);
990+
lenses.push(CodeLens { range, command: Some(all_refs), data: None });
991+
}
992+
993+
lenses
994+
})
995+
})
996+
.flatten()
997+
.collect_vec();
998+
999+
lenses.extend(ref_lenses);
1000+
}
1001+
9551002
Ok(Some(lenses))
9561003
}
9571004

@@ -1248,6 +1295,14 @@ fn implementation_title(count: usize) -> String {
12481295
}
12491296
}
12501297

1298+
fn reference_title(count: usize) -> String {
1299+
if count == 1 {
1300+
"1 reference".into()
1301+
} else {
1302+
format!("{} references", count)
1303+
}
1304+
}
1305+
12511306
fn show_references_command(
12521307
title: String,
12531308
uri: &lsp_types::Url,

editors/code/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,11 @@
554554
"type": "boolean",
555555
"default": true
556556
},
557+
"rust-analyzer.lens.methodReferences": {
558+
"markdownDescription": "Whether to show `Method References` lens. Only applies when `#rust-analyzer.lens.enable#` is set.",
559+
"type": "boolean",
560+
"default": true
561+
},
557562
"rust-analyzer.hoverActions.enable": {
558563
"description": "Whether to show HoverActions in Rust files.",
559564
"type": "boolean",

editors/code/src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ export class Config {
138138
run: this.get<boolean>("lens.run"),
139139
debug: this.get<boolean>("lens.debug"),
140140
implementations: this.get<boolean>("lens.implementations"),
141+
methodReferences: this.get<boolean>("lens.methodReferences"),
141142
};
142143
}
143144

0 commit comments

Comments
 (0)