2019-10-22 15:46:53 -05:00
|
|
|
//! This module implements a reference search.
|
|
|
|
//! First, the element at the cursor position must be either an `ast::Name`
|
|
|
|
//! or `ast::NameRef`. If it's a `ast::NameRef`, at the classification step we
|
|
|
|
//! try to resolve the direct tree parent of this element, otherwise we
|
|
|
|
//! already have a definition and just need to get its HIR together with
|
|
|
|
//! some information that is needed for futher steps of searching.
|
|
|
|
//! After that, we collect files that might contain references and look
|
|
|
|
//! for text occurrences of the identifier. If there's an `ast::NameRef`
|
|
|
|
//! at the index that the match starts at and its tree parent is
|
|
|
|
//! resolved to the search element definition, we get a reference.
|
2019-09-30 03:58:53 -05:00
|
|
|
|
2019-10-12 10:47:17 -05:00
|
|
|
mod classify;
|
2019-10-12 12:30:53 -05:00
|
|
|
mod name_definition;
|
2019-10-12 10:47:17 -05:00
|
|
|
mod rename;
|
|
|
|
mod search_scope;
|
|
|
|
|
2020-01-14 10:39:28 -06:00
|
|
|
use hir::{InFile, SourceBinder};
|
2019-10-14 06:59:02 -05:00
|
|
|
use once_cell::unsync::Lazy;
|
2019-10-14 08:20:55 -05:00
|
|
|
use ra_db::{SourceDatabase, SourceDatabaseExt};
|
2019-10-24 02:37:20 -05:00
|
|
|
use ra_prof::profile;
|
2020-01-03 09:30:45 -06:00
|
|
|
use ra_syntax::{
|
2020-01-10 13:56:58 -06:00
|
|
|
algo::find_node_at_offset,
|
|
|
|
ast::{self, NameOwner},
|
|
|
|
match_ast, AstNode, SourceFile, SyntaxKind, SyntaxNode, TextRange, TextUnit, TokenAtOffset,
|
2020-01-03 09:30:45 -06:00
|
|
|
};
|
2019-10-12 10:47:17 -05:00
|
|
|
|
2019-11-11 02:15:19 -06:00
|
|
|
use crate::{
|
|
|
|
db::RootDatabase, display::ToNav, FilePosition, FileRange, NavigationTarget, RangeInfo,
|
|
|
|
};
|
2019-10-12 10:47:17 -05:00
|
|
|
|
|
|
|
pub(crate) use self::{
|
2020-01-14 10:39:28 -06:00
|
|
|
classify::{classify_name, classify_name_ref},
|
2019-10-12 12:30:53 -05:00
|
|
|
name_definition::{NameDefinition, NameKind},
|
2019-10-12 10:47:17 -05:00
|
|
|
rename::rename,
|
2019-01-16 07:39:01 -06:00
|
|
|
};
|
2019-02-08 05:06:26 -06:00
|
|
|
|
2019-10-24 06:01:02 -05:00
|
|
|
pub use self::search_scope::SearchScope;
|
|
|
|
|
2019-02-17 05:38:32 -06:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct ReferenceSearchResult {
|
2020-01-09 15:27:10 -06:00
|
|
|
declaration: Declaration,
|
2020-01-03 09:30:45 -06:00
|
|
|
references: Vec<Reference>,
|
|
|
|
}
|
|
|
|
|
2020-01-09 15:27:10 -06:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Declaration {
|
|
|
|
pub nav: NavigationTarget,
|
|
|
|
pub kind: ReferenceKind,
|
|
|
|
pub access: Option<ReferenceAccess>,
|
|
|
|
}
|
|
|
|
|
2020-01-03 09:30:45 -06:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Reference {
|
|
|
|
pub file_range: FileRange,
|
|
|
|
pub kind: ReferenceKind,
|
2020-01-04 16:46:01 -06:00
|
|
|
pub access: Option<ReferenceAccess>,
|
2020-01-03 09:30:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub enum ReferenceKind {
|
|
|
|
StructLiteral,
|
|
|
|
Other,
|
2019-02-17 05:38:32 -06:00
|
|
|
}
|
|
|
|
|
2020-01-09 15:01:43 -06:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq)]
|
2020-01-04 16:46:01 -06:00
|
|
|
pub enum ReferenceAccess {
|
|
|
|
Read,
|
|
|
|
Write,
|
|
|
|
}
|
|
|
|
|
2019-02-17 05:38:32 -06:00
|
|
|
impl ReferenceSearchResult {
|
2020-01-09 15:27:10 -06:00
|
|
|
pub fn declaration(&self) -> &Declaration {
|
2019-02-17 05:38:32 -06:00
|
|
|
&self.declaration
|
|
|
|
}
|
|
|
|
|
2020-01-09 15:27:10 -06:00
|
|
|
pub fn decl_target(&self) -> &NavigationTarget {
|
|
|
|
&self.declaration.nav
|
|
|
|
}
|
|
|
|
|
2020-01-03 09:30:45 -06:00
|
|
|
pub fn references(&self) -> &[Reference] {
|
2019-02-17 05:38:32 -06:00
|
|
|
&self.references
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Total number of references
|
|
|
|
/// At least 1 since all valid references should
|
|
|
|
/// Have a declaration
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.references.len() + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// allow turning ReferenceSearchResult into an iterator
|
2020-01-04 16:46:01 -06:00
|
|
|
// over References
|
2019-02-17 05:38:32 -06:00
|
|
|
impl IntoIterator for ReferenceSearchResult {
|
2020-01-03 09:30:45 -06:00
|
|
|
type Item = Reference;
|
|
|
|
type IntoIter = std::vec::IntoIter<Reference>;
|
2019-02-17 05:38:32 -06:00
|
|
|
|
|
|
|
fn into_iter(mut self) -> Self::IntoIter {
|
|
|
|
let mut v = Vec::with_capacity(self.len());
|
2020-01-03 09:30:45 -06:00
|
|
|
v.push(Reference {
|
|
|
|
file_range: FileRange {
|
2020-01-09 15:27:10 -06:00
|
|
|
file_id: self.declaration.nav.file_id(),
|
|
|
|
range: self.declaration.nav.range(),
|
2020-01-03 09:30:45 -06:00
|
|
|
},
|
2020-01-09 15:27:10 -06:00
|
|
|
kind: self.declaration.kind,
|
|
|
|
access: self.declaration.access,
|
2020-01-03 09:30:45 -06:00
|
|
|
});
|
2019-02-17 05:38:32 -06:00
|
|
|
v.append(&mut self.references);
|
|
|
|
v.into_iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn find_all_refs(
|
|
|
|
db: &RootDatabase,
|
2020-01-03 09:30:45 -06:00
|
|
|
mut position: FilePosition,
|
2019-10-24 06:01:02 -05:00
|
|
|
search_scope: Option<SearchScope>,
|
2019-09-05 13:36:40 -05:00
|
|
|
) -> Option<RangeInfo<ReferenceSearchResult>> {
|
2019-07-12 11:41:13 -05:00
|
|
|
let parse = db.parse(position.file_id);
|
2019-09-14 06:38:10 -05:00
|
|
|
let syntax = parse.tree().syntax().clone();
|
2020-01-03 09:30:45 -06:00
|
|
|
|
|
|
|
let token = syntax.token_at_offset(position.offset);
|
|
|
|
let mut search_kind = ReferenceKind::Other;
|
|
|
|
|
|
|
|
if let TokenAtOffset::Between(ref left, ref right) = token {
|
|
|
|
if (right.kind() == SyntaxKind::L_CURLY || right.kind() == SyntaxKind::L_PAREN)
|
|
|
|
&& left.kind() != SyntaxKind::IDENT
|
|
|
|
{
|
|
|
|
position = FilePosition { offset: left.text_range().start(), ..position };
|
|
|
|
search_kind = ReferenceKind::StructLiteral;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-10 10:51:51 -05:00
|
|
|
let RangeInfo { range, info: (name, def) } = find_name(db, &syntax, position)?;
|
2019-09-14 06:38:10 -05:00
|
|
|
|
2019-10-14 06:59:02 -05:00
|
|
|
let declaration = match def.kind {
|
2019-11-11 02:15:19 -06:00
|
|
|
NameKind::Macro(mac) => mac.to_nav(db),
|
|
|
|
NameKind::Field(field) => field.to_nav(db),
|
|
|
|
NameKind::AssocItem(assoc) => assoc.to_nav(db),
|
2019-10-12 10:47:17 -05:00
|
|
|
NameKind::Def(def) => NavigationTarget::from_def(db, def)?,
|
2019-11-26 12:18:26 -06:00
|
|
|
NameKind::SelfType(imp) => imp.to_nav(db),
|
2019-11-09 15:32:00 -06:00
|
|
|
NameKind::Local(local) => local.to_nav(db),
|
2019-12-07 13:09:53 -06:00
|
|
|
NameKind::TypeParam(_) => return None,
|
2019-09-14 06:38:10 -05:00
|
|
|
};
|
2019-02-08 05:06:26 -06:00
|
|
|
|
2019-10-24 06:01:02 -05:00
|
|
|
let search_scope = {
|
|
|
|
let base = def.search_scope(db);
|
|
|
|
match search_scope {
|
|
|
|
None => base,
|
|
|
|
Some(scope) => base.intersection(&scope),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-10 13:56:58 -06:00
|
|
|
let decl_range = declaration.range();
|
|
|
|
|
|
|
|
let declaration = Declaration {
|
|
|
|
nav: declaration,
|
|
|
|
kind: ReferenceKind::Other,
|
|
|
|
access: decl_access(&def.kind, &name, &syntax, decl_range),
|
|
|
|
};
|
2020-01-09 15:27:10 -06:00
|
|
|
|
2020-01-03 09:30:45 -06:00
|
|
|
let references = process_definition(db, def, name, search_scope)
|
|
|
|
.into_iter()
|
|
|
|
.filter(|r| search_kind == ReferenceKind::Other || search_kind == r.kind)
|
|
|
|
.collect();
|
2019-09-14 06:38:10 -05:00
|
|
|
|
2020-01-09 15:27:10 -06:00
|
|
|
Some(RangeInfo::new(range, ReferenceSearchResult { declaration, references }))
|
2019-01-16 07:39:01 -06:00
|
|
|
}
|
|
|
|
|
2020-01-13 10:27:06 -06:00
|
|
|
fn find_name(
|
2019-10-12 10:47:17 -05:00
|
|
|
db: &RootDatabase,
|
2019-01-16 07:39:01 -06:00
|
|
|
syntax: &SyntaxNode,
|
|
|
|
position: FilePosition,
|
2019-10-12 12:30:53 -05:00
|
|
|
) -> Option<RangeInfo<(String, NameDefinition)>> {
|
2020-01-14 10:39:28 -06:00
|
|
|
let mut sb = SourceBinder::new(db);
|
2019-10-12 10:47:17 -05:00
|
|
|
if let Some(name) = find_node_at_offset::<ast::Name>(&syntax, position.offset) {
|
2020-01-14 10:39:28 -06:00
|
|
|
let def = classify_name(&mut sb, InFile::new(position.file_id.into(), &name))?;
|
2019-10-12 10:47:17 -05:00
|
|
|
let range = name.syntax().text_range();
|
|
|
|
return Some(RangeInfo::new(range, (name.text().to_string(), def)));
|
2019-02-17 05:38:32 -06:00
|
|
|
}
|
2019-10-12 10:47:17 -05:00
|
|
|
let name_ref = find_node_at_offset::<ast::NameRef>(&syntax, position.offset)?;
|
2020-01-14 10:39:28 -06:00
|
|
|
let def = classify_name_ref(&mut sb, InFile::new(position.file_id.into(), &name_ref))?;
|
2019-10-12 10:47:17 -05:00
|
|
|
let range = name_ref.syntax().text_range();
|
|
|
|
Some(RangeInfo::new(range, (name_ref.text().to_string(), def)))
|
2019-02-17 05:38:32 -06:00
|
|
|
}
|
|
|
|
|
2019-10-24 06:01:02 -05:00
|
|
|
fn process_definition(
|
|
|
|
db: &RootDatabase,
|
|
|
|
def: NameDefinition,
|
|
|
|
name: String,
|
|
|
|
scope: SearchScope,
|
2020-01-03 09:30:45 -06:00
|
|
|
) -> Vec<Reference> {
|
2019-10-24 02:37:20 -05:00
|
|
|
let _p = profile("process_definition");
|
|
|
|
|
2019-10-12 10:47:17 -05:00
|
|
|
let pat = name.as_str();
|
|
|
|
let mut refs = vec![];
|
|
|
|
|
2019-10-22 15:46:53 -05:00
|
|
|
for (file_id, search_range) in scope {
|
2019-10-12 10:47:17 -05:00
|
|
|
let text = db.file_text(file_id);
|
2020-01-14 11:47:02 -06:00
|
|
|
|
2019-10-14 06:59:02 -05:00
|
|
|
let parse = Lazy::new(|| SourceFile::parse(&text));
|
2020-01-14 11:47:02 -06:00
|
|
|
let mut sb = Lazy::new(|| SourceBinder::new(db));
|
2019-10-12 10:47:17 -05:00
|
|
|
|
|
|
|
for (idx, _) in text.match_indices(pat) {
|
|
|
|
let offset = TextUnit::from_usize(idx);
|
2019-10-14 06:59:02 -05:00
|
|
|
|
|
|
|
if let Some(name_ref) =
|
|
|
|
find_node_at_offset::<ast::NameRef>(parse.tree().syntax(), offset)
|
|
|
|
{
|
2019-10-12 10:47:17 -05:00
|
|
|
let range = name_ref.syntax().text_range();
|
2019-10-22 15:46:53 -05:00
|
|
|
if let Some(search_range) = search_range {
|
|
|
|
if !range.is_subrange(&search_range) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2020-01-14 10:39:28 -06:00
|
|
|
// FIXME: reuse sb
|
2020-01-14 10:52:42 -06:00
|
|
|
// See https://github.com/rust-lang/rust/pull/68198#issuecomment-574269098
|
2020-01-14 11:47:02 -06:00
|
|
|
|
2020-01-14 10:39:28 -06:00
|
|
|
if let Some(d) = classify_name_ref(&mut sb, InFile::new(file_id.into(), &name_ref))
|
|
|
|
{
|
2019-10-22 15:46:53 -05:00
|
|
|
if d == def {
|
2020-01-03 09:30:45 -06:00
|
|
|
let kind = if name_ref
|
|
|
|
.syntax()
|
|
|
|
.ancestors()
|
|
|
|
.find_map(ast::RecordLit::cast)
|
|
|
|
.and_then(|l| l.path())
|
|
|
|
.and_then(|p| p.segment())
|
|
|
|
.and_then(|p| p.name_ref())
|
|
|
|
.map(|n| n == name_ref)
|
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
|
|
|
ReferenceKind::StructLiteral
|
|
|
|
} else {
|
|
|
|
ReferenceKind::Other
|
|
|
|
};
|
2020-01-04 16:46:01 -06:00
|
|
|
|
|
|
|
refs.push(Reference {
|
|
|
|
file_range: FileRange { file_id, range },
|
|
|
|
kind,
|
2020-01-10 13:56:58 -06:00
|
|
|
access: reference_access(&d.kind, &name_ref),
|
2020-01-04 16:46:01 -06:00
|
|
|
});
|
2019-10-12 10:47:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-16 07:39:01 -06:00
|
|
|
}
|
2019-10-22 15:46:53 -05:00
|
|
|
refs
|
2019-01-16 07:39:01 -06:00
|
|
|
}
|
2019-01-18 02:29:09 -06:00
|
|
|
|
2020-01-10 13:56:58 -06:00
|
|
|
fn decl_access(
|
|
|
|
kind: &NameKind,
|
|
|
|
name: &str,
|
|
|
|
syntax: &SyntaxNode,
|
|
|
|
range: TextRange,
|
|
|
|
) -> Option<ReferenceAccess> {
|
|
|
|
match kind {
|
|
|
|
NameKind::Local(_) | NameKind::Field(_) => {}
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let stmt = find_node_at_offset::<ast::LetStmt>(syntax, range.start())?;
|
|
|
|
if let Some(_) = stmt.initializer() {
|
|
|
|
let pat = stmt.pat()?;
|
2020-01-13 10:27:06 -06:00
|
|
|
if let ast::Pat::BindPat(it) = pat {
|
|
|
|
if it.name()?.text().as_str() == name {
|
|
|
|
return Some(ReferenceAccess::Write);
|
2020-01-10 13:56:58 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn reference_access(kind: &NameKind, name_ref: &ast::NameRef) -> Option<ReferenceAccess> {
|
2020-01-09 15:01:43 -06:00
|
|
|
// Only Locals and Fields have accesses for now.
|
2020-01-04 16:46:01 -06:00
|
|
|
match kind {
|
2020-01-09 15:01:43 -06:00
|
|
|
NameKind::Local(_) | NameKind::Field(_) => {}
|
|
|
|
_ => return None,
|
|
|
|
};
|
2020-01-04 18:25:29 -06:00
|
|
|
|
2020-01-09 15:01:43 -06:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2020-01-04 16:46:01 -06:00
|
|
|
}
|
2020-01-13 10:27:06 -06:00
|
|
|
Some(ReferenceAccess::Read)
|
2020-01-09 15:01:43 -06:00
|
|
|
},
|
|
|
|
_ => {None}
|
|
|
|
}
|
2020-01-04 16:46:01 -06:00
|
|
|
}
|
2020-01-09 15:01:43 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
// Default Locals and Fields to read
|
|
|
|
mode.or(Some(ReferenceAccess::Read))
|
2020-01-04 16:46:01 -06:00
|
|
|
}
|
|
|
|
|
2019-01-18 02:29:09 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-07-04 15:05:17 -05:00
|
|
|
use crate::{
|
2019-10-24 06:01:02 -05:00
|
|
|
mock_analysis::{analysis_and_position, single_file_with_position, MockAnalysis},
|
2020-01-09 15:27:10 -06:00
|
|
|
Declaration, Reference, ReferenceSearchResult, SearchScope,
|
2019-07-04 15:05:17 -05:00
|
|
|
};
|
2019-01-18 02:29:09 -06:00
|
|
|
|
2020-01-03 09:30:45 -06:00
|
|
|
#[test]
|
|
|
|
fn test_struct_literal() {
|
|
|
|
let code = r#"
|
|
|
|
struct Foo <|>{
|
|
|
|
a: i32,
|
|
|
|
}
|
|
|
|
impl Foo {
|
|
|
|
fn f() -> i32 { 42 }
|
2020-01-14 10:24:00 -06:00
|
|
|
}
|
2020-01-03 09:30:45 -06:00
|
|
|
fn main() {
|
|
|
|
let f: Foo;
|
|
|
|
f = Foo {a: Foo::f()};
|
|
|
|
}"#;
|
|
|
|
|
|
|
|
let refs = get_all_refs(code);
|
2020-01-08 15:35:58 -06:00
|
|
|
check_result(
|
|
|
|
refs,
|
2020-01-09 15:27:10 -06:00
|
|
|
"Foo STRUCT_DEF FileId(1) [5; 39) [12; 15) Other",
|
2020-01-14 10:24:00 -06:00
|
|
|
&["FileId(1) [138; 141) StructLiteral"],
|
2020-01-08 15:35:58 -06:00
|
|
|
);
|
2020-01-03 09:30:45 -06:00
|
|
|
}
|
|
|
|
|
2019-03-25 15:03:32 -05:00
|
|
|
#[test]
|
|
|
|
fn test_find_all_refs_for_local() {
|
|
|
|
let code = r#"
|
|
|
|
fn main() {
|
|
|
|
let mut i = 1;
|
|
|
|
let j = 1;
|
|
|
|
i = i<|> + j;
|
|
|
|
|
|
|
|
{
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = 5;
|
|
|
|
}"#;
|
|
|
|
|
|
|
|
let refs = get_all_refs(code);
|
2020-01-08 15:35:58 -06:00
|
|
|
check_result(
|
|
|
|
refs,
|
2020-01-10 13:56:58 -06:00
|
|
|
"i BIND_PAT FileId(1) [33; 34) Other Write",
|
2020-01-08 15:35:58 -06:00
|
|
|
&[
|
2020-01-09 15:01:43 -06:00
|
|
|
"FileId(1) [67; 68) Other Write",
|
|
|
|
"FileId(1) [71; 72) Other Read",
|
|
|
|
"FileId(1) [101; 102) Other Write",
|
|
|
|
"FileId(1) [127; 128) Other Write",
|
2020-01-08 15:35:58 -06:00
|
|
|
],
|
|
|
|
);
|
2019-03-25 15:03:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_find_all_refs_for_param_inside() {
|
|
|
|
let code = r#"
|
|
|
|
fn foo(i : u32) -> u32 {
|
|
|
|
i<|>
|
|
|
|
}"#;
|
|
|
|
|
|
|
|
let refs = get_all_refs(code);
|
2020-01-08 15:35:58 -06:00
|
|
|
check_result(
|
|
|
|
refs,
|
2020-01-09 15:27:10 -06:00
|
|
|
"i BIND_PAT FileId(1) [12; 13) Other",
|
2020-01-09 15:01:43 -06:00
|
|
|
&["FileId(1) [38; 39) Other Read"],
|
2020-01-08 15:35:58 -06:00
|
|
|
);
|
2019-03-25 15:03:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_find_all_refs_for_fn_param() {
|
|
|
|
let code = r#"
|
|
|
|
fn foo(i<|> : u32) -> u32 {
|
|
|
|
i
|
|
|
|
}"#;
|
|
|
|
|
|
|
|
let refs = get_all_refs(code);
|
2020-01-08 15:35:58 -06:00
|
|
|
check_result(
|
|
|
|
refs,
|
2020-01-09 15:27:10 -06:00
|
|
|
"i BIND_PAT FileId(1) [12; 13) Other",
|
2020-01-09 15:01:43 -06:00
|
|
|
&["FileId(1) [38; 39) Other Read"],
|
2020-01-08 15:35:58 -06:00
|
|
|
);
|
2019-03-25 15:03:32 -05:00
|
|
|
}
|
|
|
|
|
2019-09-14 06:38:10 -05:00
|
|
|
#[test]
|
|
|
|
fn test_find_all_refs_field_name() {
|
|
|
|
let code = r#"
|
|
|
|
//- /lib.rs
|
|
|
|
struct Foo {
|
2019-10-10 10:51:51 -05:00
|
|
|
pub spam<|>: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main(s: Foo) {
|
|
|
|
let f = s.spam;
|
2019-09-14 06:38:10 -05:00
|
|
|
}
|
|
|
|
"#;
|
|
|
|
|
|
|
|
let refs = get_all_refs(code);
|
2020-01-08 15:35:58 -06:00
|
|
|
check_result(
|
|
|
|
refs,
|
2020-01-09 15:27:10 -06:00
|
|
|
"spam RECORD_FIELD_DEF FileId(1) [66; 79) [70; 74) Other",
|
2020-01-09 15:01:43 -06:00
|
|
|
&["FileId(1) [152; 156) Other Read"],
|
2020-01-08 15:35:58 -06:00
|
|
|
);
|
2019-09-14 06:38:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_find_all_refs_impl_item_name() {
|
|
|
|
let code = r#"
|
|
|
|
//- /lib.rs
|
|
|
|
struct Foo;
|
|
|
|
impl Foo {
|
|
|
|
fn f<|>(&self) { }
|
|
|
|
}
|
|
|
|
"#;
|
|
|
|
|
|
|
|
let refs = get_all_refs(code);
|
2020-01-09 15:27:10 -06:00
|
|
|
check_result(refs, "f FN_DEF FileId(1) [88; 104) [91; 92) Other", &[]);
|
2019-09-14 06:38:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_find_all_refs_enum_var_name() {
|
|
|
|
let code = r#"
|
|
|
|
//- /lib.rs
|
|
|
|
enum Foo {
|
|
|
|
A,
|
|
|
|
B<|>,
|
|
|
|
C,
|
|
|
|
}
|
|
|
|
"#;
|
|
|
|
|
|
|
|
let refs = get_all_refs(code);
|
2020-01-09 15:27:10 -06:00
|
|
|
check_result(refs, "B ENUM_VARIANT FileId(1) [83; 84) [83; 84) Other", &[]);
|
2019-09-14 06:38:10 -05:00
|
|
|
}
|
|
|
|
|
2019-10-10 18:11:23 -05:00
|
|
|
#[test]
|
2019-10-15 14:50:28 -05:00
|
|
|
fn test_find_all_refs_two_modules() {
|
2019-10-10 18:11:23 -05:00
|
|
|
let code = r#"
|
|
|
|
//- /lib.rs
|
|
|
|
pub mod foo;
|
|
|
|
pub mod bar;
|
|
|
|
|
|
|
|
fn f() {
|
|
|
|
let i = foo::Foo { n: 5 };
|
|
|
|
}
|
|
|
|
|
|
|
|
//- /foo.rs
|
|
|
|
use crate::bar;
|
|
|
|
|
|
|
|
pub struct Foo {
|
|
|
|
pub n: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn f() {
|
|
|
|
let i = bar::Bar { n: 5 };
|
|
|
|
}
|
|
|
|
|
|
|
|
//- /bar.rs
|
|
|
|
use crate::foo;
|
|
|
|
|
|
|
|
pub struct Bar {
|
|
|
|
pub n: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn f() {
|
|
|
|
let i = foo::Foo<|> { n: 5 };
|
|
|
|
}
|
|
|
|
"#;
|
|
|
|
|
|
|
|
let (analysis, pos) = analysis_and_position(code);
|
2019-10-24 06:01:02 -05:00
|
|
|
let refs = analysis.find_all_refs(pos, None).unwrap().unwrap();
|
2020-01-08 15:35:58 -06:00
|
|
|
check_result(
|
|
|
|
refs,
|
2020-01-09 15:27:10 -06:00
|
|
|
"Foo STRUCT_DEF FileId(2) [16; 50) [27; 30) Other",
|
2020-01-08 15:35:58 -06:00
|
|
|
&["FileId(1) [52; 55) StructLiteral", "FileId(3) [77; 80) StructLiteral"],
|
|
|
|
);
|
2019-10-10 18:11:23 -05:00
|
|
|
}
|
|
|
|
|
2019-10-15 14:50:28 -05:00
|
|
|
// `mod foo;` is not in the results because `foo` is an `ast::Name`.
|
2019-10-16 08:49:35 -05:00
|
|
|
// So, there are two references: the first one is a definition of the `foo` module,
|
2019-10-15 14:50:28 -05:00
|
|
|
// which is the whole `foo.rs`, and the second one is in `use foo::Foo`.
|
|
|
|
#[test]
|
|
|
|
fn test_find_all_refs_decl_module() {
|
|
|
|
let code = r#"
|
|
|
|
//- /lib.rs
|
|
|
|
mod foo<|>;
|
|
|
|
|
|
|
|
use foo::Foo;
|
|
|
|
|
|
|
|
fn f() {
|
|
|
|
let i = Foo { n: 5 };
|
|
|
|
}
|
|
|
|
|
|
|
|
//- /foo.rs
|
|
|
|
pub struct Foo {
|
|
|
|
pub n: u32,
|
|
|
|
}
|
|
|
|
"#;
|
|
|
|
|
|
|
|
let (analysis, pos) = analysis_and_position(code);
|
2019-10-24 06:01:02 -05:00
|
|
|
let refs = analysis.find_all_refs(pos, None).unwrap().unwrap();
|
2020-01-08 15:35:58 -06:00
|
|
|
check_result(
|
|
|
|
refs,
|
2020-01-09 15:27:10 -06:00
|
|
|
"foo SOURCE_FILE FileId(2) [0; 35) Other",
|
2020-01-08 15:35:58 -06:00
|
|
|
&["FileId(1) [13; 16) Other"],
|
|
|
|
);
|
2019-10-15 14:50:28 -05:00
|
|
|
}
|
|
|
|
|
2019-10-16 08:49:35 -05:00
|
|
|
#[test]
|
|
|
|
fn test_find_all_refs_super_mod_vis() {
|
|
|
|
let code = r#"
|
|
|
|
//- /lib.rs
|
|
|
|
mod foo;
|
|
|
|
|
|
|
|
//- /foo.rs
|
|
|
|
mod some;
|
|
|
|
use some::Foo;
|
|
|
|
|
|
|
|
fn f() {
|
|
|
|
let i = Foo { n: 5 };
|
|
|
|
}
|
|
|
|
|
|
|
|
//- /foo/some.rs
|
|
|
|
pub(super) struct Foo<|> {
|
|
|
|
pub n: u32,
|
|
|
|
}
|
|
|
|
"#;
|
|
|
|
|
|
|
|
let (analysis, pos) = analysis_and_position(code);
|
2019-10-24 06:01:02 -05:00
|
|
|
let refs = analysis.find_all_refs(pos, None).unwrap().unwrap();
|
2020-01-08 15:35:58 -06:00
|
|
|
check_result(
|
|
|
|
refs,
|
2020-01-09 15:27:10 -06:00
|
|
|
"Foo STRUCT_DEF FileId(3) [0; 41) [18; 21) Other",
|
2020-01-08 15:35:58 -06:00
|
|
|
&["FileId(2) [20; 23) Other", "FileId(2) [46; 49) StructLiteral"],
|
|
|
|
);
|
2019-10-24 06:01:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_find_all_refs_with_scope() {
|
|
|
|
let code = r#"
|
|
|
|
//- /lib.rs
|
|
|
|
mod foo;
|
|
|
|
mod bar;
|
|
|
|
|
|
|
|
pub fn quux<|>() {}
|
|
|
|
|
|
|
|
//- /foo.rs
|
|
|
|
fn f() { super::quux(); }
|
|
|
|
|
|
|
|
//- /bar.rs
|
|
|
|
fn f() { super::quux(); }
|
|
|
|
"#;
|
|
|
|
|
|
|
|
let (mock, pos) = MockAnalysis::with_files_and_position(code);
|
|
|
|
let bar = mock.id_of("/bar.rs");
|
|
|
|
let analysis = mock.analysis();
|
|
|
|
|
|
|
|
let refs = analysis.find_all_refs(pos, None).unwrap().unwrap();
|
2020-01-08 15:35:58 -06:00
|
|
|
check_result(
|
|
|
|
refs,
|
2020-01-09 15:27:10 -06:00
|
|
|
"quux FN_DEF FileId(1) [18; 34) [25; 29) Other",
|
2020-01-08 15:35:58 -06:00
|
|
|
&["FileId(2) [16; 20) Other", "FileId(3) [16; 20) Other"],
|
|
|
|
);
|
2019-10-24 06:01:02 -05:00
|
|
|
|
|
|
|
let refs =
|
|
|
|
analysis.find_all_refs(pos, Some(SearchScope::single_file(bar))).unwrap().unwrap();
|
2020-01-08 15:35:58 -06:00
|
|
|
check_result(
|
|
|
|
refs,
|
2020-01-09 15:27:10 -06:00
|
|
|
"quux FN_DEF FileId(1) [18; 34) [25; 29) Other",
|
2020-01-08 15:35:58 -06:00
|
|
|
&["FileId(3) [16; 20) Other"],
|
|
|
|
);
|
2019-10-16 08:49:35 -05:00
|
|
|
}
|
|
|
|
|
2019-11-15 16:13:52 -06:00
|
|
|
#[test]
|
|
|
|
fn test_find_all_refs_macro_def() {
|
|
|
|
let code = r#"
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! m1<|> { () => (()) }
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
m1();
|
|
|
|
m1();
|
|
|
|
}"#;
|
|
|
|
|
|
|
|
let refs = get_all_refs(code);
|
2020-01-08 15:35:58 -06:00
|
|
|
check_result(
|
|
|
|
refs,
|
2020-01-09 15:27:10 -06:00
|
|
|
"m1 MACRO_CALL FileId(1) [9; 63) [46; 48) Other",
|
2020-01-08 15:35:58 -06:00
|
|
|
&["FileId(1) [96; 98) Other", "FileId(1) [114; 116) Other"],
|
|
|
|
);
|
2019-11-15 16:13:52 -06:00
|
|
|
}
|
|
|
|
|
2020-01-04 16:46:01 -06:00
|
|
|
#[test]
|
2020-01-04 18:25:29 -06:00
|
|
|
fn test_basic_highlight_read_write() {
|
2020-01-04 16:46:01 -06:00
|
|
|
let code = r#"
|
|
|
|
fn foo() {
|
|
|
|
let i<|> = 0;
|
|
|
|
i = i + 1;
|
|
|
|
}"#;
|
|
|
|
|
|
|
|
let refs = get_all_refs(code);
|
2020-01-09 15:01:43 -06:00
|
|
|
check_result(
|
|
|
|
refs,
|
2020-01-10 13:56:58 -06:00
|
|
|
"i BIND_PAT FileId(1) [36; 37) Other Write",
|
2020-01-09 15:01:43 -06:00
|
|
|
&["FileId(1) [55; 56) Other Write", "FileId(1) [59; 60) Other Read"],
|
|
|
|
);
|
2020-01-04 16:46:01 -06:00
|
|
|
}
|
|
|
|
|
2020-01-04 18:25:29 -06:00
|
|
|
#[test]
|
|
|
|
fn test_basic_highlight_field_read_write() {
|
|
|
|
let code = r#"
|
|
|
|
struct S {
|
|
|
|
f: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
let mut s = S{f: 0};
|
|
|
|
s.f<|> = 0;
|
|
|
|
}"#;
|
|
|
|
|
|
|
|
let refs = get_all_refs(code);
|
2020-01-09 15:01:43 -06:00
|
|
|
check_result(
|
|
|
|
refs,
|
2020-01-09 15:27:10 -06:00
|
|
|
"f RECORD_FIELD_DEF FileId(1) [32; 38) [32; 33) Other",
|
2020-01-09 15:01:43 -06:00
|
|
|
&["FileId(1) [96; 97) Other Read", "FileId(1) [117; 118) Other Write"],
|
|
|
|
);
|
2020-01-04 18:25:29 -06:00
|
|
|
}
|
|
|
|
|
2020-01-10 13:56:58 -06:00
|
|
|
#[test]
|
|
|
|
fn test_basic_highlight_decl_no_write() {
|
|
|
|
let code = r#"
|
|
|
|
fn foo() {
|
|
|
|
let i<|>;
|
|
|
|
i = 1;
|
|
|
|
}"#;
|
|
|
|
|
|
|
|
let refs = get_all_refs(code);
|
|
|
|
check_result(
|
|
|
|
refs,
|
|
|
|
"i BIND_PAT FileId(1) [36; 37) Other",
|
|
|
|
&["FileId(1) [51; 52) Other Write"],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-25 15:03:32 -05:00
|
|
|
fn get_all_refs(text: &str) -> ReferenceSearchResult {
|
|
|
|
let (analysis, position) = single_file_with_position(text);
|
2019-10-24 06:01:02 -05:00
|
|
|
analysis.find_all_refs(position, None).unwrap().unwrap()
|
2019-03-25 15:03:32 -05:00
|
|
|
}
|
2020-01-08 15:35:58 -06:00
|
|
|
|
2020-01-09 15:27:10 -06:00
|
|
|
fn check_result(res: ReferenceSearchResult, expected_decl: &str, expected_refs: &[&str]) {
|
2020-01-08 15:35:58 -06:00
|
|
|
res.declaration().assert_match(expected_decl);
|
|
|
|
assert_eq!(res.references.len(), expected_refs.len());
|
|
|
|
res.references().iter().enumerate().for_each(|(i, r)| r.assert_match(expected_refs[i]));
|
|
|
|
}
|
|
|
|
|
2020-01-09 15:27:10 -06:00
|
|
|
impl Declaration {
|
|
|
|
fn debug_render(&self) -> String {
|
|
|
|
let mut s = format!("{} {:?}", self.nav.debug_render(), self.kind);
|
|
|
|
if let Some(access) = self.access {
|
|
|
|
s.push_str(&format!(" {:?}", access));
|
|
|
|
}
|
|
|
|
s
|
|
|
|
}
|
|
|
|
|
|
|
|
fn assert_match(&self, expected: &str) {
|
|
|
|
let actual = self.debug_render();
|
|
|
|
test_utils::assert_eq_text!(expected.trim(), actual.trim(),);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-08 15:35:58 -06:00
|
|
|
impl Reference {
|
2020-01-09 06:26:04 -06:00
|
|
|
fn debug_render(&self) -> String {
|
2020-01-09 15:01:43 -06:00
|
|
|
let mut s = format!(
|
|
|
|
"{:?} {:?} {:?}",
|
|
|
|
self.file_range.file_id, self.file_range.range, self.kind
|
|
|
|
);
|
|
|
|
if let Some(access) = self.access {
|
|
|
|
s.push_str(&format!(" {:?}", access));
|
|
|
|
}
|
|
|
|
s
|
2020-01-08 15:35:58 -06:00
|
|
|
}
|
|
|
|
|
2020-01-09 06:26:04 -06:00
|
|
|
fn assert_match(&self, expected: &str) {
|
2020-01-08 15:35:58 -06:00
|
|
|
let actual = self.debug_render();
|
|
|
|
test_utils::assert_eq_text!(expected.trim(), actual.trim(),);
|
|
|
|
}
|
|
|
|
}
|
2019-01-18 02:29:09 -06:00
|
|
|
}
|