From ed7e9aa5d8a5dfcfdb703f8f75977f70c2d45cb8 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 28 Feb 2024 14:36:29 +0100 Subject: [PATCH] Simplify --- .../handlers/destructure_struct_binding.rs | 2 +- crates/proc-macro-srv/src/server.rs | 54 ++++++++----------- .../src/server/rust_analyzer_span.rs | 35 +++++++----- crates/proc-macro-srv/src/server/token_id.rs | 10 ++-- .../src/handlers/notification.rs | 2 +- docs/user/generated_config.adoc | 20 +++---- editors/code/package.json | 30 +++++------ 7 files changed, 77 insertions(+), 76 deletions(-) diff --git a/crates/ide-assists/src/handlers/destructure_struct_binding.rs b/crates/ide-assists/src/handlers/destructure_struct_binding.rs index 408dfe7538b..4edc52b614a 100644 --- a/crates/ide-assists/src/handlers/destructure_struct_binding.rs +++ b/crates/ide-assists/src/handlers/destructure_struct_binding.rs @@ -1,4 +1,4 @@ -use hir::{self, HasVisibility}; +use hir::HasVisibility; use ide_db::{ assists::{AssistId, AssistKind}, defs::Definition, diff --git a/crates/proc-macro-srv/src/server.rs b/crates/proc-macro-srv/src/server.rs index 5a814e23e7a..e8b340a43d3 100644 --- a/crates/proc-macro-srv/src/server.rs +++ b/crates/proc-macro-srv/src/server.rs @@ -54,33 +54,33 @@ fn spacing_to_external(spacing: Spacing) -> proc_macro::Spacing { } } -struct LiteralFormatter(bridge::Literal); - -impl LiteralFormatter { - /// Invokes the callback with a `&[&str]` consisting of each part of the - /// literal's representation. This is done to allow the `ToString` and - /// `Display` implementations to borrow references to symbol values, and - /// both be optimized to reduce overhead. - fn with_stringify_parts( - &self, - interner: SymbolInternerRef, - f: impl FnOnce(&[&str]) -> R, - ) -> R { - /// Returns a string containing exactly `num` '#' characters. - /// Uses a 256-character source string literal which is always safe to - /// index with a `u8` index. - fn get_hashes_str(num: u8) -> &'static str { - const HASHES: &str = "\ +/// Invokes the callback with a `&[&str]` consisting of each part of the +/// literal's representation. This is done to allow the `ToString` and +/// `Display` implementations to borrow references to symbol values, and +/// both be optimized to reduce overhead. +fn literal_with_stringify_parts( + literal: &bridge::Literal, + interner: SymbolInternerRef, + f: impl FnOnce(&[&str]) -> R, +) -> R { + /// Returns a string containing exactly `num` '#' characters. + /// Uses a 256-character source string literal which is always safe to + /// index with a `u8` index. + fn get_hashes_str(num: u8) -> &'static str { + const HASHES: &str = "\ ################################################################\ ################################################################\ ################################################################\ ################################################################\ "; - const _: () = assert!(HASHES.len() == 256); - &HASHES[..num as usize] - } + const _: () = assert!(HASHES.len() == 256); + &HASHES[..num as usize] + } - self.with_symbol_and_suffix(interner, |symbol, suffix| match self.0.kind { + { + let symbol = &*literal.symbol.text(interner); + let suffix = &*literal.suffix.map(|s| s.text(interner)).unwrap_or_default(); + match literal.kind { bridge::LitKind::Byte => f(&["b'", symbol, "'", suffix]), bridge::LitKind::Char => f(&["'", symbol, "'", suffix]), bridge::LitKind::Str => f(&["\"", symbol, "\"", suffix]), @@ -101,16 +101,6 @@ impl LiteralFormatter { bridge::LitKind::Integer | bridge::LitKind::Float | bridge::LitKind::ErrWithGuar => { f(&[symbol, suffix]) } - }) - } - - fn with_symbol_and_suffix( - &self, - interner: SymbolInternerRef, - f: impl FnOnce(&str, &str) -> R, - ) -> R { - let symbol = self.0.symbol.text(interner); - let suffix = self.0.suffix.map(|s| s.text(interner)).unwrap_or_default(); - f(symbol.as_str(), suffix.as_str()) + } } } diff --git a/crates/proc-macro-srv/src/server/rust_analyzer_span.rs b/crates/proc-macro-srv/src/server/rust_analyzer_span.rs index e0d708559db..4d2ee36cc8e 100644 --- a/crates/proc-macro-srv/src/server/rust_analyzer_span.rs +++ b/crates/proc-macro-srv/src/server/rust_analyzer_span.rs @@ -15,8 +15,8 @@ use proc_macro::bridge::{self, server}; use span::{Span, FIXUP_ERASED_FILE_AST_ID_MARKER}; use crate::server::{ - delim_to_external, delim_to_internal, token_stream::TokenStreamBuilder, LiteralFormatter, - Symbol, SymbolInternerRef, SYMBOL_INTERNER, + delim_to_external, delim_to_internal, literal_with_stringify_parts, + token_stream::TokenStreamBuilder, Symbol, SymbolInternerRef, SYMBOL_INTERNER, }; mod tt { pub use ::tt::*; @@ -180,12 +180,11 @@ impl server::TokenStream for RaSpanServer { } bridge::TokenTree::Literal(literal) => { - let literal = LiteralFormatter(literal); - let text = literal.with_stringify_parts(self.interner, |parts| { + let text = literal_with_stringify_parts(&literal, self.interner, |parts| { ::tt::SmolStr::from_iter(parts.iter().copied()) }); - let literal = tt::Literal { text, span: literal.0.span }; + let literal = tt::Literal { text, span: literal.span }; let leaf: tt::Leaf = tt::Leaf::from(literal); let tree = tt::TokenTree::from(leaf); Self::TokenStream::from_iter(iter::once(tree)) @@ -251,10 +250,17 @@ impl server::TokenStream for RaSpanServer { .into_iter() .map(|tree| match tree { tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) => { - bridge::TokenTree::Ident(bridge::Ident { - sym: Symbol::intern(self.interner, ident.text.trim_start_matches("r#")), - is_raw: ident.text.starts_with("r#"), - span: ident.span, + bridge::TokenTree::Ident(match ident.text.strip_prefix("r#") { + Some(text) => bridge::Ident { + sym: Symbol::intern(self.interner, text), + is_raw: true, + span: ident.span, + }, + None => bridge::Ident { + sym: Symbol::intern(self.interner, &ident.text), + is_raw: false, + span: ident.span, + }, }) } tt::TokenTree::Leaf(tt::Leaf::Literal(lit)) => { @@ -285,11 +291,12 @@ impl server::TokenStream for RaSpanServer { } impl server::SourceFile for RaSpanServer { - // FIXME these are all stubs fn eq(&mut self, _file1: &Self::SourceFile, _file2: &Self::SourceFile) -> bool { + // FIXME true } fn path(&mut self, _file: &Self::SourceFile) -> String { + // FIXME String::new() } fn is_real(&mut self, _file: &Self::SourceFile) -> bool { @@ -306,11 +313,15 @@ impl server::Span for RaSpanServer { SourceFile {} } fn save_span(&mut self, _span: Self::Span) -> usize { - // FIXME stub, requires builtin quote! implementation + // FIXME, quote is incompatible with third-party tools + // This is called by the quote proc-macro which is expanded when the proc-macro is compiled + // As such, r-a will never observe this 0 } fn recover_proc_macro_span(&mut self, _id: usize) -> Self::Span { - // FIXME stub, requires builtin quote! implementation + // FIXME, quote is incompatible with third-party tools + // This is called by the expansion of quote!, r-a will observe this, but we don't have + // access to the spans that were encoded self.call_site } /// Recent feature, not yet in the proc_macro diff --git a/crates/proc-macro-srv/src/server/token_id.rs b/crates/proc-macro-srv/src/server/token_id.rs index d1622ab026b..7fbddfc375d 100644 --- a/crates/proc-macro-srv/src/server/token_id.rs +++ b/crates/proc-macro-srv/src/server/token_id.rs @@ -8,8 +8,8 @@ use std::{ use proc_macro::bridge::{self, server}; use crate::server::{ - delim_to_external, delim_to_internal, token_stream::TokenStreamBuilder, LiteralFormatter, - Symbol, SymbolInternerRef, SYMBOL_INTERNER, + delim_to_external, delim_to_internal, literal_with_stringify_parts, + token_stream::TokenStreamBuilder, Symbol, SymbolInternerRef, SYMBOL_INTERNER, }; mod tt { pub use proc_macro_api::msg::TokenId; @@ -171,12 +171,12 @@ impl server::TokenStream for TokenIdServer { } bridge::TokenTree::Literal(literal) => { - let literal = LiteralFormatter(literal); - let text = literal.with_stringify_parts(self.interner, |parts| { + let text = literal_with_stringify_parts(&literal, self.interner, |parts| { ::tt::SmolStr::from_iter(parts.iter().copied()) }); - let literal = tt::Literal { text, span: literal.0.span }; + let literal = tt::Literal { text, span: literal.span }; + let leaf = tt::Leaf::from(literal); let tree = TokenTree::from(leaf); Self::TokenStream::from_iter(iter::once(tree)) diff --git a/crates/rust-analyzer/src/handlers/notification.rs b/crates/rust-analyzer/src/handlers/notification.rs index e082c083507..cf646a2e282 100644 --- a/crates/rust-analyzer/src/handlers/notification.rs +++ b/crates/rust-analyzer/src/handlers/notification.rs @@ -91,7 +91,7 @@ pub(crate) fn handle_did_change_text_document( if let Ok(path) = from_proto::vfs_path(¶ms.text_document.uri) { let Some(DocumentData { version, data }) = state.mem_docs.get_mut(&path) else { - tracing::error!("unexpected DidChangeTextDocument: {}", path); + tracing::error!(?path, "unexpected DidChangeTextDocument"); return Ok(()); }; // The version passed in DidChangeTextDocument is the version after all edits are applied diff --git a/docs/user/generated_config.adoc b/docs/user/generated_config.adoc index da7654b0f64..d4ba5af9231 100644 --- a/docs/user/generated_config.adoc +++ b/docs/user/generated_config.adoc @@ -144,6 +144,16 @@ This option does not take effect until rust-analyzer is restarted. -- Compilation target override (target triple). -- +[[rust-analyzer.cargo.targetDir]]rust-analyzer.cargo.targetDir (default: `null`):: ++ +-- +Optional path to a rust-analyzer specific target directory. +This prevents rust-analyzer's `cargo check` and initial build-script and proc-macro +building from locking the `Cargo.lock` at the expense of duplicating build artifacts. + +Set to `true` to use a subdirectory of the existing target directory or +set to a path relative to the workspace to use that path. +-- [[rust-analyzer.cargo.unsetTest]]rust-analyzer.cargo.unsetTest (default: `["core"]`):: + -- @@ -814,16 +824,6 @@ Command to be executed instead of 'cargo' for runnables. Additional arguments to be passed to cargo for runnables such as tests or binaries. For example, it may be `--release`. -- -[[rust-analyzer.rust.analyzerTargetDir]]rust-analyzer.rust.analyzerTargetDir (default: `null`):: -+ --- -Optional path to a rust-analyzer specific target directory. -This prevents rust-analyzer's `cargo check` from locking the `Cargo.lock` -at the expense of duplicating build artifacts. - -Set to `true` to use a subdirectory of the existing target directory or -set to a path relative to the workspace to use that path. --- [[rust-analyzer.rustc.source]]rust-analyzer.rustc.source (default: `null`):: + -- diff --git a/editors/code/package.json b/editors/code/package.json index 1633561ae3b..d86365591a6 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -671,6 +671,21 @@ "string" ] }, + "rust-analyzer.cargo.targetDir": { + "markdownDescription": "Optional path to a rust-analyzer specific target directory.\nThis prevents rust-analyzer's `cargo check` and initial build-script and proc-macro\nbuilding from locking the `Cargo.lock` at the expense of duplicating build artifacts.\n\nSet to `true` to use a subdirectory of the existing target directory or\nset to a path relative to the workspace to use that path.", + "default": null, + "anyOf": [ + { + "type": "null" + }, + { + "type": "boolean" + }, + { + "type": "string" + } + ] + }, "rust-analyzer.cargo.unsetTest": { "markdownDescription": "Unsets the implicit `#[cfg(test)]` for the specified crates.", "default": [ @@ -1543,21 +1558,6 @@ "type": "string" } }, - "rust-analyzer.rust.analyzerTargetDir": { - "markdownDescription": "Optional path to a rust-analyzer specific target directory.\nThis prevents rust-analyzer's `cargo check` from locking the `Cargo.lock`\nat the expense of duplicating build artifacts.\n\nSet to `true` to use a subdirectory of the existing target directory or\nset to a path relative to the workspace to use that path.", - "default": null, - "anyOf": [ - { - "type": "null" - }, - { - "type": "boolean" - }, - { - "type": "string" - } - ] - }, "rust-analyzer.rustc.source": { "markdownDescription": "Path to the Cargo.toml of the rust compiler workspace, for usage in rustc_private\nprojects, or \"discover\" to try to automatically find it if the `rustc-dev` component\nis installed.\n\nAny project which uses rust-analyzer with the rustcPrivate\ncrates must set `[package.metadata.rust-analyzer] rustc_private=true` to use it.\n\nThis option does not take effect until rust-analyzer is restarted.", "default": null,