Factor out pick_best_token ide pattern into ide_db

This commit is contained in:
Lukas Wirth 2021-06-22 17:28:07 +02:00
parent 4e2ec914f4
commit f615efdfc3
9 changed files with 62 additions and 101 deletions

View file

@ -1,11 +1,8 @@
use std::iter;
use hir::Semantics;
use ide_db::RootDatabase;
use syntax::{
ast, ted, AstNode, NodeOrToken, SyntaxKind, SyntaxKind::*, SyntaxNode, SyntaxToken,
TokenAtOffset, WalkEvent, T,
};
use ide_db::{helpers::pick_best_token, RootDatabase};
use syntax::{ast, ted, AstNode, NodeOrToken, SyntaxKind, SyntaxKind::*, SyntaxNode, WalkEvent, T};
use crate::FilePosition;
@ -29,7 +26,10 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<
let sema = Semantics::new(db);
let file = sema.parse(position.file_id);
let tok = pick_best(file.syntax().token_at_offset(position.offset))?;
let tok = pick_best_token(file.syntax().token_at_offset(position.offset), |kind| match kind {
SyntaxKind::IDENT => 1,
_ => 0,
})?;
let mut expanded = None;
let mut name = None;
for node in tok.ancestors() {
@ -57,16 +57,6 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<
Some(ExpandedMacro { name: name?, expansion })
}
fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> {
return tokens.max_by_key(priority);
fn priority(n: &SyntaxToken) -> usize {
match n.kind() {
IDENT => 1,
_ => 0,
}
}
}
fn expand_macro_recur(
sema: &Semantics<RootDatabase>,
macro_call: &ast::MacroCall,