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)]
pub enum AnnotationKind {
Runnable { debug: bool, runnable: Runnable },
Runnable(Runnable),
HasImpls { position: FilePosition, data: Option<Vec<NavigationTarget>> },
HasReferences { position: FilePosition, data: Option<Vec<FileRange>> },
}
@ -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 @@ fn check(ra_fixture: &str, expect: Expect) {
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 @@ 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(
@ -837,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(
@ -880,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(
@ -930,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,

View File

@ -74,16 +74,6 @@ pub fn label(&self, target: Option<String>) -> String {
}
}
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 {

View File

@ -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,51 @@ pub(crate) fn handle_code_lens(
let file_id = from_proto::file_id(&snap, &params.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<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(

View File

@ -988,21 +988,42 @@ pub(crate) fn runnable(
}
pub(crate) fn code_lens(
acc: &mut Vec<lsp_types::CodeLens>,
snap: &GlobalStateSnapshot,
annotation: Annotation,
) -> Result<lsp_types::CodeLens> {
) -> 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 {