Skip to content

Commit 2aee17e

Browse files
bors[bot]Veykril
andauthored
Merge #10124
10124: fix: Use correct search scopes for macros r=Veykril a=Veykril bors r+ Co-authored-by: Lukas Wirth <[email protected]>
2 parents 48f84a7 + e2ede38 commit 2aee17e

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

crates/ide/src/highlight_related.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ use ide_db::{
99
use rustc_hash::FxHashSet;
1010
use syntax::{
1111
ast::{self, LoopBodyOwner},
12-
match_ast, AstNode, SyntaxNode, SyntaxToken, TextRange, TextSize, T,
12+
match_ast, AstNode,
13+
SyntaxKind::IDENT,
14+
SyntaxNode, SyntaxToken, TextRange, TextSize, T,
1315
};
1416

1517
use crate::{display::TryToNav, references, NavigationTarget};
@@ -46,9 +48,10 @@ pub(crate) fn highlight_related(
4648
let syntax = sema.parse(position.file_id).syntax().clone();
4749

4850
let token = pick_best_token(syntax.token_at_offset(position.offset), |kind| match kind {
49-
T![?] => 3, // prefer `?` when the cursor is sandwiched like in `await$0?`
50-
T![->] => 2,
51-
kind if kind.is_keyword() => 1,
51+
T![?] => 4, // prefer `?` when the cursor is sandwiched like in `await$0?`
52+
T![->] => 3,
53+
kind if kind.is_keyword() => 2,
54+
IDENT => 1,
5255
_ => 0,
5356
})?;
5457

@@ -75,7 +78,7 @@ fn highlight_references(
7578
let defs = find_defs(sema, syntax, offset);
7679
let usages = defs
7780
.iter()
78-
.flat_map(|&d| {
81+
.filter_map(|&d| {
7982
d.usages(sema)
8083
.set_scope(Some(SearchScope::single_file(file_id)))
8184
.include_self_refs()

crates/ide_db/src/search.rs

+24-7
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub enum ReferenceAccess {
7171
/// For `pub(crate)` things it's a crate, for `pub` things it's a crate and dependant crates.
7272
/// In some cases, the location of the references is known to within a `TextRange`,
7373
/// e.g. for things like local variables.
74-
#[derive(Clone)]
74+
#[derive(Clone, Debug)]
7575
pub struct SearchScope {
7676
entries: FxHashMap<FileId, Option<TextRange>>,
7777
}
@@ -216,6 +216,14 @@ impl Definition {
216216
return SearchScope::crate_graph(db);
217217
}
218218

219+
// def is crate root
220+
// FIXME: We don't do searches for crates currently, as a crate does not actually have a single name
221+
if let &Definition::ModuleDef(hir::ModuleDef::Module(module)) = self {
222+
if module.crate_root(db) == module {
223+
return SearchScope::reverse_dependencies(db, module.krate());
224+
}
225+
}
226+
219227
let module = match self.module(db) {
220228
Some(it) => it,
221229
None => return SearchScope::empty(),
@@ -273,13 +281,22 @@ impl Definition {
273281
}
274282

275283
if let Definition::Macro(macro_def) = self {
276-
if macro_def.kind() == hir::MacroKind::Declarative {
277-
return if macro_def.attrs(db).by_key("macro_export").exists() {
284+
return match macro_def.kind() {
285+
hir::MacroKind::Declarative => {
286+
if macro_def.attrs(db).by_key("macro_export").exists() {
287+
SearchScope::reverse_dependencies(db, module.krate())
288+
} else {
289+
SearchScope::krate(db, module.krate())
290+
}
291+
}
292+
hir::MacroKind::BuiltIn => SearchScope::crate_graph(db),
293+
// FIXME: We don't actually see derives in derive attributes as these do not
294+
// expand to something that references the derive macro in the output.
295+
// We could get around this by emitting dummy `use DeriveMacroPathHere as _;` items maybe?
296+
hir::MacroKind::Derive | hir::MacroKind::Attr | hir::MacroKind::ProcMacro => {
278297
SearchScope::reverse_dependencies(db, module.krate())
279-
} else {
280-
SearchScope::krate(db, module.krate())
281-
};
282-
}
298+
}
299+
};
283300
}
284301

285302
let vis = self.visibility(db);

0 commit comments

Comments
 (0)