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 rename;
|
|
|
|
|
2020-02-18 11:35:10 -06:00
|
|
|
use hir::Semantics;
|
2020-03-03 11:54:39 -06:00
|
|
|
use ra_ide_db::{
|
|
|
|
defs::{classify_name, classify_name_ref, Definition},
|
2020-03-04 05:46:40 -06:00
|
|
|
search::SearchScope,
|
2020-03-03 11:54:39 -06:00
|
|
|
RootDatabase,
|
|
|
|
};
|
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},
|
2020-03-04 05:14:48 -06:00
|
|
|
AstNode, SyntaxKind, SyntaxNode, TextRange, TokenAtOffset,
|
2020-01-03 09:30:45 -06:00
|
|
|
};
|
2019-10-12 10:47:17 -05:00
|
|
|
|
2020-02-22 09:57:29 -06:00
|
|
|
use crate::{display::TryToNav, FilePosition, FileRange, NavigationTarget, RangeInfo};
|
2019-10-12 10:47:17 -05:00
|
|
|
|
2020-03-03 11:50:15 -06:00
|
|
|
pub(crate) use self::rename::rename;
|
2019-02-08 05:06:26 -06:00
|
|
|
|
2020-03-04 05:46:40 -06:00
|
|
|
pub use ra_ide_db::search::{Reference, ReferenceAccess, ReferenceKind};
|
2019-10-24 06:01:02 -05:00
|
|
|
|
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>,
|
|
|
|
}
|
|
|
|
|
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-07-17 05:42:48 -05:00
|
|
|
file_id: self.declaration.nav.file_id,
|
|
|
|
range: self.declaration.nav.focus_or_full_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(
|
2020-07-01 07:11:34 -05:00
|
|
|
sema: &Semantics<RootDatabase>,
|
2020-01-18 06:17:34 -06:00
|
|
|
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>> {
|
2020-03-03 09:30:29 -06:00
|
|
|
let _p = profile("find_all_refs");
|
2020-02-18 11:35:10 -06:00
|
|
|
let syntax = sema.parse(position.file_id).syntax().clone();
|
2020-01-03 09:30:45 -06:00
|
|
|
|
2020-03-22 06:53:28 -05:00
|
|
|
let (opt_name, search_kind) = if let Some(name) =
|
|
|
|
get_struct_def_name_for_struct_literal_search(&sema, &syntax, position)
|
|
|
|
{
|
|
|
|
(Some(name), ReferenceKind::StructLiteral)
|
|
|
|
} else {
|
|
|
|
(
|
|
|
|
sema.find_node_at_offset_with_descend::<ast::Name>(&syntax, position.offset),
|
|
|
|
ReferenceKind::Other,
|
|
|
|
)
|
|
|
|
};
|
2020-01-03 09:30:45 -06:00
|
|
|
|
2020-03-03 11:22:52 -06:00
|
|
|
let RangeInfo { range, info: def } = find_name(&sema, &syntax, position, opt_name)?;
|
2019-02-08 05:06:26 -06:00
|
|
|
|
2020-03-04 05:17:41 -06:00
|
|
|
let references = def
|
2020-07-01 07:11:34 -05:00
|
|
|
.find_usages(sema, search_scope)
|
2020-03-03 11:22:52 -06:00
|
|
|
.into_iter()
|
|
|
|
.filter(|r| search_kind == ReferenceKind::Other || search_kind == r.kind)
|
|
|
|
.collect();
|
|
|
|
|
2020-07-17 05:42:48 -05:00
|
|
|
let decl_range = def.try_to_nav(sema.db)?.focus_or_full_range();
|
2020-03-03 11:22:52 -06:00
|
|
|
|
|
|
|
let declaration = Declaration {
|
2020-07-01 07:11:34 -05:00
|
|
|
nav: def.try_to_nav(sema.db)?,
|
2020-03-03 11:22:52 -06:00
|
|
|
kind: ReferenceKind::Other,
|
|
|
|
access: decl_access(&def, &syntax, decl_range),
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(RangeInfo::new(range, ReferenceSearchResult { declaration, references }))
|
|
|
|
}
|
|
|
|
|
2020-03-04 05:07:44 -06:00
|
|
|
fn find_name(
|
|
|
|
sema: &Semantics<RootDatabase>,
|
|
|
|
syntax: &SyntaxNode,
|
|
|
|
position: FilePosition,
|
|
|
|
opt_name: Option<ast::Name>,
|
|
|
|
) -> Option<RangeInfo<Definition>> {
|
|
|
|
if let Some(name) = opt_name {
|
|
|
|
let def = classify_name(sema, &name)?.definition();
|
|
|
|
let range = name.syntax().text_range();
|
|
|
|
return Some(RangeInfo::new(range, def));
|
|
|
|
}
|
2020-03-22 06:53:28 -05:00
|
|
|
let name_ref =
|
|
|
|
sema.find_node_at_offset_with_descend::<ast::NameRef>(&syntax, position.offset)?;
|
2020-03-04 05:07:44 -06:00
|
|
|
let def = classify_name_ref(sema, &name_ref)?.definition();
|
|
|
|
let range = name_ref.syntax().text_range();
|
|
|
|
Some(RangeInfo::new(range, def))
|
|
|
|
}
|
|
|
|
|
2020-03-03 11:36:39 -06:00
|
|
|
fn decl_access(def: &Definition, syntax: &SyntaxNode, range: TextRange) -> Option<ReferenceAccess> {
|
2020-02-19 07:56:22 -06:00
|
|
|
match def {
|
2020-04-25 07:23:34 -05:00
|
|
|
Definition::Local(_) | Definition::Field(_) => {}
|
2020-01-10 13:56:58 -06:00
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let stmt = find_node_at_offset::<ast::LetStmt>(syntax, range.start())?;
|
2020-02-18 07:32:19 -06:00
|
|
|
if stmt.initializer().is_some() {
|
2020-01-10 13:56:58 -06:00
|
|
|
let pat = stmt.pat()?;
|
2020-01-13 10:27:06 -06:00
|
|
|
if let ast::Pat::BindPat(it) = pat {
|
2020-04-09 16:35:05 -05:00
|
|
|
if it.mut_token().is_some() {
|
2020-01-13 10:27:06 -06:00
|
|
|
return Some(ReferenceAccess::Write);
|
2020-01-10 13:56:58 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2020-03-17 05:15:30 -05:00
|
|
|
fn get_struct_def_name_for_struct_literal_search(
|
2020-03-22 06:53:28 -05:00
|
|
|
sema: &Semantics<RootDatabase>,
|
2020-01-18 06:17:34 -06:00
|
|
|
syntax: &SyntaxNode,
|
|
|
|
position: FilePosition,
|
|
|
|
) -> Option<ast::Name> {
|
|
|
|
if let TokenAtOffset::Between(ref left, ref right) = syntax.token_at_offset(position.offset) {
|
|
|
|
if right.kind() != SyntaxKind::L_CURLY && right.kind() != SyntaxKind::L_PAREN {
|
|
|
|
return None;
|
|
|
|
}
|
2020-03-22 06:53:28 -05:00
|
|
|
if let Some(name) =
|
|
|
|
sema.find_node_at_offset_with_descend::<ast::Name>(&syntax, left.text_range().start())
|
|
|
|
{
|
2020-07-30 10:50:40 -05:00
|
|
|
return name.syntax().ancestors().find_map(ast::Struct::cast).and_then(|l| l.name());
|
2020-01-18 06:17:34 -06:00
|
|
|
}
|
2020-03-22 06:53:28 -05:00
|
|
|
if sema
|
2020-07-30 08:36:21 -05:00
|
|
|
.find_node_at_offset_with_descend::<ast::GenericParamList>(
|
2020-03-22 06:53:28 -05:00
|
|
|
&syntax,
|
|
|
|
left.text_range().start(),
|
|
|
|
)
|
|
|
|
.is_some()
|
|
|
|
{
|
2020-07-30 10:50:40 -05:00
|
|
|
return left.ancestors().find_map(ast::Struct::cast).and_then(|l| l.name());
|
2020-01-18 06:17:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2019-01-18 02:29:09 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-07-04 15:05:17 -05:00
|
|
|
use crate::{
|
2020-06-24 04:31:30 -05:00
|
|
|
mock_analysis::{analysis_and_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]
|
2020-01-18 06:17:34 -06:00
|
|
|
fn test_struct_literal_after_space() {
|
2020-06-24 04:29:43 -05:00
|
|
|
let refs = get_all_refs(
|
|
|
|
r#"
|
|
|
|
struct Foo <|>{
|
|
|
|
a: i32,
|
|
|
|
}
|
|
|
|
impl Foo {
|
|
|
|
fn f() -> i32 { 42 }
|
|
|
|
}
|
|
|
|
fn main() {
|
|
|
|
let f: Foo;
|
|
|
|
f = Foo {a: Foo::f()};
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
2020-01-08 15:35:58 -06:00
|
|
|
check_result(
|
|
|
|
refs,
|
2020-07-30 10:50:40 -05:00
|
|
|
"Foo STRUCT FileId(1) 0..26 7..10 Other",
|
2020-06-24 04:29:43 -05:00
|
|
|
&["FileId(1) 101..104 StructLiteral"],
|
2020-01-08 15:35:58 -06:00
|
|
|
);
|
2020-01-03 09:30:45 -06:00
|
|
|
}
|
|
|
|
|
2020-01-18 06:17:34 -06:00
|
|
|
#[test]
|
2020-06-24 04:29:43 -05:00
|
|
|
fn test_struct_literal_before_space() {
|
|
|
|
let refs = get_all_refs(
|
|
|
|
r#"
|
|
|
|
struct Foo<|> {}
|
|
|
|
fn main() {
|
|
|
|
let f: Foo;
|
|
|
|
f = Foo {};
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
2020-01-18 06:17:34 -06:00
|
|
|
check_result(
|
|
|
|
refs,
|
2020-07-30 10:50:40 -05:00
|
|
|
"Foo STRUCT FileId(1) 0..13 7..10 Other",
|
2020-06-24 04:29:43 -05:00
|
|
|
&["FileId(1) 41..44 Other", "FileId(1) 54..57 StructLiteral"],
|
2020-01-18 06:17:34 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_struct_literal_with_generic_type() {
|
2020-06-24 04:29:43 -05:00
|
|
|
let refs = get_all_refs(
|
|
|
|
r#"
|
|
|
|
struct Foo<T> <|>{}
|
|
|
|
fn main() {
|
|
|
|
let f: Foo::<i32>;
|
|
|
|
f = Foo {};
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
2020-01-18 06:17:34 -06:00
|
|
|
check_result(
|
|
|
|
refs,
|
2020-07-30 10:50:40 -05:00
|
|
|
"Foo STRUCT FileId(1) 0..16 7..10 Other",
|
2020-06-24 04:29:43 -05:00
|
|
|
&["FileId(1) 64..67 StructLiteral"],
|
2020-01-18 06:17:34 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_struct_literal_for_tuple() {
|
2020-06-24 04:29:43 -05:00
|
|
|
let refs = get_all_refs(
|
|
|
|
r#"
|
|
|
|
struct Foo<|>(i32);
|
2020-01-18 06:17:34 -06:00
|
|
|
|
2020-06-24 04:29:43 -05:00
|
|
|
fn main() {
|
|
|
|
let f: Foo;
|
|
|
|
f = Foo(1);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
2020-01-18 06:17:34 -06:00
|
|
|
check_result(
|
|
|
|
refs,
|
2020-07-30 10:50:40 -05:00
|
|
|
"Foo STRUCT FileId(1) 0..16 7..10 Other",
|
2020-06-24 04:29:43 -05:00
|
|
|
&["FileId(1) 54..57 StructLiteral"],
|
2020-01-18 06:17:34 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-25 15:03:32 -05:00
|
|
|
#[test]
|
|
|
|
fn test_find_all_refs_for_local() {
|
2020-06-24 04:29:43 -05:00
|
|
|
let refs = get_all_refs(
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
let mut i = 1;
|
|
|
|
let j = 1;
|
|
|
|
i = i<|> + j;
|
2019-03-25 15:03:32 -05:00
|
|
|
|
2020-06-24 04:29:43 -05:00
|
|
|
{
|
|
|
|
i = 0;
|
|
|
|
}
|
2019-03-25 15:03:32 -05:00
|
|
|
|
2020-06-24 04:29:43 -05:00
|
|
|
i = 5;
|
|
|
|
}"#,
|
|
|
|
);
|
2020-01-08 15:35:58 -06:00
|
|
|
check_result(
|
|
|
|
refs,
|
2020-06-24 04:29:43 -05:00
|
|
|
"i BIND_PAT FileId(1) 24..25 Other Write",
|
2020-01-08 15:35:58 -06:00
|
|
|
&[
|
2020-06-24 04:29:43 -05:00
|
|
|
"FileId(1) 50..51 Other Write",
|
|
|
|
"FileId(1) 54..55 Other Read",
|
|
|
|
"FileId(1) 76..77 Other Write",
|
|
|
|
"FileId(1) 94..95 Other Write",
|
2020-01-08 15:35:58 -06:00
|
|
|
],
|
|
|
|
);
|
2019-03-25 15:03:32 -05:00
|
|
|
}
|
|
|
|
|
2020-02-18 11:35:10 -06:00
|
|
|
#[test]
|
|
|
|
fn search_filters_by_range() {
|
2020-06-24 04:29:43 -05:00
|
|
|
let refs = get_all_refs(
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let spam<|> = 92;
|
|
|
|
spam + spam
|
|
|
|
}
|
|
|
|
fn bar() {
|
|
|
|
let spam = 92;
|
|
|
|
spam + spam
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
2020-02-18 11:35:10 -06:00
|
|
|
check_result(
|
|
|
|
refs,
|
2020-06-24 04:29:43 -05:00
|
|
|
"spam BIND_PAT FileId(1) 19..23 Other",
|
|
|
|
&["FileId(1) 34..38 Other Read", "FileId(1) 41..45 Other Read"],
|
2020-02-18 11:35:10 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-25 15:03:32 -05:00
|
|
|
#[test]
|
|
|
|
fn test_find_all_refs_for_param_inside() {
|
2020-06-24 04:29:43 -05:00
|
|
|
let refs = get_all_refs(
|
|
|
|
r#"
|
|
|
|
fn foo(i : u32) -> u32 {
|
|
|
|
i<|>
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
check_result(refs, "i BIND_PAT FileId(1) 7..8 Other", &["FileId(1) 29..30 Other Read"]);
|
2019-03-25 15:03:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_find_all_refs_for_fn_param() {
|
2020-06-24 04:29:43 -05:00
|
|
|
let refs = get_all_refs(
|
|
|
|
r#"
|
|
|
|
fn foo(i<|> : u32) -> u32 {
|
|
|
|
i
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
check_result(refs, "i BIND_PAT FileId(1) 7..8 Other", &["FileId(1) 29..30 Other Read"]);
|
2019-03-25 15:03:32 -05:00
|
|
|
}
|
|
|
|
|
2019-09-14 06:38:10 -05:00
|
|
|
#[test]
|
|
|
|
fn test_find_all_refs_field_name() {
|
2020-06-24 04:29:43 -05:00
|
|
|
let refs = get_all_refs(
|
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
|
|
|
struct Foo {
|
|
|
|
pub spam<|>: u32,
|
|
|
|
}
|
2019-09-14 06:38:10 -05:00
|
|
|
|
2020-06-24 04:29:43 -05:00
|
|
|
fn main(s: Foo) {
|
|
|
|
let f = s.spam;
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
2020-01-08 15:35:58 -06:00
|
|
|
check_result(
|
|
|
|
refs,
|
2020-07-30 09:49:13 -05:00
|
|
|
"spam RECORD_FIELD FileId(1) 17..30 21..25 Other",
|
2020-06-24 04:29:43 -05:00
|
|
|
&["FileId(1) 67..71 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() {
|
2020-06-24 04:29:43 -05:00
|
|
|
let refs = get_all_refs(
|
|
|
|
r#"
|
|
|
|
struct Foo;
|
|
|
|
impl Foo {
|
|
|
|
fn f<|>(&self) { }
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
2020-07-30 07:51:08 -05:00
|
|
|
check_result(refs, "f FN FileId(1) 27..43 30..31 Other", &[]);
|
2019-09-14 06:38:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_find_all_refs_enum_var_name() {
|
2020-06-24 04:29:43 -05:00
|
|
|
let refs = get_all_refs(
|
|
|
|
r#"
|
|
|
|
enum Foo {
|
|
|
|
A,
|
|
|
|
B<|>,
|
|
|
|
C,
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
2020-07-30 10:56:53 -05:00
|
|
|
check_result(refs, "B VARIANT FileId(1) 22..23 22..23 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() {
|
2020-06-24 04:29:43 -05:00
|
|
|
let (analysis, pos) = analysis_and_position(
|
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
|
|
|
pub mod foo;
|
|
|
|
pub mod bar;
|
|
|
|
|
|
|
|
fn f() {
|
|
|
|
let i = foo::Foo { n: 5 };
|
|
|
|
}
|
2019-10-10 18:11:23 -05:00
|
|
|
|
2020-06-24 04:29:43 -05:00
|
|
|
//- /foo.rs
|
|
|
|
use crate::bar;
|
2019-10-10 18:11:23 -05:00
|
|
|
|
2020-06-24 04:29:43 -05:00
|
|
|
pub struct Foo {
|
|
|
|
pub n: u32,
|
|
|
|
}
|
2019-10-10 18:11:23 -05:00
|
|
|
|
2020-06-24 04:29:43 -05:00
|
|
|
fn f() {
|
|
|
|
let i = bar::Bar { n: 5 };
|
|
|
|
}
|
2019-10-10 18:11:23 -05:00
|
|
|
|
2020-06-24 04:29:43 -05:00
|
|
|
//- /bar.rs
|
|
|
|
use crate::foo;
|
2019-10-10 18:11:23 -05:00
|
|
|
|
2020-06-24 04:29:43 -05:00
|
|
|
pub struct Bar {
|
|
|
|
pub n: u32,
|
|
|
|
}
|
2019-10-10 18:11:23 -05:00
|
|
|
|
2020-06-24 04:29:43 -05:00
|
|
|
fn f() {
|
|
|
|
let i = foo::Foo<|> { n: 5 };
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
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-07-30 10:50:40 -05:00
|
|
|
"Foo STRUCT FileId(2) 17..51 28..31 Other",
|
2020-06-23 15:27:24 -05:00
|
|
|
&["FileId(1) 53..56 StructLiteral", "FileId(3) 79..82 StructLiteral"],
|
2020-01-08 15:35:58 -06:00
|
|
|
);
|
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() {
|
2020-06-24 04:29:43 -05:00
|
|
|
let (analysis, pos) = analysis_and_position(
|
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
|
|
|
mod foo<|>;
|
2019-10-15 14:50:28 -05:00
|
|
|
|
2020-06-24 04:29:43 -05:00
|
|
|
use foo::Foo;
|
2019-10-15 14:50:28 -05:00
|
|
|
|
2020-06-24 04:29:43 -05:00
|
|
|
fn f() {
|
|
|
|
let i = Foo { n: 5 };
|
|
|
|
}
|
2019-10-15 14:50:28 -05:00
|
|
|
|
2020-06-24 04:29:43 -05:00
|
|
|
//- /foo.rs
|
|
|
|
pub struct Foo {
|
|
|
|
pub n: u32,
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
2019-10-24 06:01:02 -05:00
|
|
|
let refs = analysis.find_all_refs(pos, None).unwrap().unwrap();
|
2020-06-23 15:27:24 -05:00
|
|
|
check_result(refs, "foo SOURCE_FILE FileId(2) 0..35 Other", &["FileId(1) 14..17 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() {
|
2020-06-24 04:29:43 -05:00
|
|
|
let (analysis, pos) = analysis_and_position(
|
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
|
|
|
mod foo;
|
2019-10-16 08:49:35 -05:00
|
|
|
|
2020-06-24 04:29:43 -05:00
|
|
|
//- /foo.rs
|
|
|
|
mod some;
|
|
|
|
use some::Foo;
|
2019-10-16 08:49:35 -05:00
|
|
|
|
2020-06-24 04:29:43 -05:00
|
|
|
fn f() {
|
|
|
|
let i = Foo { n: 5 };
|
|
|
|
}
|
2019-10-16 08:49:35 -05:00
|
|
|
|
2020-06-24 04:29:43 -05:00
|
|
|
//- /foo/some.rs
|
|
|
|
pub(super) struct Foo<|> {
|
|
|
|
pub n: u32,
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
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-07-30 10:50:40 -05:00
|
|
|
"Foo STRUCT FileId(3) 0..41 18..21 Other",
|
2020-06-23 15:27:24 -05:00
|
|
|
&["FileId(2) 20..23 Other", "FileId(2) 47..50 StructLiteral"],
|
2020-01-08 15:35:58 -06:00
|
|
|
);
|
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-07-30 07:51:08 -05:00
|
|
|
"quux FN FileId(1) 19..35 26..30 Other",
|
2020-04-24 16:51:02 -05:00
|
|
|
&["FileId(2) 16..20 StructLiteral", "FileId(3) 16..20 StructLiteral"],
|
2020-01-08 15:35:58 -06:00
|
|
|
);
|
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-07-30 07:51:08 -05:00
|
|
|
"quux FN FileId(1) 19..35 26..30 Other",
|
2020-04-24 16:51:02 -05:00
|
|
|
&["FileId(3) 16..20 StructLiteral"],
|
2020-01-08 15:35:58 -06:00
|
|
|
);
|
2019-10-16 08:49:35 -05:00
|
|
|
}
|
|
|
|
|
2019-11-15 16:13:52 -06:00
|
|
|
#[test]
|
|
|
|
fn test_find_all_refs_macro_def() {
|
2020-06-24 04:29:43 -05:00
|
|
|
let refs = get_all_refs(
|
|
|
|
r#"
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! m1<|> { () => (()) }
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
m1();
|
|
|
|
m1();
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
2020-01-08 15:35:58 -06:00
|
|
|
check_result(
|
|
|
|
refs,
|
2020-06-24 04:29:43 -05:00
|
|
|
"m1 MACRO_CALL FileId(1) 0..46 29..31 Other",
|
|
|
|
&["FileId(1) 63..65 StructLiteral", "FileId(1) 73..75 StructLiteral"],
|
2020-01-08 15:35:58 -06:00
|
|
|
);
|
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-06-24 04:29:43 -05:00
|
|
|
let refs = get_all_refs(
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let mut i<|> = 0;
|
|
|
|
i = i + 1;
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
2020-01-09 15:01:43 -06:00
|
|
|
check_result(
|
|
|
|
refs,
|
2020-06-24 04:29:43 -05:00
|
|
|
"i BIND_PAT FileId(1) 23..24 Other Write",
|
|
|
|
&["FileId(1) 34..35 Other Write", "FileId(1) 38..39 Other Read"],
|
2020-01-09 15:01:43 -06:00
|
|
|
);
|
2020-01-04 16:46:01 -06:00
|
|
|
}
|
|
|
|
|
2020-01-04 18:25:29 -06:00
|
|
|
#[test]
|
|
|
|
fn test_basic_highlight_field_read_write() {
|
2020-06-24 04:29:43 -05:00
|
|
|
let refs = get_all_refs(
|
|
|
|
r#"
|
|
|
|
struct S {
|
|
|
|
f: u32,
|
|
|
|
}
|
2020-01-04 18:25:29 -06:00
|
|
|
|
2020-06-24 04:29:43 -05:00
|
|
|
fn foo() {
|
|
|
|
let mut s = S{f: 0};
|
|
|
|
s.f<|> = 0;
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
2020-01-09 15:01:43 -06:00
|
|
|
check_result(
|
|
|
|
refs,
|
2020-07-30 09:49:13 -05:00
|
|
|
"f RECORD_FIELD FileId(1) 15..21 15..16 Other",
|
2020-06-24 04:29:43 -05:00
|
|
|
&["FileId(1) 55..56 Other Read", "FileId(1) 68..69 Other Write"],
|
2020-01-09 15:01:43 -06:00
|
|
|
);
|
2020-01-04 18:25:29 -06:00
|
|
|
}
|
|
|
|
|
2020-01-10 13:56:58 -06:00
|
|
|
#[test]
|
|
|
|
fn test_basic_highlight_decl_no_write() {
|
2020-06-24 04:29:43 -05:00
|
|
|
let refs = get_all_refs(
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let i<|>;
|
|
|
|
i = 1;
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
check_result(refs, "i BIND_PAT FileId(1) 19..20 Other", &["FileId(1) 26..27 Other Write"]);
|
2020-01-10 13:56:58 -06:00
|
|
|
}
|
|
|
|
|
2020-05-11 04:25:18 -05:00
|
|
|
#[test]
|
|
|
|
fn test_find_struct_function_refs_outside_module() {
|
2020-06-24 04:29:43 -05:00
|
|
|
let refs = get_all_refs(
|
|
|
|
r#"
|
|
|
|
mod foo {
|
|
|
|
pub struct Foo;
|
2020-05-11 04:25:18 -05:00
|
|
|
|
2020-06-24 04:29:43 -05:00
|
|
|
impl Foo {
|
|
|
|
pub fn new<|>() -> Foo {
|
|
|
|
Foo
|
2020-05-11 04:25:18 -05:00
|
|
|
}
|
2020-06-24 04:29:43 -05:00
|
|
|
}
|
|
|
|
}
|
2020-05-11 04:25:18 -05:00
|
|
|
|
2020-06-24 04:29:43 -05:00
|
|
|
fn main() {
|
|
|
|
let _f = foo::Foo::new();
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
2020-05-11 04:25:18 -05:00
|
|
|
check_result(
|
|
|
|
refs,
|
2020-07-30 07:51:08 -05:00
|
|
|
"new FN FileId(1) 54..101 61..64 Other",
|
2020-06-24 04:29:43 -05:00
|
|
|
&["FileId(1) 146..149 StructLiteral"],
|
2020-05-11 04:25:18 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-31 11:21:45 -05:00
|
|
|
#[test]
|
|
|
|
fn test_find_all_refs_nested_module() {
|
|
|
|
let code = r#"
|
|
|
|
//- /lib.rs
|
|
|
|
mod foo {
|
|
|
|
mod bar;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn f<|>() {}
|
|
|
|
|
|
|
|
//- /foo/bar.rs
|
|
|
|
use crate::f;
|
|
|
|
|
|
|
|
fn g() {
|
|
|
|
f();
|
|
|
|
}
|
|
|
|
"#;
|
|
|
|
|
|
|
|
let (analysis, pos) = analysis_and_position(code);
|
|
|
|
let refs = analysis.find_all_refs(pos, None).unwrap().unwrap();
|
|
|
|
check_result(
|
|
|
|
refs,
|
2020-07-30 07:51:08 -05:00
|
|
|
"f FN FileId(1) 26..35 29..30 Other",
|
2020-06-23 15:27:24 -05:00
|
|
|
&["FileId(2) 11..12 Other", "FileId(2) 28..29 StructLiteral"],
|
2020-05-31 11:21:45 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-24 04:29:43 -05:00
|
|
|
fn get_all_refs(ra_fixture: &str) -> ReferenceSearchResult {
|
2020-06-24 04:31:30 -05:00
|
|
|
let (analysis, position) = analysis_and_position(ra_fixture);
|
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());
|
2020-03-04 05:20:32 -06:00
|
|
|
res.references()
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.for_each(|(i, r)| ref_assert_match(r, expected_refs[i]));
|
2020-01-08 15:35:58 -06:00
|
|
|
}
|
|
|
|
|
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-03-04 05:20:32 -06:00
|
|
|
fn ref_debug_render(r: &Reference) -> String {
|
|
|
|
let mut s = format!("{:?} {:?} {:?}", r.file_range.file_id, r.file_range.range, r.kind);
|
|
|
|
if let Some(access) = r.access {
|
|
|
|
s.push_str(&format!(" {:?}", access));
|
2020-01-08 15:35:58 -06:00
|
|
|
}
|
2020-03-04 05:20:32 -06:00
|
|
|
s
|
|
|
|
}
|
2020-01-08 15:35:58 -06:00
|
|
|
|
2020-03-04 05:20:32 -06:00
|
|
|
fn ref_assert_match(r: &Reference, expected: &str) {
|
|
|
|
let actual = ref_debug_render(r);
|
|
|
|
test_utils::assert_eq_text!(expected.trim(), actual.trim(),);
|
2020-01-08 15:35:58 -06:00
|
|
|
}
|
2019-01-18 02:29:09 -06:00
|
|
|
}
|