Skip to content

Commit 4951180

Browse files
committed
Remove DescendPreference::SameKind
1 parent ce8f320 commit 4951180

File tree

4 files changed

+53
-37
lines changed

4 files changed

+53
-37
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
<span class="brace">}</span>
5959

6060
<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>
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="operator macro">-</span><span class="operator macro">&gt;</span> <span class="builtin_type macro">u32</span> <span class="brace macro">{</span>
6262
<span class="numeric_literal macro">100</span>
6363
<span class="brace macro">}</span>
6464
<span class="brace macro">}</span>

0 commit comments

Comments
 (0)