Skip to content

Commit

Permalink
refactor: Allow Runnable to be None
Browse files Browse the repository at this point in the history
  • Loading branch information
Wilfred committed Mar 15, 2024
1 parent 7dd0ece commit e493fd4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 42 deletions.
19 changes: 10 additions & 9 deletions crates/rust-analyzer/src/handlers/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -824,15 +824,16 @@ pub(crate) fn handle_runnables(
if should_skip_target(&runnable, target_spec.as_ref()) {
continue;
}
let mut runnable = to_proto::runnable(&snap, runnable)?;
if expect_test {
#[allow(irrefutable_let_patterns)]
if let lsp_ext::RunnableArgs::Cargo(r) = &mut runnable.args {
runnable.label = format!("{} + expect", runnable.label);
r.expect_test = Some(true);
if let Some(mut runnable) = to_proto::runnable(&snap, runnable)? {
if expect_test {
#[allow(irrefutable_let_patterns)]
if let lsp_ext::RunnableArgs::Cargo(r) = &mut runnable.args {
runnable.label = format!("{} + expect", runnable.label);
r.expect_test = Some(true);
}
}
res.push(runnable);
}
res.push(runnable);
}

// Add `cargo check` and `cargo test` for all targets of the whole package
Expand Down Expand Up @@ -904,7 +905,7 @@ pub(crate) fn handle_related_tests(
let tests = snap.analysis.related_tests(position, None)?;
let mut res = Vec::new();
for it in tests {
if let Ok(runnable) = to_proto::runnable(&snap, it) {
if let Ok(Some(runnable)) = to_proto::runnable(&snap, it) {
res.push(lsp_ext::TestInfo { runnable })
}
}
Expand Down Expand Up @@ -1908,7 +1909,7 @@ fn runnable_action_links(
}

let title = runnable.title();
let r = to_proto::runnable(snap, runnable).ok()?;
let r = to_proto::runnable(snap, runnable).ok()??;

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

Expand Down
68 changes: 35 additions & 33 deletions crates/rust-analyzer/src/lsp/to_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1342,7 +1342,7 @@ pub(crate) fn code_action(
pub(crate) fn runnable(
snap: &GlobalStateSnapshot,
runnable: Runnable,
) -> Cancellable<lsp_ext::Runnable> {
) -> Cancellable<Option<lsp_ext::Runnable>> {
let config = snap.config.runnables();
let target_spec = TargetSpec::for_file(snap, runnable.nav.file_id)?;

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

Ok(lsp_ext::Runnable {
Ok(Some(lsp_ext::Runnable {
label,
location: Some(location),
kind: lsp_ext::RunnableKind::Cargo,
Expand All @@ -1369,15 +1369,15 @@ pub(crate) fn runnable(
executable_args,
expect_test: None,
}),
})
}))
}
None => {
let (cargo_args, executable_args) =
CargoTargetSpec::runnable_args(snap, None, &runnable.kind, &runnable.cfg);
let label = runnable.label(None);
let location = location_link(snap, None, runnable.nav)?;

Ok(lsp_ext::Runnable {
Ok(Some(lsp_ext::Runnable {
label,
location: Some(location),
kind: lsp_ext::RunnableKind::Cargo,
Expand All @@ -1389,7 +1389,7 @@ pub(crate) fn runnable(
executable_args,
expect_test: None,
}),
})
}))
}
}
}
Expand All @@ -1415,34 +1415,36 @@ pub(crate) fn code_lens(
};
let r = runnable(snap, run)?;

let has_root = match &r.args {
lsp_ext::RunnableArgs::Cargo(c) => c.workspace_root.is_some(),
};
if let Some(r) = r {
let has_root = match &r.args {
lsp_ext::RunnableArgs::Cargo(c) => c.workspace_root.is_some(),
};

let lens_config = snap.config.lens();
if lens_config.run && client_commands_config.run_single && has_root {
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 && client_commands_config.debug_single {
let command = command::debug_single(&r);
acc.push(lsp_types::CodeLens {
range: annotation_range,
command: Some(command),
data: None,
})
}
if lens_config.interpret {
let command = command::interpret_single(&r);
acc.push(lsp_types::CodeLens {
range: annotation_range,
command: Some(command),
data: None,
})
let lens_config = snap.config.lens();
if lens_config.run && client_commands_config.run_single && has_root {
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 && client_commands_config.debug_single {
let command = command::debug_single(&r);
acc.push(lsp_types::CodeLens {
range: annotation_range,
command: Some(command),
data: None,
})
}
if lens_config.interpret {
let command = command::interpret_single(&r);
acc.push(lsp_types::CodeLens {
range: annotation_range,
command: Some(command),
data: None,
})
}
}
}
AnnotationKind::HasImpls { pos, data } => {
Expand Down Expand Up @@ -1572,7 +1574,7 @@ pub(crate) fn test_item(
.file
.map(|f| lsp_types::TextDocumentIdentifier { uri: url(snap, f) }),
range: line_index.and_then(|l| Some(range(l, test_item.text_range?))),
runnable: test_item.runnable.and_then(|r| runnable(snap, r).ok()),
runnable: test_item.runnable.and_then(|r| runnable(snap, r).ok()).flatten(),
}
}

Expand Down

0 comments on commit e493fd4

Please sign in to comment.