Skip to content

Commit 148e11a

Browse files
committed
prepare to move run/debug splitting to handlers
1 parent 325140a commit 148e11a

File tree

2 files changed

+50
-32
lines changed

2 files changed

+50
-32
lines changed

crates/rust-analyzer/src/handlers.rs

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use lsp_types::{
2626
};
2727
use project_model::TargetKind;
2828
use serde_json::json;
29-
use stdx::format_to;
29+
use stdx::{format_to, never};
3030
use syntax::{algo, ast, AstNode, TextRange, TextSize};
3131

3232
use crate::{
@@ -1133,41 +1133,53 @@ pub(crate) fn handle_code_lens(
11331133
let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
11341134
let cargo_target_spec = CargoTargetSpec::for_file(&snap, file_id)?;
11351135

1136-
let lenses = snap
1137-
.analysis
1138-
.annotations(
1139-
&AnnotationConfig {
1140-
binary_target: cargo_target_spec
1141-
.map(|spec| {
1142-
matches!(
1143-
spec.target_kind,
1144-
TargetKind::Bin | TargetKind::Example | TargetKind::Test
1145-
)
1146-
})
1147-
.unwrap_or(false),
1148-
annotate_runnables: lens_config.runnable(),
1149-
annotate_impls: lens_config.implementations,
1150-
annotate_references: lens_config.refs,
1151-
annotate_method_references: lens_config.method_refs,
1152-
run: lens_config.run,
1153-
debug: lens_config.debug,
1154-
},
1155-
file_id,
1156-
)?
1157-
.into_iter()
1158-
.map(|annotation| to_proto::code_lens(&snap, annotation).unwrap())
1159-
.collect();
1136+
let annotations = snap.analysis.annotations(
1137+
&AnnotationConfig {
1138+
binary_target: cargo_target_spec
1139+
.map(|spec| {
1140+
matches!(
1141+
spec.target_kind,
1142+
TargetKind::Bin | TargetKind::Example | TargetKind::Test
1143+
)
1144+
})
1145+
.unwrap_or(false),
1146+
annotate_runnables: lens_config.runnable(),
1147+
annotate_impls: lens_config.implementations,
1148+
annotate_references: lens_config.refs,
1149+
annotate_method_references: lens_config.method_refs,
1150+
run: lens_config.run,
1151+
debug: lens_config.debug,
1152+
},
1153+
file_id,
1154+
)?;
11601155

1161-
Ok(Some(lenses))
1156+
let mut res = Vec::new();
1157+
for a in annotations {
1158+
to_proto::code_lens(&mut res, &snap, a)?;
1159+
}
1160+
1161+
Ok(Some(res))
11621162
}
11631163

11641164
pub(crate) fn handle_code_lens_resolve(
11651165
snap: GlobalStateSnapshot,
11661166
code_lens: CodeLens,
11671167
) -> Result<CodeLens> {
1168-
let annotation = from_proto::annotation(&snap, code_lens)?;
1168+
let annotation = from_proto::annotation(&snap, code_lens.clone())?;
1169+
let annotation = snap.analysis.resolve_annotation(annotation)?;
1170+
1171+
let mut acc = Vec::new();
1172+
to_proto::code_lens(&mut acc, &snap, annotation)?;
11691173

1170-
to_proto::code_lens(&snap, snap.analysis.resolve_annotation(annotation)?)
1174+
let res = match acc.pop() {
1175+
Some(it) if acc.is_empty() => it,
1176+
_ => {
1177+
never!();
1178+
code_lens
1179+
}
1180+
};
1181+
1182+
Ok(res)
11711183
}
11721184

11731185
pub(crate) fn handle_document_highlight(

crates/rust-analyzer/src/to_proto.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -988,9 +988,10 @@ pub(crate) fn runnable(
988988
}
989989

990990
pub(crate) fn code_lens(
991+
acc: &mut Vec<lsp_types::CodeLens>,
991992
snap: &GlobalStateSnapshot,
992993
annotation: Annotation,
993-
) -> Result<lsp_types::CodeLens> {
994+
) -> Result<()> {
994995
match annotation.kind {
995996
AnnotationKind::Runnable { debug, runnable: run } => {
996997
let line_index = snap.file_line_index(run.nav.file_id)?;
@@ -1002,7 +1003,11 @@ pub(crate) fn code_lens(
10021003
let command =
10031004
if debug { command::debug_single(&r) } else { command::run_single(&r, &title) };
10041005

1005-
Ok(lsp_types::CodeLens { range: annotation_range, command: Some(command), data: None })
1006+
acc.push(lsp_types::CodeLens {
1007+
range: annotation_range,
1008+
command: Some(command),
1009+
data: None,
1010+
})
10061011
}
10071012
AnnotationKind::HasImpls { position: file_position, data } => {
10081013
let line_index = snap.file_line_index(file_position.file_id)?;
@@ -1041,7 +1046,7 @@ pub(crate) fn code_lens(
10411046
)
10421047
});
10431048

1044-
Ok(lsp_types::CodeLens {
1049+
acc.push(lsp_types::CodeLens {
10451050
range: annotation_range,
10461051
command,
10471052
data: Some(to_value(lsp_ext::CodeLensResolveData::Impls(goto_params)).unwrap()),
@@ -1070,13 +1075,14 @@ pub(crate) fn code_lens(
10701075
)
10711076
});
10721077

1073-
Ok(lsp_types::CodeLens {
1078+
acc.push(lsp_types::CodeLens {
10741079
range: annotation_range,
10751080
command,
10761081
data: Some(to_value(lsp_ext::CodeLensResolveData::References(doc_pos)).unwrap()),
10771082
})
10781083
}
10791084
}
1085+
Ok(())
10801086
}
10811087

10821088
pub(crate) mod command {

0 commit comments

Comments
 (0)