Add References code lens.

For Struct, Enum, Union and Trait symbols.
This commit is contained in:
vsrs 2021-01-23 16:56:20 +03:00
parent fb2b9c7212
commit 3618c4e0d3
5 changed files with 52 additions and 33 deletions

View file

@ -147,6 +147,9 @@ config_data! {
/// Whether to show `Method References` lens. Only applies when /// Whether to show `Method References` lens. Only applies when
/// `#rust-analyzer.lens.enable#` is set. /// `#rust-analyzer.lens.enable#` is set.
lens_methodReferences: bool = "false", 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 /// Disable project auto-discovery in favor of explicitly specified set
/// of projects.\n\nElements must be paths pointing to `Cargo.toml`, /// of projects.\n\nElements must be paths pointing to `Cargo.toml`,
@ -221,6 +224,7 @@ pub struct LensConfig {
pub debug: bool, pub debug: bool,
pub implementations: bool, pub implementations: bool,
pub method_refs: bool, pub method_refs: bool,
pub refs: bool, // for Struct, Enum, Union and Trait
} }
impl LensConfig { impl LensConfig {
@ -237,7 +241,7 @@ impl LensConfig {
} }
pub fn references(&self) -> bool { 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, debug: self.data.lens_enable && self.data.lens_debug,
implementations: self.data.lens_enable && self.data.lens_implementations, implementations: self.data.lens_enable && self.data.lens_implementations,
method_refs: self.data.lens_enable && self.data.lens_methodReferences, method_refs: self.data.lens_enable && self.data.lens_methodReferences,
refs: self.data.lens_enable && self.data.lens_references,
} }
} }
pub fn hover(&self) -> HoverConfig { pub fn hover(&self) -> HoverConfig {

View file

@ -1112,42 +1112,48 @@ pub(crate) fn handle_code_lens(
} }
} }
if lens_config.implementations { if lens_config.implementations || lens_config.refs {
// Handle impls snap.analysis
lenses.extend( .file_structure(file_id)?
snap.analysis .into_iter()
.file_structure(file_id)? .filter(|it| {
.into_iter() matches!(
.filter(|it| { it.kind,
matches!( SymbolKind::Trait | SymbolKind::Struct | SymbolKind::Enum | SymbolKind::Union
it.kind, )
SymbolKind::Trait })
| SymbolKind::Struct .for_each(|it| {
| SymbolKind::Enum let range = to_proto::range(&line_index, it.node_range);
| SymbolKind::Union let position = to_proto::position(&line_index, it.navigation_range.start());
) let doc_pos = lsp_types::TextDocumentPositionParams::new(
}) params.text_document.clone(),
.map(|it| { position,
let range = to_proto::range(&line_index, it.node_range); );
let pos = range.start; let goto_params = lsp_types::request::GotoImplementationParams {
let lens_params = lsp_types::request::GotoImplementationParams { text_document_position_params: doc_pos.clone(),
text_document_position_params: lsp_types::TextDocumentPositionParams::new( work_done_progress_params: Default::default(),
params.text_document.clone(), partial_result_params: Default::default(),
pos, };
),
work_done_progress_params: Default::default(), if lens_config.implementations {
partial_result_params: Default::default(), lenses.push(CodeLens {
};
CodeLens {
range, range,
command: None, 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| { lenses.extend(snap.analysis.find_all_methods(file_id)?.into_iter().map(|it| {
let range = to_proto::range(&line_index, it.range); let range = to_proto::range(&line_index, it.range);
let position = to_proto::position(&line_index, it.range.start()); let position = to_proto::position(&line_index, it.range.start());

View file

@ -86,6 +86,8 @@
Whether to show `Run` lens. Only applies when `#rust-analyzer.lens.enable#` is set. Whether to show `Run` lens. Only applies when `#rust-analyzer.lens.enable#` is set.
[[rust-analyzer.lens.methodReferences]]rust-analyzer.lens.methodReferences (default: `false`):: [[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. 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: `[]`):: [[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. 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`):: [[rust-analyzer.lruCapacity]]rust-analyzer.lruCapacity (default: `null`)::

View file

@ -633,6 +633,11 @@
"default": false, "default": false,
"type": "boolean" "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": { "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.", "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": [], "default": [],

View file

@ -144,6 +144,7 @@ export class Config {
debug: this.get<boolean>("lens.debug"), debug: this.get<boolean>("lens.debug"),
implementations: this.get<boolean>("lens.implementations"), implementations: this.get<boolean>("lens.implementations"),
methodReferences: this.get<boolean>("lens.methodReferences"), methodReferences: this.get<boolean>("lens.methodReferences"),
references: this.get<boolean>("lens.references"),
}; };
} }