Rename fails on renaming definitions created by macros

This commit is contained in:
Lukas Wirth 2021-08-19 22:53:00 +02:00
parent 320bb72b7f
commit c67ecbebc4
5 changed files with 62 additions and 66 deletions

View file

@ -22,8 +22,7 @@ use either::Either;
pub use mbe::{ExpandError, ExpandResult};
pub use parser::FragmentKind;
use std::hash::Hash;
use std::sync::Arc;
use std::{hash::Hash, sync::Arc};
use base_db::{impl_intern_key, salsa, CrateId, FileId, FileRange};
use syntax::{
@ -32,11 +31,13 @@ use syntax::{
Direction, SyntaxNode, SyntaxToken, TextRange, TextSize,
};
use crate::ast_id_map::FileAstId;
use crate::builtin_attr::BuiltinAttrExpander;
use crate::builtin_derive::BuiltinDeriveExpander;
use crate::builtin_macro::{BuiltinFnLikeExpander, EagerExpander};
use crate::proc_macro::ProcMacroExpander;
use crate::{
ast_id_map::FileAstId,
builtin_attr::BuiltinAttrExpander,
builtin_derive::BuiltinDeriveExpander,
builtin_macro::{BuiltinFnLikeExpander, EagerExpander},
proc_macro::ProcMacroExpander,
};
#[cfg(test)]
mod test_db;
@ -210,12 +211,12 @@ impl MacroDefId {
pub fn ast_id(&self) -> Either<AstId<ast::Macro>, AstId<ast::Fn>> {
let id = match &self.kind {
MacroDefKind::Declarative(id) => id,
MacroDefKind::BuiltIn(_, id) => id,
MacroDefKind::BuiltInAttr(_, id) => id,
MacroDefKind::BuiltInDerive(_, id) => id,
MacroDefKind::BuiltInEager(_, id) => id,
MacroDefKind::ProcMacro(.., id) => return Either::Right(*id),
MacroDefKind::Declarative(id)
| MacroDefKind::BuiltIn(_, id)
| MacroDefKind::BuiltInAttr(_, id)
| MacroDefKind::BuiltInDerive(_, id)
| MacroDefKind::BuiltInEager(_, id) => id,
};
Either::Left(*id)
}
@ -464,15 +465,10 @@ impl InFile<SyntaxNode> {
}
impl<'a> InFile<&'a SyntaxNode> {
/// Falls back to the macro call range if the node cannot be mapped up fully.
pub fn original_file_range(self, db: &dyn db::AstDatabase) -> FileRange {
if let Some(range) = original_range_opt(db, self) {
let original_file = range.file_id.original_file(db);
if range.file_id == original_file.into() {
return FileRange { file_id: original_file, range: range.value };
}
log::error!("Fail to mapping up more for {:?}", range);
return FileRange { file_id: range.file_id.original_file(db), range: range.value };
if let Some(res) = self.original_file_range_opt(db) {
return res;
}
// Fall back to whole macro call.
@ -483,8 +479,27 @@ impl<'a> InFile<&'a SyntaxNode> {
let orig_file = node.file_id.original_file(db);
assert_eq!(node.file_id, orig_file.into());
FileRange { file_id: orig_file, range: node.value.text_range() }
}
/// Attempts to map the syntax node back up its macro calls.
pub fn original_file_range_opt(self, db: &dyn db::AstDatabase) -> Option<FileRange> {
match original_range_opt(db, self) {
Some(range) => {
let original_file = range.file_id.original_file(db);
if range.file_id != original_file.into() {
log::error!("Failed mapping up more for {:?}", range);
}
Some(FileRange { file_id: original_file, range: range.value })
}
_ if !self.file_id.is_macro() => Some(FileRange {
file_id: self.file_id.original_file(db),
range: self.value.text_range(),
}),
_ => None,
}
}
}
fn original_range_opt(