diff --git a/crates/ide/src/annotations.rs b/crates/ide/src/annotations.rs index be74502112..9069854353 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), HasImpls { position: FilePosition, data: Option> }, HasReferences { position: FilePosition, data: Option> }, } @@ -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( @@ -59,20 +57,7 @@ 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 { - range, - kind: AnnotationKind::Runnable { debug: true, runnable: runnable.clone() }, - }); - - if config.run { - annotations.push(Annotation { - range, - kind: AnnotationKind::Runnable { debug: false, runnable }, - }); - } - - annotations.extend(dbg_runnable); + annotations.push(Annotation { range, kind: AnnotationKind::Runnable(runnable) }); } } @@ -194,8 +179,6 @@ mod tests { annotate_impls: true, annotate_references: true, annotate_method_references: true, - run: true, - debug: true, }, file_id, ) @@ -223,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( @@ -239,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, @@ -329,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( @@ -345,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, @@ -439,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( @@ -455,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, @@ -602,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( @@ -618,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, @@ -677,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( @@ -693,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, @@ -821,9 +699,8 @@ mod tests { [ Annotation { range: 3..7, - kind: Runnable { - debug: false, - runnable: Runnable { + kind: Runnable( + Runnable { use_name_in_title: false, nav: NavigationTarget { file_id: FileId( @@ -837,33 +714,12 @@ mod tests { 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( @@ -880,36 +736,12 @@ mod tests { }, 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( @@ -930,34 +762,7 @@ mod tests { }, 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, diff --git a/crates/ide/src/runnables.rs b/crates/ide/src/runnables.rs index c1a068170b..51c01ea31d 100644 --- a/crates/ide/src/runnables.rs +++ b/crates/ide/src/runnables.rs @@ -74,16 +74,6 @@ impl Runnable { } } - pub fn debugee(&self) -> bool { - matches!( - &self.kind, - RunnableKind::TestMod { .. } - | RunnableKind::Test { .. } - | RunnableKind::Bench { .. } - | RunnableKind::Bin - ) - } - 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/handlers.rs b/crates/rust-analyzer/src/handlers.rs index 57b309a075..af53eb2396 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -26,7 +26,7 @@ use lsp_types::{ }; 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,51 @@ 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, + }, + 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 8c8ff826e1..060b69e088 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -988,21 +988,42 @@ 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 } => { + AnnotationKind::Runnable(run) => { let line_index = snap.file_line_index(run.nav.file_id)?; let annotation_range = range(&line_index, annotation.range); let title = run.title(); + 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 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 }) + 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)?; @@ -1041,7 +1062,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 +1091,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 {