mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 21:43:37 +00:00
CodeLens configuration options.
This commit is contained in:
parent
71e94b1d0b
commit
dc217bdf90
4 changed files with 168 additions and 92 deletions
|
@ -33,6 +33,34 @@ pub struct Config {
|
||||||
pub inlay_hints: InlayHintsConfig,
|
pub inlay_hints: InlayHintsConfig,
|
||||||
pub completion: CompletionConfig,
|
pub completion: CompletionConfig,
|
||||||
pub call_info_full: bool,
|
pub call_info_full: bool,
|
||||||
|
pub lens: LensConfig,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||||
|
pub struct LensConfig {
|
||||||
|
pub run: bool,
|
||||||
|
pub debug: bool,
|
||||||
|
pub impementations: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for LensConfig {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self { run: true, debug: true, impementations: true }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LensConfig {
|
||||||
|
pub fn any(&self) -> bool {
|
||||||
|
self.impementations || self.runnable()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn none(&self) -> bool {
|
||||||
|
!self.any()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn runnable(&self) -> bool {
|
||||||
|
self.run || self.debug
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
@ -107,6 +135,7 @@ impl Default for Config {
|
||||||
..CompletionConfig::default()
|
..CompletionConfig::default()
|
||||||
},
|
},
|
||||||
call_info_full: true,
|
call_info_full: true,
|
||||||
|
lens: LensConfig::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -195,6 +224,9 @@ impl Config {
|
||||||
set(value, "/completion/addCallParenthesis", &mut self.completion.add_call_parenthesis);
|
set(value, "/completion/addCallParenthesis", &mut self.completion.add_call_parenthesis);
|
||||||
set(value, "/completion/addCallArgumentSnippets", &mut self.completion.add_call_argument_snippets);
|
set(value, "/completion/addCallArgumentSnippets", &mut self.completion.add_call_argument_snippets);
|
||||||
set(value, "/callInfo/full", &mut self.call_info_full);
|
set(value, "/callInfo/full", &mut self.call_info_full);
|
||||||
|
set(value, "/lens/run", &mut self.lens.run);
|
||||||
|
set(value, "/lens/debug", &mut self.lens.debug);
|
||||||
|
set(value, "/lens/implementations", &mut self.lens.impementations);
|
||||||
|
|
||||||
log::info!("Config::update() = {:#?}", self);
|
log::info!("Config::update() = {:#?}", self);
|
||||||
|
|
||||||
|
|
|
@ -812,42 +812,57 @@ pub fn handle_code_lens(
|
||||||
params: lsp_types::CodeLensParams,
|
params: lsp_types::CodeLensParams,
|
||||||
) -> Result<Option<Vec<CodeLens>>> {
|
) -> Result<Option<Vec<CodeLens>>> {
|
||||||
let _p = profile("handle_code_lens");
|
let _p = profile("handle_code_lens");
|
||||||
let file_id = from_proto::file_id(&world, ¶ms.text_document.uri)?;
|
|
||||||
let line_index = world.analysis().file_line_index(file_id)?;
|
|
||||||
|
|
||||||
let mut lenses: Vec<CodeLens> = Default::default();
|
let mut lenses: Vec<CodeLens> = Default::default();
|
||||||
|
|
||||||
|
if world.config.lens.none() {
|
||||||
|
// early return before any db query!
|
||||||
|
return Ok(Some(lenses));
|
||||||
|
}
|
||||||
|
|
||||||
|
let file_id = from_proto::file_id(&world, ¶ms.text_document.uri)?;
|
||||||
|
let line_index = world.analysis().file_line_index(file_id)?;
|
||||||
let cargo_spec = CargoTargetSpec::for_file(&world, file_id)?;
|
let cargo_spec = CargoTargetSpec::for_file(&world, file_id)?;
|
||||||
|
|
||||||
|
if world.config.lens.runnable() {
|
||||||
// Gather runnables
|
// Gather runnables
|
||||||
for runnable in world.analysis().runnables(file_id)? {
|
for runnable in world.analysis().runnables(file_id)? {
|
||||||
let title = match &runnable.kind {
|
let (run_title, debugee ) = match &runnable.kind {
|
||||||
RunnableKind::Test { .. } | RunnableKind::TestMod { .. } => "▶\u{fe0e} Run Test",
|
RunnableKind::Test { .. } | RunnableKind::TestMod { .. } => ("▶️\u{fe0e}Run Test", true),
|
||||||
RunnableKind::DocTest { .. } => "▶\u{fe0e} Run Doctest",
|
RunnableKind::DocTest { .. } => {
|
||||||
RunnableKind::Bench { .. } => "Run Bench",
|
// cargo does not support -no-run for doctests
|
||||||
|
("▶️\u{fe0e}Run Doctest", false)
|
||||||
|
}
|
||||||
|
RunnableKind::Bench { .. } => {
|
||||||
|
// Nothing wrong with bench debugging
|
||||||
|
("Run Bench", true)
|
||||||
|
},
|
||||||
RunnableKind::Bin => {
|
RunnableKind::Bin => {
|
||||||
// Do not suggest binary run on other target than binary
|
// Do not suggest binary run on other target than binary
|
||||||
match &cargo_spec {
|
match &cargo_spec {
|
||||||
Some(spec) => match spec.target_kind {
|
Some(spec) => match spec.target_kind {
|
||||||
TargetKind::Bin => "Run",
|
TargetKind::Bin => ("Run", true),
|
||||||
_ => continue,
|
_ => continue,
|
||||||
},
|
},
|
||||||
None => continue,
|
None => continue,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
.to_string();
|
|
||||||
let mut r = to_lsp_runnable(&world, file_id, runnable)?;
|
let mut r = to_lsp_runnable(&world, file_id, runnable)?;
|
||||||
|
if world.config.lens.run {
|
||||||
let lens = CodeLens {
|
let lens = CodeLens {
|
||||||
range: r.range,
|
range: r.range,
|
||||||
command: Some(Command {
|
command: Some(Command {
|
||||||
title,
|
title: run_title.to_string(),
|
||||||
command: "rust-analyzer.runSingle".into(),
|
command: "rust-analyzer.runSingle".into(),
|
||||||
arguments: Some(vec![to_value(&r).unwrap()]),
|
arguments: Some(vec![to_value(&r).unwrap()]),
|
||||||
}),
|
}),
|
||||||
data: None,
|
data: None,
|
||||||
};
|
};
|
||||||
lenses.push(lens);
|
lenses.push(lens);
|
||||||
|
}
|
||||||
|
|
||||||
|
if debugee && world.config.lens.debug {
|
||||||
if r.args[0] == "run" {
|
if r.args[0] == "run" {
|
||||||
r.args[0] = "build".into();
|
r.args[0] = "build".into();
|
||||||
} else {
|
} else {
|
||||||
|
@ -864,7 +879,10 @@ pub fn handle_code_lens(
|
||||||
};
|
};
|
||||||
lenses.push(debug_lens);
|
lenses.push(debug_lens);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if world.config.lens.impementations {
|
||||||
// Handle impls
|
// Handle impls
|
||||||
lenses.extend(
|
lenses.extend(
|
||||||
world
|
world
|
||||||
|
@ -893,7 +911,7 @@ pub fn handle_code_lens(
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
}
|
||||||
Ok(Some(lenses))
|
Ok(Some(lenses))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -443,6 +443,21 @@
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"default": {},
|
"default": {},
|
||||||
"description": "Optional settings passed to the debug engine. Example:\n{ \"lldb\": { \"terminal\":\"external\"} }"
|
"description": "Optional settings passed to the debug engine. Example:\n{ \"lldb\": { \"terminal\":\"external\"} }"
|
||||||
|
},
|
||||||
|
"rust-analyzer.lens.run": {
|
||||||
|
"description": "Whether to show Run lens.",
|
||||||
|
"type": "boolean",
|
||||||
|
"default": true
|
||||||
|
},
|
||||||
|
"rust-analyzer.lens.debug": {
|
||||||
|
"description": "Whether to show Debug lens.",
|
||||||
|
"type": "boolean",
|
||||||
|
"default": true
|
||||||
|
},
|
||||||
|
"rust-analyzer.lens.implementations": {
|
||||||
|
"description": "Whether to show Implementations lens.",
|
||||||
|
"type": "boolean",
|
||||||
|
"default": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -16,6 +16,9 @@ export class Config {
|
||||||
"files",
|
"files",
|
||||||
"highlighting",
|
"highlighting",
|
||||||
"updates.channel",
|
"updates.channel",
|
||||||
|
"lens.run",
|
||||||
|
"lens.debug",
|
||||||
|
"lens.implementations",
|
||||||
]
|
]
|
||||||
.map(opt => `${this.rootSection}.${opt}`);
|
.map(opt => `${this.rootSection}.${opt}`);
|
||||||
|
|
||||||
|
@ -119,4 +122,12 @@ export class Config {
|
||||||
sourceFileMap: sourceFileMap
|
sourceFileMap: sourceFileMap
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get lens() {
|
||||||
|
return {
|
||||||
|
run: this.get<boolean>("lens.run"),
|
||||||
|
debug: this.get<boolean>("lens.debug"),
|
||||||
|
implementations: this.get<boolean>("lens.implementations"),
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue