mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-30 03:27:44 +00:00
Merge pull request #20160 from Veykril/push-pqvskktpnylu
fix: Improve diagnostic ranges for `macro_calls!`
This commit is contained in:
commit
f14bf95931
12 changed files with 71 additions and 49 deletions
|
|
@ -315,11 +315,11 @@ impl<SN: Borrow<SyntaxNode>> InFile<SN> {
|
|||
}
|
||||
|
||||
/// Falls back to the macro call range if the node cannot be mapped up fully.
|
||||
pub fn original_file_range_with_macro_call_body(
|
||||
pub fn original_file_range_with_macro_call_input(
|
||||
self,
|
||||
db: &dyn db::ExpandDatabase,
|
||||
) -> FileRange {
|
||||
self.borrow().map(SyntaxNode::text_range).original_node_file_range_with_macro_call_body(db)
|
||||
self.borrow().map(SyntaxNode::text_range).original_node_file_range_with_macro_call_input(db)
|
||||
}
|
||||
|
||||
pub fn original_syntax_node_rooted(
|
||||
|
|
@ -465,7 +465,7 @@ impl InFile<TextRange> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn original_node_file_range_with_macro_call_body(
|
||||
pub fn original_node_file_range_with_macro_call_input(
|
||||
self,
|
||||
db: &dyn db::ExpandDatabase,
|
||||
) -> FileRange {
|
||||
|
|
@ -476,7 +476,7 @@ impl InFile<TextRange> {
|
|||
Some(it) => it,
|
||||
_ => {
|
||||
let loc = db.lookup_intern_macro_call(mac_file);
|
||||
loc.kind.original_call_range_with_body(db)
|
||||
loc.kind.original_call_range_with_input(db)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -497,6 +497,18 @@ impl InFile<TextRange> {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn original_node_file_range_rooted_opt(
|
||||
self,
|
||||
db: &dyn db::ExpandDatabase,
|
||||
) -> Option<FileRange> {
|
||||
match self.file_id {
|
||||
HirFileId::FileId(file_id) => Some(FileRange { file_id, range: self.value }),
|
||||
HirFileId::MacroFile(mac_file) => {
|
||||
map_node_range_up_rooted(db, &db.expansion_span_map(mac_file), self.value)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<N: AstNode> InFile<N> {
|
||||
|
|
|
|||
|
|
@ -688,8 +688,11 @@ impl MacroCallKind {
|
|||
|
||||
/// Returns the original file range that best describes the location of this macro call.
|
||||
///
|
||||
/// Unlike `MacroCallKind::original_call_range`, this also spans the item of attributes and derives.
|
||||
pub fn original_call_range_with_body(self, db: &dyn ExpandDatabase) -> FileRange {
|
||||
/// This spans the entire macro call, including its input. That is for
|
||||
/// - fn_like! {}, it spans the path and token tree
|
||||
/// - #\[derive], it spans the `#[derive(...)]` attribute and the annotated item
|
||||
/// - #\[attr], it spans the `#[attr(...)]` attribute and the annotated item
|
||||
pub fn original_call_range_with_input(self, db: &dyn ExpandDatabase) -> FileRange {
|
||||
let mut kind = self;
|
||||
let file_id = loop {
|
||||
match kind.file_id() {
|
||||
|
|
@ -712,8 +715,8 @@ impl MacroCallKind {
|
|||
/// Returns the original file range that best describes the location of this macro call.
|
||||
///
|
||||
/// Here we try to roughly match what rustc does to improve diagnostics: fn-like macros
|
||||
/// get the whole `ast::MacroCall`, attribute macros get the attribute's range, and derives
|
||||
/// get only the specific derive that is being referred to.
|
||||
/// get the macro path (rustc shows the whole `ast::MacroCall`), attribute macros get the
|
||||
/// attribute's range, and derives get only the specific derive that is being referred to.
|
||||
pub fn original_call_range(self, db: &dyn ExpandDatabase) -> FileRange {
|
||||
let mut kind = self;
|
||||
let file_id = loop {
|
||||
|
|
@ -726,7 +729,14 @@ impl MacroCallKind {
|
|||
};
|
||||
|
||||
let range = match kind {
|
||||
MacroCallKind::FnLike { ast_id, .. } => ast_id.to_ptr(db).text_range(),
|
||||
MacroCallKind::FnLike { ast_id, .. } => {
|
||||
let node = ast_id.to_node(db);
|
||||
node.path()
|
||||
.unwrap()
|
||||
.syntax()
|
||||
.text_range()
|
||||
.cover(node.excl_token().unwrap().text_range())
|
||||
}
|
||||
MacroCallKind::Derive { ast_id, derive_attr_index, .. } => {
|
||||
// FIXME: should be the range of the macro name, not the whole derive
|
||||
// FIXME: handle `cfg_attr`
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue