From 148e11aa9e04ff2570c16d828340af1fcbc6d641 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 11 Jul 2021 13:34:22 +0300 Subject: [PATCH 1/9] prepare to move run/debug splitting to handlers --- crates/rust-analyzer/src/handlers.rs | 68 ++++++++++++++++------------ crates/rust-analyzer/src/to_proto.rs | 14 ++++-- 2 files changed, 50 insertions(+), 32 deletions(-) diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index 57b309a075b..1c0a0b27192 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -26,7 +26,7 @@ }; use project_model::TargetKind; use serde_json::json; -use stdx::format_to; +use stdx::{format_to, never}; use syntax::{algo, ast, AstNode, TextRange, TextSize}; use crate::{ @@ -1133,41 +1133,53 @@ pub(crate) fn handle_code_lens( let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?; let cargo_target_spec = CargoTargetSpec::for_file(&snap, file_id)?; - let lenses = snap - .analysis - .annotations( - &AnnotationConfig { - binary_target: cargo_target_spec - .map(|spec| { - matches!( - spec.target_kind, - TargetKind::Bin | TargetKind::Example | TargetKind::Test - ) - }) - .unwrap_or(false), - annotate_runnables: lens_config.runnable(), - annotate_impls: lens_config.implementations, - annotate_references: lens_config.refs, - annotate_method_references: lens_config.method_refs, - run: lens_config.run, - debug: lens_config.debug, - }, - file_id, - )? - .into_iter() - .map(|annotation| to_proto::code_lens(&snap, annotation).unwrap()) - .collect(); + let annotations = snap.analysis.annotations( + &AnnotationConfig { + binary_target: cargo_target_spec + .map(|spec| { + matches!( + spec.target_kind, + TargetKind::Bin | TargetKind::Example | TargetKind::Test + ) + }) + .unwrap_or(false), + annotate_runnables: lens_config.runnable(), + annotate_impls: lens_config.implementations, + annotate_references: lens_config.refs, + annotate_method_references: lens_config.method_refs, + run: lens_config.run, + debug: lens_config.debug, + }, + file_id, + )?; - Ok(Some(lenses)) + let mut res = Vec::new(); + for a in annotations { + to_proto::code_lens(&mut res, &snap, a)?; + } + + Ok(Some(res)) } pub(crate) fn handle_code_lens_resolve( snap: GlobalStateSnapshot, code_lens: CodeLens, ) -> Result { - let annotation = from_proto::annotation(&snap, code_lens)?; + let annotation = from_proto::annotation(&snap, code_lens.clone())?; + let annotation = snap.analysis.resolve_annotation(annotation)?; - to_proto::code_lens(&snap, snap.analysis.resolve_annotation(annotation)?) + let mut acc = Vec::new(); + to_proto::code_lens(&mut acc, &snap, annotation)?; + + let res = match acc.pop() { + Some(it) if acc.is_empty() => it, + _ => { + never!(); + code_lens + } + }; + + Ok(res) } pub(crate) fn handle_document_highlight( diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index 8c8ff826e19..3f565e7cdcf 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -988,9 +988,10 @@ pub(crate) fn runnable( } pub(crate) fn code_lens( + acc: &mut Vec, snap: &GlobalStateSnapshot, annotation: Annotation, -) -> Result { +) -> Result<()> { match annotation.kind { AnnotationKind::Runnable { debug, runnable: run } => { let line_index = snap.file_line_index(run.nav.file_id)?; @@ -1002,7 +1003,11 @@ pub(crate) fn code_lens( let command = if debug { command::debug_single(&r) } else { command::run_single(&r, &title) }; - Ok(lsp_types::CodeLens { range: annotation_range, command: Some(command), data: None }) + acc.push(lsp_types::CodeLens { + range: annotation_range, + command: Some(command), + data: None, + }) } AnnotationKind::HasImpls { position: file_position, data } => { let line_index = snap.file_line_index(file_position.file_id)?; @@ -1041,7 +1046,7 @@ pub(crate) fn code_lens( ) }); - Ok(lsp_types::CodeLens { + acc.push(lsp_types::CodeLens { range: annotation_range, command, data: Some(to_value(lsp_ext::CodeLensResolveData::Impls(goto_params)).unwrap()), @@ -1070,13 +1075,14 @@ pub(crate) fn code_lens( ) }); - Ok(lsp_types::CodeLens { + acc.push(lsp_types::CodeLens { range: annotation_range, command, data: Some(to_value(lsp_ext::CodeLensResolveData::References(doc_pos)).unwrap()), }) } } + Ok(()) } pub(crate) mod command { From 0dc186f61250134400846c4784568ad2ce072161 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 11 Jul 2021 13:42:19 +0300 Subject: [PATCH 2/9] Let the client care about presentation --- crates/ide/src/annotations.rs | 14 ++------------ crates/rust-analyzer/src/to_proto.rs | 26 ++++++++++++++++++-------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/crates/ide/src/annotations.rs b/crates/ide/src/annotations.rs index be745021129..ef3aab5b652 100644 --- a/crates/ide/src/annotations.rs +++ b/crates/ide/src/annotations.rs @@ -59,20 +59,10 @@ pub(crate) fn annotations( let range = runnable.nav.focus_or_full_range(); - // dbg_runnable should go after the run annotation, to prevent a clone we do it this way - let dbg_runnable = (runnable.debugee() && config.debug).then(|| Annotation { + annotations.push(Annotation { range, - kind: AnnotationKind::Runnable { debug: true, runnable: runnable.clone() }, + kind: AnnotationKind::Runnable { debug: false, runnable }, }); - - if config.run { - annotations.push(Annotation { - range, - kind: AnnotationKind::Runnable { debug: false, runnable }, - }); - } - - annotations.extend(dbg_runnable); } } diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index 3f565e7cdcf..0e18581a318 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -998,16 +998,26 @@ pub(crate) fn code_lens( let annotation_range = range(&line_index, annotation.range); let title = run.title(); + let can_debug = run.debugee(); let r = runnable(snap, run)?; - let command = - if debug { command::debug_single(&r) } else { command::run_single(&r, &title) }; - - acc.push(lsp_types::CodeLens { - range: annotation_range, - command: Some(command), - data: None, - }) + let lens_config = snap.config.lens(); + if lens_config.run { + let command = command::run_single(&r, &title); + acc.push(lsp_types::CodeLens { + range: annotation_range, + command: Some(command), + data: None, + }) + } + if lens_config.debug && can_debug { + let command = command::debug_single(&r); + acc.push(lsp_types::CodeLens { + range: annotation_range, + command: Some(command), + data: None, + }) + } } AnnotationKind::HasImpls { position: file_position, data } => { let line_index = snap.file_line_index(file_position.file_id)?; From fa6f78c95bd0bf647eda7339e5b317dea0f4d649 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 11 Jul 2021 13:43:43 +0300 Subject: [PATCH 3/9] better name --- crates/ide/src/runnables.rs | 2 +- crates/rust-analyzer/src/to_proto.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/ide/src/runnables.rs b/crates/ide/src/runnables.rs index c1a068170bb..b748d84ecc0 100644 --- a/crates/ide/src/runnables.rs +++ b/crates/ide/src/runnables.rs @@ -74,7 +74,7 @@ pub fn label(&self, target: Option) -> String { } } - pub fn debugee(&self) -> bool { + pub fn can_debug(&self) -> bool { matches!( &self.kind, RunnableKind::TestMod { .. } diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index 0e18581a318..f270d9de95a 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -998,7 +998,7 @@ pub(crate) fn code_lens( let annotation_range = range(&line_index, annotation.range); let title = run.title(); - let can_debug = run.debugee(); + let can_debug = run.can_debug(); let r = runnable(snap, run)?; let lens_config = snap.config.lens(); From 96d85a9efb2c58d127961fb59b3a69747833803d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 11 Jul 2021 13:44:28 +0300 Subject: [PATCH 4/9] be explicit about what we *can't* debug --- crates/ide/src/runnables.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/ide/src/runnables.rs b/crates/ide/src/runnables.rs index b748d84ecc0..22e0a2afd64 100644 --- a/crates/ide/src/runnables.rs +++ b/crates/ide/src/runnables.rs @@ -75,13 +75,13 @@ pub fn label(&self, target: Option) -> String { } pub fn can_debug(&self) -> bool { - matches!( - &self.kind, + match self.kind { + RunnableKind::DocTest { .. } => false, RunnableKind::TestMod { .. } - | RunnableKind::Test { .. } - | RunnableKind::Bench { .. } - | RunnableKind::Bin - ) + | RunnableKind::Test { .. } + | RunnableKind::Bench { .. } + | RunnableKind::Bin => true, + } } pub fn title(&self) -> String { From beb81a40eb53d8f79c4e0e404f922ebfaad15425 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 11 Jul 2021 13:45:58 +0300 Subject: [PATCH 5/9] simplify --- crates/ide/src/runnables.rs | 10 ---------- crates/rust-analyzer/src/to_proto.rs | 8 +++++++- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/crates/ide/src/runnables.rs b/crates/ide/src/runnables.rs index 22e0a2afd64..51c01ea31dd 100644 --- a/crates/ide/src/runnables.rs +++ b/crates/ide/src/runnables.rs @@ -74,16 +74,6 @@ pub fn label(&self, target: Option) -> String { } } - pub fn can_debug(&self) -> bool { - match self.kind { - RunnableKind::DocTest { .. } => false, - RunnableKind::TestMod { .. } - | RunnableKind::Test { .. } - | RunnableKind::Bench { .. } - | RunnableKind::Bin => true, - } - } - pub fn title(&self) -> String { let mut s = String::from("▶\u{fe0e} Run "); if self.use_name_in_title { diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index f270d9de95a..94ba4fbaeaf 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -998,7 +998,13 @@ pub(crate) fn code_lens( let annotation_range = range(&line_index, annotation.range); let title = run.title(); - let can_debug = run.can_debug(); + let can_debug = match run.kind { + ide::RunnableKind::DocTest { .. } => false, + ide::RunnableKind::TestMod { .. } + | ide::RunnableKind::Test { .. } + | ide::RunnableKind::Bench { .. } + | ide::RunnableKind::Bin => true, + }; let r = runnable(snap, run)?; let lens_config = snap.config.lens(); From ddce16a1265a5c578d0d36c7a5081fef2f434135 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 11 Jul 2021 13:48:26 +0300 Subject: [PATCH 6/9] drop unused field --- crates/ide/src/annotations.rs | 7 ++----- crates/rust-analyzer/src/to_proto.rs | 2 +- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/crates/ide/src/annotations.rs b/crates/ide/src/annotations.rs index ef3aab5b652..27a335f04e1 100644 --- a/crates/ide/src/annotations.rs +++ b/crates/ide/src/annotations.rs @@ -29,7 +29,7 @@ pub struct Annotation { #[derive(Debug)] pub enum AnnotationKind { - Runnable { debug: bool, runnable: Runnable }, + Runnable { runnable: Runnable }, HasImpls { position: FilePosition, data: Option> }, HasReferences { position: FilePosition, data: Option> }, } @@ -59,10 +59,7 @@ pub(crate) fn annotations( let range = runnable.nav.focus_or_full_range(); - annotations.push(Annotation { - range, - kind: AnnotationKind::Runnable { debug: false, runnable }, - }); + annotations.push(Annotation { range, kind: AnnotationKind::Runnable { runnable } }); } } diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index 94ba4fbaeaf..d7b278f011c 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -993,7 +993,7 @@ pub(crate) fn code_lens( annotation: Annotation, ) -> Result<()> { match annotation.kind { - AnnotationKind::Runnable { debug, runnable: run } => { + AnnotationKind::Runnable { runnable: run } => { let line_index = snap.file_line_index(run.nav.file_id)?; let annotation_range = range(&line_index, annotation.range); From a04775060cb8c645e20e5fdcf0a1e73c3a4eec6e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 11 Jul 2021 13:49:38 +0300 Subject: [PATCH 7/9] simplify --- crates/ide/src/annotations.rs | 4 ++-- crates/rust-analyzer/src/to_proto.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/ide/src/annotations.rs b/crates/ide/src/annotations.rs index 27a335f04e1..fd234358c59 100644 --- a/crates/ide/src/annotations.rs +++ b/crates/ide/src/annotations.rs @@ -29,7 +29,7 @@ pub struct Annotation { #[derive(Debug)] pub enum AnnotationKind { - Runnable { runnable: Runnable }, + Runnable(Runnable), HasImpls { position: FilePosition, data: Option> }, HasReferences { position: FilePosition, data: Option> }, } @@ -59,7 +59,7 @@ pub(crate) fn annotations( let range = runnable.nav.focus_or_full_range(); - annotations.push(Annotation { range, kind: AnnotationKind::Runnable { runnable } }); + annotations.push(Annotation { range, kind: AnnotationKind::Runnable(runnable) }); } } diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index d7b278f011c..060b69e0880 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -993,7 +993,7 @@ pub(crate) fn code_lens( annotation: Annotation, ) -> Result<()> { match annotation.kind { - AnnotationKind::Runnable { runnable: run } => { + AnnotationKind::Runnable(run) => { let line_index = snap.file_line_index(run.nav.file_id)?; let annotation_range = range(&line_index, annotation.range); From 5c26523e94da21233e8f3004f3b02eace5b81e5d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 11 Jul 2021 13:51:52 +0300 Subject: [PATCH 8/9] drop unused fields --- crates/ide/src/annotations.rs | 2 -- crates/rust-analyzer/src/handlers.rs | 2 -- 2 files changed, 4 deletions(-) diff --git a/crates/ide/src/annotations.rs b/crates/ide/src/annotations.rs index fd234358c59..3bd72d40e5e 100644 --- a/crates/ide/src/annotations.rs +++ b/crates/ide/src/annotations.rs @@ -40,8 +40,6 @@ pub struct AnnotationConfig { pub annotate_impls: bool, pub annotate_references: bool, pub annotate_method_references: bool, - pub run: bool, - pub debug: bool, } pub(crate) fn annotations( diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index 1c0a0b27192..af53eb2396f 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -1147,8 +1147,6 @@ pub(crate) fn handle_code_lens( annotate_impls: lens_config.implementations, annotate_references: lens_config.refs, annotate_method_references: lens_config.method_refs, - run: lens_config.run, - debug: lens_config.debug, }, file_id, )?; From 92059ea438a77f14cc0a93eb6ccc67b91871fbc4 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 11 Jul 2021 13:55:00 +0300 Subject: [PATCH 9/9] update tests --- crates/ide/src/annotations.rs | 228 ++++------------------------------ 1 file changed, 24 insertions(+), 204 deletions(-) diff --git a/crates/ide/src/annotations.rs b/crates/ide/src/annotations.rs index 3bd72d40e5e..9069854353c 100644 --- a/crates/ide/src/annotations.rs +++ b/crates/ide/src/annotations.rs @@ -179,8 +179,6 @@ fn check(ra_fixture: &str, expect: Expect) { annotate_impls: true, annotate_references: true, annotate_method_references: true, - run: true, - debug: true, }, file_id, ) @@ -208,9 +206,8 @@ fn main() { [ Annotation { range: 53..57, - kind: Runnable { - debug: false, - runnable: Runnable { + kind: Runnable( + Runnable { use_name_in_title: false, nav: NavigationTarget { file_id: FileId( @@ -224,27 +221,7 @@ fn main() { kind: Bin, cfg: None, }, - }, - }, - Annotation { - range: 53..57, - kind: Runnable { - debug: true, - runnable: Runnable { - use_name_in_title: false, - nav: NavigationTarget { - file_id: FileId( - 0, - ), - full_range: 50..85, - focus_range: 53..57, - name: "main", - kind: Function, - }, - kind: Bin, - cfg: None, - }, - }, + ), }, Annotation { range: 6..10, @@ -314,9 +291,8 @@ fn main() { [ Annotation { range: 17..21, - kind: Runnable { - debug: false, - runnable: Runnable { + kind: Runnable( + Runnable { use_name_in_title: false, nav: NavigationTarget { file_id: FileId( @@ -330,27 +306,7 @@ fn main() { kind: Bin, cfg: None, }, - }, - }, - Annotation { - range: 17..21, - kind: Runnable { - debug: true, - runnable: Runnable { - use_name_in_title: false, - nav: NavigationTarget { - file_id: FileId( - 0, - ), - full_range: 14..48, - focus_range: 17..21, - name: "main", - kind: Function, - }, - kind: Bin, - cfg: None, - }, - }, + ), }, Annotation { range: 7..11, @@ -424,9 +380,8 @@ fn main() { [ Annotation { range: 69..73, - kind: Runnable { - debug: false, - runnable: Runnable { + kind: Runnable( + Runnable { use_name_in_title: false, nav: NavigationTarget { file_id: FileId( @@ -440,27 +395,7 @@ fn main() { kind: Bin, cfg: None, }, - }, - }, - Annotation { - range: 69..73, - kind: Runnable { - debug: true, - runnable: Runnable { - use_name_in_title: false, - nav: NavigationTarget { - file_id: FileId( - 0, - ), - full_range: 66..100, - focus_range: 69..73, - name: "main", - kind: Function, - }, - kind: Bin, - cfg: None, - }, - }, + ), }, Annotation { range: 7..11, @@ -587,9 +522,8 @@ fn main() {} [ Annotation { range: 3..7, - kind: Runnable { - debug: false, - runnable: Runnable { + kind: Runnable( + Runnable { use_name_in_title: false, nav: NavigationTarget { file_id: FileId( @@ -603,27 +537,7 @@ fn main() {} kind: Bin, cfg: None, }, - }, - }, - Annotation { - range: 3..7, - kind: Runnable { - debug: true, - runnable: Runnable { - use_name_in_title: false, - nav: NavigationTarget { - file_id: FileId( - 0, - ), - full_range: 0..12, - focus_range: 3..7, - name: "main", - kind: Function, - }, - kind: Bin, - cfg: None, - }, - }, + ), }, Annotation { range: 3..7, @@ -662,9 +576,8 @@ fn main() { [ Annotation { range: 61..65, - kind: Runnable { - debug: false, - runnable: Runnable { + kind: Runnable( + Runnable { use_name_in_title: false, nav: NavigationTarget { file_id: FileId( @@ -678,27 +591,7 @@ fn main() { kind: Bin, cfg: None, }, - }, - }, - Annotation { - range: 61..65, - kind: Runnable { - debug: true, - runnable: Runnable { - use_name_in_title: false, - nav: NavigationTarget { - file_id: FileId( - 0, - ), - full_range: 58..95, - focus_range: 61..65, - name: "main", - kind: Function, - }, - kind: Bin, - cfg: None, - }, - }, + ), }, Annotation { range: 7..11, @@ -806,9 +699,8 @@ fn my_cool_test() {} [ Annotation { range: 3..7, - kind: Runnable { - debug: false, - runnable: Runnable { + kind: Runnable( + Runnable { use_name_in_title: false, nav: NavigationTarget { file_id: FileId( @@ -822,33 +714,12 @@ fn my_cool_test() {} kind: Bin, cfg: None, }, - }, - }, - Annotation { - range: 3..7, - kind: Runnable { - debug: true, - runnable: Runnable { - use_name_in_title: false, - nav: NavigationTarget { - file_id: FileId( - 0, - ), - full_range: 0..12, - focus_range: 3..7, - name: "main", - kind: Function, - }, - kind: Bin, - cfg: None, - }, - }, + ), }, Annotation { range: 18..23, - kind: Runnable { - debug: false, - runnable: Runnable { + kind: Runnable( + Runnable { use_name_in_title: false, nav: NavigationTarget { file_id: FileId( @@ -865,36 +736,12 @@ fn my_cool_test() {} }, cfg: None, }, - }, - }, - Annotation { - range: 18..23, - kind: Runnable { - debug: true, - runnable: Runnable { - use_name_in_title: false, - nav: NavigationTarget { - file_id: FileId( - 0, - ), - full_range: 14..64, - focus_range: 18..23, - name: "tests", - kind: Module, - description: "mod tests", - }, - kind: TestMod { - path: "tests", - }, - cfg: None, - }, - }, + ), }, Annotation { range: 45..57, - kind: Runnable { - debug: false, - runnable: Runnable { + kind: Runnable( + Runnable { use_name_in_title: false, nav: NavigationTarget { file_id: FileId( @@ -915,34 +762,7 @@ fn my_cool_test() {} }, cfg: None, }, - }, - }, - Annotation { - range: 45..57, - kind: Runnable { - debug: true, - runnable: Runnable { - use_name_in_title: false, - nav: NavigationTarget { - file_id: FileId( - 0, - ), - full_range: 30..62, - focus_range: 45..57, - name: "my_cool_test", - kind: Function, - }, - kind: Test { - test_id: Path( - "tests::my_cool_test", - ), - attr: TestAttr { - ignore: false, - }, - }, - cfg: None, - }, - }, + ), }, Annotation { range: 3..7,