2020-03-04 11:48:50 +00: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.
|
|
|
|
|
2020-04-24 22:57:47 +00:00
|
|
|
use std::{convert::TryInto, mem};
|
2020-03-04 11:05:14 +00:00
|
|
|
|
2020-03-24 20:45:55 +00:00
|
|
|
use hir::{DefWithBody, HasSource, Module, ModuleSource, Semantics, Visibility};
|
2020-03-04 11:14:48 +00:00
|
|
|
use once_cell::unsync::Lazy;
|
2020-03-04 11:06:37 +00:00
|
|
|
use ra_db::{FileId, FileRange, SourceDatabaseExt};
|
2020-03-04 11:05:14 +00:00
|
|
|
use ra_prof::profile;
|
2020-04-24 21:40:41 +00:00
|
|
|
use ra_syntax::{ast, match_ast, AstNode, TextRange, TextSize};
|
2020-03-04 11:05:14 +00:00
|
|
|
use rustc_hash::FxHashMap;
|
|
|
|
|
2020-03-04 11:14:48 +00:00
|
|
|
use crate::{
|
2020-03-10 02:18:55 +00:00
|
|
|
defs::{classify_name_ref, Definition, NameRefClass},
|
2020-03-04 11:14:48 +00:00
|
|
|
RootDatabase,
|
|
|
|
};
|
2020-03-04 11:05:14 +00:00
|
|
|
|
2020-03-04 11:06:37 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Reference {
|
|
|
|
pub file_range: FileRange,
|
|
|
|
pub kind: ReferenceKind,
|
|
|
|
pub access: Option<ReferenceAccess>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub enum ReferenceKind {
|
2020-04-25 12:23:34 +00:00
|
|
|
FieldShorthandForField,
|
|
|
|
FieldShorthandForLocal,
|
2020-03-04 11:06:37 +00:00
|
|
|
StructLiteral,
|
|
|
|
Other,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq)]
|
|
|
|
pub enum ReferenceAccess {
|
|
|
|
Read,
|
|
|
|
Write,
|
|
|
|
}
|
|
|
|
|
2020-03-04 11:48:50 +00: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.
|
2020-03-04 11:05:14 +00:00
|
|
|
pub struct SearchScope {
|
|
|
|
entries: FxHashMap<FileId, Option<TextRange>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SearchScope {
|
|
|
|
fn new(entries: FxHashMap<FileId, Option<TextRange>>) -> SearchScope {
|
|
|
|
SearchScope { entries }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn empty() -> SearchScope {
|
|
|
|
SearchScope::new(FxHashMap::default())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn single_file(file: FileId) -> SearchScope {
|
|
|
|
SearchScope::new(std::iter::once((file, None)).collect())
|
|
|
|
}
|
|
|
|
|
2020-03-04 11:46:40 +00: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)
|
|
|
|
}
|
|
|
|
|
|
|
|
let res = small
|
|
|
|
.iter()
|
|
|
|
.filter_map(|(file_id, r1)| {
|
|
|
|
let r2 = large.get(file_id)?;
|
|
|
|
let r = intersect_ranges(*r1, *r2)?;
|
|
|
|
Some((*file_id, r))
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
return SearchScope::new(res);
|
|
|
|
|
|
|
|
fn intersect_ranges(
|
|
|
|
r1: Option<TextRange>,
|
|
|
|
r2: Option<TextRange>,
|
|
|
|
) -> Option<Option<TextRange>> {
|
|
|
|
match (r1, r2) {
|
|
|
|
(None, r) | (r, None) => Some(r),
|
|
|
|
(Some(r1), Some(r2)) => {
|
2020-04-24 21:40:41 +00:00
|
|
|
let r = r1.intersect(r2)?;
|
2020-03-04 11:46:40 +00:00
|
|
|
Some(Some(r))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2020-03-04 11:05:14 +00:00
|
|
|
let _p = profile("search_scope");
|
2020-03-04 11:46:40 +00:00
|
|
|
let module = match self.module(db) {
|
2020-03-04 11:05:14 +00:00
|
|
|
Some(it) => it,
|
|
|
|
None => return SearchScope::empty(),
|
|
|
|
};
|
|
|
|
let module_src = module.definition_source(db);
|
|
|
|
let file_id = module_src.file_id.original_file(db);
|
|
|
|
|
2020-03-04 11:46:40 +00:00
|
|
|
if let Definition::Local(var) = self {
|
2020-03-04 11:05:14 +00:00
|
|
|
let range = match var.parent(db) {
|
|
|
|
DefWithBody::Function(f) => f.source(db).value.syntax().text_range(),
|
|
|
|
DefWithBody::Const(c) => c.source(db).value.syntax().text_range(),
|
|
|
|
DefWithBody::Static(s) => s.source(db).value.syntax().text_range(),
|
|
|
|
};
|
|
|
|
let mut res = FxHashMap::default();
|
|
|
|
res.insert(file_id, Some(range));
|
|
|
|
return SearchScope::new(res);
|
|
|
|
}
|
|
|
|
|
2020-03-24 20:45:55 +00:00
|
|
|
let vis = self.visibility(db);
|
|
|
|
|
|
|
|
// FIXME:
|
|
|
|
// The following logic are wrong that it does not search
|
|
|
|
// for submodules within other files recursively.
|
|
|
|
|
|
|
|
if let Some(Visibility::Module(module)) = vis.and_then(|it| it.into()) {
|
|
|
|
let module: Module = module.into();
|
|
|
|
let mut res = FxHashMap::default();
|
|
|
|
let src = module.definition_source(db);
|
|
|
|
let file_id = src.file_id.original_file(db);
|
|
|
|
|
|
|
|
match src.value {
|
|
|
|
ModuleSource::Module(m) => {
|
|
|
|
let range = Some(m.syntax().text_range());
|
|
|
|
res.insert(file_id, range);
|
|
|
|
}
|
|
|
|
ModuleSource::SourceFile(_) => {
|
|
|
|
res.insert(file_id, None);
|
|
|
|
res.extend(module.children(db).map(|m| {
|
|
|
|
let src = m.definition_source(db);
|
|
|
|
(src.file_id.original_file(db), None)
|
|
|
|
}));
|
2020-03-04 11:05:14 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-24 20:45:55 +00:00
|
|
|
return SearchScope::new(res);
|
2020-03-04 11:05:14 +00:00
|
|
|
}
|
|
|
|
|
2020-03-24 20:45:55 +00:00
|
|
|
if let Some(Visibility::Public) = vis {
|
2020-03-04 11:05:14 +00:00
|
|
|
let source_root_id = db.file_source_root(file_id);
|
|
|
|
let source_root = db.source_root(source_root_id);
|
|
|
|
let mut res = source_root.walk().map(|id| (id, None)).collect::<FxHashMap<_, _>>();
|
|
|
|
|
2020-03-24 20:45:55 +00:00
|
|
|
let krate = module.krate();
|
|
|
|
for rev_dep in krate.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);
|
|
|
|
res.extend(source_root.walk().map(|id| (id, None)));
|
2020-03-04 11:05:14 +00:00
|
|
|
}
|
2020-03-24 20:45:55 +00:00
|
|
|
return SearchScope::new(res);
|
2020-03-04 11:05:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut res = FxHashMap::default();
|
|
|
|
let range = match module_src.value {
|
|
|
|
ModuleSource::Module(m) => Some(m.syntax().text_range()),
|
|
|
|
ModuleSource::SourceFile(_) => None,
|
|
|
|
};
|
|
|
|
res.insert(file_id, range);
|
|
|
|
SearchScope::new(res)
|
|
|
|
}
|
|
|
|
|
2020-03-04 11:17:41 +00:00
|
|
|
pub fn find_usages(
|
|
|
|
&self,
|
|
|
|
db: &RootDatabase,
|
|
|
|
search_scope: Option<SearchScope>,
|
|
|
|
) -> Vec<Reference> {
|
|
|
|
let _p = profile("Definition::find_usages");
|
|
|
|
|
|
|
|
let search_scope = {
|
2020-03-04 11:46:40 +00:00
|
|
|
let base = self.search_scope(db);
|
2020-03-04 11:17:41 +00:00
|
|
|
match search_scope {
|
|
|
|
None => base,
|
|
|
|
Some(scope) => base.intersection(&scope),
|
|
|
|
}
|
|
|
|
};
|
2020-03-04 11:14:48 +00:00
|
|
|
|
2020-03-04 11:17:41 +00:00
|
|
|
let name = match self.name(db) {
|
|
|
|
None => return Vec::new(),
|
|
|
|
Some(it) => it.to_string(),
|
|
|
|
};
|
2020-03-04 11:14:48 +00:00
|
|
|
|
2020-03-04 11:17:41 +00:00
|
|
|
let pat = name.as_str();
|
|
|
|
let mut refs = vec![];
|
2020-03-04 11:14:48 +00:00
|
|
|
|
2020-03-04 11:17:41 +00:00
|
|
|
for (file_id, search_range) in search_scope {
|
|
|
|
let text = db.file_text(file_id);
|
2020-04-24 22:17:50 +00:00
|
|
|
let search_range =
|
|
|
|
search_range.unwrap_or(TextRange::up_to(TextSize::of(text.as_str())));
|
2020-03-04 11:14:48 +00:00
|
|
|
|
2020-03-04 11:17:41 +00:00
|
|
|
let sema = Semantics::new(db);
|
|
|
|
let tree = Lazy::new(|| sema.parse(file_id).syntax().clone());
|
2020-03-04 11:14:48 +00:00
|
|
|
|
2020-03-04 11:17:41 +00:00
|
|
|
for (idx, _) in text.match_indices(pat) {
|
2020-04-24 22:57:47 +00:00
|
|
|
let offset: TextSize = idx.try_into().unwrap();
|
2020-03-04 11:17:41 +00:00
|
|
|
if !search_range.contains_inclusive(offset) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-03-04 11:14:48 +00:00
|
|
|
|
2020-03-22 11:52:45 +00:00
|
|
|
let name_ref: ast::NameRef =
|
|
|
|
if let Some(name_ref) = sema.find_node_at_offset_with_descend(&tree, offset) {
|
2020-03-04 11:17:41 +00:00
|
|
|
name_ref
|
|
|
|
} else {
|
2020-03-22 11:52:45 +00:00
|
|
|
continue;
|
2020-03-04 11:14:48 +00:00
|
|
|
};
|
|
|
|
|
2020-03-04 11:17:41 +00:00
|
|
|
// FIXME: reuse sb
|
|
|
|
// See https://github.com/rust-lang/rust/pull/68198#issuecomment-574269098
|
2020-03-04 11:14:48 +00:00
|
|
|
|
2020-03-10 02:34:33 +00:00
|
|
|
match classify_name_ref(&sema, &name_ref) {
|
|
|
|
Some(NameRefClass::Definition(def)) if &def == self => {
|
2020-03-04 11:17:41 +00:00
|
|
|
let kind = if is_record_lit_name_ref(&name_ref)
|
|
|
|
|| is_call_expr_name_ref(&name_ref)
|
|
|
|
{
|
2020-03-04 11:14:48 +00:00
|
|
|
ReferenceKind::StructLiteral
|
|
|
|
} else {
|
|
|
|
ReferenceKind::Other
|
|
|
|
};
|
|
|
|
|
2020-03-04 11:17:41 +00:00
|
|
|
let file_range = sema.original_range(name_ref.syntax());
|
|
|
|
refs.push(Reference {
|
|
|
|
file_range,
|
|
|
|
kind,
|
2020-03-10 02:18:55 +00:00
|
|
|
access: reference_access(&def, &name_ref),
|
2020-03-04 11:17:41 +00:00
|
|
|
});
|
|
|
|
}
|
2020-03-11 03:27:38 +00:00
|
|
|
Some(NameRefClass::FieldShorthand { local, field }) => {
|
|
|
|
match self {
|
2020-04-25 12:23:34 +00:00
|
|
|
Definition::Field(_) if &field == self => refs.push(Reference {
|
2020-03-11 03:27:38 +00:00
|
|
|
file_range: sema.original_range(name_ref.syntax()),
|
2020-04-25 12:23:34 +00:00
|
|
|
kind: ReferenceKind::FieldShorthandForField,
|
2020-03-11 03:27:38 +00:00
|
|
|
access: reference_access(&field, &name_ref),
|
|
|
|
}),
|
|
|
|
Definition::Local(l) if &local == l => refs.push(Reference {
|
|
|
|
file_range: sema.original_range(name_ref.syntax()),
|
2020-04-25 12:23:34 +00:00
|
|
|
kind: ReferenceKind::FieldShorthandForLocal,
|
2020-03-11 03:27:38 +00:00
|
|
|
access: reference_access(&Definition::Local(local), &name_ref),
|
|
|
|
}),
|
|
|
|
|
|
|
|
_ => {} // not a usage
|
2020-03-10 02:34:33 +00:00
|
|
|
};
|
2020-03-10 02:18:55 +00:00
|
|
|
}
|
|
|
|
_ => {} // not a usage
|
2020-03-04 11:14:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 11:17:41 +00:00
|
|
|
refs
|
2020-03-04 11:14:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn reference_access(def: &Definition, name_ref: &ast::NameRef) -> Option<ReferenceAccess> {
|
|
|
|
// Only Locals and Fields have accesses for now.
|
|
|
|
match def {
|
2020-04-25 12:23:34 +00:00
|
|
|
Definition::Local(_) | Definition::Field(_) => {}
|
2020-03-04 11:14:48 +00:00
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mode = name_ref.syntax().ancestors().find_map(|node| {
|
|
|
|
match_ast! {
|
|
|
|
match (node) {
|
|
|
|
ast::BinExpr(expr) => {
|
|
|
|
if expr.op_kind()?.is_assignment() {
|
|
|
|
// 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() {
|
|
|
|
if lhs.syntax().text_range().end() == name_ref.syntax().text_range().end() {
|
|
|
|
return Some(ReferenceAccess::Write);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(ReferenceAccess::Read)
|
|
|
|
},
|
2020-04-06 14:21:33 +00:00
|
|
|
_ => None
|
2020-03-04 11:14:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Default Locals and Fields to read
|
|
|
|
mode.or(Some(ReferenceAccess::Read))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_call_expr_name_ref(name_ref: &ast::NameRef) -> bool {
|
|
|
|
name_ref
|
|
|
|
.syntax()
|
|
|
|
.ancestors()
|
|
|
|
.find_map(ast::CallExpr::cast)
|
|
|
|
.and_then(|c| match c.expr()? {
|
|
|
|
ast::Expr::PathExpr(p) => {
|
|
|
|
Some(p.path()?.segment()?.name_ref().as_ref() == Some(name_ref))
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.unwrap_or(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_record_lit_name_ref(name_ref: &ast::NameRef) -> bool {
|
|
|
|
name_ref
|
|
|
|
.syntax()
|
|
|
|
.ancestors()
|
|
|
|
.find_map(ast::RecordLit::cast)
|
|
|
|
.and_then(|l| l.path())
|
|
|
|
.and_then(|p| p.segment())
|
|
|
|
.map(|p| p.name_ref().as_ref() == Some(name_ref))
|
|
|
|
.unwrap_or(false)
|
|
|
|
}
|