Use correct search scopes for macros

This commit is contained in:
Lukas Wirth 2021-09-02 17:30:02 +02:00
parent 48f84a7b60
commit e2ede38d47
2 changed files with 32 additions and 12 deletions

View file

@ -9,7 +9,9 @@ use ide_db::{
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use syntax::{ use syntax::{
ast::{self, LoopBodyOwner}, ast::{self, LoopBodyOwner},
match_ast, AstNode, SyntaxNode, SyntaxToken, TextRange, TextSize, T, match_ast, AstNode,
SyntaxKind::IDENT,
SyntaxNode, SyntaxToken, TextRange, TextSize, T,
}; };
use crate::{display::TryToNav, references, NavigationTarget}; use crate::{display::TryToNav, references, NavigationTarget};
@ -46,9 +48,10 @@ pub(crate) fn highlight_related(
let syntax = sema.parse(position.file_id).syntax().clone(); let syntax = sema.parse(position.file_id).syntax().clone();
let token = pick_best_token(syntax.token_at_offset(position.offset), |kind| match kind { let token = pick_best_token(syntax.token_at_offset(position.offset), |kind| match kind {
T![?] => 3, // prefer `?` when the cursor is sandwiched like in `await$0?` T![?] => 4, // prefer `?` when the cursor is sandwiched like in `await$0?`
T![->] => 2, T![->] => 3,
kind if kind.is_keyword() => 1, kind if kind.is_keyword() => 2,
IDENT => 1,
_ => 0, _ => 0,
})?; })?;
@ -75,7 +78,7 @@ fn highlight_references(
let defs = find_defs(sema, syntax, offset); let defs = find_defs(sema, syntax, offset);
let usages = defs let usages = defs
.iter() .iter()
.flat_map(|&d| { .filter_map(|&d| {
d.usages(sema) d.usages(sema)
.set_scope(Some(SearchScope::single_file(file_id))) .set_scope(Some(SearchScope::single_file(file_id)))
.include_self_refs() .include_self_refs()

View file

@ -71,7 +71,7 @@ pub enum ReferenceAccess {
/// For `pub(crate)` things it's a crate, for `pub` things it's a crate and dependant crates. /// For `pub(crate)` things it's a crate, for `pub` things it's a crate and dependant crates.
/// In some cases, the location of the references is known to within a `TextRange`, /// In some cases, the location of the references is known to within a `TextRange`,
/// e.g. for things like local variables. /// e.g. for things like local variables.
#[derive(Clone)] #[derive(Clone, Debug)]
pub struct SearchScope { pub struct SearchScope {
entries: FxHashMap<FileId, Option<TextRange>>, entries: FxHashMap<FileId, Option<TextRange>>,
} }
@ -216,6 +216,14 @@ impl Definition {
return SearchScope::crate_graph(db); return SearchScope::crate_graph(db);
} }
// def is crate root
// FIXME: We don't do searches for crates currently, as a crate does not actually have a single name
if let &Definition::ModuleDef(hir::ModuleDef::Module(module)) = self {
if module.crate_root(db) == module {
return SearchScope::reverse_dependencies(db, module.krate());
}
}
let module = match self.module(db) { let module = match self.module(db) {
Some(it) => it, Some(it) => it,
None => return SearchScope::empty(), None => return SearchScope::empty(),
@ -273,14 +281,23 @@ impl Definition {
} }
if let Definition::Macro(macro_def) = self { if let Definition::Macro(macro_def) = self {
if macro_def.kind() == hir::MacroKind::Declarative { return match macro_def.kind() {
return if macro_def.attrs(db).by_key("macro_export").exists() { hir::MacroKind::Declarative => {
if macro_def.attrs(db).by_key("macro_export").exists() {
SearchScope::reverse_dependencies(db, module.krate()) SearchScope::reverse_dependencies(db, module.krate())
} else { } else {
SearchScope::krate(db, module.krate()) SearchScope::krate(db, module.krate())
};
} }
} }
hir::MacroKind::BuiltIn => SearchScope::crate_graph(db),
// FIXME: We don't actually see derives in derive attributes as these do not
// expand to something that references the derive macro in the output.
// We could get around this by emitting dummy `use DeriveMacroPathHere as _;` items maybe?
hir::MacroKind::Derive | hir::MacroKind::Attr | hir::MacroKind::ProcMacro => {
SearchScope::reverse_dependencies(db, module.krate())
}
};
}
let vis = self.visibility(db); let vis = self.visibility(db);
if let Some(Visibility::Public) = vis { if let Some(Visibility::Public) = vis {