Unnamed repository; edit this file 'description' to name the repository.
Merge #10447
10447: Add enum variant references CodeLens. r=Veykril a=ericsampson Co-authored-by: Eric Sampson <[email protected]>
bors[bot] 2021-10-06
parent c409cf0 · parent 6d05b07 · commit 5ff9924
-rw-r--r--crates/ide/src/annotations.rs45
-rw-r--r--crates/rust-analyzer/src/config.rs11
-rw-r--r--crates/rust-analyzer/src/handlers.rs1
-rw-r--r--docs/user/generated_config.adoc10
-rw-r--r--editors/code/package.json7
5 files changed, 61 insertions, 13 deletions
diff --git a/crates/ide/src/annotations.rs b/crates/ide/src/annotations.rs
index 472b396ac5..12c6852ea0 100644
--- a/crates/ide/src/annotations.rs
+++ b/crates/ide/src/annotations.rs
@@ -40,6 +40,7 @@ pub struct AnnotationConfig {
pub annotate_impls: bool,
pub annotate_references: bool,
pub annotate_method_references: bool,
+ pub annotate_enum_variant_references: bool,
}
pub(crate) fn annotations(
@@ -63,18 +64,33 @@ pub(crate) fn annotations(
visit_file_defs(&Semantics::new(db), file_id, &mut |def| match def {
Either::Left(def) => {
- let range = match def {
+ let (range, ranges_enum_variants) = match def {
hir::ModuleDef::Const(konst) => {
- konst.source(db).and_then(|node| name_range(&node, file_id))
+ (konst.source(db).and_then(|node| name_range(&node, file_id)), vec![])
}
hir::ModuleDef::Trait(trait_) => {
- trait_.source(db).and_then(|node| name_range(&node, file_id))
+ (trait_.source(db).and_then(|node| name_range(&node, file_id)), vec![])
}
- hir::ModuleDef::Adt(adt) => {
- adt.source(db).and_then(|node| name_range(&node, file_id))
- }
- _ => None,
+ hir::ModuleDef::Adt(adt) => match adt {
+ hir::Adt::Enum(enum_) => (
+ enum_.source(db).and_then(|node| name_range(&node, file_id)),
+ if config.annotate_enum_variant_references {
+ enum_
+ .variants(db)
+ .into_iter()
+ .map(|variant| {
+ variant.source(db).and_then(|node| name_range(&node, file_id))
+ })
+ .collect()
+ } else {
+ vec![]
+ },
+ ),
+ _ => (adt.source(db).and_then(|node| name_range(&node, file_id)), vec![]),
+ },
+ _ => (None, vec![]),
};
+
let (range, offset) = match range {
Some(range) => (range, range.start()),
None => return,
@@ -99,6 +115,20 @@ pub(crate) fn annotations(
});
}
+ if config.annotate_enum_variant_references {
+ for range_enum_variant in
+ ranges_enum_variants.into_iter().filter_map(std::convert::identity)
+ {
+ annotations.push(Annotation {
+ range: range_enum_variant,
+ kind: AnnotationKind::HasReferences {
+ position: FilePosition { file_id, offset: range_enum_variant.start() },
+ data: None,
+ },
+ });
+ }
+ }
+
fn name_range<T: HasName>(node: &InFile<T>, file_id: FileId) -> Option<TextRange> {
if node.file_id == file_id.into() {
node.value.name().map(|it| it.syntax().text_range())
@@ -173,6 +203,7 @@ mod tests {
annotate_impls: true,
annotate_references: true,
annotate_method_references: true,
+ annotate_enum_variant_references: true,
},
file_id,
)
diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs
index a032c2b653..b51234c153 100644
--- a/crates/rust-analyzer/src/config.rs
+++ b/crates/rust-analyzer/src/config.rs
@@ -227,9 +227,12 @@ config_data! {
/// Whether to show `Method References` lens. Only applies when
/// `#rust-analyzer.lens.enable#` is set.
lens_methodReferences: bool = "false",
- /// Whether to show `References` lens. Only applies when
- /// `#rust-analyzer.lens.enable#` is set.
+ /// Whether to show `References` lens for Struct, Enum, Union and Trait.
+ /// Only applies when `#rust-analyzer.lens.enable#` is set.
lens_references: bool = "false",
+ /// Whether to show `References` lens for Enum Variants.
+ /// Only applies when `#rust-analyzer.lens.enable#` is set.
+ lens_enumVariantReferences: bool = "false",
/// Internal config: use custom client-side commands even when the
/// client doesn't set the corresponding capability.
lens_forceCustomCommands: bool = "true",
@@ -326,6 +329,7 @@ pub struct LensConfig {
pub implementations: bool,
pub method_refs: bool,
pub refs: bool, // for Struct, Enum, Union and Trait
+ pub enum_variant_refs: bool,
}
impl LensConfig {
@@ -342,7 +346,7 @@ impl LensConfig {
}
pub fn references(&self) -> bool {
- self.method_refs || self.refs
+ self.method_refs || self.refs || self.enum_variant_refs
}
}
@@ -832,6 +836,7 @@ impl Config {
implementations: self.data.lens_enable && self.data.lens_implementations,
method_refs: self.data.lens_enable && self.data.lens_methodReferences,
refs: self.data.lens_enable && self.data.lens_references,
+ enum_variant_refs: self.data.lens_enable && self.data.lens_enumVariantReferences,
}
}
pub fn hover_actions(&self) -> HoverActionsConfig {
diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs
index ca286b7de9..c3583df713 100644
--- a/crates/rust-analyzer/src/handlers.rs
+++ b/crates/rust-analyzer/src/handlers.rs
@@ -1135,6 +1135,7 @@ pub(crate) fn handle_code_lens(
annotate_impls: lens_config.implementations,
annotate_references: lens_config.refs,
annotate_method_references: lens_config.method_refs,
+ annotate_enum_variant_references: lens_config.enum_variant_refs,
},
file_id,
)?;
diff --git a/docs/user/generated_config.adoc b/docs/user/generated_config.adoc
index f80b978545..7955bcfba9 100644
--- a/docs/user/generated_config.adoc
+++ b/docs/user/generated_config.adoc
@@ -360,8 +360,14 @@ Whether to show `Method References` lens. Only applies when
[[rust-analyzer.lens.references]]rust-analyzer.lens.references (default: `false`)::
+
--
-Whether to show `References` lens. Only applies when
-`#rust-analyzer.lens.enable#` is set.
+Whether to show `References` lens for Struct, Enum, Union and Trait.
+Only applies when `#rust-analyzer.lens.enable#` is set.
+--
+[[rust-analyzer.lens.enumVariantReferences]]rust-analyzer.lens.enumVariantReferences (default: `false`)::
++
+--
+Whether to show `References` lens for Enum Variants.
+Only applies when `#rust-analyzer.lens.enable#` is set.
--
[[rust-analyzer.lens.forceCustomCommands]]rust-analyzer.lens.forceCustomCommands (default: `true`)::
+
diff --git a/editors/code/package.json b/editors/code/package.json
index d9afdfad1b..b62060c398 100644
--- a/editors/code/package.json
+++ b/editors/code/package.json
@@ -798,7 +798,12 @@
"type": "boolean"
},
"rust-analyzer.lens.references": {
- "markdownDescription": "Whether to show `References` lens. Only applies when\n`#rust-analyzer.lens.enable#` is set.",
+ "markdownDescription": "Whether to show `References` lens for Struct, Enum, Union and Trait.\nOnly applies when `#rust-analyzer.lens.enable#` is set.",
+ "default": false,
+ "type": "boolean"
+ },
+ "rust-analyzer.lens.enumVariantReferences": {
+ "markdownDescription": "Whether to show `References` lens for Enum Variants.\nOnly applies when `#rust-analyzer.lens.enable#` is set.",
"default": false,
"type": "boolean"
},