mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-25 12:33:33 +00:00
Add References code lens.
For Struct, Enum, Union and Trait symbols.
This commit is contained in:
parent
fb2b9c7212
commit
3618c4e0d3
5 changed files with 52 additions and 33 deletions
|
@ -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 {
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -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`)::
|
||||
|
|
|
@ -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": [],
|
||||
|
|
|
@ -144,6 +144,7 @@ export class Config {
|
|||
debug: this.get<boolean>("lens.debug"),
|
||||
implementations: this.get<boolean>("lens.implementations"),
|
||||
methodReferences: this.get<boolean>("lens.methodReferences"),
|
||||
references: this.get<boolean>("lens.references"),
|
||||
};
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue