Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 57683cf

Browse files
committed
Factor out token ranking
1 parent e37c6dc commit 57683cf

File tree

4 files changed

+38
-37
lines changed

4 files changed

+38
-37
lines changed

src/tools/rust-analyzer/crates/ide-db/src/lib.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,33 @@ impl SnippetCap {
293293
}
294294
}
295295
}
296+
297+
pub struct Ranker<'a> {
298+
pub kind: parser::SyntaxKind,
299+
pub text: &'a str,
300+
pub ident_kind: bool,
301+
}
302+
303+
impl<'a> Ranker<'a> {
304+
pub fn from_token(token: &'a syntax::SyntaxToken) -> Self {
305+
let kind = token.kind();
306+
Ranker { kind, text: token.text(), ident_kind: kind.is_any_identifier() }
307+
}
308+
309+
/// A utility function that ranks a token again a given kind and text, returning a number that
310+
/// represents how close the token is to the given kind and text.
311+
pub fn rank_token(&self, tok: &syntax::SyntaxToken) -> usize {
312+
let tok_kind = tok.kind();
313+
314+
let exact_same_kind = tok_kind == self.kind;
315+
let both_idents = exact_same_kind || (tok_kind.is_any_identifier() && self.ident_kind);
316+
let same_text = tok.text() == self.text;
317+
// anything that mapped into a token tree has likely no semantic information
318+
let no_tt_parent =
319+
tok.parent().map_or(false, |it| it.kind() != parser::SyntaxKind::TOKEN_TREE);
320+
!((both_idents as usize)
321+
| ((exact_same_kind as usize) << 1)
322+
| ((same_text as usize) << 2)
323+
| ((no_tt_parent as usize) << 3))
324+
}
325+
}

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

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use ide_db::{
1111
defs::{Definition, IdentClass, NameRefClass, OperatorClass},
1212
famous_defs::FamousDefs,
1313
helpers::pick_best_token,
14-
FileRange, FxIndexSet, RootDatabase,
14+
FileRange, FxIndexSet, Ranker, RootDatabase,
1515
};
1616
use itertools::{multizip, Itertools};
1717
use span::Edition;
@@ -182,27 +182,13 @@ fn hover_offset(
182182
// equivalency is more important
183183
let mut descended = sema.descend_into_macros(original_token.clone());
184184

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_cached_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))
201-
});
185+
let ranker = Ranker::from_token(&original_token);
186+
187+
descended.sort_by_cached_key(|tok| ranker.rank_token(tok));
202188

203189
let mut res = vec![];
204190
for token in descended {
205-
let is_same_kind = token.kind() == kind;
191+
let is_same_kind = token.kind() == ranker.kind;
206192
let lint_hover = (|| {
207193
// FIXME: Definition should include known lints and the like instead of having this special case here
208194
let attr = token.parent_ancestors().find_map(ast::Attr::cast)?;

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

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ mod tests;
1616
use std::ops::ControlFlow;
1717

1818
use hir::{InRealFile, Name, Semantics};
19-
use ide_db::{FxHashMap, RootDatabase, SymbolKind};
19+
use ide_db::{FxHashMap, Ranker, RootDatabase, SymbolKind};
2020
use span::EditionedFileId;
2121
use syntax::{
2222
ast::{self, IsString},
@@ -401,9 +401,7 @@ fn traverse(
401401
// Attempt to descend tokens into macro-calls.
402402
let res = match element {
403403
NodeOrToken::Token(token) if token.kind() != COMMENT => {
404-
let kind = token.kind();
405-
let text = token.text();
406-
let ident_kind = kind.is_any_identifier();
404+
let ranker = Ranker::from_token(&token);
407405

408406
let mut t = None;
409407
let mut r = 0;
@@ -412,19 +410,7 @@ fn traverse(
412410
|tok, _ctx| {
413411
// FIXME: Consider checking ctx transparency for being opaque?
414412
let tok = tok.value;
415-
let tok_kind = tok.kind();
416-
417-
let exact_same_kind = tok_kind == kind;
418-
let both_idents =
419-
exact_same_kind || (tok_kind.is_any_identifier() && ident_kind);
420-
let same_text = tok.text() == text;
421-
// anything that mapped into a token tree has likely no semantic information
422-
let no_tt_parent =
423-
tok.parent().map_or(false, |it| it.kind() != TOKEN_TREE);
424-
let my_rank = (both_idents as usize)
425-
| ((exact_same_kind as usize) << 1)
426-
| ((same_text as usize) << 2)
427-
| ((no_tt_parent as usize) << 3);
413+
let my_rank = ranker.rank_token(&tok);
428414

429415
if my_rank > 0b1110 {
430416
// a rank of 0b1110 means that we have found a maximally interesting

src/tools/rust-analyzer/rust-bors.toml

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)