fix: Fix SearchScope using incorrect text ranges for macro-emitted inline modules

This commit is contained in:
Lukas Wirth 2022-04-06 13:58:40 +02:00
parent 2366d8e05f
commit d9f6cee100
2 changed files with 89 additions and 59 deletions

View file

@ -175,15 +175,17 @@ impl HirFileId {
/// For macro-expansion files, returns the file original source file the
/// expansion originated from.
pub fn original_file(self, db: &dyn db::AstDatabase) -> FileId {
match self.0 {
HirFileIdRepr::FileId(file_id) => file_id,
HirFileIdRepr::MacroFile(macro_file) => {
let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_file.macro_call_id);
let file_id = match loc.eager {
Some(EagerCallInfo { included_file: Some(file), .. }) => file.into(),
_ => loc.kind.file_id(),
};
file_id.original_file(db)
let mut file_id = self;
loop {
match file_id.0 {
HirFileIdRepr::FileId(id) => break id,
HirFileIdRepr::MacroFile(MacroFile { macro_call_id }) => {
let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_call_id);
file_id = match loc.eager {
Some(EagerCallInfo { included_file: Some(file), .. }) => file.into(),
_ => loc.kind.file_id(),
};
}
}
}
}
@ -211,6 +213,24 @@ impl HirFileId {
}
}
/// If this is a macro call, returns the syntax node of the very first macro call this file resides in.
pub fn original_call_node(self, db: &dyn db::AstDatabase) -> Option<(FileId, SyntaxNode)> {
let mut call = match self.0 {
HirFileIdRepr::FileId(_) => return None,
HirFileIdRepr::MacroFile(MacroFile { macro_call_id }) => {
db.lookup_intern_macro_call(macro_call_id).kind.to_node(db)
}
};
loop {
match call.file_id.0 {
HirFileIdRepr::FileId(file_id) => break Some((file_id, call.value)),
HirFileIdRepr::MacroFile(MacroFile { macro_call_id }) => {
call = db.lookup_intern_macro_call(macro_call_id).kind.to_node(db);
}
}
}
}
/// Return expansion information if it is a macro-expansion file
pub fn expansion_info(self, db: &dyn db::AstDatabase) -> Option<ExpansionInfo> {
match self.0 {
@ -675,9 +695,11 @@ impl<T> InFile<T> {
pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> InFile<U> {
InFile::new(self.file_id, f(self.value))
}
pub fn as_ref(&self) -> InFile<&T> {
self.with_value(&self.value)
}
pub fn file_syntax(&self, db: &dyn db::AstDatabase) -> SyntaxNode {
db.parse_or_expand(self.file_id).expect("source created from invalid file")
}