Remove DescendPreference::SameKind

This commit is contained in:
Lukas Wirth 2024-08-22 15:57:19 +02:00
parent ce8f32022b
commit 495118015e
4 changed files with 53 additions and 37 deletions

View file

@ -52,7 +52,6 @@ use crate::{
const CONTINUE_NO_BREAKS: ControlFlow<Infallible, ()> = ControlFlow::Continue(()); const CONTINUE_NO_BREAKS: ControlFlow<Infallible, ()> = ControlFlow::Continue(());
pub enum DescendPreference { pub enum DescendPreference {
SameKind,
None, None,
} }
@ -675,20 +674,9 @@ impl<'db> SemanticsImpl<'db> {
token: SyntaxToken, token: SyntaxToken,
) -> SmallVec<[SyntaxToken; 1]> { ) -> SmallVec<[SyntaxToken; 1]> {
enum Dp { enum Dp {
// SameText(&'t str),
SameKind(SyntaxKind),
None, None,
} }
let fetch_kind = |token: &SyntaxToken| match token.parent() {
Some(node) => match node.kind() {
kind @ (SyntaxKind::NAME | SyntaxKind::NAME_REF) => kind,
_ => token.kind(),
},
None => token.kind(),
};
let mode = match mode { let mode = match mode {
// DescendPreference::SameText => Dp::SameText(token.text()),
DescendPreference::SameKind => Dp::SameKind(fetch_kind(&token)),
DescendPreference::None => Dp::None, DescendPreference::None => Dp::None,
}; };
let mut res = smallvec![]; let mut res = smallvec![];
@ -696,13 +684,6 @@ impl<'db> SemanticsImpl<'db> {
token.clone(), token.clone(),
&mut |InFile { value, .. }| { &mut |InFile { value, .. }| {
let is_a_match = match mode { let is_a_match = match mode {
// Dp::SameText(text) => value.text() == text,
Dp::SameKind(preferred_kind) => {
let kind = fetch_kind(&value);
kind == preferred_kind
// special case for derive macros
|| (preferred_kind == SyntaxKind::IDENT && kind == SyntaxKind::NAME_REF)
}
Dp::None => true, Dp::None => true,
}; };
if is_a_match { if is_a_match {
@ -733,7 +714,7 @@ impl<'db> SemanticsImpl<'db> {
token: SyntaxToken, token: SyntaxToken,
mut cb: impl FnMut(InFile<SyntaxToken>) -> ControlFlow<T>, mut cb: impl FnMut(InFile<SyntaxToken>) -> ControlFlow<T>,
) -> Option<T> { ) -> Option<T> {
self.descend_into_macros_impl(token.clone(), &mut |t| cb(t)) self.descend_into_macros_impl(token.clone(), &mut cb)
} }
/// Descends the token into expansions, returning the tokens that matches the input /// Descends the token into expansions, returning the tokens that matches the input

View file

@ -186,7 +186,7 @@ fn hover_simple(
}); });
let descended = || descended.iter(); let descended = || descended.iter();
// FIXME: WE should not try these step by step, instead to accommodate for macros we should run // TODO: WE should not try these step by step, instead to accommodate for macros we should run
// all of these in "parallel" and rank their results // all of these in "parallel" and rank their results
let result = None let result = None
// try lint hover // try lint hover

View file

@ -13,7 +13,9 @@ mod html;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
use hir::{DescendPreference, Name, Semantics}; use std::ops::ControlFlow;
use hir::{Name, Semantics};
use ide_db::{FxHashMap, RootDatabase, SymbolKind}; use ide_db::{FxHashMap, RootDatabase, SymbolKind};
use span::EditionedFileId; use span::EditionedFileId;
use syntax::{ use syntax::{
@ -399,20 +401,53 @@ fn traverse(
// Attempt to descend tokens into macro-calls. // Attempt to descend tokens into macro-calls.
let res = match element { let res = match element {
NodeOrToken::Token(token) if token.kind() != COMMENT => { NodeOrToken::Token(token) if token.kind() != COMMENT => {
let token = if token.kind() == STRING { let kind = token.kind();
// for strings, try to prefer a string that has not been lost in a token let text = token.text();
// tree let ident_kind = kind.is_any_identifier();
// FIXME: This should be done for everything, but check perf first
sema.descend_into_macros(DescendPreference::SameKind, token) let mut t = None;
.into_iter() let mut r = 0;
.max_by_key(|it| { // FIXME: Add an extra API that takes the file id of this. That is a simple way
it.parent().map_or(false, |it| it.kind() != TOKEN_TREE) // to prevent us constantly walking up the tree to fetch the file
}) sema.descend_into_macros_ng_b(token.clone(), |tok| {
.unwrap() let tok = tok.value;
} else { let tok_kind = tok.kind();
// FIXME: We should probably rank the tokens and find the most suitable?
sema.descend_into_macros_single_exact(token) let exact_same_kind = tok_kind == kind;
}; let both_idents =
exact_same_kind || (tok_kind.is_any_identifier() && ident_kind);
let same_text = tok.text() == text;
// anything that mapped into a token tree has likely no semantic information
let no_tt_parent = tok.parent().map_or(false, |it| it.kind() != TOKEN_TREE);
let my_rank = (both_idents as usize)
| ((exact_same_kind as usize) << 1)
| ((same_text as usize) << 2)
| ((no_tt_parent as usize) << 3);
if my_rank > 0b1110 {
// a rank of 0b1110 means that we have found a maximally interesting
// token so stop early.
t = Some(tok);
return ControlFlow::Break(());
}
// r = r.max(my_rank);
// t = Some(t.take_if(|_| r < my_rank).unwrap_or(tok));
match &mut t {
Some(prev) if r < my_rank => {
*prev = tok;
r = my_rank;
}
Some(_) => (),
None => {
r = my_rank;
t = Some(tok)
}
}
ControlFlow::Continue(())
});
let token = t.unwrap_or(token);
match token.parent().and_then(ast::NameLike::cast) { match token.parent().and_then(ast::NameLike::cast) {
// Remap the token into the wrapping single token nodes // Remap the token into the wrapping single token nodes
Some(parent) => match (token.kind(), parent.syntax().kind()) { Some(parent) => match (token.kind(), parent.syntax().kind()) {

View file

@ -58,7 +58,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
<span class="brace">}</span> <span class="brace">}</span>
<span class="macro">def_fn</span><span class="macro_bang">!</span> <span class="brace macro">{</span> <span class="macro">def_fn</span><span class="macro_bang">!</span> <span class="brace macro">{</span>
<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> <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>
<span class="numeric_literal macro">100</span> <span class="numeric_literal macro">100</span>
<span class="brace macro">}</span> <span class="brace macro">}</span>
<span class="brace macro">}</span> <span class="brace macro">}</span>