From 733753c53c142846abfc5aa4a02a7a67d9be34f6 Mon Sep 17 00:00:00 2001 From: Wilfred Hughes Date: Thu, 14 Mar 2024 11:03:30 -0700 Subject: [PATCH] refactor: Allow Runnable to be None --- crates/rust-analyzer/src/handlers/request.rs | 19 +++--- crates/rust-analyzer/src/lsp/to_proto.rs | 68 ++++++++++---------- 2 files changed, 45 insertions(+), 42 deletions(-) diff --git a/crates/rust-analyzer/src/handlers/request.rs b/crates/rust-analyzer/src/handlers/request.rs index 4a6a546848bd..13a5e8b66431 100644 --- a/crates/rust-analyzer/src/handlers/request.rs +++ b/crates/rust-analyzer/src/handlers/request.rs @@ -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 @@ -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 }) } } @@ -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(); diff --git a/crates/rust-analyzer/src/lsp/to_proto.rs b/crates/rust-analyzer/src/lsp/to_proto.rs index 8dc2744a6327..2e0f735ca74c 100644 --- a/crates/rust-analyzer/src/lsp/to_proto.rs +++ b/crates/rust-analyzer/src/lsp/to_proto.rs @@ -1345,7 +1345,7 @@ pub(crate) fn code_action( pub(crate) fn runnable( snap: &GlobalStateSnapshot, runnable: Runnable, -) -> Cancellable { +) -> Cancellable> { let config = snap.config.runnables(); let target_spec = TargetSpec::for_file(snap, runnable.nav.file_id)?; @@ -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, @@ -1372,7 +1372,7 @@ pub(crate) fn runnable( executable_args, expect_test: None, }), - }) + })) } None => { let (cargo_args, executable_args) = @@ -1380,7 +1380,7 @@ pub(crate) fn runnable( 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, @@ -1392,7 +1392,7 @@ pub(crate) fn runnable( executable_args, expect_test: None, }), - }) + })) } } } @@ -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 } => { @@ -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(), }) }