From e493fd4e84190afb7b3eacb5865e7814cece3fec 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 a05ae5548771..96f0c41ab651 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 }) } } @@ -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(); diff --git a/crates/rust-analyzer/src/lsp/to_proto.rs b/crates/rust-analyzer/src/lsp/to_proto.rs index e7e8432deb7e..d1f5d23afedc 100644 --- a/crates/rust-analyzer/src/lsp/to_proto.rs +++ b/crates/rust-analyzer/src/lsp/to_proto.rs @@ -1342,7 +1342,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)?; @@ -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, @@ -1369,7 +1369,7 @@ pub(crate) fn runnable( executable_args, expect_test: None, }), - }) + })) } None => { let (cargo_args, executable_args) = @@ -1377,7 +1377,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, @@ -1389,7 +1389,7 @@ pub(crate) fn runnable( executable_args, expect_test: None, }), - }) + })) } } } @@ -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 } => { @@ -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(), } }