Skip to content

Commit c61bb6b

Browse files
committed
Auto merge of #12064 - Veykril:attr-range, r=Veykril
fix: Fix `ide_db::search` not searching bodies of attributed items Fixes #12050
2 parents 23e47e1 + d524e43 commit c61bb6b

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

crates/hir_expand/src/lib.rs

+42
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,29 @@ impl MacroCallKind {
435435
}
436436
}
437437

438+
/// Returns the original file range that best describes the location of this macro call.
439+
///
440+
/// Unlike `MacroCallKind::original_call_range`, this also spans the item of attributes and derives.
441+
pub fn original_call_range_with_body(self, db: &dyn db::AstDatabase) -> FileRange {
442+
let mut kind = self;
443+
let file_id = loop {
444+
match kind.file_id().0 {
445+
HirFileIdRepr::MacroFile(file) => {
446+
kind = db.lookup_intern_macro_call(file.macro_call_id).kind;
447+
}
448+
HirFileIdRepr::FileId(file_id) => break file_id,
449+
}
450+
};
451+
452+
let range = match kind {
453+
MacroCallKind::FnLike { ast_id, .. } => ast_id.to_node(db).syntax().text_range(),
454+
MacroCallKind::Derive { ast_id, .. } => ast_id.to_node(db).syntax().text_range(),
455+
MacroCallKind::Attr { ast_id, .. } => ast_id.to_node(db).syntax().text_range(),
456+
};
457+
458+
FileRange { range, file_id }
459+
}
460+
438461
/// Returns the original file range that best describes the location of this macro call.
439462
///
440463
/// Here we try to roughly match what rustc does to improve diagnostics: fn-like macros
@@ -751,6 +774,9 @@ impl<'a> InFile<&'a SyntaxNode> {
751774
}
752775

753776
/// Falls back to the macro call range if the node cannot be mapped up fully.
777+
///
778+
/// For attributes and derives, this will point back to the attribute only.
779+
/// For the entire item `InFile::use original_file_range_full`.
754780
pub fn original_file_range(self, db: &dyn db::AstDatabase) -> FileRange {
755781
if let Some(res) = self.original_file_range_opt(db) {
756782
return res;
@@ -766,6 +792,22 @@ impl<'a> InFile<&'a SyntaxNode> {
766792
}
767793
}
768794

795+
/// Falls back to the macro call range if the node cannot be mapped up fully.
796+
pub fn original_file_range_full(self, db: &dyn db::AstDatabase) -> FileRange {
797+
if let Some(res) = self.original_file_range_opt(db) {
798+
return res;
799+
}
800+
801+
// Fall back to whole macro call.
802+
match self.file_id.0 {
803+
HirFileIdRepr::FileId(file_id) => FileRange { file_id, range: self.value.text_range() },
804+
HirFileIdRepr::MacroFile(mac_file) => {
805+
let loc = db.lookup_intern_macro_call(mac_file.macro_call_id);
806+
loc.kind.original_call_range_with_body(db)
807+
}
808+
}
809+
}
810+
769811
/// Attempts to map the syntax node back up its macro calls.
770812
pub fn original_file_range_opt(self, db: &dyn db::AstDatabase) -> Option<FileRange> {
771813
match ascend_node_border_tokens(db, self) {

crates/ide_assists/src/handlers/generate_function.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ fn get_adt_source(
171171
adt: &hir::Adt,
172172
fn_name: &str,
173173
) -> Option<(Option<ast::Impl>, FileId)> {
174-
let range = adt.source(ctx.sema.db)?.syntax().original_file_range(ctx.sema.db);
174+
let range = adt.source(ctx.sema.db)?.syntax().original_file_range_full(ctx.sema.db);
175175
let file = ctx.sema.parse(range.file_id);
176176
let adt_source =
177177
ctx.sema.find_node_at_offset_with_macros(file.syntax(), range.range.start())?;

crates/ide_assists/src/handlers/inline_call.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ pub(crate) fn inline_call(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
198198
let fn_body = fn_source.value.body()?;
199199
let param_list = fn_source.value.param_list()?;
200200

201-
let FileRange { file_id, range } = fn_source.syntax().original_file_range(ctx.sema.db);
201+
let FileRange { file_id, range } = fn_source.syntax().original_file_range_full(ctx.sema.db);
202202
if file_id == ctx.file_id() && range.contains(ctx.offset()) {
203203
cov_mark::hit!(inline_call_recursive);
204204
return None;

crates/ide_db/src/search.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -239,14 +239,14 @@ impl Definition {
239239
DefWithBody::Static(s) => s.source(db).map(|src| src.syntax().cloned()),
240240
};
241241
return match def {
242-
Some(def) => SearchScope::file_range(def.as_ref().original_file_range(db)),
242+
Some(def) => SearchScope::file_range(def.as_ref().original_file_range_full(db)),
243243
None => SearchScope::single_file(file_id),
244244
};
245245
}
246246

247247
if let Definition::SelfType(impl_) = self {
248248
return match impl_.source(db).map(|src| src.syntax().cloned()) {
249-
Some(def) => SearchScope::file_range(def.as_ref().original_file_range(db)),
249+
Some(def) => SearchScope::file_range(def.as_ref().original_file_range_full(db)),
250250
None => SearchScope::single_file(file_id),
251251
};
252252
}
@@ -262,7 +262,7 @@ impl Definition {
262262
hir::GenericDef::Const(it) => it.source(db).map(|src| src.syntax().cloned()),
263263
};
264264
return match def {
265-
Some(def) => SearchScope::file_range(def.as_ref().original_file_range(db)),
265+
Some(def) => SearchScope::file_range(def.as_ref().original_file_range_full(db)),
266266
None => SearchScope::single_file(file_id),
267267
};
268268
}

0 commit comments

Comments
 (0)