Skip to content

Commit 5ff9924

Browse files
Merge #10447
10447: Add enum variant references CodeLens. r=Veykril a=ericsampson Co-authored-by: Eric Sampson <[email protected]>
2 parents c409cf0 + 6d05b07 commit 5ff9924

File tree

5 files changed

+61
-13
lines changed

5 files changed

+61
-13
lines changed

crates/ide/src/annotations.rs

+38-7
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub struct AnnotationConfig {
4040
pub annotate_impls: bool,
4141
pub annotate_references: bool,
4242
pub annotate_method_references: bool,
43+
pub annotate_enum_variant_references: bool,
4344
}
4445

4546
pub(crate) fn annotations(
@@ -63,18 +64,33 @@ pub(crate) fn annotations(
6364

6465
visit_file_defs(&Semantics::new(db), file_id, &mut |def| match def {
6566
Either::Left(def) => {
66-
let range = match def {
67+
let (range, ranges_enum_variants) = match def {
6768
hir::ModuleDef::Const(konst) => {
68-
konst.source(db).and_then(|node| name_range(&node, file_id))
69+
(konst.source(db).and_then(|node| name_range(&node, file_id)), vec![])
6970
}
7071
hir::ModuleDef::Trait(trait_) => {
71-
trait_.source(db).and_then(|node| name_range(&node, file_id))
72+
(trait_.source(db).and_then(|node| name_range(&node, file_id)), vec![])
7273
}
73-
hir::ModuleDef::Adt(adt) => {
74-
adt.source(db).and_then(|node| name_range(&node, file_id))
75-
}
76-
_ => None,
74+
hir::ModuleDef::Adt(adt) => match adt {
75+
hir::Adt::Enum(enum_) => (
76+
enum_.source(db).and_then(|node| name_range(&node, file_id)),
77+
if config.annotate_enum_variant_references {
78+
enum_
79+
.variants(db)
80+
.into_iter()
81+
.map(|variant| {
82+
variant.source(db).and_then(|node| name_range(&node, file_id))
83+
})
84+
.collect()
85+
} else {
86+
vec![]
87+
},
88+
),
89+
_ => (adt.source(db).and_then(|node| name_range(&node, file_id)), vec![]),
90+
},
91+
_ => (None, vec![]),
7792
};
93+
7894
let (range, offset) = match range {
7995
Some(range) => (range, range.start()),
8096
None => return,
@@ -99,6 +115,20 @@ pub(crate) fn annotations(
99115
});
100116
}
101117

118+
if config.annotate_enum_variant_references {
119+
for range_enum_variant in
120+
ranges_enum_variants.into_iter().filter_map(std::convert::identity)
121+
{
122+
annotations.push(Annotation {
123+
range: range_enum_variant,
124+
kind: AnnotationKind::HasReferences {
125+
position: FilePosition { file_id, offset: range_enum_variant.start() },
126+
data: None,
127+
},
128+
});
129+
}
130+
}
131+
102132
fn name_range<T: HasName>(node: &InFile<T>, file_id: FileId) -> Option<TextRange> {
103133
if node.file_id == file_id.into() {
104134
node.value.name().map(|it| it.syntax().text_range())
@@ -173,6 +203,7 @@ mod tests {
173203
annotate_impls: true,
174204
annotate_references: true,
175205
annotate_method_references: true,
206+
annotate_enum_variant_references: true,
176207
},
177208
file_id,
178209
)

crates/rust-analyzer/src/config.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,12 @@ config_data! {
227227
/// Whether to show `Method References` lens. Only applies when
228228
/// `#rust-analyzer.lens.enable#` is set.
229229
lens_methodReferences: bool = "false",
230-
/// Whether to show `References` lens. Only applies when
231-
/// `#rust-analyzer.lens.enable#` is set.
230+
/// Whether to show `References` lens for Struct, Enum, Union and Trait.
231+
/// Only applies when `#rust-analyzer.lens.enable#` is set.
232232
lens_references: bool = "false",
233+
/// Whether to show `References` lens for Enum Variants.
234+
/// Only applies when `#rust-analyzer.lens.enable#` is set.
235+
lens_enumVariantReferences: bool = "false",
233236
/// Internal config: use custom client-side commands even when the
234237
/// client doesn't set the corresponding capability.
235238
lens_forceCustomCommands: bool = "true",
@@ -326,6 +329,7 @@ pub struct LensConfig {
326329
pub implementations: bool,
327330
pub method_refs: bool,
328331
pub refs: bool, // for Struct, Enum, Union and Trait
332+
pub enum_variant_refs: bool,
329333
}
330334

331335
impl LensConfig {
@@ -342,7 +346,7 @@ impl LensConfig {
342346
}
343347

344348
pub fn references(&self) -> bool {
345-
self.method_refs || self.refs
349+
self.method_refs || self.refs || self.enum_variant_refs
346350
}
347351
}
348352

@@ -832,6 +836,7 @@ impl Config {
832836
implementations: self.data.lens_enable && self.data.lens_implementations,
833837
method_refs: self.data.lens_enable && self.data.lens_methodReferences,
834838
refs: self.data.lens_enable && self.data.lens_references,
839+
enum_variant_refs: self.data.lens_enable && self.data.lens_enumVariantReferences,
835840
}
836841
}
837842
pub fn hover_actions(&self) -> HoverActionsConfig {

crates/rust-analyzer/src/handlers.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,7 @@ pub(crate) fn handle_code_lens(
11351135
annotate_impls: lens_config.implementations,
11361136
annotate_references: lens_config.refs,
11371137
annotate_method_references: lens_config.method_refs,
1138+
annotate_enum_variant_references: lens_config.enum_variant_refs,
11381139
},
11391140
file_id,
11401141
)?;

docs/user/generated_config.adoc

+8-2
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,14 @@ Whether to show `Method References` lens. Only applies when
360360
[[rust-analyzer.lens.references]]rust-analyzer.lens.references (default: `false`)::
361361
+
362362
--
363-
Whether to show `References` lens. Only applies when
364-
`#rust-analyzer.lens.enable#` is set.
363+
Whether to show `References` lens for Struct, Enum, Union and Trait.
364+
Only applies when `#rust-analyzer.lens.enable#` is set.
365+
--
366+
[[rust-analyzer.lens.enumVariantReferences]]rust-analyzer.lens.enumVariantReferences (default: `false`)::
367+
+
368+
--
369+
Whether to show `References` lens for Enum Variants.
370+
Only applies when `#rust-analyzer.lens.enable#` is set.
365371
--
366372
[[rust-analyzer.lens.forceCustomCommands]]rust-analyzer.lens.forceCustomCommands (default: `true`)::
367373
+

editors/code/package.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,12 @@
798798
"type": "boolean"
799799
},
800800
"rust-analyzer.lens.references": {
801-
"markdownDescription": "Whether to show `References` lens. Only applies when\n`#rust-analyzer.lens.enable#` is set.",
801+
"markdownDescription": "Whether to show `References` lens for Struct, Enum, Union and Trait.\nOnly applies when `#rust-analyzer.lens.enable#` is set.",
802+
"default": false,
803+
"type": "boolean"
804+
},
805+
"rust-analyzer.lens.enumVariantReferences": {
806+
"markdownDescription": "Whether to show `References` lens for Enum Variants.\nOnly applies when `#rust-analyzer.lens.enable#` is set.",
802807
"default": false,
803808
"type": "boolean"
804809
},

0 commit comments

Comments
 (0)