Skip to content

Commit 428a34a

Browse files
committed
Implement server::Span::Join
1 parent 6be83b8 commit 428a34a

File tree

7 files changed

+148
-75
lines changed

7 files changed

+148
-75
lines changed

crates/hir-expand/src/builtin_fn_macro.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ register_builtin! {
108108
(format_args, FormatArgs) => format_args_expand,
109109
(const_format_args, ConstFormatArgs) => format_args_expand,
110110
(format_args_nl, FormatArgsNl) => format_args_nl_expand,
111+
(quote, Quote) => quote_expand,
111112

112113
EAGER:
113114
(compile_error, CompileError) => compile_error_expand,
@@ -770,3 +771,12 @@ fn option_env_expand(
770771

771772
ExpandResult::ok(expanded)
772773
}
774+
775+
fn quote_expand(
776+
_db: &dyn ExpandDatabase,
777+
_arg_id: MacroCallId,
778+
_tt: &tt::Subtree,
779+
_span: SpanData,
780+
) -> ExpandResult<tt::Subtree> {
781+
ExpandResult::only_err(ExpandError::other("quote! is not implemented"))
782+
}

crates/hir-expand/src/name.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ pub mod known {
388388
log_syntax,
389389
module_path,
390390
option_env,
391+
quote,
391392
std_panic,
392393
stringify,
393394
trace_macros,

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,14 @@ impl ProcMacroSrvSpan for TokenId {
6060
impl ProcMacroSrvSpan for Span {
6161
type Server = server::rust_analyzer_span::RaSpanServer;
6262
fn make_server(call_site: Self, def_site: Self, mixed_site: Self) -> Self::Server {
63-
Self::Server { interner: &server::SYMBOL_INTERNER, call_site, def_site, mixed_site }
63+
Self::Server {
64+
interner: &server::SYMBOL_INTERNER,
65+
call_site,
66+
def_site,
67+
mixed_site,
68+
tracked_env_vars: Default::default(),
69+
tracked_paths: Default::default(),
70+
}
6471
}
6572
}
6673

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

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::{
2+
collections::{HashMap, HashSet},
23
iter,
34
ops::{Bound, Range},
45
};
@@ -35,6 +36,10 @@ pub struct FreeFunctions;
3536

3637
pub struct RaSpanServer {
3738
pub(crate) interner: SymbolInternerRef,
39+
// FIXME: Report this back to the caller to track as dependencies
40+
pub tracked_env_vars: HashMap<Box<str>, Option<Box<str>>>,
41+
// FIXME: Report this back to the caller to track as dependencies
42+
pub tracked_paths: HashSet<Box<str>>,
3843
pub call_site: Span,
3944
pub def_site: Span,
4045
pub mixed_site: Span,
@@ -49,11 +54,12 @@ impl server::Types for RaSpanServer {
4954
}
5055

5156
impl server::FreeFunctions for RaSpanServer {
52-
fn track_env_var(&mut self, _var: &str, _value: Option<&str>) {
53-
// FIXME: track env var accesses
54-
// https://github.com/rust-lang/rust/pull/71858
57+
fn track_env_var(&mut self, var: &str, value: Option<&str>) {
58+
self.tracked_env_vars.insert(var.into(), value.map(Into::into));
59+
}
60+
fn track_path(&mut self, path: &str) {
61+
self.tracked_paths.insert(path.into());
5562
}
56-
fn track_path(&mut self, _path: &str) {}
5763

5864
fn literal_from_str(
5965
&mut self,
@@ -247,24 +253,38 @@ impl server::Span for RaSpanServer {
247253
/// See PR:
248254
/// https://github.com/rust-lang/rust/pull/55780
249255
fn source_text(&mut self, _span: Self::Span) -> Option<String> {
256+
// FIXME requires db
250257
None
251258
}
252259

253260
fn parent(&mut self, _span: Self::Span) -> Option<Self::Span> {
254-
// FIXME handle span
261+
// FIXME requires db, looks up the parent call site
255262
None
256263
}
257264
fn source(&mut self, span: Self::Span) -> Self::Span {
258-
// FIXME handle span
265+
// FIXME requires db, returns the top level call site
259266
span
260267
}
261-
fn byte_range(&mut self, _span: Self::Span) -> Range<usize> {
262-
// FIXME handle span
263-
Range { start: 0, end: 0 }
268+
fn byte_range(&mut self, span: Self::Span) -> Range<usize> {
269+
// FIXME requires db to resolve the ast id, THIS IS NOT INCREMENTAL
270+
Range { start: span.range.start().into(), end: span.range.end().into() }
264271
}
265-
fn join(&mut self, first: Self::Span, _second: Self::Span) -> Option<Self::Span> {
266-
// Just return the first span again, because some macros will unwrap the result.
267-
Some(first)
272+
fn join(&mut self, first: Self::Span, second: Self::Span) -> Option<Self::Span> {
273+
if first.anchor != second.anchor {
274+
return None;
275+
}
276+
if first.ctx != second.ctx {
277+
if first.ctx.is_root() {
278+
return Some(second);
279+
} else if second.ctx.is_root() {
280+
return Some(first);
281+
}
282+
}
283+
Some(Span {
284+
range: first.range.cover(second.range),
285+
anchor: second.anchor,
286+
ctx: second.ctx,
287+
})
268288
}
269289
fn subspan(
270290
&mut self,

0 commit comments

Comments
 (0)