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 27, 2024
1 parent 4d6a8c6 commit 733753c
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 @@ -1909,7 +1910,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 @@ -1345,7 +1345,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 @@ -1360,7 +1360,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 @@ -1372,15 +1372,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 @@ -1392,7 +1392,7 @@ pub(crate) fn runnable(
executable_args,
expect_test: None,
}),
})
}))
}
}
}
Expand All @@ -1418,34 +1418,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 @@ -1575,7 +1577,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 733753c

Please sign in to comment.