Skip to content

Commit addd93e

Browse files
committed
Don't search for root nodes unnecessarily
1 parent 8dad1b9 commit addd93e

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

crates/hir/src/semantics.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use smallvec::{smallvec, SmallVec};
1818
use syntax::{
1919
algo::skip_trivia_token,
2020
ast::{self, HasAttrs, HasGenericParams, HasLoopBody},
21-
match_ast, AstNode, Direction, SyntaxNode, SyntaxNodePtr, SyntaxToken, TextRange, TextSize,
21+
match_ast, AstNode, Direction, SyntaxNode, SyntaxNodePtr, SyntaxToken, TextSize,
2222
};
2323

2424
use crate::{
@@ -556,25 +556,27 @@ impl<'db> SemanticsImpl<'db> {
556556
None => return,
557557
};
558558
let sa = self.analyze(&parent);
559-
let mut stack: SmallVec<[_; 1]> = smallvec![InFile::new(sa.file_id, token)];
559+
let mut stack: SmallVec<[_; 4]> = smallvec![InFile::new(sa.file_id, token)];
560560
let mut cache = self.expansion_info_cache.borrow_mut();
561561
let mut mcache = self.macro_call_cache.borrow_mut();
562562

563563
let mut process_expansion_for_token =
564-
|stack: &mut SmallVec<_>, file_id, item, token: InFile<&_>| {
565-
let mapped_tokens = cache
566-
.entry(file_id)
567-
.or_insert_with(|| file_id.expansion_info(self.db.upcast()))
568-
.as_ref()?
569-
.map_token_down(self.db.upcast(), item, token)?;
564+
|stack: &mut SmallVec<_>, macro_file, item, token: InFile<&_>| {
565+
let expansion_info = cache
566+
.entry(macro_file)
567+
.or_insert_with(|| macro_file.expansion_info(self.db.upcast()))
568+
.as_ref()?;
569+
570+
{
571+
let InFile { file_id, value } = expansion_info.expanded();
572+
self.cache(value, file_id);
573+
}
574+
575+
let mapped_tokens = expansion_info.map_token_down(self.db.upcast(), item, token)?;
570576

571577
let len = stack.len();
572578
// requeue the tokens we got from mapping our current token down
573-
stack.extend(mapped_tokens.inspect(|token| {
574-
if let Some(parent) = token.value.parent() {
575-
self.cache(find_root(&parent), token.file_id);
576-
}
577-
}));
579+
stack.extend(mapped_tokens);
578580
// if the length changed we have found a mapping for the token
579581
(stack.len() != len).then(|| ())
580582
};
@@ -606,17 +608,15 @@ impl<'db> SemanticsImpl<'db> {
606608
}
607609

608610
// or are we inside a function-like macro call
609-
if let Some(macro_call) = token.value.ancestors().find_map(ast::MacroCall::cast) {
610-
let tt = macro_call.token_tree()?;
611-
let l_delim = match tt.left_delimiter_token() {
612-
Some(it) => it.text_range().end(),
613-
None => tt.syntax().text_range().start(),
614-
};
615-
let r_delim = match tt.right_delimiter_token() {
616-
Some(it) => it.text_range().start(),
617-
None => tt.syntax().text_range().end(),
618-
};
619-
if !TextRange::new(l_delim, r_delim).contains_range(token.value.text_range()) {
611+
if let Some(tt) =
612+
// FIXME replace map.while_some with take_while once stable
613+
token.value.ancestors().map(ast::TokenTree::cast).while_some().last()
614+
{
615+
let macro_call = tt.syntax().parent().and_then(ast::MacroCall::cast)?;
616+
if tt.left_delimiter_token().map_or(false, |it| it == token.value) {
617+
return None;
618+
}
619+
if tt.right_delimiter_token().map_or(false, |it| it == token.value) {
620620
return None;
621621
}
622622

crates/hir_expand/src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub mod eager;
1818
use base_db::ProcMacroKind;
1919
use either::Either;
2020

21-
pub use mbe::{ExpandError, ExpandResult};
21+
pub use mbe::{ExpandError, ExpandResult, Origin};
2222

2323
use std::{hash::Hash, iter, sync::Arc};
2424

@@ -380,9 +380,11 @@ pub struct ExpansionInfo {
380380
exp_map: Arc<mbe::TokenMap>,
381381
}
382382

383-
pub use mbe::Origin;
384-
385383
impl ExpansionInfo {
384+
pub fn expanded(&self) -> InFile<SyntaxNode> {
385+
self.expanded.clone()
386+
}
387+
386388
pub fn call_node(&self) -> Option<InFile<SyntaxNode>> {
387389
Some(self.arg.with_value(self.arg.value.parent()?))
388390
}

0 commit comments

Comments
 (0)