From b1b7727e046b4b25dcca034ee767a7fc3238409d Mon Sep 17 00:00:00 2001 From: Anatol Liu Date: Thu, 12 Nov 2020 17:48:07 -0800 Subject: add open Cargo.toml action --- .gitignore | 1 + Cargo.lock | 5 +++-- crates/hir_ty/Cargo.toml | 1 + crates/hir_ty/src/tests.rs | 7 ++++--- crates/ide/.DS_Store | Bin 0 -> 6148 bytes crates/rust-analyzer/src/config.rs | 9 +++------ crates/rust-analyzer/src/handlers.rs | 22 ++++++++++++++++++++++ crates/rust-analyzer/src/lsp_ext.rs | 14 ++++++++++++++ crates/rust-analyzer/src/main_loop.rs | 1 + crates/stdx/src/lib.rs | 30 +----------------------------- docs/dev/lsp-extensions.md | 29 +++++++++++++++++++++++++++-- editors/code/package.json | 9 +++++++++ editors/code/src/commands.ts | 21 +++++++++++++++++++++ editors/code/src/lsp_ext.ts | 6 ++++++ editors/code/src/main.ts | 1 + 15 files changed, 114 insertions(+), 42 deletions(-) create mode 100644 crates/ide/.DS_Store diff --git a/.gitignore b/.gitignore index b205bf3fb..7e097c015 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ crates/*/target generated_assists.adoc generated_features.adoc generated_diagnostic.adoc +.DS_Store diff --git a/Cargo.lock b/Cargo.lock index 1a4a63550..c5645b2d2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -612,6 +612,7 @@ dependencies = [ "hir_expand", "itertools", "log", + "once_cell", "profile", "rustc-hash", "scoped-tls", @@ -1053,9 +1054,9 @@ checksum = "8d3b63360ec3cb337817c2dbd47ab4a0f170d285d8e5a2064600f3def1402397" [[package]] name = "once_cell" -version = "1.4.1" +version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "260e51e7efe62b592207e9e13a68e43692a7a279171d6ba57abd208bf23645ad" +checksum = "f53cef67919d7d247eb9a2f128ca9e522789967ef1eb4ccd8c71a95a8aedf596" [[package]] name = "oorandom" diff --git a/crates/hir_ty/Cargo.toml b/crates/hir_ty/Cargo.toml index fdc65a5c3..cf5c38a23 100644 --- a/crates/hir_ty/Cargo.toml +++ b/crates/hir_ty/Cargo.toml @@ -35,3 +35,4 @@ expect-test = "1.0" tracing = "0.1" tracing-subscriber = { version = "0.2", default-features = false, features = ["env-filter", "registry"] } tracing-tree = { version = "0.1.4" } +once_cell = { version = "1.5.0", features = ["unstable"] } diff --git a/crates/hir_ty/src/tests.rs b/crates/hir_ty/src/tests.rs index 104ef334c..0a400cb70 100644 --- a/crates/hir_ty/src/tests.rs +++ b/crates/hir_ty/src/tests.rs @@ -22,7 +22,8 @@ use hir_def::{ AssocItemId, DefWithBodyId, LocalModuleId, Lookup, ModuleDefId, }; use hir_expand::{db::AstDatabase, InFile}; -use stdx::{format_to, RacyFlag}; +use once_cell::race::OnceBool; +use stdx::format_to; use syntax::{ algo, ast::{self, AstNode}, @@ -40,8 +41,8 @@ use crate::{ // `env UPDATE_EXPECT=1 cargo test -p hir_ty` to update the snapshots. fn setup_tracing() -> Option { - static ENABLE: RacyFlag = RacyFlag::new(); - if !ENABLE.get(|| env::var("CHALK_DEBUG").is_ok()) { + static ENABLE: OnceBool = OnceBool::new(); + if !ENABLE.get_or_init(|| env::var("CHALK_DEBUG").is_ok()) { return None; } diff --git a/crates/ide/.DS_Store b/crates/ide/.DS_Store new file mode 100644 index 000000000..a566736aa Binary files /dev/null and b/crates/ide/.DS_Store differ diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index b4c738272..9cc14fe82 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -385,13 +385,10 @@ impl Config { } if let Some(code_action) = &doc_caps.code_action { - match (code_action.data_support, &code_action.resolve_support) { - (Some(true), Some(resolve_support)) => { - if resolve_support.properties.iter().any(|it| it == "edit") { - self.client_caps.code_action_resolve = true; - } + if let Some(resolve_support) = &code_action.resolve_support { + if resolve_support.properties.iter().any(|it| it == "edit") { + self.client_caps.code_action_resolve = true; } - _ => (), } } } diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index 95659b0db..782797e85 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -1322,6 +1322,28 @@ pub(crate) fn handle_open_docs( Ok(remote.and_then(|remote| Url::parse(&remote).ok())) } +pub(crate) fn handle_open_cargo_toml( + snap: GlobalStateSnapshot, + params: lsp_ext::OpenCargoTomlParams, +) -> 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_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); + Ok(Some(res)) +} + fn implementation_title(count: usize) -> String { if count == 1 { "1 implementation".into() diff --git a/crates/rust-analyzer/src/lsp_ext.rs b/crates/rust-analyzer/src/lsp_ext.rs index a7c3028e4..a5c65fa3e 100644 --- a/crates/rust-analyzer/src/lsp_ext.rs +++ b/crates/rust-analyzer/src/lsp_ext.rs @@ -354,3 +354,17 @@ impl Request for ExternalDocs { type Result = Option; const METHOD: &'static str = "experimental/externalDocs"; } + +pub enum OpenCargoToml {} + +impl Request for OpenCargoToml { + type Params = OpenCargoTomlParams; + type Result = Option; + const METHOD: &'static str = "experimental/openCargoToml"; +} + +#[derive(Serialize, Deserialize, Debug)] +#[serde(rename_all = "camelCase")] +pub struct OpenCargoTomlParams { + pub text_document: TextDocumentIdentifier, +} diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index 6e6cac42e..68a53bbcb 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs @@ -438,6 +438,7 @@ impl GlobalState { .on::(handlers::handle_code_action_resolve) .on::(handlers::handle_hover) .on::(handlers::handle_open_docs) + .on::(handlers::handle_open_cargo_toml) .on::(handlers::handle_on_type_formatting) .on::(handlers::handle_document_symbol) .on::(handlers::handle_workspace_symbol) diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs index 59d89f47d..374ed5910 100644 --- a/crates/stdx/src/lib.rs +++ b/crates/stdx/src/lib.rs @@ -1,8 +1,5 @@ //! Missing batteries for standard libraries. -use std::{ - sync::atomic::{AtomicUsize, Ordering}, - time::Instant, -}; +use std::time::Instant; mod macros; pub mod panic_context; @@ -150,31 +147,6 @@ where left } -pub struct RacyFlag(AtomicUsize); - -impl RacyFlag { - pub const fn new() -> RacyFlag { - RacyFlag(AtomicUsize::new(!0)) - } - - pub fn get(&self, init: impl FnMut() -> bool) -> bool { - let mut init = Some(init); - self.get_impl(&mut || init.take().map_or(false, |mut f| f())) - } - - fn get_impl(&self, init: &mut dyn FnMut() -> bool) -> bool { - match self.0.load(Ordering::Relaxed) { - 0 => false, - 1 => true, - _ => { - let res = init(); - self.0.store(if res { 1 } else { 0 }, Ordering::Relaxed); - res - } - } - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/docs/dev/lsp-extensions.md b/docs/dev/lsp-extensions.md index 77d4e0ec9..db9727bee 100644 --- a/docs/dev/lsp-extensions.md +++ b/docs/dev/lsp-extensions.md @@ -1,8 +1,8 @@