about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLukas Wirth <lukastw97@gmail.com>2024-08-22 17:01:51 +0200
committerLukas Wirth <lukastw97@gmail.com>2024-08-22 17:01:51 +0200
commitb0e7ef4031cc6215cfb9045512d44ebba3ae82fd (patch)
tree7692ec7911974892da0ddd58277a56e489d2694c
parentd79999aaa00679bfc581d30858d703b5b959137c (diff)
downloadrust-b0e7ef4031cc6215cfb9045512d44ebba3ae82fd.tar.gz
rust-b0e7ef4031cc6215cfb9045512d44ebba3ae82fd.zip
Sort hover results by relevance
-rw-r--r--src/tools/rust-analyzer/crates/ide/src/hover.rs22
-rw-r--r--src/tools/rust-analyzer/crates/ide/src/syntax_highlighting.rs2
2 files changed, 18 insertions, 6 deletions
diff --git a/src/tools/rust-analyzer/crates/ide/src/hover.rs b/src/tools/rust-analyzer/crates/ide/src/hover.rs
index d76d9afc185..edf14a6f4bf 100644
--- a/src/tools/rust-analyzer/crates/ide/src/hover.rs
+++ b/src/tools/rust-analyzer/crates/ide/src/hover.rs
@@ -180,11 +180,25 @@ fn hover_simple(
 
     // prefer descending the same token kind in attribute expansions, in normal macros text
     // equivalency is more important
-    let mut descended = vec![];
-    sema.descend_into_macros_cb(original_token.clone(), |token| {
-        descended.push(token.value);
+    let mut descended = sema.descend_into_macros(original_token.clone());
+
+    let kind = original_token.kind();
+    let text = original_token.text();
+    let ident_kind = kind.is_any_identifier();
+
+    descended.sort_by_key(|tok| {
+        let tok_kind = tok.kind();
+
+        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);
+        (both_idents as usize)
+            | ((exact_same_kind as usize) << 1)
+            | ((same_text as usize) << 2)
+            | ((no_tt_parent as usize) << 3)
     });
-    let descended = || descended.iter();
 
     // 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
diff --git a/src/tools/rust-analyzer/crates/ide/src/syntax_highlighting.rs b/src/tools/rust-analyzer/crates/ide/src/syntax_highlighting.rs
index bfab5ceb129..927fdaa178c 100644
--- a/src/tools/rust-analyzer/crates/ide/src/syntax_highlighting.rs
+++ b/src/tools/rust-analyzer/crates/ide/src/syntax_highlighting.rs
@@ -407,8 +407,6 @@ fn traverse(
 
                     let mut t = None;
                     let mut r = 0;
-                    // FIXME: Add an extra API that takes the file id of this. That is a simple way
-                    // to prevent us constantly walking up the tree to fetch the file
                     sema.descend_into_macros_breakable(
                         InRealFile::new(file_id, token.clone()),
                         |tok| {