From 3618c4e0d3127e6d6eab1b55fd88c353b8d3f6d7 Mon Sep 17 00:00:00 2001 From: vsrs Date: Sat, 23 Jan 2021 16:56:20 +0300 Subject: 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 +++++++++++++++++++----------------- 2 files changed, 44 insertions(+), 33 deletions(-) (limited to 'crates') diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 3ddb9e19a..247bfe71e 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 809452e6d..07204436c 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()); -- cgit v1.2.3