Skip to content

Commit 93cfef1

Browse files
committed
Simplify
1 parent c8fdcea commit 93cfef1

File tree

5 files changed

+75
-74
lines changed

5 files changed

+75
-74
lines changed

crates/proc-macro-srv/src/server.rs

+22-32
Original file line numberDiff line numberDiff line change
@@ -54,33 +54,33 @@ fn spacing_to_external(spacing: Spacing) -> proc_macro::Spacing {
5454
}
5555
}
5656

57-
struct LiteralFormatter<S>(bridge::Literal<S, Symbol>);
58-
59-
impl<S> LiteralFormatter<S> {
60-
/// Invokes the callback with a `&[&str]` consisting of each part of the
61-
/// literal's representation. This is done to allow the `ToString` and
62-
/// `Display` implementations to borrow references to symbol values, and
63-
/// both be optimized to reduce overhead.
64-
fn with_stringify_parts<R>(
65-
&self,
66-
interner: SymbolInternerRef,
67-
f: impl FnOnce(&[&str]) -> R,
68-
) -> R {
69-
/// Returns a string containing exactly `num` '#' characters.
70-
/// Uses a 256-character source string literal which is always safe to
71-
/// index with a `u8` index.
72-
fn get_hashes_str(num: u8) -> &'static str {
73-
const HASHES: &str = "\
57+
/// Invokes the callback with a `&[&str]` consisting of each part of the
58+
/// literal's representation. This is done to allow the `ToString` and
59+
/// `Display` implementations to borrow references to symbol values, and
60+
/// both be optimized to reduce overhead.
61+
fn literal_with_stringify_parts<S, R>(
62+
literal: &bridge::Literal<S, Symbol>,
63+
interner: SymbolInternerRef,
64+
f: impl FnOnce(&[&str]) -> R,
65+
) -> R {
66+
/// Returns a string containing exactly `num` '#' characters.
67+
/// Uses a 256-character source string literal which is always safe to
68+
/// index with a `u8` index.
69+
fn get_hashes_str(num: u8) -> &'static str {
70+
const HASHES: &str = "\
7471
################################################################\
7572
################################################################\
7673
################################################################\
7774
################################################################\
7875
";
79-
const _: () = assert!(HASHES.len() == 256);
80-
&HASHES[..num as usize]
81-
}
76+
const _: () = assert!(HASHES.len() == 256);
77+
&HASHES[..num as usize]
78+
}
8279

83-
self.with_symbol_and_suffix(interner, |symbol, suffix| match self.0.kind {
80+
{
81+
let symbol = &*literal.symbol.text(interner);
82+
let suffix = &*literal.suffix.map(|s| s.text(interner)).unwrap_or_default();
83+
match literal.kind {
8484
bridge::LitKind::Byte => f(&["b'", symbol, "'", suffix]),
8585
bridge::LitKind::Char => f(&["'", symbol, "'", suffix]),
8686
bridge::LitKind::Str => f(&["\"", symbol, "\"", suffix]),
@@ -101,16 +101,6 @@ impl<S> LiteralFormatter<S> {
101101
bridge::LitKind::Integer | bridge::LitKind::Float | bridge::LitKind::ErrWithGuar => {
102102
f(&[symbol, suffix])
103103
}
104-
})
105-
}
106-
107-
fn with_symbol_and_suffix<R>(
108-
&self,
109-
interner: SymbolInternerRef,
110-
f: impl FnOnce(&str, &str) -> R,
111-
) -> R {
112-
let symbol = self.0.symbol.text(interner);
113-
let suffix = self.0.suffix.map(|s| s.text(interner)).unwrap_or_default();
114-
f(symbol.as_str(), suffix.as_str())
104+
}
115105
}
116106
}

crates/proc-macro-srv/src/server/rust_analyzer_span.rs

+23-12
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ use proc_macro::bridge::{self, server};
1515
use span::{Span, FIXUP_ERASED_FILE_AST_ID_MARKER};
1616

1717
use crate::server::{
18-
delim_to_external, delim_to_internal, token_stream::TokenStreamBuilder, LiteralFormatter,
19-
Symbol, SymbolInternerRef, SYMBOL_INTERNER,
18+
delim_to_external, delim_to_internal, literal_with_stringify_parts,
19+
token_stream::TokenStreamBuilder, Symbol, SymbolInternerRef, SYMBOL_INTERNER,
2020
};
2121
mod tt {
2222
pub use ::tt::*;
@@ -180,12 +180,11 @@ impl server::TokenStream for RaSpanServer {
180180
}
181181

182182
bridge::TokenTree::Literal(literal) => {
183-
let literal = LiteralFormatter(literal);
184-
let text = literal.with_stringify_parts(self.interner, |parts| {
183+
let text = literal_with_stringify_parts(&literal, self.interner, |parts| {
185184
::tt::SmolStr::from_iter(parts.iter().copied())
186185
});
187186

188-
let literal = tt::Literal { text, span: literal.0.span };
187+
let literal = tt::Literal { text, span: literal.span };
189188
let leaf: tt::Leaf = tt::Leaf::from(literal);
190189
let tree = tt::TokenTree::from(leaf);
191190
Self::TokenStream::from_iter(iter::once(tree))
@@ -251,10 +250,17 @@ impl server::TokenStream for RaSpanServer {
251250
.into_iter()
252251
.map(|tree| match tree {
253252
tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) => {
254-
bridge::TokenTree::Ident(bridge::Ident {
255-
sym: Symbol::intern(self.interner, ident.text.trim_start_matches("r#")),
256-
is_raw: ident.text.starts_with("r#"),
257-
span: ident.span,
253+
bridge::TokenTree::Ident(match ident.text.strip_prefix("r#") {
254+
Some(text) => bridge::Ident {
255+
sym: Symbol::intern(self.interner, text),
256+
is_raw: true,
257+
span: ident.span,
258+
},
259+
None => bridge::Ident {
260+
sym: Symbol::intern(self.interner, &ident.text),
261+
is_raw: false,
262+
span: ident.span,
263+
},
258264
})
259265
}
260266
tt::TokenTree::Leaf(tt::Leaf::Literal(lit)) => {
@@ -285,11 +291,12 @@ impl server::TokenStream for RaSpanServer {
285291
}
286292

287293
impl server::SourceFile for RaSpanServer {
288-
// FIXME these are all stubs
289294
fn eq(&mut self, _file1: &Self::SourceFile, _file2: &Self::SourceFile) -> bool {
295+
// FIXME
290296
true
291297
}
292298
fn path(&mut self, _file: &Self::SourceFile) -> String {
299+
// FIXME
293300
String::new()
294301
}
295302
fn is_real(&mut self, _file: &Self::SourceFile) -> bool {
@@ -306,11 +313,15 @@ impl server::Span for RaSpanServer {
306313
SourceFile {}
307314
}
308315
fn save_span(&mut self, _span: Self::Span) -> usize {
309-
// FIXME stub, requires builtin quote! implementation
316+
// FIXME, quote is incompatible with third-party tools
317+
// This is called by the quote proc-macro which is expanded when the proc-macro is compiled
318+
// As such, r-a will never observe this
310319
0
311320
}
312321
fn recover_proc_macro_span(&mut self, _id: usize) -> Self::Span {
313-
// FIXME stub, requires builtin quote! implementation
322+
// FIXME, quote is incompatible with third-party tools
323+
// This is called by the expansion of quote!, r-a will observe this, but we don't have
324+
// access to the spans that were encoded
314325
self.call_site
315326
}
316327
/// Recent feature, not yet in the proc_macro

crates/proc-macro-srv/src/server/token_id.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use std::{
88
use proc_macro::bridge::{self, server};
99

1010
use crate::server::{
11-
delim_to_external, delim_to_internal, token_stream::TokenStreamBuilder, LiteralFormatter,
12-
Symbol, SymbolInternerRef, SYMBOL_INTERNER,
11+
delim_to_external, delim_to_internal, literal_with_stringify_parts,
12+
token_stream::TokenStreamBuilder, Symbol, SymbolInternerRef, SYMBOL_INTERNER,
1313
};
1414
mod tt {
1515
pub use proc_macro_api::msg::TokenId;
@@ -171,12 +171,12 @@ impl server::TokenStream for TokenIdServer {
171171
}
172172

173173
bridge::TokenTree::Literal(literal) => {
174-
let literal = LiteralFormatter(literal);
175-
let text = literal.with_stringify_parts(self.interner, |parts| {
174+
let text = literal_with_stringify_parts(&literal, self.interner, |parts| {
176175
::tt::SmolStr::from_iter(parts.iter().copied())
177176
});
178177

179-
let literal = tt::Literal { text, span: literal.0.span };
178+
let literal = tt::Literal { text, span: literal.span };
179+
180180
let leaf = tt::Leaf::from(literal);
181181
let tree = TokenTree::from(leaf);
182182
Self::TokenStream::from_iter(iter::once(tree))

docs/user/generated_config.adoc

+10-10
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,16 @@ This option does not take effect until rust-analyzer is restarted.
144144
--
145145
Compilation target override (target triple).
146146
--
147+
[[rust-analyzer.cargo.targetDir]]rust-analyzer.cargo.targetDir (default: `null`)::
148+
+
149+
--
150+
Optional path to a rust-analyzer specific target directory.
151+
This prevents rust-analyzer's `cargo check` and initial build-script and proc-macro
152+
building from locking the `Cargo.lock` at the expense of duplicating build artifacts.
153+
154+
Set to `true` to use a subdirectory of the existing target directory or
155+
set to a path relative to the workspace to use that path.
156+
--
147157
[[rust-analyzer.cargo.unsetTest]]rust-analyzer.cargo.unsetTest (default: `["core"]`)::
148158
+
149159
--
@@ -814,16 +824,6 @@ Command to be executed instead of 'cargo' for runnables.
814824
Additional arguments to be passed to cargo for runnables such as
815825
tests or binaries. For example, it may be `--release`.
816826
--
817-
[[rust-analyzer.rust.analyzerTargetDir]]rust-analyzer.rust.analyzerTargetDir (default: `null`)::
818-
+
819-
--
820-
Optional path to a rust-analyzer specific target directory.
821-
This prevents rust-analyzer's `cargo check` from locking the `Cargo.lock`
822-
at the expense of duplicating build artifacts.
823-
824-
Set to `true` to use a subdirectory of the existing target directory or
825-
set to a path relative to the workspace to use that path.
826-
--
827827
[[rust-analyzer.rustc.source]]rust-analyzer.rustc.source (default: `null`)::
828828
+
829829
--

editors/code/package.json

+15-15
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,21 @@
671671
"string"
672672
]
673673
},
674+
"rust-analyzer.cargo.targetDir": {
675+
"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.",
676+
"default": null,
677+
"anyOf": [
678+
{
679+
"type": "null"
680+
},
681+
{
682+
"type": "boolean"
683+
},
684+
{
685+
"type": "string"
686+
}
687+
]
688+
},
674689
"rust-analyzer.cargo.unsetTest": {
675690
"markdownDescription": "Unsets the implicit `#[cfg(test)]` for the specified crates.",
676691
"default": [
@@ -1543,21 +1558,6 @@
15431558
"type": "string"
15441559
}
15451560
},
1546-
"rust-analyzer.rust.analyzerTargetDir": {
1547-
"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.",
1548-
"default": null,
1549-
"anyOf": [
1550-
{
1551-
"type": "null"
1552-
},
1553-
{
1554-
"type": "boolean"
1555-
},
1556-
{
1557-
"type": "string"
1558-
}
1559-
]
1560-
},
15611561
"rust-analyzer.rustc.source": {
15621562
"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.",
15631563
"default": null,

0 commit comments

Comments
 (0)