Skip to content

Commit 06ccc8e

Browse files
committed
Remove DescendPreference::SameKind
1 parent ce8f320 commit 06ccc8e

File tree

4 files changed

+55
-98
lines changed

4 files changed

+55
-98
lines changed

crates/hir/src/semantics.rs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ use crate::{
5252
const CONTINUE_NO_BREAKS: ControlFlow<Infallible, ()> = ControlFlow::Continue(());
5353

5454
pub enum DescendPreference {
55-
SameKind,
5655
None,
5756
}
5857

@@ -675,34 +674,16 @@ impl<'db> SemanticsImpl<'db> {
675674
token: SyntaxToken,
676675
) -> SmallVec<[SyntaxToken; 1]> {
677676
enum Dp {
678-
// SameText(&'t str),
679-
SameKind(SyntaxKind),
680677
None,
681678
}
682-
let fetch_kind = |token: &SyntaxToken| match token.parent() {
683-
Some(node) => match node.kind() {
684-
kind @ (SyntaxKind::NAME | SyntaxKind::NAME_REF) => kind,
685-
_ => token.kind(),
686-
},
687-
None => token.kind(),
688-
};
689679
let mode = match mode {
690-
// DescendPreference::SameText => Dp::SameText(token.text()),
691-
DescendPreference::SameKind => Dp::SameKind(fetch_kind(&token)),
692680
DescendPreference::None => Dp::None,
693681
};
694682
let mut res = smallvec![];
695683
self.descend_into_macros_impl::<Infallible>(
696684
token.clone(),
697685
&mut |InFile { value, .. }| {
698686
let is_a_match = match mode {
699-
// Dp::SameText(text) => value.text() == text,
700-
Dp::SameKind(preferred_kind) => {
701-
let kind = fetch_kind(&value);
702-
kind == preferred_kind
703-
// special case for derive macros
704-
|| (preferred_kind == SyntaxKind::IDENT && kind == SyntaxKind::NAME_REF)
705-
}
706687
Dp::None => true,
707688
};
708689
if is_a_match {
@@ -733,7 +714,7 @@ impl<'db> SemanticsImpl<'db> {
733714
token: SyntaxToken,
734715
mut cb: impl FnMut(InFile<SyntaxToken>) -> ControlFlow<T>,
735716
) -> Option<T> {
736-
self.descend_into_macros_impl(token.clone(), &mut |t| cb(t))
717+
self.descend_into_macros_impl(token.clone(), &mut cb)
737718
}
738719

739720
/// Descends the token into expansions, returning the tokens that matches the input

crates/ide/src/hover.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ fn hover_simple(
186186
});
187187
let descended = || descended.iter();
188188

189-
// FIXME: WE should not try these step by step, instead to accommodate for macros we should run
189+
// TODO: WE should not try these step by step, instead to accommodate for macros we should run
190190
// all of these in "parallel" and rank their results
191191
let result = None
192192
// try lint hover

crates/ide/src/syntax_highlighting.rs

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ mod html;
1313
#[cfg(test)]
1414
mod tests;
1515

16-
use hir::{DescendPreference, Name, Semantics};
16+
use std::ops::ControlFlow;
17+
18+
use hir::{Name, Semantics};
1719
use ide_db::{FxHashMap, RootDatabase, SymbolKind};
1820
use span::EditionedFileId;
1921
use syntax::{
@@ -399,20 +401,53 @@ fn traverse(
399401
// Attempt to descend tokens into macro-calls.
400402
let res = match element {
401403
NodeOrToken::Token(token) if token.kind() != COMMENT => {
402-
let token = if token.kind() == STRING {
403-
// for strings, try to prefer a string that has not been lost in a token
404-
// tree
405-
// FIXME: This should be done for everything, but check perf first
406-
sema.descend_into_macros(DescendPreference::SameKind, token)
407-
.into_iter()
408-
.max_by_key(|it| {
409-
it.parent().map_or(false, |it| it.kind() != TOKEN_TREE)
410-
})
411-
.unwrap()
412-
} else {
413-
// FIXME: We should probably rank the tokens and find the most suitable?
414-
sema.descend_into_macros_single_exact(token)
415-
};
404+
let kind = token.kind();
405+
let text = token.text();
406+
let ident_kind = kind.is_any_identifier();
407+
408+
let mut t = None;
409+
let mut r = 0;
410+
// FIXME: Add an extra API that takes the file id of this. That is a simple way
411+
// to prevent us constantly walking up the tree to fetch the file
412+
sema.descend_into_macros_ng_b(token.clone(), |tok| {
413+
let tok = tok.value;
414+
let tok_kind = tok.kind();
415+
416+
let exact_same_kind = tok_kind == kind;
417+
let both_idents =
418+
exact_same_kind || (tok_kind.is_any_identifier() && ident_kind);
419+
let same_text = tok.text() == text;
420+
// anything that mapped into a token tree has likely no semantic information
421+
let no_tt_parent = tok.parent().map_or(false, |it| it.kind() != TOKEN_TREE);
422+
let my_rank = (both_idents as usize)
423+
| ((exact_same_kind as usize) << 1)
424+
| ((same_text as usize) << 2)
425+
| ((no_tt_parent as usize) << 3);
426+
427+
if my_rank > 0b1110 {
428+
// a rank of 0b1110 means that we have found a maximally interesting
429+
// token so stop early.
430+
t = Some(tok);
431+
return ControlFlow::Break(());
432+
}
433+
434+
// r = r.max(my_rank);
435+
// t = Some(t.take_if(|_| r < my_rank).unwrap_or(tok));
436+
match &mut t {
437+
Some(prev) if r < my_rank => {
438+
*prev = tok;
439+
r = my_rank;
440+
}
441+
Some(_) => (),
442+
None => {
443+
r = my_rank;
444+
t = Some(tok)
445+
}
446+
}
447+
ControlFlow::Continue(())
448+
});
449+
450+
let token = t.unwrap_or(token);
416451
match token.parent().and_then(ast::NameLike::cast) {
417452
// Remap the token into the wrapping single token nodes
418453
Some(parent) => match (token.kind(), parent.syntax().kind()) {

crates/ide/src/syntax_highlighting/test_data/highlight_macros.html

Lines changed: 3 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -45,71 +45,12 @@
4545
.invalid_escape_sequence { color: #FC5555; text-decoration: wavy underline; }
4646
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
4747
</style>
48-
<pre><code><span class="keyword">use</span> <span class="module crate_root library">proc_macros</span><span class="operator">::</span><span class="brace">{</span><span class="function library">mirror</span><span class="comma">,</span> <span class="function library">identity</span><span class="comma">,</span> <span class="derive library">DeriveIdentity</span><span class="brace">}</span><span class="semicolon">;</span>
49-
50-
<span class="proc_macro library">mirror</span><span class="macro_bang">!</span> <span class="brace macro proc_macro">{</span>
51-
<span class="brace macro proc_macro">{</span>
52-
<span class="comma macro proc_macro">,</span><span class="builtin_type macro proc_macro">i32</span> <span class="colon macro proc_macro">:</span><span class="field declaration macro proc_macro public">x</span> <span class="keyword macro proc_macro">pub</span>
53-
<span class="comma macro proc_macro">,</span><span class="builtin_type macro proc_macro">i32</span> <span class="colon macro proc_macro">:</span><span class="field declaration macro proc_macro public">y</span> <span class="keyword macro proc_macro">pub</span>
54-
<span class="brace macro proc_macro">}</span> <span class="struct declaration macro proc_macro">Foo</span> <span class="keyword macro proc_macro">struct</span>
55-
<span class="brace macro proc_macro">}</span>
56-
<span class="keyword">macro_rules</span><span class="macro_bang">!</span> <span class="macro declaration">def_fn</span> <span class="brace">{</span>
48+
<pre><code><span class="keyword">macro_rules</span><span class="macro_bang">!</span> <span class="macro declaration">def_fn</span> <span class="brace">{</span>
5749
<span class="parenthesis">(</span><span class="punctuation">$</span><span class="parenthesis">(</span><span class="punctuation">$</span>tt<span class="colon">:</span>tt<span class="parenthesis">)</span><span class="punctuation">*</span><span class="parenthesis">)</span> <span class="operator">=</span><span class="angle">&gt;</span> <span class="brace">{</span><span class="punctuation">$</span><span class="parenthesis">(</span><span class="punctuation">$</span>tt<span class="parenthesis">)</span><span class="punctuation">*</span><span class="brace">}</span>
5850
<span class="brace">}</span>
5951

6052
<span class="macro">def_fn</span><span class="macro_bang">!</span> <span class="brace macro">{</span>
61-
<span class="keyword macro">fn</span> <span class="function declaration macro">bar</span><span class="parenthesis macro">(</span><span class="parenthesis macro">)</span> <span class="punctuation macro">-</span><span class="angle macro">&gt;</span> <span class="builtin_type macro">u32</span> <span class="brace macro">{</span>
53+
<span class="keyword macro">fn</span> <span class="function declaration macro">bar</span><span class="parenthesis macro">(</span><span class="parenthesis macro">)</span> <span class="operator macro">-</span><span class="operator macro">&gt;</span> <span class="builtin_type macro">u32</span> <span class="brace macro">{</span>
6254
<span class="numeric_literal macro">100</span>
6355
<span class="brace macro">}</span>
64-
<span class="brace macro">}</span>
65-
66-
<span class="keyword">macro_rules</span><span class="macro_bang">!</span> <span class="macro declaration">dont_color_me_braces</span> <span class="brace">{</span>
67-
<span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="operator">=</span><span class="angle">&gt;</span> <span class="brace">{</span><span class="numeric_literal">0</span><span class="brace">}</span>
68-
<span class="brace">}</span>
69-
70-
<span class="keyword">macro_rules</span><span class="macro_bang">!</span> <span class="macro declaration">noop</span> <span class="brace">{</span>
71-
<span class="parenthesis">(</span><span class="punctuation">$</span>expr<span class="colon">:</span>expr<span class="parenthesis">)</span> <span class="operator">=</span><span class="angle">&gt;</span> <span class="brace">{</span>
72-
<span class="punctuation">$</span>expr
73-
<span class="brace">}</span>
74-
<span class="brace">}</span>
75-
76-
<span class="comment documentation">/// textually shadow previous definition</span>
77-
<span class="keyword">macro_rules</span><span class="macro_bang">!</span> <span class="macro declaration">noop</span> <span class="brace">{</span>
78-
<span class="parenthesis">(</span><span class="punctuation">$</span>expr<span class="colon">:</span>expr<span class="parenthesis">)</span> <span class="operator">=</span><span class="angle">&gt;</span> <span class="brace">{</span>
79-
<span class="punctuation">$</span>expr
80-
<span class="brace">}</span>
81-
<span class="brace">}</span>
82-
83-
<span class="keyword">macro_rules</span><span class="macro_bang">!</span> <span class="macro declaration">keyword_frag</span> <span class="brace">{</span>
84-
<span class="parenthesis">(</span><span class="punctuation">$</span>type<span class="colon">:</span>ty<span class="parenthesis">)</span> <span class="operator">=</span><span class="angle">&gt;</span> <span class="parenthesis">(</span><span class="punctuation">$</span>type<span class="parenthesis">)</span>
85-
<span class="brace">}</span>
86-
87-
<span class="keyword">macro</span> <span class="macro declaration">with_args</span><span class="parenthesis">(</span><span class="punctuation">$</span>i<span class="colon">:</span>ident<span class="parenthesis">)</span> <span class="brace">{</span>
88-
<span class="punctuation">$</span>i
89-
<span class="brace">}</span>
90-
91-
<span class="keyword">macro</span> <span class="macro declaration">without_args</span> <span class="brace">{</span>
92-
<span class="parenthesis">(</span><span class="punctuation">$</span>i<span class="colon">:</span>ident<span class="parenthesis">)</span> <span class="operator">=</span><span class="angle">&gt;</span> <span class="brace">{</span>
93-
<span class="punctuation">$</span>i
94-
<span class="brace">}</span>
95-
<span class="brace">}</span>
96-
97-
<span class="keyword">macro_rules</span><span class="macro_bang">!</span> <span class="macro declaration">id</span> <span class="brace">{</span>
98-
<span class="parenthesis">(</span><span class="punctuation">$</span><span class="parenthesis">(</span><span class="punctuation">$</span>tt<span class="colon">:</span>tt<span class="parenthesis">)</span><span class="punctuation">*</span><span class="parenthesis">)</span> <span class="operator">=</span><span class="angle">&gt;</span> <span class="brace">{</span>
99-
<span class="punctuation">$</span><span class="parenthesis">(</span><span class="punctuation">$</span>tt<span class="parenthesis">)</span><span class="punctuation">*</span>
100-
<span class="brace">}</span><span class="semicolon">;</span>
101-
<span class="brace">}</span>
102-
103-
<span class="macro default_library library">include</span><span class="macro_bang">!</span><span class="parenthesis macro">(</span><span class="macro default_library library macro">concat</span><span class="macro_bang macro">!</span><span class="parenthesis macro">(</span><span class="string_literal macro">"foo/"</span><span class="comma macro">,</span> <span class="string_literal macro">"foo.rs"</span><span class="parenthesis macro">)</span><span class="parenthesis macro">)</span><span class="semicolon">;</span>
104-
105-
<span class="keyword">struct</span> <span class="struct declaration">S</span><span class="angle">&lt;</span><span class="type_param declaration">T</span><span class="angle">&gt;</span><span class="parenthesis">(</span><span class="type_param">T</span><span class="parenthesis">)</span><span class="semicolon">;</span>
106-
<span class="keyword">fn</span> <span class="function declaration">main</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span>
107-
<span class="keyword">struct</span> <span class="struct declaration">TestLocal</span><span class="semicolon">;</span>
108-
<span class="comment">// regression test, TestLocal here used to not resolve</span>
109-
<span class="keyword">let</span> <span class="punctuation">_</span><span class="colon">:</span> <span class="struct">S</span><span class="angle">&lt;</span><span class="macro">id</span><span class="macro_bang">!</span><span class="bracket macro">[</span><span class="struct macro">TestLocal</span><span class="bracket macro">]</span><span class="angle">&gt;</span><span class="semicolon">;</span>
110-
111-
<span class="macro default_library library">format_args</span><span class="macro_bang">!</span><span class="parenthesis macro">(</span><span class="string_literal macro">"Hello, </span><span class="format_specifier">{</span><span class="format_specifier">}</span><span class="string_literal macro">!"</span><span class="comma macro">,</span> <span class="parenthesis macro">(</span><span class="numeric_literal macro">92</span><span class="comma macro">,</span><span class="parenthesis macro">)</span><span class="operator macro">.</span><span class="field library macro">0</span><span class="parenthesis macro">)</span><span class="semicolon">;</span>
112-
<span class="macro">dont_color_me_braces</span><span class="macro_bang">!</span><span class="parenthesis macro">(</span><span class="parenthesis macro">)</span><span class="semicolon">;</span>
113-
<span class="macro">noop</span><span class="macro_bang">!</span><span class="parenthesis macro">(</span><span class="macro macro">noop</span><span class="macro_bang macro">!</span><span class="parenthesis macro">(</span><span class="numeric_literal macro">1</span><span class="parenthesis macro">)</span><span class="parenthesis macro">)</span><span class="semicolon">;</span>
114-
<span class="brace">}</span>
115-
</code></pre>
56+
<span class="brace macro">}</span></code></pre>

0 commit comments

Comments
 (0)