Skip to content

Commit b92854c

Browse files
committed
refactor: Allow Runnable to be None
1 parent 5aa990f commit b92854c

File tree

2 files changed

+45
-42
lines changed

2 files changed

+45
-42
lines changed

crates/rust-analyzer/src/handlers/request.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -824,15 +824,16 @@ pub(crate) fn handle_runnables(
824824
if should_skip_target(&runnable, target_spec.as_ref()) {
825825
continue;
826826
}
827-
let mut runnable = to_proto::runnable(&snap, runnable)?;
828-
if expect_test {
829-
#[allow(irrefutable_let_patterns)]
830-
if let lsp_ext::RunnableArgs::Cargo(r) = &mut runnable.args {
831-
runnable.label = format!("{} + expect", runnable.label);
832-
r.expect_test = Some(true);
827+
if let Some(mut runnable) = to_proto::runnable(&snap, runnable)? {
828+
if expect_test {
829+
#[allow(irrefutable_let_patterns)]
830+
if let lsp_ext::RunnableArgs::Cargo(r) = &mut runnable.args {
831+
runnable.label = format!("{} + expect", runnable.label);
832+
r.expect_test = Some(true);
833+
}
833834
}
835+
res.push(runnable);
834836
}
835-
res.push(runnable);
836837
}
837838

838839
// Add `cargo check` and `cargo test` for all targets of the whole package
@@ -904,7 +905,7 @@ pub(crate) fn handle_related_tests(
904905
let tests = snap.analysis.related_tests(position, None)?;
905906
let mut res = Vec::new();
906907
for it in tests {
907-
if let Ok(runnable) = to_proto::runnable(&snap, it) {
908+
if let Ok(Some(runnable)) = to_proto::runnable(&snap, it) {
908909
res.push(lsp_ext::TestInfo { runnable })
909910
}
910911
}
@@ -1909,7 +1910,7 @@ fn runnable_action_links(
19091910
}
19101911

19111912
let title = runnable.title();
1912-
let r = to_proto::runnable(snap, runnable).ok()?;
1913+
let r = to_proto::runnable(snap, runnable).ok()??;
19131914

19141915
let mut group = lsp_ext::CommandLinkGroup::default();
19151916

crates/rust-analyzer/src/lsp/to_proto.rs

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,7 +1345,7 @@ pub(crate) fn code_action(
13451345
pub(crate) fn runnable(
13461346
snap: &GlobalStateSnapshot,
13471347
runnable: Runnable,
1348-
) -> Cancellable<lsp_ext::Runnable> {
1348+
) -> Cancellable<Option<lsp_ext::Runnable>> {
13491349
let config = snap.config.runnables();
13501350
let target_spec = TargetSpec::for_file(snap, runnable.nav.file_id)?;
13511351

@@ -1360,7 +1360,7 @@ pub(crate) fn runnable(
13601360
let label = runnable.label(Some(target));
13611361
let location = location_link(snap, None, runnable.nav)?;
13621362

1363-
Ok(lsp_ext::Runnable {
1363+
Ok(Some(lsp_ext::Runnable {
13641364
label,
13651365
location: Some(location),
13661366
kind: lsp_ext::RunnableKind::Cargo,
@@ -1372,15 +1372,15 @@ pub(crate) fn runnable(
13721372
executable_args,
13731373
expect_test: None,
13741374
}),
1375-
})
1375+
}))
13761376
}
13771377
None => {
13781378
let (cargo_args, executable_args) =
13791379
CargoTargetSpec::runnable_args(snap, None, &runnable.kind, &runnable.cfg);
13801380
let label = runnable.label(None);
13811381
let location = location_link(snap, None, runnable.nav)?;
13821382

1383-
Ok(lsp_ext::Runnable {
1383+
Ok(Some(lsp_ext::Runnable {
13841384
label,
13851385
location: Some(location),
13861386
kind: lsp_ext::RunnableKind::Cargo,
@@ -1392,7 +1392,7 @@ pub(crate) fn runnable(
13921392
executable_args,
13931393
expect_test: None,
13941394
}),
1395-
})
1395+
}))
13961396
}
13971397
}
13981398
}
@@ -1418,34 +1418,36 @@ pub(crate) fn code_lens(
14181418
};
14191419
let r = runnable(snap, run)?;
14201420

1421-
let has_root = match &r.args {
1422-
lsp_ext::RunnableArgs::Cargo(c) => c.workspace_root.is_some(),
1423-
};
1421+
if let Some(r) = r {
1422+
let has_root = match &r.args {
1423+
lsp_ext::RunnableArgs::Cargo(c) => c.workspace_root.is_some(),
1424+
};
14241425

1425-
let lens_config = snap.config.lens();
1426-
if lens_config.run && client_commands_config.run_single && has_root {
1427-
let command = command::run_single(&r, &title);
1428-
acc.push(lsp_types::CodeLens {
1429-
range: annotation_range,
1430-
command: Some(command),
1431-
data: None,
1432-
})
1433-
}
1434-
if lens_config.debug && can_debug && client_commands_config.debug_single {
1435-
let command = command::debug_single(&r);
1436-
acc.push(lsp_types::CodeLens {
1437-
range: annotation_range,
1438-
command: Some(command),
1439-
data: None,
1440-
})
1441-
}
1442-
if lens_config.interpret {
1443-
let command = command::interpret_single(&r);
1444-
acc.push(lsp_types::CodeLens {
1445-
range: annotation_range,
1446-
command: Some(command),
1447-
data: None,
1448-
})
1426+
let lens_config = snap.config.lens();
1427+
if lens_config.run && client_commands_config.run_single && has_root {
1428+
let command = command::run_single(&r, &title);
1429+
acc.push(lsp_types::CodeLens {
1430+
range: annotation_range,
1431+
command: Some(command),
1432+
data: None,
1433+
})
1434+
}
1435+
if lens_config.debug && can_debug && client_commands_config.debug_single {
1436+
let command = command::debug_single(&r);
1437+
acc.push(lsp_types::CodeLens {
1438+
range: annotation_range,
1439+
command: Some(command),
1440+
data: None,
1441+
})
1442+
}
1443+
if lens_config.interpret {
1444+
let command = command::interpret_single(&r);
1445+
acc.push(lsp_types::CodeLens {
1446+
range: annotation_range,
1447+
command: Some(command),
1448+
data: None,
1449+
})
1450+
}
14491451
}
14501452
}
14511453
AnnotationKind::HasImpls { pos, data } => {
@@ -1575,7 +1577,7 @@ pub(crate) fn test_item(
15751577
.file
15761578
.map(|f| lsp_types::TextDocumentIdentifier { uri: url(snap, f) }),
15771579
range: line_index.and_then(|l| Some(range(l, test_item.text_range?))),
1578-
runnable: test_item.runnable.and_then(|r| runnable(snap, r).ok()),
1580+
runnable: test_item.runnable.and_then(|r| runnable(snap, r).ok()).flatten(),
15791581
})
15801582
}
15811583

0 commit comments

Comments
 (0)