9563: internal: move some presentation-specific concerns out of IDE crate r=Veykril a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2021-07-11 11:07:56 +00:00 committed by GitHub
commit 06cee26a2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 94 additions and 267 deletions

View File

@ -29,7 +29,7 @@ pub struct Annotation {
#[derive(Debug)] #[derive(Debug)]
pub enum AnnotationKind { pub enum AnnotationKind {
Runnable { debug: bool, runnable: Runnable }, Runnable(Runnable),
HasImpls { position: FilePosition, data: Option<Vec<NavigationTarget>> }, HasImpls { position: FilePosition, data: Option<Vec<NavigationTarget>> },
HasReferences { position: FilePosition, data: Option<Vec<FileRange>> }, HasReferences { position: FilePosition, data: Option<Vec<FileRange>> },
} }
@ -40,8 +40,6 @@ pub struct AnnotationConfig {
pub annotate_impls: bool, pub annotate_impls: bool,
pub annotate_references: bool, pub annotate_references: bool,
pub annotate_method_references: bool, pub annotate_method_references: bool,
pub run: bool,
pub debug: bool,
} }
pub(crate) fn annotations( pub(crate) fn annotations(
@ -59,20 +57,7 @@ pub(crate) fn annotations(
let range = runnable.nav.focus_or_full_range(); 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 annotations.push(Annotation { range, kind: AnnotationKind::Runnable(runnable) });
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);
} }
} }
@ -194,8 +179,6 @@ mod tests {
annotate_impls: true, annotate_impls: true,
annotate_references: true, annotate_references: true,
annotate_method_references: true, annotate_method_references: true,
run: true,
debug: true,
}, },
file_id, file_id,
) )
@ -223,9 +206,8 @@ fn main() {
[ [
Annotation { Annotation {
range: 53..57, range: 53..57,
kind: Runnable { kind: Runnable(
debug: false, Runnable {
runnable: Runnable {
use_name_in_title: false, use_name_in_title: false,
nav: NavigationTarget { nav: NavigationTarget {
file_id: FileId( file_id: FileId(
@ -239,27 +221,7 @@ fn main() {
kind: Bin, kind: Bin,
cfg: None, 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 { Annotation {
range: 6..10, range: 6..10,
@ -329,9 +291,8 @@ fn main() {
[ [
Annotation { Annotation {
range: 17..21, range: 17..21,
kind: Runnable { kind: Runnable(
debug: false, Runnable {
runnable: Runnable {
use_name_in_title: false, use_name_in_title: false,
nav: NavigationTarget { nav: NavigationTarget {
file_id: FileId( file_id: FileId(
@ -345,27 +306,7 @@ fn main() {
kind: Bin, kind: Bin,
cfg: None, 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 { Annotation {
range: 7..11, range: 7..11,
@ -439,9 +380,8 @@ fn main() {
[ [
Annotation { Annotation {
range: 69..73, range: 69..73,
kind: Runnable { kind: Runnable(
debug: false, Runnable {
runnable: Runnable {
use_name_in_title: false, use_name_in_title: false,
nav: NavigationTarget { nav: NavigationTarget {
file_id: FileId( file_id: FileId(
@ -455,27 +395,7 @@ fn main() {
kind: Bin, kind: Bin,
cfg: None, 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 { Annotation {
range: 7..11, range: 7..11,
@ -602,9 +522,8 @@ fn main() {}
[ [
Annotation { Annotation {
range: 3..7, range: 3..7,
kind: Runnable { kind: Runnable(
debug: false, Runnable {
runnable: Runnable {
use_name_in_title: false, use_name_in_title: false,
nav: NavigationTarget { nav: NavigationTarget {
file_id: FileId( file_id: FileId(
@ -618,27 +537,7 @@ fn main() {}
kind: Bin, kind: Bin,
cfg: None, 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 { Annotation {
range: 3..7, range: 3..7,
@ -677,9 +576,8 @@ fn main() {
[ [
Annotation { Annotation {
range: 61..65, range: 61..65,
kind: Runnable { kind: Runnable(
debug: false, Runnable {
runnable: Runnable {
use_name_in_title: false, use_name_in_title: false,
nav: NavigationTarget { nav: NavigationTarget {
file_id: FileId( file_id: FileId(
@ -693,27 +591,7 @@ fn main() {
kind: Bin, kind: Bin,
cfg: None, 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 { Annotation {
range: 7..11, range: 7..11,
@ -821,9 +699,8 @@ mod tests {
[ [
Annotation { Annotation {
range: 3..7, range: 3..7,
kind: Runnable { kind: Runnable(
debug: false, Runnable {
runnable: Runnable {
use_name_in_title: false, use_name_in_title: false,
nav: NavigationTarget { nav: NavigationTarget {
file_id: FileId( file_id: FileId(
@ -837,33 +714,12 @@ mod tests {
kind: Bin, kind: Bin,
cfg: None, 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 { Annotation {
range: 18..23, range: 18..23,
kind: Runnable { kind: Runnable(
debug: false, Runnable {
runnable: Runnable {
use_name_in_title: false, use_name_in_title: false,
nav: NavigationTarget { nav: NavigationTarget {
file_id: FileId( file_id: FileId(
@ -880,36 +736,12 @@ mod tests {
}, },
cfg: None, 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 { Annotation {
range: 45..57, range: 45..57,
kind: Runnable { kind: Runnable(
debug: false, Runnable {
runnable: Runnable {
use_name_in_title: false, use_name_in_title: false,
nav: NavigationTarget { nav: NavigationTarget {
file_id: FileId( file_id: FileId(
@ -930,34 +762,7 @@ mod tests {
}, },
cfg: None, 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 { Annotation {
range: 3..7, range: 3..7,

View File

@ -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 { pub fn title(&self) -> String {
let mut s = String::from("\u{fe0e} Run "); let mut s = String::from("\u{fe0e} Run ");
if self.use_name_in_title { if self.use_name_in_title {

View File

@ -26,7 +26,7 @@ use lsp_types::{
}; };
use project_model::TargetKind; use project_model::TargetKind;
use serde_json::json; use serde_json::json;
use stdx::format_to; use stdx::{format_to, never};
use syntax::{algo, ast, AstNode, TextRange, TextSize}; use syntax::{algo, ast, AstNode, TextRange, TextSize};
use crate::{ use crate::{
@ -1133,9 +1133,7 @@ pub(crate) fn handle_code_lens(
let file_id = from_proto::file_id(&snap, &params.text_document.uri)?; let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
let cargo_target_spec = CargoTargetSpec::for_file(&snap, file_id)?; let cargo_target_spec = CargoTargetSpec::for_file(&snap, file_id)?;
let lenses = snap let annotations = snap.analysis.annotations(
.analysis
.annotations(
&AnnotationConfig { &AnnotationConfig {
binary_target: cargo_target_spec binary_target: cargo_target_spec
.map(|spec| { .map(|spec| {
@ -1149,25 +1147,37 @@ pub(crate) fn handle_code_lens(
annotate_impls: lens_config.implementations, annotate_impls: lens_config.implementations,
annotate_references: lens_config.refs, annotate_references: lens_config.refs,
annotate_method_references: lens_config.method_refs, annotate_method_references: lens_config.method_refs,
run: lens_config.run,
debug: lens_config.debug,
}, },
file_id, file_id,
)? )?;
.into_iter()
.map(|annotation| to_proto::code_lens(&snap, annotation).unwrap())
.collect();
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( pub(crate) fn handle_code_lens_resolve(
snap: GlobalStateSnapshot, snap: GlobalStateSnapshot,
code_lens: CodeLens, code_lens: CodeLens,
) -> Result<CodeLens> { ) -> Result<CodeLens> {
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( pub(crate) fn handle_document_highlight(

View File

@ -988,21 +988,42 @@ pub(crate) fn runnable(
} }
pub(crate) fn code_lens( pub(crate) fn code_lens(
acc: &mut Vec<lsp_types::CodeLens>,
snap: &GlobalStateSnapshot, snap: &GlobalStateSnapshot,
annotation: Annotation, annotation: Annotation,
) -> Result<lsp_types::CodeLens> { ) -> Result<()> {
match annotation.kind { match annotation.kind {
AnnotationKind::Runnable { debug, runnable: run } => { AnnotationKind::Runnable(run) => {
let line_index = snap.file_line_index(run.nav.file_id)?; let line_index = snap.file_line_index(run.nav.file_id)?;
let annotation_range = range(&line_index, annotation.range); let annotation_range = range(&line_index, annotation.range);
let title = run.title(); 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 r = runnable(snap, run)?;
let command = let lens_config = snap.config.lens();
if debug { command::debug_single(&r) } else { command::run_single(&r, &title) }; if lens_config.run {
let command = 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,
})
}
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 } => { AnnotationKind::HasImpls { position: file_position, data } => {
let line_index = snap.file_line_index(file_position.file_id)?; 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, range: annotation_range,
command, command,
data: Some(to_value(lsp_ext::CodeLensResolveData::Impls(goto_params)).unwrap()), 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, range: annotation_range,
command, command,
data: Some(to_value(lsp_ext::CodeLensResolveData::References(doc_pos)).unwrap()), data: Some(to_value(lsp_ext::CodeLensResolveData::References(doc_pos)).unwrap()),
}) })
} }
} }
Ok(())
} }
pub(crate) mod command { pub(crate) mod command {