From ee8154912fe8a2af22bfe8750a45ac4eb9ffe4bc Mon Sep 17 00:00:00 2001 From: Anatol Liu Date: Mon, 9 Nov 2020 13:05:20 -0800 Subject: [PATCH 1/6] add open cargo.toml action --- crates/hir/src/code_model.rs | 15 +- crates/ide/.DS_Store | Bin 0 -> 6148 bytes crates/ide/src/syntax_highlighting.rs | 3 - crates/rust-analyzer/src/handlers.rs | 23 + crates/rust-analyzer/src/lsp_ext.rs | 14 + crates/rust-analyzer/src/main_loop.rs | 1 + docs/dev/lsp-extensions.md | 27 +- editors/code/package-lock.json | 3385 ++++++++++++++++++++++++- editors/code/package.json | 9 + editors/code/src/commands.ts | 23 +- editors/code/src/lsp_ext.ts | 6 + editors/code/src/main.ts | 1 + 12 files changed, 3478 insertions(+), 29 deletions(-) create mode 100644 crates/ide/.DS_Store diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index fadd010e2a78..30a5e45809b9 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs @@ -41,7 +41,7 @@ use rustc_hash::FxHashSet; use stdx::impl_from; use syntax::{ ast::{self, AttrsOwner, NameOwner}, - AstNode, SmolStr, SyntaxKind, + AstNode, SmolStr, }; use tt::{Ident, Leaf, Literal, TokenTree}; @@ -787,19 +787,6 @@ impl Function { pub fn has_body(self, db: &dyn HirDatabase) -> bool { db.function_data(self.id).has_body } - - /// whether this function is associated with some trait/impl - pub fn is_assoc_item(self, db: &dyn HirDatabase) -> bool { - let fn_parent_kind = self - .source(db) - .value - .syntax() - .parent() - .and_then(|s| s.parent()) - .and_then(|s| Some(s.kind())); - - matches!(fn_parent_kind, Some(SyntaxKind::IMPL) | Some(SyntaxKind::TRAIT)) - } } // Note: logically, this belongs to `hir_ty`, but we are not using it there yet. diff --git a/crates/ide/.DS_Store b/crates/ide/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..a566736aa9e5aab4c0493be03fee3ac09047de3d GIT binary patch literal 6148 zcmeHK%}T>S5Z-O8CKe=u2L%rS4_~6(Uy@^Pff!S|%c4n6S6848N#_Jt#g)xIMrhy_BbZEX297i3Ig7P48 ztdU;2VeCTh-Mt)%CdYqd0KZ+4r7U6RZujf=3xhPPR^ND`Selre()8)FUY;rICw4pT z+L@De(z=thj72-}eAh{uftR%#ezYC>SAMjY>^hmB9AR)Hjzb8Lw&NZKez#@JZN^dN z1d*SJda}lGGl;ynTaP=Dmx_2*T-f_g)`mLTlI7%rNnO)h#(b|=TU)D`wc2XGV)iyR zs$j3K_xrl0EiCUGoOZ5nZtw0N9-p3HB>xfCjIRI9@_T0UO^Eh^9hIVJ;Cy zK)6f=l&M@jF}O?zzp3IZg}FqT&bXKv>ZqBydZBPJJNQiz&bTF!T4I107-pa-y9S>B z+dsemhm)vB3=jij#Q-mD*;{p3k~&*Q7Kdl833>*Kf^oUT&lE7kQ4Fzo6wiYS0l$d` VprtUE2p$l+2nZUeAqM`Gfe+oaU=jcT literal 0 HcmV?d00001 diff --git a/crates/ide/src/syntax_highlighting.rs b/crates/ide/src/syntax_highlighting.rs index 41fd36edf8ff..efcc8ecfe6b0 100644 --- a/crates/ide/src/syntax_highlighting.rs +++ b/crates/ide/src/syntax_highlighting.rs @@ -746,9 +746,6 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight { if func.is_unsafe(db) { h |= HighlightModifier::Unsafe; } - if func.is_assoc_item(db) && func.self_param(db).is_none() { - h |= HighlightModifier::Static; - } return h; } hir::ModuleDef::Adt(hir::Adt::Struct(_)) => HighlightTag::Struct, diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index 049c583a42cb..9920e7fbcbe8 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -1315,6 +1315,29 @@ 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.clone()); + let cargo_toml_location = + Location::new(cargo_toml_url.clone(), Range::new(Position::new(0, 0), Position::new(0, 0))); + 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 f31f8d9001b7..5d1a203062fc 100644 --- a/crates/rust-analyzer/src/lsp_ext.rs +++ b/crates/rust-analyzer/src/lsp_ext.rs @@ -355,3 +355,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 d504572a83ee..2657479112f9 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_resolve_code_action) .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/docs/dev/lsp-extensions.md b/docs/dev/lsp-extensions.md index 780f5cb91fb4..8f287b77c8c2 100644 --- a/docs/dev/lsp-extensions.md +++ b/docs/dev/lsp-extensions.md @@ -1,5 +1,5 @@