Skip to content

fix: Fix ide_db::search not searching bodies of attributed items #12064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions crates/hir_expand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,29 @@ 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 db::AstDatabase) -> FileRange {
let mut kind = self;
let file_id = loop {
match kind.file_id().0 {
HirFileIdRepr::MacroFile(file) => {
kind = db.lookup_intern_macro_call(file.macro_call_id).kind;
}
HirFileIdRepr::FileId(file_id) => break file_id,
}
};

let range = match kind {
MacroCallKind::FnLike { ast_id, .. } => ast_id.to_node(db).syntax().text_range(),
MacroCallKind::Derive { ast_id, .. } => ast_id.to_node(db).syntax().text_range(),
MacroCallKind::Attr { ast_id, .. } => ast_id.to_node(db).syntax().text_range(),
};

FileRange { range, file_id }
}

/// 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
Expand Down Expand Up @@ -751,6 +774,9 @@ impl<'a> InFile<&'a SyntaxNode> {
}

/// Falls back to the macro call range if the node cannot be mapped up fully.
///
/// For attributes and derives, this will point back to the attribute only.
/// For the entire item `InFile::use original_file_range_full`.
pub fn original_file_range(self, db: &dyn db::AstDatabase) -> FileRange {
if let Some(res) = self.original_file_range_opt(db) {
return res;
Expand All @@ -766,6 +792,22 @@ 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_full(self, db: &dyn db::AstDatabase) -> FileRange {
if let Some(res) = self.original_file_range_opt(db) {
return res;
}

// Fall back to whole macro call.
match self.file_id.0 {
HirFileIdRepr::FileId(file_id) => FileRange { file_id, range: self.value.text_range() },
HirFileIdRepr::MacroFile(mac_file) => {
let loc = db.lookup_intern_macro_call(mac_file.macro_call_id);
loc.kind.original_call_range_with_body(db)
}
}
}

/// 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 ascend_node_border_tokens(db, self) {
Expand Down
2 changes: 1 addition & 1 deletion crates/ide_assists/src/handlers/generate_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ fn get_adt_source(
adt: &hir::Adt,
fn_name: &str,
) -> Option<(Option<ast::Impl>, FileId)> {
let range = adt.source(ctx.sema.db)?.syntax().original_file_range(ctx.sema.db);
let range = adt.source(ctx.sema.db)?.syntax().original_file_range_full(ctx.sema.db);
let file = ctx.sema.parse(range.file_id);
let adt_source =
ctx.sema.find_node_at_offset_with_macros(file.syntax(), range.range.start())?;
Expand Down
2 changes: 1 addition & 1 deletion crates/ide_assists/src/handlers/inline_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ pub(crate) fn inline_call(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
let fn_body = fn_source.value.body()?;
let param_list = fn_source.value.param_list()?;

let FileRange { file_id, range } = fn_source.syntax().original_file_range(ctx.sema.db);
let FileRange { file_id, range } = fn_source.syntax().original_file_range_full(ctx.sema.db);
if file_id == ctx.file_id() && range.contains(ctx.offset()) {
cov_mark::hit!(inline_call_recursive);
return None;
Expand Down
6 changes: 3 additions & 3 deletions crates/ide_db/src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,14 @@ impl Definition {
DefWithBody::Static(s) => s.source(db).map(|src| src.syntax().cloned()),
};
return match def {
Some(def) => SearchScope::file_range(def.as_ref().original_file_range(db)),
Some(def) => SearchScope::file_range(def.as_ref().original_file_range_full(db)),
None => SearchScope::single_file(file_id),
};
}

if let Definition::SelfType(impl_) = self {
return match impl_.source(db).map(|src| src.syntax().cloned()) {
Some(def) => SearchScope::file_range(def.as_ref().original_file_range(db)),
Some(def) => SearchScope::file_range(def.as_ref().original_file_range_full(db)),
None => SearchScope::single_file(file_id),
};
}
Expand All @@ -262,7 +262,7 @@ impl Definition {
hir::GenericDef::Const(it) => it.source(db).map(|src| src.syntax().cloned()),
};
return match def {
Some(def) => SearchScope::file_range(def.as_ref().original_file_range(db)),
Some(def) => SearchScope::file_range(def.as_ref().original_file_range_full(db)),
None => SearchScope::single_file(file_id),
};
}
Expand Down