Skip to content

Commit dc217bd

Browse files
committed
CodeLens configuration options.
1 parent 71e94b1 commit dc217bd

File tree

4 files changed

+164
-88
lines changed

4 files changed

+164
-88
lines changed

crates/rust-analyzer/src/config.rs

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,34 @@ pub struct Config {
3333
pub inlay_hints: InlayHintsConfig,
3434
pub completion: CompletionConfig,
3535
pub call_info_full: bool,
36+
pub lens: LensConfig,
37+
}
38+
39+
#[derive(Clone, Debug, PartialEq, Eq)]
40+
pub struct LensConfig {
41+
pub run: bool,
42+
pub debug: bool,
43+
pub impementations: bool,
44+
}
45+
46+
impl Default for LensConfig {
47+
fn default() -> Self {
48+
Self { run: true, debug: true, impementations: true }
49+
}
50+
}
51+
52+
impl LensConfig {
53+
pub fn any(&self) -> bool {
54+
self.impementations || self.runnable()
55+
}
56+
57+
pub fn none(&self) -> bool {
58+
!self.any()
59+
}
60+
61+
pub fn runnable(&self) -> bool {
62+
self.run || self.debug
63+
}
3664
}
3765

3866
#[derive(Debug, Clone)]
@@ -107,6 +135,7 @@ impl Default for Config {
107135
..CompletionConfig::default()
108136
},
109137
call_info_full: true,
138+
lens: LensConfig::default(),
110139
}
111140
}
112141
}
@@ -195,6 +224,9 @@ impl Config {
195224
set(value, "/completion/addCallParenthesis", &mut self.completion.add_call_parenthesis);
196225
set(value, "/completion/addCallArgumentSnippets", &mut self.completion.add_call_argument_snippets);
197226
set(value, "/callInfo/full", &mut self.call_info_full);
227+
set(value, "/lens/run", &mut self.lens.run);
228+
set(value, "/lens/debug", &mut self.lens.debug);
229+
set(value, "/lens/implementations", &mut self.lens.impementations);
198230

199231
log::info!("Config::update() = {:#?}", self);
200232

@@ -212,35 +244,35 @@ impl Config {
212244
pub fn update_caps(&mut self, caps: &ClientCapabilities) {
213245
if let Some(doc_caps) = caps.text_document.as_ref() {
214246
if let Some(value) = doc_caps.definition.as_ref().and_then(|it| it.link_support) {
215-
self.client_caps.location_link = value;
216-
}
247+
self.client_caps.location_link = value;
248+
}
217249
if let Some(value) = doc_caps.folding_range.as_ref().and_then(|it| it.line_folding_only)
218250
{
219-
self.client_caps.line_folding_only = value
220-
}
251+
self.client_caps.line_folding_only = value
252+
}
221253
if let Some(value) = doc_caps
222254
.document_symbol
223255
.as_ref()
224256
.and_then(|it| it.hierarchical_document_symbol_support)
225-
{
226-
self.client_caps.hierarchical_symbols = value
227-
}
257+
{
258+
self.client_caps.hierarchical_symbols = value
259+
}
228260
if let Some(value) = doc_caps
229261
.code_action
230262
.as_ref()
231263
.and_then(|it| Some(it.code_action_literal_support.is_some()))
232-
{
233-
self.client_caps.code_action_literals = value;
234-
}
235-
self.completion.allow_snippets(false);
264+
{
265+
self.client_caps.code_action_literals = value;
266+
}
267+
self.completion.allow_snippets(false);
236268
if let Some(completion) = &doc_caps.completion {
237-
if let Some(completion_item) = &completion.completion_item {
238-
if let Some(value) = completion_item.snippet_support {
239-
self.completion.allow_snippets(value);
240-
}
269+
if let Some(completion_item) = &completion.completion_item {
270+
if let Some(value) = completion_item.snippet_support {
271+
self.completion.allow_snippets(value);
241272
}
242273
}
243274
}
275+
}
244276

245277
if let Some(window_caps) = caps.window.as_ref() {
246278
if let Some(value) = window_caps.work_done_progress {

crates/rust-analyzer/src/main_loop/handlers.rs

Lines changed: 91 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -812,88 +812,106 @@ pub fn handle_code_lens(
812812
params: lsp_types::CodeLensParams,
813813
) -> Result<Option<Vec<CodeLens>>> {
814814
let _p = profile("handle_code_lens");
815-
let file_id = from_proto::file_id(&world, &params.text_document.uri)?;
816-
let line_index = world.analysis().file_line_index(file_id)?;
817-
818815
let mut lenses: Vec<CodeLens> = Default::default();
819816

817+
if world.config.lens.none() {
818+
// early return before any db query!
819+
return Ok(Some(lenses));
820+
}
821+
822+
let file_id = from_proto::file_id(&world, &params.text_document.uri)?;
823+
let line_index = world.analysis().file_line_index(file_id)?;
820824
let cargo_spec = CargoTargetSpec::for_file(&world, file_id)?;
821-
// Gather runnables
822-
for runnable in world.analysis().runnables(file_id)? {
823-
let title = match &runnable.kind {
824-
RunnableKind::Test { .. } | RunnableKind::TestMod { .. } => "▶\u{fe0e} Run Test",
825-
RunnableKind::DocTest { .. } => "▶\u{fe0e} Run Doctest",
826-
RunnableKind::Bench { .. } => "Run Bench",
827-
RunnableKind::Bin => {
828-
// Do not suggest binary run on other target than binary
829-
match &cargo_spec {
830-
Some(spec) => match spec.target_kind {
831-
TargetKind::Bin => "Run",
832-
_ => continue,
833-
},
834-
None => continue,
825+
826+
if world.config.lens.runnable() {
827+
// Gather runnables
828+
for runnable in world.analysis().runnables(file_id)? {
829+
let (run_title, debugee ) = match &runnable.kind {
830+
RunnableKind::Test { .. } | RunnableKind::TestMod { .. } => ("▶️\u{fe0e}Run Test", true),
831+
RunnableKind::DocTest { .. } => {
832+
// cargo does not support -no-run for doctests
833+
("▶️\u{fe0e}Run Doctest", false)
835834
}
835+
RunnableKind::Bench { .. } => {
836+
// Nothing wrong with bench debugging
837+
("Run Bench", true)
838+
},
839+
RunnableKind::Bin => {
840+
// Do not suggest binary run on other target than binary
841+
match &cargo_spec {
842+
Some(spec) => match spec.target_kind {
843+
TargetKind::Bin => ("Run", true),
844+
_ => continue,
845+
},
846+
None => continue,
847+
}
848+
}
849+
};
850+
851+
let mut r = to_lsp_runnable(&world, file_id, runnable)?;
852+
if world.config.lens.run {
853+
let lens = CodeLens {
854+
range: r.range,
855+
command: Some(Command {
856+
title: run_title.to_string(),
857+
command: "rust-analyzer.runSingle".into(),
858+
arguments: Some(vec![to_value(&r).unwrap()]),
859+
}),
860+
data: None,
861+
};
862+
lenses.push(lens);
836863
}
837-
}
838-
.to_string();
839-
let mut r = to_lsp_runnable(&world, file_id, runnable)?;
840-
let lens = CodeLens {
841-
range: r.range,
842-
command: Some(Command {
843-
title,
844-
command: "rust-analyzer.runSingle".into(),
845-
arguments: Some(vec![to_value(&r).unwrap()]),
846-
}),
847-
data: None,
848-
};
849-
lenses.push(lens);
850864

851-
if r.args[0] == "run" {
852-
r.args[0] = "build".into();
853-
} else {
854-
r.args.push("--no-run".into());
865+
if debugee && world.config.lens.debug {
866+
if r.args[0] == "run" {
867+
r.args[0] = "build".into();
868+
} else {
869+
r.args.push("--no-run".into());
870+
}
871+
let debug_lens = CodeLens {
872+
range: r.range,
873+
command: Some(Command {
874+
title: "Debug".into(),
875+
command: "rust-analyzer.debugSingle".into(),
876+
arguments: Some(vec![to_value(r).unwrap()]),
877+
}),
878+
data: None,
879+
};
880+
lenses.push(debug_lens);
881+
}
855882
}
856-
let debug_lens = CodeLens {
857-
range: r.range,
858-
command: Some(Command {
859-
title: "Debug".into(),
860-
command: "rust-analyzer.debugSingle".into(),
861-
arguments: Some(vec![to_value(r).unwrap()]),
862-
}),
863-
data: None,
864-
};
865-
lenses.push(debug_lens);
866883
}
867884

868-
// Handle impls
869-
lenses.extend(
870-
world
871-
.analysis()
872-
.file_structure(file_id)?
873-
.into_iter()
874-
.filter(|it| match it.kind {
875-
SyntaxKind::TRAIT_DEF | SyntaxKind::STRUCT_DEF | SyntaxKind::ENUM_DEF => true,
876-
_ => false,
877-
})
878-
.map(|it| {
879-
let range = to_proto::range(&line_index, it.node_range);
880-
let pos = range.start;
881-
let lens_params = lsp_types::request::GotoImplementationParams {
882-
text_document_position_params: lsp_types::TextDocumentPositionParams::new(
883-
params.text_document.clone(),
884-
pos,
885-
),
886-
work_done_progress_params: Default::default(),
887-
partial_result_params: Default::default(),
888-
};
889-
CodeLens {
890-
range,
891-
command: None,
892-
data: Some(to_value(CodeLensResolveData::Impls(lens_params)).unwrap()),
893-
}
894-
}),
895-
);
896-
885+
if world.config.lens.impementations {
886+
// Handle impls
887+
lenses.extend(
888+
world
889+
.analysis()
890+
.file_structure(file_id)?
891+
.into_iter()
892+
.filter(|it| match it.kind {
893+
SyntaxKind::TRAIT_DEF | SyntaxKind::STRUCT_DEF | SyntaxKind::ENUM_DEF => true,
894+
_ => false,
895+
})
896+
.map(|it| {
897+
let range = to_proto::range(&line_index, it.node_range);
898+
let pos = range.start;
899+
let lens_params = lsp_types::request::GotoImplementationParams {
900+
text_document_position_params: lsp_types::TextDocumentPositionParams::new(
901+
params.text_document.clone(),
902+
pos,
903+
),
904+
work_done_progress_params: Default::default(),
905+
partial_result_params: Default::default(),
906+
};
907+
CodeLens {
908+
range,
909+
command: None,
910+
data: Some(to_value(CodeLensResolveData::Impls(lens_params)).unwrap()),
911+
}
912+
}),
913+
);
914+
}
897915
Ok(Some(lenses))
898916
}
899917

editors/code/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,21 @@
443443
"type": "object",
444444
"default": {},
445445
"description": "Optional settings passed to the debug engine. Example:\n{ \"lldb\": { \"terminal\":\"external\"} }"
446+
},
447+
"rust-analyzer.lens.run": {
448+
"description": "Whether to show Run lens.",
449+
"type": "boolean",
450+
"default": true
451+
},
452+
"rust-analyzer.lens.debug": {
453+
"description": "Whether to show Debug lens.",
454+
"type": "boolean",
455+
"default": true
456+
},
457+
"rust-analyzer.lens.implementations": {
458+
"description": "Whether to show Implementations lens.",
459+
"type": "boolean",
460+
"default": true
446461
}
447462
}
448463
},

editors/code/src/config.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ export class Config {
1616
"files",
1717
"highlighting",
1818
"updates.channel",
19+
"lens.run",
20+
"lens.debug",
21+
"lens.implementations",
1922
]
2023
.map(opt => `${this.rootSection}.${opt}`);
2124

@@ -119,4 +122,12 @@ export class Config {
119122
sourceFileMap: sourceFileMap
120123
};
121124
}
125+
126+
get lens() {
127+
return {
128+
run: this.get<boolean>("lens.run"),
129+
debug: this.get<boolean>("lens.debug"),
130+
implementations: this.get<boolean>("lens.implementations"),
131+
};
132+
}
122133
}

0 commit comments

Comments
 (0)