From ecd1c11f9e8ae7718f4243d1eb4c9a45efd060a2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 8 Dec 2020 16:17:46 +0300 Subject: cleanup: match over unwrap --- crates/rust-analyzer/src/handlers.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'crates') diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index 1cf4139d2..7bfbc6103 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -1336,12 +1336,12 @@ pub(crate) fn handle_open_cargo_toml( ) -> Result> { let _p = profile::span("handle_open_cargo_toml"); let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?; - let maybe_cargo_spec = CargoTargetSpec::for_file(&snap, file_id)?; - if maybe_cargo_spec.is_none() { - return Ok(None); - } - let cargo_spec = maybe_cargo_spec.unwrap(); + let cargo_spec = match CargoTargetSpec::for_file(&snap, file_id)? { + Some(it) => it, + None => return Ok(None), + }; + let cargo_toml_path = cargo_spec.workspace_root.join("Cargo.toml"); if !cargo_toml_path.exists() { return Ok(None); -- cgit v1.2.3 From 944f30bd694be71d76e1a175033e19409fe9f8b4 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 8 Dec 2020 16:28:21 +0300 Subject: Open Cargo.toml opens more specific manifest --- crates/rust-analyzer/src/cargo_target_spec.rs | 12 +++++++++--- crates/rust-analyzer/src/handlers.rs | 10 +++------- 2 files changed, 12 insertions(+), 10 deletions(-) (limited to 'crates') diff --git a/crates/rust-analyzer/src/cargo_target_spec.rs b/crates/rust-analyzer/src/cargo_target_spec.rs index 1ab72bd91..8a8b4a32c 100644 --- a/crates/rust-analyzer/src/cargo_target_spec.rs +++ b/crates/rust-analyzer/src/cargo_target_spec.rs @@ -14,6 +14,7 @@ use crate::{global_state::GlobalStateSnapshot, Result}; #[derive(Clone)] pub(crate) struct CargoTargetSpec { pub(crate) workspace_root: AbsPathBuf, + pub(crate) cargo_toml: AbsPathBuf, pub(crate) package: String, pub(crate) target: String, pub(crate) target_kind: TargetKind, @@ -115,12 +116,17 @@ impl CargoTargetSpec { Some(it) => it, None => return Ok(None), }; + + let target_data = &cargo_ws[target]; + let package_data = &cargo_ws[target_data.package]; let res = CargoTargetSpec { workspace_root: cargo_ws.workspace_root().to_path_buf(), - package: cargo_ws.package_flag(&cargo_ws[cargo_ws[target].package]), - target: cargo_ws[target].name.clone(), - target_kind: cargo_ws[target].kind, + cargo_toml: package_data.manifest.clone(), + package: cargo_ws.package_flag(&package_data), + target: target_data.name.clone(), + target_kind: target_data.kind, }; + Ok(Some(res)) } diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index 7bfbc6103..6250171ba 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -1342,13 +1342,9 @@ pub(crate) fn handle_open_cargo_toml( None => return Ok(None), }; - let cargo_toml_path = cargo_spec.workspace_root.join("Cargo.toml"); - if !cargo_toml_path.exists() { - return Ok(None); - } - let cargo_toml_url = to_proto::url_from_abs_path(&cargo_toml_path); - let cargo_toml_location = Location::new(cargo_toml_url, Range::default()); - let res = lsp_types::GotoDefinitionResponse::from(cargo_toml_location); + let cargo_toml_url = to_proto::url_from_abs_path(&cargo_spec.cargo_toml); + let res: lsp_types::GotoDefinitionResponse = + Location::new(cargo_toml_url, Range::default()).into(); Ok(Some(res)) } -- cgit v1.2.3