2020-03-04 05:48:50 -06:00
|
|
|
//! Implementation of find-usages functionality.
|
|
|
|
//!
|
|
|
|
//! It is based on the standard ide trick: first, we run a fast text search to
|
|
|
|
//! get a super-set of matches. Then, we we confirm each match using precise
|
|
|
|
//! name resolution.
|
|
|
|
|
2023-05-02 09:12:22 -05:00
|
|
|
use std::mem;
|
2020-03-04 05:05:14 -06:00
|
|
|
|
2023-09-13 15:01:04 -05:00
|
|
|
use base_db::{salsa::Database, FileId, FileRange, SourceDatabase, SourceDatabaseExt};
|
2023-01-11 10:10:04 -06:00
|
|
|
use hir::{
|
2023-12-03 13:20:38 -06:00
|
|
|
AsAssocItem, DefWithBody, DescendPreference, HasAttrs, HasSource, HirFileIdExt, InFile,
|
2023-12-05 08:42:39 -06:00
|
|
|
InRealFile, ModuleSource, PathResolution, Semantics, Visibility,
|
2023-01-11 10:10:04 -06:00
|
|
|
};
|
2022-09-16 09:26:54 -05:00
|
|
|
use memchr::memmem::Finder;
|
2023-05-04 18:28:15 -05:00
|
|
|
use nohash_hasher::IntMap;
|
2020-03-04 05:14:48 -06:00
|
|
|
use once_cell::unsync::Lazy;
|
2022-09-13 07:47:26 -05:00
|
|
|
use parser::SyntaxKind;
|
2023-12-05 08:42:39 -06:00
|
|
|
use syntax::{ast, match_ast, AstNode, AstToken, SyntaxElement, TextRange, TextSize};
|
2023-05-02 09:12:22 -05:00
|
|
|
use triomphe::Arc;
|
2020-03-04 05:05:14 -06:00
|
|
|
|
2020-03-04 05:14:48 -06:00
|
|
|
use crate::{
|
2021-03-27 15:51:00 -05:00
|
|
|
defs::{Definition, NameClass, NameRefClass},
|
2022-07-20 06:59:31 -05:00
|
|
|
traits::{as_trait_assoc_def, convert_to_def_in_trait},
|
2020-03-04 05:14:48 -06:00
|
|
|
RootDatabase,
|
|
|
|
};
|
2020-03-04 05:05:14 -06:00
|
|
|
|
2021-01-12 08:51:02 -06:00
|
|
|
#[derive(Debug, Default, Clone)]
|
2021-01-12 08:56:24 -06:00
|
|
|
pub struct UsageSearchResult {
|
2023-05-04 18:28:15 -05:00
|
|
|
pub references: IntMap<FileId, Vec<FileReference>>,
|
2021-01-11 17:05:07 -06:00
|
|
|
}
|
|
|
|
|
2021-01-12 08:56:24 -06:00
|
|
|
impl UsageSearchResult {
|
2021-01-12 08:51:02 -06:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.references.is_empty()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.references.len()
|
|
|
|
}
|
|
|
|
|
2021-07-31 07:29:15 -05:00
|
|
|
pub fn iter(&self) -> impl Iterator<Item = (&FileId, &[FileReference])> + '_ {
|
|
|
|
self.references.iter().map(|(file_id, refs)| (file_id, &**refs))
|
2021-01-12 08:51:02 -06:00
|
|
|
}
|
|
|
|
|
2021-01-11 17:05:07 -06:00
|
|
|
pub fn file_ranges(&self) -> impl Iterator<Item = FileRange> + '_ {
|
2021-01-12 08:51:02 -06:00
|
|
|
self.references.iter().flat_map(|(&file_id, refs)| {
|
|
|
|
refs.iter().map(move |&FileReference { range, .. }| FileRange { file_id, range })
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-12 08:56:24 -06:00
|
|
|
impl IntoIterator for UsageSearchResult {
|
2021-01-12 08:51:02 -06:00
|
|
|
type Item = (FileId, Vec<FileReference>);
|
2023-05-04 18:28:15 -05:00
|
|
|
type IntoIter = <IntMap<FileId, Vec<FileReference>> as IntoIterator>::IntoIter;
|
2021-01-12 08:51:02 -06:00
|
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
self.references.into_iter()
|
2021-01-11 17:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct FileReference {
|
2022-07-24 05:04:15 -05:00
|
|
|
/// The range of the reference in the original file
|
2021-01-11 17:05:07 -06:00
|
|
|
pub range: TextRange,
|
2022-07-24 05:04:15 -05:00
|
|
|
/// The node of the reference in the (macro-)file
|
2023-12-05 08:42:39 -06:00
|
|
|
pub name: FileReferenceNode,
|
2021-10-02 04:18:18 -05:00
|
|
|
pub category: Option<ReferenceCategory>,
|
2020-03-04 05:06:37 -06:00
|
|
|
}
|
|
|
|
|
2023-12-05 08:42:39 -06:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum FileReferenceNode {
|
|
|
|
Name(ast::Name),
|
|
|
|
NameRef(ast::NameRef),
|
|
|
|
Lifetime(ast::Lifetime),
|
|
|
|
FormatStringEntry(ast::String, TextRange),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FileReferenceNode {
|
|
|
|
pub fn text_range(&self) -> TextRange {
|
|
|
|
match self {
|
|
|
|
FileReferenceNode::Name(it) => it.syntax().text_range(),
|
|
|
|
FileReferenceNode::NameRef(it) => it.syntax().text_range(),
|
|
|
|
FileReferenceNode::Lifetime(it) => it.syntax().text_range(),
|
|
|
|
FileReferenceNode::FormatStringEntry(_, range) => *range,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn syntax(&self) -> SyntaxElement {
|
|
|
|
match self {
|
|
|
|
FileReferenceNode::Name(it) => it.syntax().clone().into(),
|
|
|
|
FileReferenceNode::NameRef(it) => it.syntax().clone().into(),
|
|
|
|
FileReferenceNode::Lifetime(it) => it.syntax().clone().into(),
|
|
|
|
FileReferenceNode::FormatStringEntry(it, _) => it.syntax().clone().into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn into_name_like(self) -> Option<ast::NameLike> {
|
|
|
|
match self {
|
|
|
|
FileReferenceNode::Name(it) => Some(ast::NameLike::Name(it)),
|
|
|
|
FileReferenceNode::NameRef(it) => Some(ast::NameLike::NameRef(it)),
|
|
|
|
FileReferenceNode::Lifetime(it) => Some(ast::NameLike::Lifetime(it)),
|
|
|
|
FileReferenceNode::FormatStringEntry(_, _) => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn as_name_ref(&self) -> Option<&ast::NameRef> {
|
|
|
|
match self {
|
|
|
|
FileReferenceNode::NameRef(name_ref) => Some(name_ref),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn as_lifetime(&self) -> Option<&ast::Lifetime> {
|
|
|
|
match self {
|
|
|
|
FileReferenceNode::Lifetime(lifetime) => Some(lifetime),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn text(&self) -> syntax::TokenText<'_> {
|
|
|
|
match self {
|
|
|
|
FileReferenceNode::NameRef(name_ref) => name_ref.text(),
|
|
|
|
FileReferenceNode::Name(name) => name.text(),
|
|
|
|
FileReferenceNode::Lifetime(lifetime) => lifetime.text(),
|
|
|
|
FileReferenceNode::FormatStringEntry(it, range) => {
|
|
|
|
syntax::TokenText::borrowed(&it.text()[*range - it.syntax().text_range().start()])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-28 17:45:55 -05:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
2021-10-02 04:18:18 -05:00
|
|
|
pub enum ReferenceCategory {
|
|
|
|
// FIXME: Add this variant and delete the `retain_adt_literal_usages` function.
|
|
|
|
// Create
|
2020-03-04 05:06:37 -06:00
|
|
|
Write,
|
2021-10-02 04:18:18 -05:00
|
|
|
Read,
|
2022-09-13 07:47:26 -05:00
|
|
|
Import,
|
2021-10-02 04:18:18 -05:00
|
|
|
// FIXME: Some day should be able to search in doc comments. Would probably
|
|
|
|
// need to switch from enum to bitflags then?
|
|
|
|
// DocComment
|
2024-01-28 04:28:13 -06:00
|
|
|
Test,
|
2020-03-04 05:06:37 -06:00
|
|
|
}
|
|
|
|
|
2020-03-04 05:48:50 -06:00
|
|
|
/// Generally, `search_scope` returns files that might contain references for the element.
|
|
|
|
/// 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`,
|
|
|
|
/// e.g. for things like local variables.
|
2021-09-02 10:30:02 -05:00
|
|
|
#[derive(Clone, Debug)]
|
2020-03-04 05:05:14 -06:00
|
|
|
pub struct SearchScope {
|
2023-05-04 18:28:15 -05:00
|
|
|
entries: IntMap<FileId, Option<TextRange>>,
|
2020-03-04 05:05:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SearchScope {
|
2023-05-04 18:28:15 -05:00
|
|
|
fn new(entries: IntMap<FileId, Option<TextRange>>) -> SearchScope {
|
2020-03-04 05:05:14 -06:00
|
|
|
SearchScope { entries }
|
|
|
|
}
|
|
|
|
|
2022-04-06 06:58:40 -05:00
|
|
|
/// Build a search scope spanning the entire crate graph of files.
|
2021-03-22 11:11:33 -05:00
|
|
|
fn crate_graph(db: &RootDatabase) -> SearchScope {
|
2023-05-04 18:28:15 -05:00
|
|
|
let mut entries = IntMap::default();
|
2021-03-22 11:11:33 -05:00
|
|
|
|
|
|
|
let graph = db.crate_graph();
|
|
|
|
for krate in graph.iter() {
|
|
|
|
let root_file = graph[krate].root_file_id;
|
|
|
|
let source_root_id = db.file_source_root(root_file);
|
|
|
|
let source_root = db.source_root(source_root_id);
|
|
|
|
entries.extend(source_root.iter().map(|id| (id, None)));
|
|
|
|
}
|
|
|
|
SearchScope { entries }
|
|
|
|
}
|
|
|
|
|
2022-04-06 06:58:40 -05:00
|
|
|
/// Build a search scope spanning all the reverse dependencies of the given crate.
|
2021-03-22 11:11:33 -05:00
|
|
|
fn reverse_dependencies(db: &RootDatabase, of: hir::Crate) -> SearchScope {
|
2023-05-04 18:28:15 -05:00
|
|
|
let mut entries = IntMap::default();
|
2022-04-09 06:40:48 -05:00
|
|
|
for rev_dep in of.transitive_reverse_dependencies(db) {
|
|
|
|
let root_file = rev_dep.root_file(db);
|
|
|
|
let source_root_id = db.file_source_root(root_file);
|
|
|
|
let source_root = db.source_root(source_root_id);
|
|
|
|
entries.extend(source_root.iter().map(|id| (id, None)));
|
|
|
|
}
|
2021-03-22 11:11:33 -05:00
|
|
|
SearchScope { entries }
|
|
|
|
}
|
|
|
|
|
2022-04-06 06:58:40 -05:00
|
|
|
/// Build a search scope spanning the given crate.
|
2021-03-22 11:11:33 -05:00
|
|
|
fn krate(db: &RootDatabase, of: hir::Crate) -> SearchScope {
|
|
|
|
let root_file = of.root_file(db);
|
|
|
|
let source_root_id = db.file_source_root(root_file);
|
|
|
|
let source_root = db.source_root(source_root_id);
|
2022-08-25 13:31:02 -05:00
|
|
|
SearchScope { entries: source_root.iter().map(|id| (id, None)).collect() }
|
2021-03-22 11:11:33 -05:00
|
|
|
}
|
|
|
|
|
2022-04-06 06:58:40 -05:00
|
|
|
/// Build a search scope spanning the given module and all its submodules.
|
2023-07-09 16:20:18 -05:00
|
|
|
pub fn module_and_children(db: &RootDatabase, module: hir::Module) -> SearchScope {
|
2023-05-04 18:28:15 -05:00
|
|
|
let mut entries = IntMap::default();
|
2021-03-22 11:11:33 -05:00
|
|
|
|
2022-04-06 06:58:40 -05:00
|
|
|
let (file_id, range) = {
|
|
|
|
let InFile { file_id, value } = module.definition_source(db);
|
2023-12-02 12:32:53 -06:00
|
|
|
if let Some(InRealFile { file_id, value: call_source }) = file_id.original_call_node(db)
|
|
|
|
{
|
2022-04-06 06:58:40 -05:00
|
|
|
(file_id, Some(call_source.text_range()))
|
|
|
|
} else {
|
|
|
|
(
|
|
|
|
file_id.original_file(db),
|
|
|
|
match value {
|
|
|
|
ModuleSource::SourceFile(_) => None,
|
|
|
|
ModuleSource::Module(it) => Some(it.syntax().text_range()),
|
|
|
|
ModuleSource::BlockExpr(it) => Some(it.syntax().text_range()),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
entries.insert(file_id, range);
|
|
|
|
|
|
|
|
let mut to_visit: Vec<_> = module.children(db).collect();
|
2021-03-22 11:11:33 -05:00
|
|
|
while let Some(module) = to_visit.pop() {
|
2023-06-17 03:58:52 -05:00
|
|
|
if let Some(file_id) = module.as_source_file_id(db) {
|
|
|
|
entries.insert(file_id, None);
|
2022-04-06 06:58:40 -05:00
|
|
|
}
|
2021-03-22 11:11:33 -05:00
|
|
|
to_visit.extend(module.children(db));
|
|
|
|
}
|
|
|
|
SearchScope { entries }
|
|
|
|
}
|
|
|
|
|
2022-04-06 06:58:40 -05:00
|
|
|
/// Build an empty search scope.
|
2020-03-04 05:05:14 -06:00
|
|
|
pub fn empty() -> SearchScope {
|
2023-05-04 18:28:15 -05:00
|
|
|
SearchScope::new(IntMap::default())
|
2020-03-04 05:05:14 -06:00
|
|
|
}
|
|
|
|
|
2022-04-06 06:58:40 -05:00
|
|
|
/// Build a empty search scope spanning the given file.
|
2020-03-04 05:05:14 -06:00
|
|
|
pub fn single_file(file: FileId) -> SearchScope {
|
|
|
|
SearchScope::new(std::iter::once((file, None)).collect())
|
|
|
|
}
|
|
|
|
|
2022-04-06 06:58:40 -05:00
|
|
|
/// Build a empty search scope spanning the text range of the given file.
|
2021-03-11 08:39:41 -06:00
|
|
|
pub fn file_range(range: FileRange) -> SearchScope {
|
|
|
|
SearchScope::new(std::iter::once((range.file_id, Some(range.range))).collect())
|
2021-02-27 08:59:52 -06:00
|
|
|
}
|
|
|
|
|
2022-04-06 06:58:40 -05:00
|
|
|
/// Build a empty search scope spanning the given files.
|
2020-07-21 23:01:21 -05:00
|
|
|
pub fn files(files: &[FileId]) -> SearchScope {
|
|
|
|
SearchScope::new(files.iter().map(|f| (*f, None)).collect())
|
|
|
|
}
|
|
|
|
|
2020-03-04 05:46:40 -06:00
|
|
|
pub fn intersection(&self, other: &SearchScope) -> SearchScope {
|
|
|
|
let (mut small, mut large) = (&self.entries, &other.entries);
|
|
|
|
if small.len() > large.len() {
|
|
|
|
mem::swap(&mut small, &mut large)
|
|
|
|
}
|
|
|
|
|
2022-04-06 06:58:40 -05:00
|
|
|
let intersect_ranges =
|
|
|
|
|r1: Option<TextRange>, r2: Option<TextRange>| -> Option<Option<TextRange>> {
|
|
|
|
match (r1, r2) {
|
|
|
|
(None, r) | (r, None) => Some(r),
|
|
|
|
(Some(r1), Some(r2)) => r1.intersect(r2).map(Some),
|
|
|
|
}
|
|
|
|
};
|
2020-03-04 05:46:40 -06:00
|
|
|
let res = small
|
|
|
|
.iter()
|
2022-04-06 06:58:40 -05:00
|
|
|
.filter_map(|(&file_id, &r1)| {
|
|
|
|
let &r2 = large.get(&file_id)?;
|
|
|
|
let r = intersect_ranges(r1, r2)?;
|
|
|
|
Some((file_id, r))
|
2020-03-04 05:46:40 -06:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2022-04-06 06:58:40 -05:00
|
|
|
SearchScope::new(res)
|
2020-03-04 05:46:40 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoIterator for SearchScope {
|
|
|
|
type Item = (FileId, Option<TextRange>);
|
|
|
|
type IntoIter = std::collections::hash_map::IntoIter<FileId, Option<TextRange>>;
|
|
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
self.entries.into_iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Definition {
|
|
|
|
fn search_scope(&self, db: &RootDatabase) -> SearchScope {
|
2024-01-17 20:27:38 -06:00
|
|
|
let _p = tracing::span!(tracing::Level::INFO, "search_scope").entered();
|
2021-03-15 03:32:06 -05:00
|
|
|
|
2021-11-10 15:02:50 -06:00
|
|
|
if let Definition::BuiltinType(_) = self {
|
2021-03-22 11:11:33 -05:00
|
|
|
return SearchScope::crate_graph(db);
|
2021-03-15 03:32:06 -05:00
|
|
|
}
|
|
|
|
|
2021-09-02 10:30:02 -05:00
|
|
|
// def is crate root
|
2021-11-10 15:02:50 -06:00
|
|
|
if let &Definition::Module(module) = self {
|
2023-06-01 07:46:36 -05:00
|
|
|
if module.is_crate_root() {
|
2021-09-02 10:30:02 -05:00
|
|
|
return SearchScope::reverse_dependencies(db, module.krate());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 05:46:40 -06:00
|
|
|
let module = match self.module(db) {
|
2020-03-04 05:05:14 -06:00
|
|
|
Some(it) => it,
|
|
|
|
None => return SearchScope::empty(),
|
|
|
|
};
|
2021-03-22 11:11:33 -05:00
|
|
|
let InFile { file_id, value: module_source } = module.definition_source(db);
|
|
|
|
let file_id = file_id.original_file(db);
|
2020-03-04 05:05:14 -06:00
|
|
|
|
2020-03-04 05:46:40 -06:00
|
|
|
if let Definition::Local(var) = self {
|
2021-09-13 19:49:06 -05:00
|
|
|
let def = match var.parent(db) {
|
|
|
|
DefWithBody::Function(f) => f.source(db).map(|src| src.syntax().cloned()),
|
|
|
|
DefWithBody::Const(c) => c.source(db).map(|src| src.syntax().cloned()),
|
|
|
|
DefWithBody::Static(s) => s.source(db).map(|src| src.syntax().cloned()),
|
2022-08-06 11:50:21 -05:00
|
|
|
DefWithBody::Variant(v) => v.source(db).map(|src| src.syntax().cloned()),
|
2023-06-05 06:27:19 -05:00
|
|
|
// FIXME: implement
|
|
|
|
DefWithBody::InTypeConst(_) => return SearchScope::empty(),
|
2020-03-04 05:05:14 -06:00
|
|
|
};
|
2021-09-13 19:49:06 -05:00
|
|
|
return match def {
|
2024-01-31 02:57:17 -06:00
|
|
|
Some(def) => SearchScope::file_range(
|
|
|
|
def.as_ref().original_file_range_with_macro_call_body(db),
|
|
|
|
),
|
2021-03-22 11:11:33 -05:00
|
|
|
None => SearchScope::single_file(file_id),
|
|
|
|
};
|
2020-03-04 05:05:14 -06:00
|
|
|
}
|
|
|
|
|
2021-05-08 15:34:55 -05:00
|
|
|
if let Definition::SelfType(impl_) = self {
|
2021-09-13 19:49:06 -05:00
|
|
|
return match impl_.source(db).map(|src| src.syntax().cloned()) {
|
2024-01-31 02:57:17 -06:00
|
|
|
Some(def) => SearchScope::file_range(
|
|
|
|
def.as_ref().original_file_range_with_macro_call_body(db),
|
|
|
|
),
|
2021-05-08 15:34:55 -05:00
|
|
|
None => SearchScope::single_file(file_id),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-01-08 05:28:02 -06:00
|
|
|
if let Definition::GenericParam(hir::GenericParam::LifetimeParam(param)) = self {
|
2021-09-13 19:49:06 -05:00
|
|
|
let def = match param.parent(db) {
|
|
|
|
hir::GenericDef::Function(it) => it.source(db).map(|src| src.syntax().cloned()),
|
|
|
|
hir::GenericDef::Adt(it) => it.source(db).map(|src| src.syntax().cloned()),
|
|
|
|
hir::GenericDef::Trait(it) => it.source(db).map(|src| src.syntax().cloned()),
|
2023-03-03 09:24:07 -06:00
|
|
|
hir::GenericDef::TraitAlias(it) => it.source(db).map(|src| src.syntax().cloned()),
|
2021-09-13 19:49:06 -05:00
|
|
|
hir::GenericDef::TypeAlias(it) => it.source(db).map(|src| src.syntax().cloned()),
|
|
|
|
hir::GenericDef::Impl(it) => it.source(db).map(|src| src.syntax().cloned()),
|
|
|
|
hir::GenericDef::Variant(it) => it.source(db).map(|src| src.syntax().cloned()),
|
|
|
|
hir::GenericDef::Const(it) => it.source(db).map(|src| src.syntax().cloned()),
|
2020-12-16 14:35:15 -06:00
|
|
|
};
|
2021-09-13 19:49:06 -05:00
|
|
|
return match def {
|
2024-01-31 02:57:17 -06:00
|
|
|
Some(def) => SearchScope::file_range(
|
|
|
|
def.as_ref().original_file_range_with_macro_call_body(db),
|
|
|
|
),
|
2021-03-22 11:11:33 -05:00
|
|
|
None => SearchScope::single_file(file_id),
|
|
|
|
};
|
2020-03-04 05:05:14 -06:00
|
|
|
}
|
|
|
|
|
2021-03-21 14:08:08 -05:00
|
|
|
if let Definition::Macro(macro_def) = self {
|
2022-03-08 16:52:26 -06:00
|
|
|
return match macro_def.kind(db) {
|
2021-09-02 10:30:02 -05:00
|
|
|
hir::MacroKind::Declarative => {
|
|
|
|
if macro_def.attrs(db).by_key("macro_export").exists() {
|
|
|
|
SearchScope::reverse_dependencies(db, module.krate())
|
|
|
|
} else {
|
|
|
|
SearchScope::krate(db, module.krate())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hir::MacroKind::BuiltIn => SearchScope::crate_graph(db),
|
|
|
|
hir::MacroKind::Derive | hir::MacroKind::Attr | hir::MacroKind::ProcMacro => {
|
2021-03-22 11:11:33 -05:00
|
|
|
SearchScope::reverse_dependencies(db, module.krate())
|
2021-09-02 10:30:02 -05:00
|
|
|
}
|
|
|
|
};
|
2021-03-21 14:08:08 -05:00
|
|
|
}
|
|
|
|
|
2022-07-24 07:32:39 -05:00
|
|
|
if let Definition::DeriveHelper(_) = self {
|
|
|
|
return SearchScope::reverse_dependencies(db, module.krate());
|
|
|
|
}
|
|
|
|
|
2021-03-22 11:11:33 -05:00
|
|
|
let vis = self.visibility(db);
|
2021-03-21 14:08:08 -05:00
|
|
|
if let Some(Visibility::Public) = vis {
|
2021-03-22 11:11:33 -05:00
|
|
|
return SearchScope::reverse_dependencies(db, module.krate());
|
|
|
|
}
|
2024-01-05 04:00:29 -06:00
|
|
|
if let Some(Visibility::Module(module, _)) = vis {
|
2022-04-06 06:58:40 -05:00
|
|
|
return SearchScope::module_and_children(db, module.into());
|
2020-03-04 05:05:14 -06:00
|
|
|
}
|
|
|
|
|
2021-03-22 11:11:33 -05:00
|
|
|
let range = match module_source {
|
2020-03-04 05:05:14 -06:00
|
|
|
ModuleSource::Module(m) => Some(m.syntax().text_range()),
|
2021-01-20 13:05:48 -06:00
|
|
|
ModuleSource::BlockExpr(b) => Some(b.syntax().text_range()),
|
2020-03-04 05:05:14 -06:00
|
|
|
ModuleSource::SourceFile(_) => None,
|
|
|
|
};
|
2021-03-22 11:11:33 -05:00
|
|
|
match range {
|
|
|
|
Some(range) => SearchScope::file_range(FileRange { file_id, range }),
|
|
|
|
None => SearchScope::single_file(file_id),
|
|
|
|
}
|
2020-03-04 05:05:14 -06:00
|
|
|
}
|
|
|
|
|
2022-07-20 08:02:08 -05:00
|
|
|
pub fn usages<'a>(self, sema: &'a Semantics<'_, RootDatabase>) -> FindUsages<'a> {
|
2021-06-28 09:41:35 -05:00
|
|
|
FindUsages {
|
|
|
|
def: self,
|
2023-01-11 10:10:04 -06:00
|
|
|
assoc_item_container: self.as_assoc_item(sema.db).map(|a| a.container(sema.db)),
|
2021-06-28 09:41:35 -05:00
|
|
|
sema,
|
|
|
|
scope: None,
|
|
|
|
include_self_kw_refs: None,
|
|
|
|
search_self_mod: false,
|
|
|
|
}
|
2020-08-19 11:58:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-26 07:38:58 -05:00
|
|
|
#[derive(Clone)]
|
2020-08-19 11:58:48 -05:00
|
|
|
pub struct FindUsages<'a> {
|
2021-06-11 12:23:59 -05:00
|
|
|
def: Definition,
|
2020-08-19 11:58:48 -05:00
|
|
|
sema: &'a Semantics<'a, RootDatabase>,
|
2023-07-09 16:20:18 -05:00
|
|
|
scope: Option<&'a SearchScope>,
|
2023-01-24 07:11:02 -06:00
|
|
|
/// The container of our definition should it be an assoc item
|
|
|
|
assoc_item_container: Option<hir::AssocItemContainer>,
|
|
|
|
/// whether to search for the `Self` type of the definition
|
2021-05-08 15:34:55 -05:00
|
|
|
include_self_kw_refs: Option<hir::Type>,
|
2023-01-24 07:11:02 -06:00
|
|
|
/// whether to search for the `self` module
|
2021-06-28 09:41:35 -05:00
|
|
|
search_self_mod: bool,
|
2020-08-19 11:58:48 -05:00
|
|
|
}
|
|
|
|
|
2023-07-09 16:20:18 -05:00
|
|
|
impl<'a> FindUsages<'a> {
|
2021-06-28 09:41:35 -05:00
|
|
|
/// Enable searching for `Self` when the definition is a type or `self` for modules.
|
2023-06-29 09:27:28 -05:00
|
|
|
pub fn include_self_refs(mut self) -> Self {
|
2021-06-11 12:23:59 -05:00
|
|
|
self.include_self_kw_refs = def_to_ty(self.sema, &self.def);
|
2021-06-28 09:41:35 -05:00
|
|
|
self.search_self_mod = true;
|
2021-04-03 16:01:49 -05:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-04-06 06:58:40 -05:00
|
|
|
/// Limit the search to a given [`SearchScope`].
|
2023-07-09 16:20:18 -05:00
|
|
|
pub fn in_scope(self, scope: &'a SearchScope) -> Self {
|
2020-08-19 11:58:48 -05:00
|
|
|
self.set_scope(Some(scope))
|
|
|
|
}
|
2021-02-16 12:27:08 -06:00
|
|
|
|
2022-04-06 06:58:40 -05:00
|
|
|
/// Limit the search to a given [`SearchScope`].
|
2023-07-09 16:20:18 -05:00
|
|
|
pub fn set_scope(mut self, scope: Option<&'a SearchScope>) -> Self {
|
2020-08-19 11:58:48 -05:00
|
|
|
assert!(self.scope.is_none());
|
|
|
|
self.scope = scope;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-09-26 07:38:58 -05:00
|
|
|
pub fn at_least_one(&self) -> bool {
|
2020-08-19 17:54:44 -05:00
|
|
|
let mut found = false;
|
2021-01-11 17:05:07 -06:00
|
|
|
self.search(&mut |_, _| {
|
2020-08-19 17:54:44 -05:00
|
|
|
found = true;
|
|
|
|
true
|
|
|
|
});
|
|
|
|
found
|
2020-08-19 11:58:48 -05:00
|
|
|
}
|
|
|
|
|
2021-01-12 08:56:24 -06:00
|
|
|
pub fn all(self) -> UsageSearchResult {
|
|
|
|
let mut res = UsageSearchResult::default();
|
2021-01-11 17:05:07 -06:00
|
|
|
self.search(&mut |file_id, reference| {
|
2021-01-12 08:51:02 -06:00
|
|
|
res.references.entry(file_id).or_default().push(reference);
|
2020-08-19 17:54:44 -05:00
|
|
|
false
|
|
|
|
});
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2023-07-09 16:20:18 -05:00
|
|
|
pub fn search(&self, sink: &mut dyn FnMut(FileId, FileReference) -> bool) {
|
2024-01-17 20:27:38 -06:00
|
|
|
let _p = tracing::span!(tracing::Level::INFO, "FindUsages:search").entered();
|
2020-08-19 11:58:48 -05:00
|
|
|
let sema = self.sema;
|
2020-03-04 05:17:41 -06:00
|
|
|
|
|
|
|
let search_scope = {
|
2023-01-11 10:10:04 -06:00
|
|
|
// FIXME: Is the trait scope needed for trait impl assoc items?
|
|
|
|
let base =
|
|
|
|
as_trait_assoc_def(sema.db, self.def).unwrap_or(self.def).search_scope(sema.db);
|
2020-10-09 12:55:30 -05:00
|
|
|
match &self.scope {
|
2020-03-04 05:17:41 -06:00
|
|
|
None => base,
|
2020-10-09 12:55:30 -05:00
|
|
|
Some(scope) => base.intersection(scope),
|
2020-03-04 05:17:41 -06:00
|
|
|
}
|
|
|
|
};
|
2020-03-04 05:14:48 -06:00
|
|
|
|
2021-12-20 10:48:47 -06:00
|
|
|
let name = match self.def {
|
|
|
|
// special case crate modules as these do not have a proper name
|
2023-06-01 07:46:36 -05:00
|
|
|
Definition::Module(module) if module.is_crate_root() => {
|
2023-08-29 03:08:34 -05:00
|
|
|
// FIXME: This assumes the crate name is always equal to its display name when it
|
|
|
|
// really isn't
|
|
|
|
// we should instead look at the dependency edge name and recursively search our way
|
|
|
|
// up the ancestors
|
2021-12-20 10:48:47 -06:00
|
|
|
module
|
|
|
|
.krate()
|
|
|
|
.display_name(self.sema.db)
|
|
|
|
.map(|crate_name| crate_name.crate_name().as_smol_str().clone())
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
let self_kw_refs = || {
|
|
|
|
self.include_self_kw_refs.as_ref().and_then(|ty| {
|
|
|
|
ty.as_adt()
|
|
|
|
.map(|adt| adt.name(self.sema.db))
|
|
|
|
.or_else(|| ty.as_builtin().map(|builtin| builtin.name()))
|
|
|
|
})
|
|
|
|
};
|
2022-08-15 20:08:45 -05:00
|
|
|
// We need to unescape the name in case it is written without "r#" in earlier
|
|
|
|
// editions of Rust where it isn't a keyword.
|
|
|
|
self.def.name(sema.db).or_else(self_kw_refs).map(|it| it.unescaped().to_smol_str())
|
2021-12-20 10:48:47 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
let name = match &name {
|
|
|
|
Some(s) => s.as_str(),
|
2020-08-19 17:54:44 -05:00
|
|
|
None => return,
|
2020-03-04 05:17:41 -06:00
|
|
|
};
|
2022-09-16 09:26:54 -05:00
|
|
|
let finder = &Finder::new(name);
|
|
|
|
let include_self_kw_refs =
|
|
|
|
self.include_self_kw_refs.as_ref().map(|ty| (ty, Finder::new("Self")));
|
2020-03-04 05:14:48 -06:00
|
|
|
|
2022-09-16 09:26:54 -05:00
|
|
|
// for<'a> |text: &'a str, name: &'a str, search_range: TextRange| -> impl Iterator<Item = TextSize> + 'a { ... }
|
2021-12-20 10:48:47 -06:00
|
|
|
fn match_indices<'a>(
|
|
|
|
text: &'a str,
|
2022-09-16 09:26:54 -05:00
|
|
|
finder: &'a Finder<'a>,
|
2021-12-20 10:48:47 -06:00
|
|
|
search_range: TextRange,
|
|
|
|
) -> impl Iterator<Item = TextSize> + 'a {
|
2022-09-16 09:26:54 -05:00
|
|
|
finder.find_iter(text.as_bytes()).filter_map(move |idx| {
|
2020-04-24 17:57:47 -05:00
|
|
|
let offset: TextSize = idx.try_into().unwrap();
|
2020-03-04 05:17:41 -06:00
|
|
|
if !search_range.contains_inclusive(offset) {
|
2021-12-20 10:48:47 -06:00
|
|
|
return None;
|
2020-03-04 05:17:41 -06:00
|
|
|
}
|
2021-12-20 10:48:47 -06:00
|
|
|
Some(offset)
|
|
|
|
})
|
|
|
|
}
|
2022-04-06 06:58:40 -05:00
|
|
|
|
2022-09-16 09:26:54 -05:00
|
|
|
// for<'a> |scope: &'a SearchScope| -> impl Iterator<Item = (Arc<String>, FileId, TextRange)> + 'a { ... }
|
2021-12-20 10:48:47 -06:00
|
|
|
fn scope_files<'a>(
|
2022-07-20 08:02:08 -05:00
|
|
|
sema: &'a Semantics<'_, RootDatabase>,
|
2021-12-20 10:48:47 -06:00
|
|
|
scope: &'a SearchScope,
|
2023-04-22 02:48:37 -05:00
|
|
|
) -> impl Iterator<Item = (Arc<str>, FileId, TextRange)> + 'a {
|
2021-12-20 10:48:47 -06:00
|
|
|
scope.entries.iter().map(|(&file_id, &search_range)| {
|
|
|
|
let text = sema.db.file_text(file_id);
|
|
|
|
let search_range =
|
2023-04-22 02:48:37 -05:00
|
|
|
search_range.unwrap_or_else(|| TextRange::up_to(TextSize::of(&*text)));
|
2020-03-04 05:14:48 -06:00
|
|
|
|
2021-12-20 10:48:47 -06:00
|
|
|
(text, file_id, search_range)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-05 06:04:21 -05:00
|
|
|
let find_nodes = move |name: &str, node: &syntax::SyntaxNode, offset: TextSize| {
|
2023-02-14 02:34:14 -06:00
|
|
|
node.token_at_offset(offset)
|
|
|
|
.find(|it| {
|
|
|
|
// `name` is stripped of raw ident prefix. See the comment on name retrieval above.
|
|
|
|
it.text().trim_start_matches("r#") == name
|
|
|
|
})
|
2023-02-14 02:34:19 -06:00
|
|
|
.into_iter()
|
2023-08-16 03:07:18 -05:00
|
|
|
.flat_map(move |token| {
|
2023-02-14 02:34:14 -06:00
|
|
|
// FIXME: There should be optimization potential here
|
|
|
|
// Currently we try to descend everything we find which
|
|
|
|
// means we call `Semantics::descend_into_macros` on
|
|
|
|
// every textual hit. That function is notoriously
|
|
|
|
// expensive even for things that do not get down mapped
|
|
|
|
// into macros.
|
2023-12-05 08:42:39 -06:00
|
|
|
sema.descend_into_macros(DescendPreference::None, token)
|
2023-12-03 13:20:38 -06:00
|
|
|
.into_iter()
|
|
|
|
.filter_map(|it| it.parent())
|
2023-02-14 02:34:14 -06:00
|
|
|
})
|
2022-11-05 06:04:21 -05:00
|
|
|
};
|
|
|
|
|
2021-12-20 10:48:47 -06:00
|
|
|
for (text, file_id, search_range) in scope_files(sema, &search_scope) {
|
2023-09-13 15:01:04 -05:00
|
|
|
self.sema.db.unwind_if_cancelled();
|
2021-12-20 10:48:47 -06:00
|
|
|
let tree = Lazy::new(move || sema.parse(file_id).syntax().clone());
|
|
|
|
|
|
|
|
// Search for occurrences of the items name
|
2022-09-16 09:26:54 -05:00
|
|
|
for offset in match_indices(&text, finder, search_range) {
|
2024-01-18 06:59:49 -06:00
|
|
|
tree.token_at_offset(offset).for_each(|token| {
|
2023-12-05 08:42:39 -06:00
|
|
|
let Some(str_token) = ast::String::cast(token.clone()) else { return };
|
|
|
|
if let Some((range, nameres)) =
|
2024-01-06 17:17:48 -06:00
|
|
|
sema.check_for_format_args_template(token, offset)
|
2023-12-05 08:42:39 -06:00
|
|
|
{
|
2024-01-18 06:59:49 -06:00
|
|
|
if self.found_format_args_ref(file_id, range, str_token, nameres, sink) {}
|
2023-12-05 08:42:39 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-02-14 02:34:19 -06:00
|
|
|
for name in find_nodes(name, &tree, offset).filter_map(ast::NameLike::cast) {
|
|
|
|
if match name {
|
|
|
|
ast::NameLike::NameRef(name_ref) => self.found_name_ref(&name_ref, sink),
|
|
|
|
ast::NameLike::Name(name) => self.found_name(&name, sink),
|
|
|
|
ast::NameLike::Lifetime(lifetime) => self.found_lifetime(&lifetime, sink),
|
|
|
|
} {
|
|
|
|
return;
|
2020-03-04 05:17:41 -06:00
|
|
|
}
|
2020-03-04 05:14:48 -06:00
|
|
|
}
|
2021-04-21 08:42:47 -05:00
|
|
|
}
|
2021-12-20 10:48:47 -06:00
|
|
|
// Search for occurrences of the `Self` referring to our type
|
2022-09-16 09:26:54 -05:00
|
|
|
if let Some((self_ty, finder)) = &include_self_kw_refs {
|
|
|
|
for offset in match_indices(&text, finder, search_range) {
|
2023-02-14 02:34:19 -06:00
|
|
|
for name_ref in find_nodes("Self", &tree, offset).filter_map(ast::NameRef::cast)
|
|
|
|
{
|
|
|
|
if self.found_self_ty_name_ref(self_ty, &name_ref, sink) {
|
|
|
|
return;
|
2021-05-08 15:34:55 -05:00
|
|
|
}
|
2021-04-21 08:42:47 -05:00
|
|
|
}
|
|
|
|
}
|
2020-03-04 05:14:48 -06:00
|
|
|
}
|
|
|
|
}
|
2021-06-28 09:41:35 -05:00
|
|
|
|
2021-12-20 10:48:47 -06:00
|
|
|
// Search for `super` and `crate` resolving to our module
|
2023-01-10 12:48:51 -06:00
|
|
|
if let Definition::Module(module) = self.def {
|
|
|
|
let scope =
|
|
|
|
search_scope.intersection(&SearchScope::module_and_children(self.sema.db, module));
|
2021-12-20 10:48:47 -06:00
|
|
|
|
2023-06-01 07:46:36 -05:00
|
|
|
let is_crate_root = module.is_crate_root().then(|| Finder::new("crate"));
|
2023-01-10 12:48:51 -06:00
|
|
|
let finder = &Finder::new("super");
|
2021-12-20 10:48:47 -06:00
|
|
|
|
2023-01-10 12:48:51 -06:00
|
|
|
for (text, file_id, search_range) in scope_files(sema, &scope) {
|
2023-09-13 15:01:04 -05:00
|
|
|
self.sema.db.unwind_if_cancelled();
|
2023-01-10 12:48:51 -06:00
|
|
|
let tree = Lazy::new(move || sema.parse(file_id).syntax().clone());
|
2021-12-20 10:48:47 -06:00
|
|
|
|
2023-01-10 12:48:51 -06:00
|
|
|
for offset in match_indices(&text, finder, search_range) {
|
2023-02-14 02:34:19 -06:00
|
|
|
for name_ref in
|
|
|
|
find_nodes("super", &tree, offset).filter_map(ast::NameRef::cast)
|
|
|
|
{
|
|
|
|
if self.found_name_ref(&name_ref, sink) {
|
|
|
|
return;
|
2023-01-10 12:48:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(finder) = &is_crate_root {
|
2022-09-16 09:26:54 -05:00
|
|
|
for offset in match_indices(&text, finder, search_range) {
|
2023-02-14 02:34:19 -06:00
|
|
|
for name_ref in
|
|
|
|
find_nodes("crate", &tree, offset).filter_map(ast::NameRef::cast)
|
|
|
|
{
|
|
|
|
if self.found_name_ref(&name_ref, sink) {
|
|
|
|
return;
|
2021-12-20 10:48:47 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-28 09:41:35 -05:00
|
|
|
// search for module `self` references in our module's definition source
|
|
|
|
match self.def {
|
2021-11-10 15:02:50 -06:00
|
|
|
Definition::Module(module) if self.search_self_mod => {
|
2021-06-28 09:41:35 -05:00
|
|
|
let src = module.definition_source(sema.db);
|
|
|
|
let file_id = src.file_id.original_file(sema.db);
|
|
|
|
let (file_id, search_range) = match src.value {
|
|
|
|
ModuleSource::Module(m) => (file_id, Some(m.syntax().text_range())),
|
|
|
|
ModuleSource::BlockExpr(b) => (file_id, Some(b.syntax().text_range())),
|
|
|
|
ModuleSource::SourceFile(_) => (file_id, None),
|
|
|
|
};
|
|
|
|
|
2021-12-20 10:48:47 -06:00
|
|
|
let search_range = if let Some(&range) = search_scope.entries.get(&file_id) {
|
|
|
|
match (range, search_range) {
|
|
|
|
(None, range) | (range, None) => range,
|
|
|
|
(Some(range), Some(search_range)) => match range.intersect(search_range) {
|
|
|
|
Some(range) => Some(range),
|
|
|
|
None => return,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
2021-06-28 09:41:35 -05:00
|
|
|
let text = sema.db.file_text(file_id);
|
|
|
|
let search_range =
|
2023-04-22 02:48:37 -05:00
|
|
|
search_range.unwrap_or_else(|| TextRange::up_to(TextSize::of(&*text)));
|
2021-06-28 09:41:35 -05:00
|
|
|
|
|
|
|
let tree = Lazy::new(|| sema.parse(file_id).syntax().clone());
|
2022-09-16 09:26:54 -05:00
|
|
|
let finder = &Finder::new("self");
|
2021-06-28 09:41:35 -05:00
|
|
|
|
2022-09-16 09:26:54 -05:00
|
|
|
for offset in match_indices(&text, finder, search_range) {
|
2023-02-14 02:34:19 -06:00
|
|
|
for name_ref in find_nodes("self", &tree, offset).filter_map(ast::NameRef::cast)
|
|
|
|
{
|
|
|
|
if self.found_self_module_name_ref(&name_ref, sink) {
|
|
|
|
return;
|
2021-06-28 09:41:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2020-03-04 05:14:48 -06:00
|
|
|
}
|
2020-10-09 12:55:30 -05:00
|
|
|
|
2021-05-08 15:34:55 -05:00
|
|
|
fn found_self_ty_name_ref(
|
|
|
|
&self,
|
|
|
|
self_ty: &hir::Type,
|
|
|
|
name_ref: &ast::NameRef,
|
|
|
|
sink: &mut dyn FnMut(FileId, FileReference) -> bool,
|
|
|
|
) -> bool {
|
2021-06-12 22:54:16 -05:00
|
|
|
match NameRefClass::classify(self.sema, name_ref) {
|
2021-05-08 15:34:55 -05:00
|
|
|
Some(NameRefClass::Definition(Definition::SelfType(impl_)))
|
2023-11-10 05:54:43 -06:00
|
|
|
if impl_.self_ty(self.sema.db).as_adt() == self_ty.as_adt() =>
|
2021-05-08 15:34:55 -05:00
|
|
|
{
|
|
|
|
let FileRange { file_id, range } = self.sema.original_range(name_ref.syntax());
|
|
|
|
let reference = FileReference {
|
|
|
|
range,
|
2023-12-05 08:42:39 -06:00
|
|
|
name: FileReferenceNode::NameRef(name_ref.clone()),
|
2021-10-02 04:18:18 -05:00
|
|
|
category: None,
|
2021-05-08 15:34:55 -05:00
|
|
|
};
|
|
|
|
sink(file_id, reference)
|
|
|
|
}
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-28 09:41:35 -05:00
|
|
|
fn found_self_module_name_ref(
|
|
|
|
&self,
|
|
|
|
name_ref: &ast::NameRef,
|
|
|
|
sink: &mut dyn FnMut(FileId, FileReference) -> bool,
|
|
|
|
) -> bool {
|
|
|
|
match NameRefClass::classify(self.sema, name_ref) {
|
2021-11-10 15:02:50 -06:00
|
|
|
Some(NameRefClass::Definition(def @ Definition::Module(_))) if def == self.def => {
|
2021-06-28 09:41:35 -05:00
|
|
|
let FileRange { file_id, range } = self.sema.original_range(name_ref.syntax());
|
|
|
|
let reference = FileReference {
|
|
|
|
range,
|
2023-12-05 08:42:39 -06:00
|
|
|
name: FileReferenceNode::NameRef(name_ref.clone()),
|
2022-12-30 02:30:23 -06:00
|
|
|
category: is_name_ref_in_import(name_ref).then_some(ReferenceCategory::Import),
|
2021-06-28 09:41:35 -05:00
|
|
|
};
|
|
|
|
sink(file_id, reference)
|
|
|
|
}
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-05 08:42:39 -06:00
|
|
|
fn found_format_args_ref(
|
|
|
|
&self,
|
|
|
|
file_id: FileId,
|
|
|
|
range: TextRange,
|
|
|
|
token: ast::String,
|
|
|
|
res: Option<PathResolution>,
|
|
|
|
sink: &mut dyn FnMut(FileId, FileReference) -> bool,
|
|
|
|
) -> bool {
|
|
|
|
match res.map(Definition::from) {
|
|
|
|
Some(def) if def == self.def => {
|
|
|
|
let reference = FileReference {
|
|
|
|
range,
|
|
|
|
name: FileReferenceNode::FormatStringEntry(token, range),
|
|
|
|
category: Some(ReferenceCategory::Read),
|
|
|
|
};
|
|
|
|
sink(file_id, reference)
|
|
|
|
}
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-16 14:35:15 -06:00
|
|
|
fn found_lifetime(
|
|
|
|
&self,
|
|
|
|
lifetime: &ast::Lifetime,
|
2021-01-11 17:05:07 -06:00
|
|
|
sink: &mut dyn FnMut(FileId, FileReference) -> bool,
|
2020-12-16 14:35:15 -06:00
|
|
|
) -> bool {
|
|
|
|
match NameRefClass::classify_lifetime(self.sema, lifetime) {
|
2021-06-11 12:23:59 -05:00
|
|
|
Some(NameRefClass::Definition(def)) if def == self.def => {
|
2021-01-11 17:05:07 -06:00
|
|
|
let FileRange { file_id, range } = self.sema.original_range(lifetime.syntax());
|
2021-02-09 09:03:39 -06:00
|
|
|
let reference = FileReference {
|
|
|
|
range,
|
2023-12-05 08:42:39 -06:00
|
|
|
name: FileReferenceNode::Lifetime(lifetime.clone()),
|
2021-10-02 04:18:18 -05:00
|
|
|
category: None,
|
2021-02-09 09:03:39 -06:00
|
|
|
};
|
2021-01-11 17:05:07 -06:00
|
|
|
sink(file_id, reference)
|
2020-12-16 14:35:15 -06:00
|
|
|
}
|
2021-05-08 15:34:55 -05:00
|
|
|
_ => false,
|
2020-12-16 14:35:15 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-09 12:55:30 -05:00
|
|
|
fn found_name_ref(
|
|
|
|
&self,
|
|
|
|
name_ref: &ast::NameRef,
|
2021-01-11 17:05:07 -06:00
|
|
|
sink: &mut dyn FnMut(FileId, FileReference) -> bool,
|
2020-10-09 12:55:30 -05:00
|
|
|
) -> bool {
|
2021-06-12 22:54:16 -05:00
|
|
|
match NameRefClass::classify(self.sema, name_ref) {
|
2022-06-24 06:15:16 -05:00
|
|
|
Some(NameRefClass::Definition(def))
|
2023-01-11 10:10:04 -06:00
|
|
|
if self.def == def
|
2023-01-24 07:11:02 -06:00
|
|
|
// is our def a trait assoc item? then we want to find all assoc items from trait impls of our trait
|
2023-01-11 10:10:04 -06:00
|
|
|
|| matches!(self.assoc_item_container, Some(hir::AssocItemContainer::Trait(_)))
|
|
|
|
&& convert_to_def_in_trait(self.sema.db, def) == self.def =>
|
|
|
|
{
|
|
|
|
let FileRange { file_id, range } = self.sema.original_range(name_ref.syntax());
|
|
|
|
let reference = FileReference {
|
|
|
|
range,
|
2023-12-05 08:42:39 -06:00
|
|
|
name: FileReferenceNode::NameRef(name_ref.clone()),
|
2024-01-29 04:42:41 -06:00
|
|
|
category: ReferenceCategory::new(self.sema, &def, name_ref),
|
2023-01-11 10:10:04 -06:00
|
|
|
};
|
|
|
|
sink(file_id, reference)
|
|
|
|
}
|
|
|
|
// FIXME: special case type aliases, we can't filter between impl and trait defs here as we lack the substitutions
|
|
|
|
// so we always resolve all assoc type aliases to both their trait def and impl defs
|
|
|
|
Some(NameRefClass::Definition(def))
|
|
|
|
if self.assoc_item_container.is_some()
|
|
|
|
&& matches!(self.def, Definition::TypeAlias(_))
|
|
|
|
&& convert_to_def_in_trait(self.sema.db, def)
|
|
|
|
== convert_to_def_in_trait(self.sema.db, self.def) =>
|
2022-06-24 06:15:16 -05:00
|
|
|
{
|
2021-04-03 16:04:31 -05:00
|
|
|
let FileRange { file_id, range } = self.sema.original_range(name_ref.syntax());
|
|
|
|
let reference = FileReference {
|
|
|
|
range,
|
2023-12-05 08:42:39 -06:00
|
|
|
name: FileReferenceNode::NameRef(name_ref.clone()),
|
2024-01-29 04:42:41 -06:00
|
|
|
category: ReferenceCategory::new(self.sema, &def, name_ref),
|
2021-04-03 16:04:31 -05:00
|
|
|
};
|
|
|
|
sink(file_id, reference)
|
|
|
|
}
|
2021-05-08 15:34:55 -05:00
|
|
|
Some(NameRefClass::Definition(def)) if self.include_self_kw_refs.is_some() => {
|
2021-05-08 15:43:26 -05:00
|
|
|
if self.include_self_kw_refs == def_to_ty(self.sema, &def) {
|
2021-05-08 15:34:55 -05:00
|
|
|
let FileRange { file_id, range } = self.sema.original_range(name_ref.syntax());
|
|
|
|
let reference = FileReference {
|
|
|
|
range,
|
2023-12-05 08:42:39 -06:00
|
|
|
name: FileReferenceNode::NameRef(name_ref.clone()),
|
2024-01-29 04:42:41 -06:00
|
|
|
category: ReferenceCategory::new(self.sema, &def, name_ref),
|
2021-05-08 15:34:55 -05:00
|
|
|
};
|
|
|
|
sink(file_id, reference)
|
|
|
|
} else {
|
|
|
|
false
|
2021-04-03 16:01:49 -05:00
|
|
|
}
|
|
|
|
}
|
2020-10-15 10:33:32 -05:00
|
|
|
Some(NameRefClass::FieldShorthand { local_ref: local, field_ref: field }) => {
|
2021-01-11 17:05:07 -06:00
|
|
|
let FileRange { file_id, range } = self.sema.original_range(name_ref.syntax());
|
2023-03-09 08:10:26 -06:00
|
|
|
|
|
|
|
let field = Definition::Field(field);
|
|
|
|
let local = Definition::Local(local);
|
2021-05-08 15:34:55 -05:00
|
|
|
let access = match self.def {
|
2021-10-02 04:18:18 -05:00
|
|
|
Definition::Field(_) if field == self.def => {
|
2024-01-29 04:42:41 -06:00
|
|
|
ReferenceCategory::new(self.sema, &field, name_ref)
|
2021-10-02 04:18:18 -05:00
|
|
|
}
|
2023-03-09 08:10:26 -06:00
|
|
|
Definition::Local(_) if local == self.def => {
|
2024-01-29 04:42:41 -06:00
|
|
|
ReferenceCategory::new(self.sema, &local, name_ref)
|
2021-05-08 15:34:55 -05:00
|
|
|
}
|
|
|
|
_ => return false,
|
2020-10-09 12:55:30 -05:00
|
|
|
};
|
2021-10-02 04:18:18 -05:00
|
|
|
let reference = FileReference {
|
|
|
|
range,
|
2023-12-05 08:42:39 -06:00
|
|
|
name: FileReferenceNode::NameRef(name_ref.clone()),
|
2021-10-02 04:18:18 -05:00
|
|
|
category: access,
|
|
|
|
};
|
2021-01-11 17:05:07 -06:00
|
|
|
sink(file_id, reference)
|
2020-10-09 12:55:30 -05:00
|
|
|
}
|
2021-05-08 15:34:55 -05:00
|
|
|
_ => false,
|
2020-10-09 12:55:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-11 17:05:07 -06:00
|
|
|
fn found_name(
|
|
|
|
&self,
|
|
|
|
name: &ast::Name,
|
|
|
|
sink: &mut dyn FnMut(FileId, FileReference) -> bool,
|
|
|
|
) -> bool {
|
2020-10-15 10:27:50 -05:00
|
|
|
match NameClass::classify(self.sema, name) {
|
2021-02-23 16:31:07 -06:00
|
|
|
Some(NameClass::PatFieldShorthand { local_def: _, field_ref })
|
|
|
|
if matches!(
|
2021-07-11 07:03:35 -05:00
|
|
|
self.def, Definition::Field(_) if Definition::Field(field_ref) == self.def
|
2021-02-23 16:31:07 -06:00
|
|
|
) =>
|
|
|
|
{
|
2021-01-11 17:05:07 -06:00
|
|
|
let FileRange { file_id, range } = self.sema.original_range(name.syntax());
|
|
|
|
let reference = FileReference {
|
|
|
|
range,
|
2023-12-05 08:42:39 -06:00
|
|
|
name: FileReferenceNode::Name(name.clone()),
|
2021-01-11 17:05:07 -06:00
|
|
|
// FIXME: mutable patterns should have `Write` access
|
2021-10-02 04:18:18 -05:00
|
|
|
category: Some(ReferenceCategory::Read),
|
2020-10-09 12:55:30 -05:00
|
|
|
};
|
2021-01-11 17:05:07 -06:00
|
|
|
sink(file_id, reference)
|
2020-10-09 12:55:30 -05:00
|
|
|
}
|
2021-06-11 12:23:59 -05:00
|
|
|
Some(NameClass::ConstReference(def)) if self.def == def => {
|
2021-02-23 16:31:07 -06:00
|
|
|
let FileRange { file_id, range } = self.sema.original_range(name.syntax());
|
2021-10-02 04:18:18 -05:00
|
|
|
let reference = FileReference {
|
|
|
|
range,
|
2023-12-05 08:42:39 -06:00
|
|
|
name: FileReferenceNode::Name(name.clone()),
|
2021-10-02 04:18:18 -05:00
|
|
|
category: None,
|
|
|
|
};
|
2021-02-23 16:31:07 -06:00
|
|
|
sink(file_id, reference)
|
|
|
|
}
|
2021-11-10 15:02:50 -06:00
|
|
|
Some(NameClass::Definition(def)) if def != self.def => {
|
2023-01-24 07:11:02 -06:00
|
|
|
match (&self.assoc_item_container, self.def) {
|
|
|
|
// for type aliases we always want to reference the trait def and all the trait impl counterparts
|
|
|
|
// FIXME: only until we can resolve them correctly, see FIXME above
|
|
|
|
(Some(_), Definition::TypeAlias(_))
|
|
|
|
if convert_to_def_in_trait(self.sema.db, def)
|
|
|
|
!= convert_to_def_in_trait(self.sema.db, self.def) =>
|
|
|
|
{
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
(Some(_), Definition::TypeAlias(_)) => {}
|
|
|
|
// We looking at an assoc item of a trait definition, so reference all the
|
|
|
|
// corresponding assoc items belonging to this trait's trait implementations
|
|
|
|
(Some(hir::AssocItemContainer::Trait(_)), _)
|
|
|
|
if convert_to_def_in_trait(self.sema.db, def) == self.def => {}
|
|
|
|
_ => return false,
|
2022-07-20 06:59:31 -05:00
|
|
|
}
|
|
|
|
let FileRange { file_id, range } = self.sema.original_range(name.syntax());
|
|
|
|
let reference = FileReference {
|
|
|
|
range,
|
2023-12-05 08:42:39 -06:00
|
|
|
name: FileReferenceNode::Name(name.clone()),
|
2022-07-20 06:59:31 -05:00
|
|
|
category: None,
|
|
|
|
};
|
|
|
|
sink(file_id, reference)
|
2021-06-11 12:23:59 -05:00
|
|
|
}
|
2021-05-08 15:34:55 -05:00
|
|
|
_ => false,
|
2020-10-09 12:55:30 -05:00
|
|
|
}
|
|
|
|
}
|
2020-03-04 05:14:48 -06:00
|
|
|
}
|
|
|
|
|
2022-07-20 08:02:08 -05:00
|
|
|
fn def_to_ty(sema: &Semantics<'_, RootDatabase>, def: &Definition) -> Option<hir::Type> {
|
2021-05-08 15:34:55 -05:00
|
|
|
match def {
|
2021-11-10 15:02:50 -06:00
|
|
|
Definition::Adt(adt) => Some(adt.ty(sema.db)),
|
|
|
|
Definition::TypeAlias(it) => Some(it.ty(sema.db)),
|
2022-03-26 15:22:35 -05:00
|
|
|
Definition::BuiltinType(it) => Some(it.ty(sema.db)),
|
2021-05-08 15:43:26 -05:00
|
|
|
Definition::SelfType(it) => Some(it.self_ty(sema.db)),
|
2021-05-08 15:34:55 -05:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-02 04:18:18 -05:00
|
|
|
impl ReferenceCategory {
|
2024-01-29 04:42:41 -06:00
|
|
|
fn new(
|
|
|
|
sema: &Semantics<'_, RootDatabase>,
|
|
|
|
def: &Definition,
|
|
|
|
r: &ast::NameRef,
|
|
|
|
) -> Option<ReferenceCategory> {
|
|
|
|
if is_name_ref_in_test(sema, r) {
|
2024-01-28 04:28:13 -06:00
|
|
|
return Some(ReferenceCategory::Test);
|
|
|
|
}
|
|
|
|
|
2021-10-02 04:18:18 -05:00
|
|
|
// Only Locals and Fields have accesses for now.
|
|
|
|
if !matches!(def, Definition::Local(_) | Definition::Field(_)) {
|
2022-12-30 02:30:23 -06:00
|
|
|
return is_name_ref_in_import(r).then_some(ReferenceCategory::Import);
|
2021-10-02 04:18:18 -05:00
|
|
|
}
|
2020-03-04 05:14:48 -06:00
|
|
|
|
2021-10-02 04:18:18 -05:00
|
|
|
let mode = r.syntax().ancestors().find_map(|node| {
|
2020-03-04 05:14:48 -06:00
|
|
|
match_ast! {
|
2021-12-14 05:44:31 -06:00
|
|
|
match node {
|
2020-03-04 05:14:48 -06:00
|
|
|
ast::BinExpr(expr) => {
|
2021-08-14 10:08:31 -05:00
|
|
|
if matches!(expr.op_kind()?, ast::BinaryOp::Assignment { .. }) {
|
2020-03-04 05:14:48 -06:00
|
|
|
// If the variable or field ends on the LHS's end then it's a Write (covers fields and locals).
|
|
|
|
// FIXME: This is not terribly accurate.
|
|
|
|
if let Some(lhs) = expr.lhs() {
|
2021-10-02 04:18:18 -05:00
|
|
|
if lhs.syntax().text_range().end() == r.syntax().text_range().end() {
|
|
|
|
return Some(ReferenceCategory::Write);
|
2020-03-04 05:14:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-02 04:18:18 -05:00
|
|
|
Some(ReferenceCategory::Read)
|
2020-03-04 05:14:48 -06:00
|
|
|
},
|
2020-04-06 09:21:33 -05:00
|
|
|
_ => None
|
2020-03-04 05:14:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-10-02 04:18:18 -05:00
|
|
|
// Default Locals and Fields to read
|
|
|
|
mode.or(Some(ReferenceCategory::Read))
|
|
|
|
}
|
2020-03-04 05:14:48 -06:00
|
|
|
}
|
2022-09-13 07:47:26 -05:00
|
|
|
|
|
|
|
fn is_name_ref_in_import(name_ref: &ast::NameRef) -> bool {
|
|
|
|
name_ref
|
|
|
|
.syntax()
|
|
|
|
.parent()
|
|
|
|
.and_then(ast::PathSegment::cast)
|
|
|
|
.and_then(|it| it.parent_path().top_path().syntax().parent())
|
|
|
|
.map_or(false, |it| it.kind() == SyntaxKind::USE_TREE)
|
|
|
|
}
|
2024-01-28 04:28:13 -06:00
|
|
|
|
2024-01-29 04:42:41 -06:00
|
|
|
fn is_name_ref_in_test(sema: &Semantics<'_, RootDatabase>, name_ref: &ast::NameRef) -> bool {
|
|
|
|
name_ref.syntax().ancestors().any(|node| match ast::Fn::cast(node) {
|
|
|
|
Some(it) => sema.to_def(&it).map_or(false, |func| func.is_test(sema.db)),
|
|
|
|
None => false,
|
|
|
|
})
|
2024-01-28 04:28:13 -06:00
|
|
|
}
|