Skip to content

Commit b0e7ef4

Browse files
committed
Sort hover results by relevance
1 parent d79999a commit b0e7ef4

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/tools/rust-analyzer/crates/ide/src/hover.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,25 @@ fn hover_simple(
180180

181181
// prefer descending the same token kind in attribute expansions, in normal macros text
182182
// equivalency is more important
183-
let mut descended = vec![];
184-
sema.descend_into_macros_cb(original_token.clone(), |token| {
185-
descended.push(token.value);
183+
let mut descended = sema.descend_into_macros(original_token.clone());
184+
185+
let kind = original_token.kind();
186+
let text = original_token.text();
187+
let ident_kind = kind.is_any_identifier();
188+
189+
descended.sort_by_key(|tok| {
190+
let tok_kind = tok.kind();
191+
192+
let exact_same_kind = tok_kind == kind;
193+
let both_idents = exact_same_kind || (tok_kind.is_any_identifier() && ident_kind);
194+
let same_text = tok.text() == text;
195+
// anything that mapped into a token tree has likely no semantic information
196+
let no_tt_parent = tok.parent().map_or(false, |it| it.kind() != TOKEN_TREE);
197+
(both_idents as usize)
198+
| ((exact_same_kind as usize) << 1)
199+
| ((same_text as usize) << 2)
200+
| ((no_tt_parent as usize) << 3)
186201
});
187-
let descended = || descended.iter();
188202

189203
// TODO: WE should not try these step by step, instead to accommodate for macros we should run
190204
// all of these in "parallel" and rank their results

src/tools/rust-analyzer/crates/ide/src/syntax_highlighting.rs

-2
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,6 @@ fn traverse(
407407

408408
let mut t = None;
409409
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
412410
sema.descend_into_macros_breakable(
413411
InRealFile::new(file_id, token.clone()),
414412
|tok| {

0 commit comments

Comments
 (0)