From 3618c4e0d3127e6d6eab1b55fd88c353b8d3f6d7 Mon Sep 17 00:00:00 2001 From: vsrs Date: Sat, 23 Jan 2021 16:56:20 +0300 Subject: [PATCH] Add References code lens. For Struct, Enum, Union and Trait symbols. --- crates/rust-analyzer/src/config.rs | 7 ++- crates/rust-analyzer/src/handlers.rs | 70 +++++++++++++++------------- docs/user/generated_config.adoc | 2 + editors/code/package.json | 5 ++ editors/code/src/config.ts | 1 + 5 files changed, 52 insertions(+), 33 deletions(-) diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 3ddb9e19af..247bfe71ef 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -147,6 +147,9 @@ 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. + lens_references: bool = "false", /// Disable project auto-discovery in favor of explicitly specified set /// of projects.\n\nElements must be paths pointing to `Cargo.toml`, @@ -221,6 +224,7 @@ pub struct LensConfig { pub debug: bool, pub implementations: bool, pub method_refs: bool, + pub refs: bool, // for Struct, Enum, Union and Trait } impl LensConfig { @@ -237,7 +241,7 @@ impl LensConfig { } pub fn references(&self) -> bool { - self.method_refs + self.method_refs || self.refs } } @@ -593,6 +597,7 @@ impl Config { debug: self.data.lens_enable && self.data.lens_debug, 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, } } pub fn hover(&self) -> HoverConfig { diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index 809452e6d0..07204436c5 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -1112,42 +1112,48 @@ pub(crate) fn handle_code_lens( } } - if lens_config.implementations { - // Handle impls - lenses.extend( - snap.analysis - .file_structure(file_id)? - .into_iter() - .filter(|it| { - matches!( - it.kind, - SymbolKind::Trait - | SymbolKind::Struct - | SymbolKind::Enum - | SymbolKind::Union - ) - }) - .map(|it| { - let range = to_proto::range(&line_index, it.node_range); - let pos = range.start; - let lens_params = lsp_types::request::GotoImplementationParams { - text_document_position_params: lsp_types::TextDocumentPositionParams::new( - params.text_document.clone(), - pos, - ), - work_done_progress_params: Default::default(), - partial_result_params: Default::default(), - }; - CodeLens { + if lens_config.implementations || lens_config.refs { + snap.analysis + .file_structure(file_id)? + .into_iter() + .filter(|it| { + matches!( + it.kind, + SymbolKind::Trait | SymbolKind::Struct | SymbolKind::Enum | SymbolKind::Union + ) + }) + .for_each(|it| { + let range = to_proto::range(&line_index, it.node_range); + let position = to_proto::position(&line_index, it.navigation_range.start()); + let doc_pos = lsp_types::TextDocumentPositionParams::new( + params.text_document.clone(), + position, + ); + let goto_params = lsp_types::request::GotoImplementationParams { + text_document_position_params: doc_pos.clone(), + work_done_progress_params: Default::default(), + partial_result_params: Default::default(), + }; + + if lens_config.implementations { + lenses.push(CodeLens { range, command: None, - data: Some(to_value(CodeLensResolveData::Impls(lens_params)).unwrap()), - } - }), - ); + data: Some(to_value(CodeLensResolveData::Impls(goto_params)).unwrap()), + }) + } + + if lens_config.refs { + lenses.push(CodeLens { + range, + command: None, + data: Some(to_value(CodeLensResolveData::References(doc_pos)).unwrap()), + }) + } + }); } - if lens_config.references() { + if lens_config.method_refs { lenses.extend(snap.analysis.find_all_methods(file_id)?.into_iter().map(|it| { let range = to_proto::range(&line_index, it.range); let position = to_proto::position(&line_index, it.range.start()); diff --git a/docs/user/generated_config.adoc b/docs/user/generated_config.adoc index a76c99d1e4..2f681b01aa 100644 --- a/docs/user/generated_config.adoc +++ b/docs/user/generated_config.adoc @@ -86,6 +86,8 @@ Whether to show `Run` lens. Only applies when `#rust-analyzer.lens.enable#` is set. [[rust-analyzer.lens.methodReferences]]rust-analyzer.lens.methodReferences (default: `false`):: Whether to show `Method References` lens. Only applies when `#rust-analyzer.lens.enable#` is set. +[[rust-analyzer.lens.references]]rust-analyzer.lens.references (default: `false`):: + Whether to show `References` lens. Only applies when `#rust-analyzer.lens.enable#` is set. [[rust-analyzer.linkedProjects]]rust-analyzer.linkedProjects (default: `[]`):: Disable project auto-discovery in favor of explicitly specified set of projects.\n\nElements must be paths pointing to `Cargo.toml`, `rust-project.json`, or JSON objects in `rust-project.json` format. [[rust-analyzer.lruCapacity]]rust-analyzer.lruCapacity (default: `null`):: diff --git a/editors/code/package.json b/editors/code/package.json index 3e6ebd7ed6..2225cf1ddb 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -633,6 +633,11 @@ "default": false, "type": "boolean" }, + "rust-analyzer.lens.references": { + "markdownDescription": "Whether to show `References` lens. Only applies when `#rust-analyzer.lens.enable#` is set.", + "default": false, + "type": "boolean" + }, "rust-analyzer.linkedProjects": { "markdownDescription": "Disable project auto-discovery in favor of explicitly specified set of projects.\\n\\nElements must be paths pointing to `Cargo.toml`, `rust-project.json`, or JSON objects in `rust-project.json` format.", "default": [], diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts index ebe4de1ea5..ddb5cfbd33 100644 --- a/editors/code/src/config.ts +++ b/editors/code/src/config.ts @@ -144,6 +144,7 @@ export class Config { debug: this.get("lens.debug"), implementations: this.get("lens.implementations"), methodReferences: this.get("lens.methodReferences"), + references: this.get("lens.references"), }; }