fix: Fix container search failing for tokens originating within derive attributes

This commit is contained in:
Lukas Wirth 2024-06-01 20:57:29 +02:00
parent d6d735e6f2
commit c0171bdd32
5 changed files with 48 additions and 40 deletions

View file

@ -298,7 +298,7 @@ pub fn expand_speculative(
// prefer tokens of the same kind and text
// Note the inversion of the score here, as we want to prefer the first token in case
// of all tokens having the same score
(t.kind() != token_to_map.kind()) as u8 + (t.text() != token_to_map.text()) as u8
(t.kind() != token_to_map.kind()) as u8 + 2 * ((t.text() != token_to_map.text()) as u8)
})?;
Some((node.syntax_node(), token))
}

View file

@ -153,24 +153,20 @@ impl<FileId: Copy, N: AstNode> InFileWrapper<FileId, N> {
// region:specific impls
impl InFile<&SyntaxNode> {
/// Skips the attributed item that caused the macro invocation we are climbing up
pub fn ancestors_with_macros_skip_attr_item(
/// Traverse up macro calls and skips the macro invocation node
pub fn ancestors_with_macros(
self,
db: &dyn db::ExpandDatabase,
) -> impl Iterator<Item = InFile<SyntaxNode>> + '_ {
let succ = move |node: &InFile<SyntaxNode>| match node.value.parent() {
Some(parent) => Some(node.with_value(parent)),
None => {
let macro_file_id = node.file_id.macro_file()?;
let parent_node = macro_file_id.call_node(db);
if macro_file_id.is_attr_macro(db) {
// macro call was an attributed item, skip it
// FIXME: does this fail if this is a direct expansion of another macro?
parent_node.map(|node| node.parent()).transpose()
} else {
Some(parent_node)
}
}
None => db
.lookup_intern_macro_call(node.file_id.macro_file()?.macro_call_id)
.to_node_item(db)
.syntax()
.cloned()
.map(|node| node.parent())
.transpose(),
};
iter::successors(succ(&self.cloned()), succ)
}

View file

@ -33,8 +33,8 @@ use std::{fmt, hash::Hash};
use base_db::{salsa::impl_intern_value_trivial, CrateId, FileId};
use either::Either;
use span::{
Edition, ErasedFileAstId, FileRange, HirFileIdRepr, Span, SpanAnchor, SyntaxContextData,
SyntaxContextId,
Edition, ErasedFileAstId, FileAstId, FileRange, HirFileIdRepr, Span, SpanAnchor,
SyntaxContextData, SyntaxContextId,
};
use syntax::{
ast::{self, AstNode},
@ -546,6 +546,18 @@ impl MacroCallLoc {
}
}
pub fn to_node_item(&self, db: &dyn ExpandDatabase) -> InFile<ast::Item> {
match self.kind {
MacroCallKind::FnLike { ast_id, .. } => {
InFile::new(ast_id.file_id, ast_id.map(FileAstId::upcast).to_node(db))
}
MacroCallKind::Derive { ast_id, .. } => {
InFile::new(ast_id.file_id, ast_id.map(FileAstId::upcast).to_node(db))
}
MacroCallKind::Attr { ast_id, .. } => InFile::new(ast_id.file_id, ast_id.to_node(db)),
}
}
fn expand_to(&self) -> ExpandTo {
match self.kind {
MacroCallKind::FnLike { expand_to, .. } => expand_to,