Skip to content

Commit e493fd4

Browse files
committed
refactor: Allow Runnable to be None
1 parent 7dd0ece commit e493fd4

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
}
@@ -1908,7 +1909,7 @@ fn runnable_action_links(
19081909
}
19091910

19101911
let title = runnable.title();
1911-
let r = to_proto::runnable(snap, runnable).ok()?;
1912+
let r = to_proto::runnable(snap, runnable).ok()??;
19121913

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

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

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

@@ -1357,7 +1357,7 @@ pub(crate) fn runnable(
13571357
let label = runnable.label(Some(target));
13581358
let location = location_link(snap, None, runnable.nav)?;
13591359

1360-
Ok(lsp_ext::Runnable {
1360+
Ok(Some(lsp_ext::Runnable {
13611361
label,
13621362
location: Some(location),
13631363
kind: lsp_ext::RunnableKind::Cargo,
@@ -1369,15 +1369,15 @@ pub(crate) fn runnable(
13691369
executable_args,
13701370
expect_test: None,
13711371
}),
1372-
})
1372+
}))
13731373
}
13741374
None => {
13751375
let (cargo_args, executable_args) =
13761376
CargoTargetSpec::runnable_args(snap, None, &runnable.kind, &runnable.cfg);
13771377
let label = runnable.label(None);
13781378
let location = location_link(snap, None, runnable.nav)?;
13791379

1380-
Ok(lsp_ext::Runnable {
1380+
Ok(Some(lsp_ext::Runnable {
13811381
label,
13821382
location: Some(location),
13831383
kind: lsp_ext::RunnableKind::Cargo,
@@ -1389,7 +1389,7 @@ pub(crate) fn runnable(
13891389
executable_args,
13901390
expect_test: None,
13911391
}),
1392-
})
1392+
}))
13931393
}
13941394
}
13951395
}
@@ -1415,34 +1415,36 @@ pub(crate) fn code_lens(
14151415
};
14161416
let r = runnable(snap, run)?;
14171417

1418-
let has_root = match &r.args {
1419-
lsp_ext::RunnableArgs::Cargo(c) => c.workspace_root.is_some(),
1420-
};
1418+
if let Some(r) = r {
1419+
let has_root = match &r.args {
1420+
lsp_ext::RunnableArgs::Cargo(c) => c.workspace_root.is_some(),
1421+
};
14211422

1422-
let lens_config = snap.config.lens();
1423-
if lens_config.run && client_commands_config.run_single && has_root {
1424-
let command = command::run_single(&r, &title);
1425-
acc.push(lsp_types::CodeLens {
1426-
range: annotation_range,
1427-
command: Some(command),
1428-
data: None,
1429-
})
1430-
}
1431-
if lens_config.debug && can_debug && client_commands_config.debug_single {
1432-
let command = command::debug_single(&r);
1433-
acc.push(lsp_types::CodeLens {
1434-
range: annotation_range,
1435-
command: Some(command),
1436-
data: None,
1437-
})
1438-
}
1439-
if lens_config.interpret {
1440-
let command = command::interpret_single(&r);
1441-
acc.push(lsp_types::CodeLens {
1442-
range: annotation_range,
1443-
command: Some(command),
1444-
data: None,
1445-
})
1423+
let lens_config = snap.config.lens();
1424+
if lens_config.run && client_commands_config.run_single && has_root {
1425+
let command = command::run_single(&r, &title);
1426+
acc.push(lsp_types::CodeLens {
1427+
range: annotation_range,
1428+
command: Some(command),
1429+
data: None,
1430+
})
1431+
}
1432+
if lens_config.debug && can_debug && client_commands_config.debug_single {
1433+
let command = command::debug_single(&r);
1434+
acc.push(lsp_types::CodeLens {
1435+
range: annotation_range,
1436+
command: Some(command),
1437+
data: None,
1438+
})
1439+
}
1440+
if lens_config.interpret {
1441+
let command = command::interpret_single(&r);
1442+
acc.push(lsp_types::CodeLens {
1443+
range: annotation_range,
1444+
command: Some(command),
1445+
data: None,
1446+
})
1447+
}
14461448
}
14471449
}
14481450
AnnotationKind::HasImpls { pos, data } => {
@@ -1572,7 +1574,7 @@ pub(crate) fn test_item(
15721574
.file
15731575
.map(|f| lsp_types::TextDocumentIdentifier { uri: url(snap, f) }),
15741576
range: line_index.and_then(|l| Some(range(l, test_item.text_range?))),
1575-
runnable: test_item.runnable.and_then(|r| runnable(snap, r).ok()),
1577+
runnable: test_item.runnable.and_then(|r| runnable(snap, r).ok()).flatten(),
15761578
}
15771579
}
15781580

0 commit comments

Comments
 (0)