rust/crates/ide/src/references.rs

1172 lines
26 KiB
Rust
Raw Normal View History

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
2021-01-08 08:46:48 -06:00
//! some information that is needed for further steps of searching.
2019-10-22 15:46:53 -05:00
//! 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.
2020-10-03 09:34:52 -05:00
pub(crate) mod rename;
2019-10-12 10:47:17 -05:00
use hir::Semantics;
2020-08-13 09:39:16 -05:00
use ide_db::{
2020-10-15 10:27:50 -05:00
defs::{Definition, NameClass, NameRefClass},
2021-01-11 17:05:07 -06:00
search::{FileReference, FileReferences, ReferenceAccess, ReferenceKind, SearchScope},
2020-03-03 11:54:39 -06:00
RootDatabase,
};
2020-08-12 11:26:51 -05:00
use syntax::{
2020-01-10 13:56:58 -06:00
algo::find_node_at_offset,
ast::{self, NameOwner},
match_ast, AstNode, SyntaxNode, TextRange, TokenAtOffset, T,
};
2019-10-12 10:47:17 -05:00
use crate::{display::TryToNav, FilePosition, FileRange, NavigationTarget, RangeInfo, SymbolKind};
2019-10-12 10:47:17 -05:00
#[derive(Debug, Clone)]
pub struct ReferenceSearchResult {
declaration: Declaration,
2021-01-11 17:05:07 -06:00
references: Vec<FileReferences>,
}
#[derive(Debug, Clone)]
pub struct Declaration {
pub nav: NavigationTarget,
pub kind: ReferenceKind,
pub access: Option<ReferenceAccess>,
}
impl ReferenceSearchResult {
pub fn declaration(&self) -> &Declaration {
&self.declaration
}
pub fn decl_target(&self) -> &NavigationTarget {
&self.declaration.nav
}
2021-01-11 17:05:07 -06:00
pub fn references(&self) -> &[FileReferences] {
&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
// over References
impl IntoIterator for ReferenceSearchResult {
2021-01-11 17:05:07 -06:00
type Item = FileReferences;
type IntoIter = std::vec::IntoIter<FileReferences>;
fn into_iter(mut self) -> Self::IntoIter {
let mut v = Vec::with_capacity(self.len());
2021-01-11 17:05:07 -06:00
v.append(&mut self.references);
let decl_ref = FileReference {
range: self.declaration.nav.focus_or_full_range(),
kind: self.declaration.kind,
access: self.declaration.access,
2021-01-11 17:05:07 -06:00
};
let file_id = self.declaration.nav.file_id;
match v.iter_mut().find(|it| it.file_id == file_id) {
Some(file_refs) => file_refs.references.push(decl_ref),
None => v.push(FileReferences { file_id, references: vec![decl_ref] }),
}
v.into_iter()
}
}
pub(crate) fn find_all_refs(
2020-07-01 07:11:34 -05:00
sema: &Semantics<RootDatabase>,
position: FilePosition,
search_scope: Option<SearchScope>,
2019-09-05 13:36:40 -05:00
) -> Option<RangeInfo<ReferenceSearchResult>> {
2020-08-12 09:32:36 -05:00
let _p = profile::span("find_all_refs");
let syntax = sema.parse(position.file_id).syntax().clone();
2020-11-28 15:55:34 -06:00
if let Some(res) = try_find_self_references(&syntax, position) {
return Some(res);
}
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)
2020-12-14 09:39:42 -06:00
} else if let Some(name) = get_enum_def_name_for_struct_literal_search(&sema, &syntax, position)
{
(Some(name), ReferenceKind::EnumLiteral)
2020-03-22 06:53:28 -05:00
} else {
(
sema.find_node_at_offset_with_descend::<ast::Name>(&syntax, position.offset),
ReferenceKind::Other,
)
};
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
2021-01-11 17:05:07 -06:00
let mut references = def.usages(sema).set_scope(search_scope).all();
references.iter_mut().for_each(|it| {
it.references.retain(|r| search_kind == ReferenceKind::Other || search_kind == r.kind)
});
references.retain(|r| !r.references.is_empty());
2020-03-03 11:22:52 -06:00
let nav = def.try_to_nav(sema.db)?;
let decl_range = nav.focus_or_full_range();
let mut kind = ReferenceKind::Other;
if let Definition::Local(local) = def {
if let either::Either::Left(pat) = local.source(sema.db).value {
if matches!(
pat.syntax().parent().and_then(ast::RecordPatField::cast),
Some(pat_field) if pat_field.name_ref().is_none()
) {
kind = ReferenceKind::FieldShorthandForLocal;
}
}
} else if matches!(
def,
Definition::GenericParam(hir::GenericParam::LifetimeParam(_)) | Definition::Label(_)
) {
2020-12-16 14:35:15 -06:00
kind = ReferenceKind::Lifetime;
2020-03-03 11:22:52 -06:00
};
let declaration = Declaration { nav, kind, access: decl_access(&def, &syntax, decl_range) };
2020-03-03 11:22:52 -06:00
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 {
2020-10-15 10:33:32 -05:00
let def = NameClass::classify(sema, &name)?.referenced_or_defined(sema.db);
let FileRange { range, .. } = sema.original_range(name.syntax());
2020-03-04 05:07:44 -06:00
return Some(RangeInfo::new(range, def));
}
2020-12-16 14:35:15 -06:00
let (FileRange { range, .. }, def) = if let Some(lifetime) =
2020-12-16 14:35:15 -06:00
sema.find_node_at_offset_with_descend::<ast::Lifetime>(&syntax, position.offset)
{
if let Some(def) = NameRefClass::classify_lifetime(sema, &lifetime)
.map(|class| NameRefClass::referenced(class, sema.db))
{
(sema.original_range(lifetime.syntax()), def)
2020-12-16 14:35:15 -06:00
} else {
(
sema.original_range(lifetime.syntax()),
2020-12-16 14:35:15 -06:00
NameClass::classify_lifetime(sema, &lifetime)?.referenced_or_defined(sema.db),
)
}
} else {
let name_ref =
sema.find_node_at_offset_with_descend::<ast::NameRef>(&syntax, position.offset)?;
(
sema.original_range(name_ref.syntax()),
2020-12-16 14:35:15 -06:00
NameRefClass::classify(sema, &name_ref)?.referenced(sema.db),
)
};
Some(RangeInfo::new(range, def))
2020-03-04 05:07:44 -06:00
}
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-07-31 13:09:09 -05:00
if let ast::Pat::IdentPat(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>,
syntax: &SyntaxNode,
position: FilePosition,
) -> Option<ast::Name> {
if let TokenAtOffset::Between(ref left, ref right) = syntax.token_at_offset(position.offset) {
if right.kind() != T!['{'] && right.kind() != T!['('] {
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-03-22 06:53:28 -05:00
if sema
.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());
}
}
None
}
2020-12-14 09:39:42 -06:00
fn get_enum_def_name_for_struct_literal_search(
sema: &Semantics<RootDatabase>,
syntax: &SyntaxNode,
position: FilePosition,
) -> Option<ast::Name> {
if let TokenAtOffset::Between(ref left, ref right) = syntax.token_at_offset(position.offset) {
if right.kind() != T!['{'] && right.kind() != T!['('] {
2020-12-14 09:39:42 -06:00
return None;
}
if let Some(name) =
sema.find_node_at_offset_with_descend::<ast::Name>(&syntax, left.text_range().start())
{
return name.syntax().ancestors().find_map(ast::Enum::cast).and_then(|l| l.name());
}
if sema
.find_node_at_offset_with_descend::<ast::GenericParamList>(
&syntax,
left.text_range().start(),
)
.is_some()
{
return left.ancestors().find_map(ast::Enum::cast).and_then(|l| l.name());
}
}
None
}
2020-11-28 15:55:34 -06:00
fn try_find_self_references(
syntax: &SyntaxNode,
position: FilePosition,
) -> Option<RangeInfo<ReferenceSearchResult>> {
2021-01-11 17:05:07 -06:00
let FilePosition { file_id, offset } = position;
let self_token = syntax.token_at_offset(offset).find(|t| t.kind() == T![self])?;
2020-11-28 15:55:34 -06:00
let parent = self_token.parent();
match_ast! {
match parent {
ast::SelfParam(it) => (),
ast::PathSegment(segment) => {
segment.self_token()?;
let path = segment.parent_path();
if path.qualifier().is_some() && !ast::PathExpr::can_cast(path.syntax().parent()?.kind()) {
return None;
}
},
_ => return None,
}
};
let function = parent.ancestors().find_map(ast::Fn::cast)?;
let self_param = function.param_list()?.self_param()?;
let param_self_token = self_param.self_token()?;
let declaration = Declaration {
nav: NavigationTarget {
2021-01-11 17:05:07 -06:00
file_id,
2020-11-28 15:55:34 -06:00
full_range: self_param.syntax().text_range(),
focus_range: Some(param_self_token.text_range()),
name: param_self_token.text().clone(),
kind: Some(SymbolKind::SelfParam),
2020-11-28 15:55:34 -06:00
container_name: None,
description: None,
docs: None,
},
kind: ReferenceKind::SelfKw,
access: Some(if self_param.mut_token().is_some() {
ReferenceAccess::Write
} else {
ReferenceAccess::Read
}),
};
let references = function
.body()
.map(|body| {
2021-01-11 17:05:07 -06:00
FileReferences {
file_id,
references: body
.syntax()
.descendants()
.filter_map(ast::PathExpr::cast)
.filter_map(|expr| {
let path = expr.path()?;
if path.qualifier().is_none() {
path.segment()?.self_token()
} else {
None
}
})
.map(|token| FileReference {
range: token.text_range(),
kind: ReferenceKind::SelfKw,
access: declaration.access, // FIXME: properly check access kind here instead of copying it from the declaration
})
.collect(),
}
2020-11-28 15:55:34 -06:00
})
2021-01-11 17:05:07 -06:00
.map_or_else(Vec::default, |it| vec![it]);
2020-11-28 15:55:34 -06:00
Some(RangeInfo::new(
param_self_token.text_range(),
ReferenceSearchResult { declaration, references },
))
}
2019-01-18 02:29:09 -06:00
#[cfg(test)]
mod tests {
2020-10-02 09:42:48 -05:00
use expect_test::{expect, Expect};
2021-01-11 17:05:07 -06:00
use ide_db::{base_db::FileId, search::FileReferences};
2020-10-02 09:42:48 -05:00
use stdx::format_to;
2020-10-02 10:34:31 -05:00
use crate::{fixture, SearchScope};
2019-01-18 02:29:09 -06:00
#[test]
fn test_struct_literal_after_space() {
2020-10-02 09:42:48 -05:00
check(
2020-06-24 04:29:43 -05:00
r#"
2021-01-06 14:15:48 -06:00
struct Foo $0{
2020-06-24 04:29:43 -05:00
a: i32,
}
impl Foo {
fn f() -> i32 { 42 }
}
fn main() {
let f: Foo;
f = Foo {a: Foo::f()};
}
"#,
2020-10-02 09:42:48 -05:00
expect![[r#"
Foo Struct FileId(0) 0..26 7..10 Other
2020-10-02 09:42:48 -05:00
2020-10-02 09:13:48 -05:00
FileId(0) 101..104 StructLiteral
2020-10-02 09:42:48 -05:00
"#]],
2020-01-08 15:35:58 -06:00
);
}
#[test]
2020-06-24 04:29:43 -05:00
fn test_struct_literal_before_space() {
2020-10-02 09:42:48 -05:00
check(
2020-06-24 04:29:43 -05:00
r#"
2021-01-06 14:15:48 -06:00
struct Foo$0 {}
2020-06-24 04:29:43 -05:00
fn main() {
let f: Foo;
f = Foo {};
}
"#,
2020-10-02 09:42:48 -05:00
expect![[r#"
Foo Struct FileId(0) 0..13 7..10 Other
2020-10-02 09:42:48 -05:00
2020-10-02 09:13:48 -05:00
FileId(0) 41..44 Other
FileId(0) 54..57 StructLiteral
2020-10-02 09:42:48 -05:00
"#]],
);
}
#[test]
fn test_struct_literal_with_generic_type() {
2020-10-02 09:42:48 -05:00
check(
2020-06-24 04:29:43 -05:00
r#"
2021-01-06 14:15:48 -06:00
struct Foo<T> $0{}
2020-06-24 04:29:43 -05:00
fn main() {
let f: Foo::<i32>;
f = Foo {};
}
"#,
2020-10-02 09:42:48 -05:00
expect![[r#"
Foo Struct FileId(0) 0..16 7..10 Other
2020-10-02 09:42:48 -05:00
2020-10-02 09:13:48 -05:00
FileId(0) 64..67 StructLiteral
2020-10-02 09:42:48 -05:00
"#]],
);
}
#[test]
fn test_struct_literal_for_tuple() {
2020-10-02 09:42:48 -05:00
check(
2020-06-24 04:29:43 -05:00
r#"
2021-01-06 14:15:48 -06:00
struct Foo$0(i32);
2020-06-24 04:29:43 -05:00
fn main() {
let f: Foo;
f = Foo(1);
}
"#,
2020-10-02 09:42:48 -05:00
expect![[r#"
Foo Struct FileId(0) 0..16 7..10 Other
2020-10-02 09:42:48 -05:00
2020-10-02 09:13:48 -05:00
FileId(0) 54..57 StructLiteral
2020-10-02 09:42:48 -05:00
"#]],
);
}
2020-12-14 09:39:42 -06:00
#[test]
fn test_enum_after_space() {
check(
r#"
2021-01-06 14:15:48 -06:00
enum Foo $0{
2020-12-14 09:39:42 -06:00
A,
B,
}
fn main() {
let f: Foo;
f = Foo::A;
}
"#,
expect![[r#"
Foo Enum FileId(0) 0..26 5..8 Other
2020-12-14 09:39:42 -06:00
FileId(0) 63..66 EnumLiteral
"#]],
);
}
#[test]
fn test_enum_before_space() {
check(
r#"
2021-01-06 14:15:48 -06:00
enum Foo$0 {
2020-12-14 09:39:42 -06:00
A,
B,
}
fn main() {
let f: Foo;
f = Foo::A;
}
"#,
expect![[r#"
Foo Enum FileId(0) 0..26 5..8 Other
2020-12-14 09:39:42 -06:00
FileId(0) 50..53 Other
FileId(0) 63..66 EnumLiteral
"#]],
);
}
#[test]
fn test_enum_with_generic_type() {
check(
r#"
2021-01-06 14:15:48 -06:00
enum Foo<T> $0{
2020-12-14 09:39:42 -06:00
A(T),
B,
}
fn main() {
let f: Foo<i8>;
f = Foo::A(1);
}
"#,
expect![[r#"
Foo Enum FileId(0) 0..32 5..8 Other
2020-12-14 09:39:42 -06:00
FileId(0) 73..76 EnumLiteral
"#]],
);
}
#[test]
fn test_enum_for_tuple() {
check(
r#"
2021-01-06 14:15:48 -06:00
enum Foo$0{
2020-12-14 09:39:42 -06:00
A(i8),
B(i8),
}
fn main() {
let f: Foo;
f = Foo::A(1);
}
"#,
expect![[r#"
Foo Enum FileId(0) 0..33 5..8 Other
2020-12-14 09:39:42 -06:00
FileId(0) 70..73 EnumLiteral
"#]],
);
}
2019-03-25 15:03:32 -05:00
#[test]
fn test_find_all_refs_for_local() {
2020-10-02 09:42:48 -05:00
check(
2020-06-24 04:29:43 -05:00
r#"
fn main() {
let mut i = 1;
let j = 1;
2021-01-06 14:15:48 -06:00
i = i$0 + 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-10-02 09:42:48 -05:00
expect![[r#"
i Local FileId(0) 24..25 Other Write
2020-10-02 09:42:48 -05:00
2020-10-02 09:13:48 -05:00
FileId(0) 50..51 Other Write
FileId(0) 54..55 Other Read
FileId(0) 76..77 Other Write
FileId(0) 94..95 Other Write
2020-10-02 09:42:48 -05:00
"#]],
2020-01-08 15:35:58 -06:00
);
2019-03-25 15:03:32 -05:00
}
#[test]
fn search_filters_by_range() {
2020-10-02 09:42:48 -05:00
check(
2020-06-24 04:29:43 -05:00
r#"
fn foo() {
2021-01-06 14:15:48 -06:00
let spam$0 = 92;
2020-06-24 04:29:43 -05:00
spam + spam
}
fn bar() {
let spam = 92;
spam + spam
}
"#,
2020-10-02 09:42:48 -05:00
expect![[r#"
spam Local FileId(0) 19..23 Other
2020-10-02 09:42:48 -05:00
2020-10-02 09:13:48 -05:00
FileId(0) 34..38 Other Read
FileId(0) 41..45 Other Read
2020-10-02 09:42:48 -05:00
"#]],
);
}
2019-03-25 15:03:32 -05:00
#[test]
fn test_find_all_refs_for_param_inside() {
2020-10-02 09:42:48 -05:00
check(
2020-06-24 04:29:43 -05:00
r#"
2021-01-06 14:15:48 -06:00
fn foo(i : u32) -> u32 { i$0 }
2020-06-24 04:29:43 -05:00
"#,
2020-10-02 09:42:48 -05:00
expect![[r#"
i ValueParam FileId(0) 7..8 Other
2020-10-02 09:42:48 -05:00
2020-10-02 09:13:48 -05:00
FileId(0) 25..26 Other Read
2020-10-02 09:42:48 -05:00
"#]],
2020-06-24 04:29:43 -05:00
);
2019-03-25 15:03:32 -05:00
}
#[test]
fn test_find_all_refs_for_fn_param() {
2020-10-02 09:42:48 -05:00
check(
2020-06-24 04:29:43 -05:00
r#"
2021-01-06 14:15:48 -06:00
fn foo(i$0 : u32) -> u32 { i }
2020-06-24 04:29:43 -05:00
"#,
2020-10-02 09:42:48 -05:00
expect![[r#"
i ValueParam FileId(0) 7..8 Other
2020-10-02 09:42:48 -05:00
2020-10-02 09:13:48 -05:00
FileId(0) 25..26 Other Read
2020-10-02 09:42:48 -05:00
"#]],
2020-06-24 04:29:43 -05: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() {
2020-10-02 09:42:48 -05:00
check(
2020-06-24 04:29:43 -05:00
r#"
//- /lib.rs
struct Foo {
2021-01-06 14:15:48 -06:00
pub spam$0: u32,
2020-06-24 04:29:43 -05:00
}
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-10-02 09:42:48 -05:00
expect![[r#"
spam Field FileId(0) 17..30 21..25 Other
2020-10-02 09:42:48 -05:00
2020-10-02 09:13:48 -05:00
FileId(0) 67..71 Other Read
2020-10-02 09:42:48 -05:00
"#]],
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-10-02 09:42:48 -05:00
check(
2020-06-24 04:29:43 -05:00
r#"
struct Foo;
impl Foo {
2021-01-06 14:15:48 -06:00
fn f$0(&self) { }
2020-06-24 04:29:43 -05:00
}
"#,
2020-10-02 09:42:48 -05:00
expect![[r#"
f Function FileId(0) 27..43 30..31 Other
2020-10-02 09:42:48 -05:00
"#]],
2020-06-24 04:29:43 -05:00
);
2019-09-14 06:38:10 -05:00
}
#[test]
fn test_find_all_refs_enum_var_name() {
2020-10-02 09:42:48 -05:00
check(
2020-06-24 04:29:43 -05:00
r#"
enum Foo {
A,
2021-01-06 14:15:48 -06:00
B$0,
2020-06-24 04:29:43 -05:00
C,
}
"#,
2020-10-02 09:42:48 -05:00
expect![[r#"
B Variant FileId(0) 22..23 22..23 Other
2020-10-02 09:42:48 -05:00
"#]],
2020-06-24 04:29:43 -05:00
);
2019-09-14 06:38:10 -05:00
}
#[test]
fn test_find_all_refs_enum_var_field() {
check(
r#"
enum Foo {
A,
2021-01-06 14:15:48 -06:00
B { field$0: u8 },
C,
}
"#,
expect![[r#"
field Field FileId(0) 26..35 26..31 Other
"#]],
);
}
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-10-02 09:42:48 -05:00
check(
2020-06-24 04:29:43 -05:00
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() {
2021-01-06 14:15:48 -06:00
let i = foo::Foo$0 { n: 5 };
2020-06-24 04:29:43 -05:00
}
"#,
2020-10-02 09:42:48 -05:00
expect![[r#"
Foo Struct FileId(1) 17..51 28..31 Other
2020-10-02 09:42:48 -05:00
2020-10-02 09:13:48 -05:00
FileId(0) 53..56 StructLiteral
FileId(2) 79..82 StructLiteral
2020-10-02 09:42:48 -05:00
"#]],
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`.
// 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-10-02 09:42:48 -05:00
check(
2020-06-24 04:29:43 -05:00
r#"
//- /lib.rs
2021-01-06 14:15:48 -06:00
mod foo$0;
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,
}
"#,
2020-10-02 09:42:48 -05:00
expect![[r#"
foo Module FileId(1) 0..35 Other
2020-10-02 09:42:48 -05:00
2020-10-02 09:13:48 -05:00
FileId(0) 14..17 Other
2020-10-02 09:42:48 -05:00
"#]],
2020-06-24 04:29:43 -05:00
);
2019-10-15 14:50:28 -05:00
}
#[test]
fn test_find_all_refs_super_mod_vis() {
2020-10-02 09:42:48 -05:00
check(
2020-06-24 04:29:43 -05:00
r#"
//- /lib.rs
mod foo;
2020-06-24 04:29:43 -05:00
//- /foo.rs
mod some;
use some::Foo;
2020-06-24 04:29:43 -05:00
fn f() {
let i = Foo { n: 5 };
}
2020-06-24 04:29:43 -05:00
//- /foo/some.rs
2021-01-06 14:15:48 -06:00
pub(super) struct Foo$0 {
2020-06-24 04:29:43 -05:00
pub n: u32,
}
"#,
2020-10-02 09:42:48 -05:00
expect![[r#"
Foo Struct FileId(2) 0..41 18..21 Other
2020-10-02 09:42:48 -05:00
2020-10-02 09:13:48 -05:00
FileId(1) 20..23 Other
FileId(1) 47..50 StructLiteral
2020-10-02 09:42:48 -05:00
"#]],
2020-01-08 15:35:58 -06:00
);
}
#[test]
fn test_find_all_refs_with_scope() {
let code = r#"
//- /lib.rs
mod foo;
mod bar;
2021-01-06 14:15:48 -06:00
pub fn quux$0() {}
//- /foo.rs
fn f() { super::quux(); }
//- /bar.rs
fn f() { super::quux(); }
"#;
2020-10-02 09:42:48 -05:00
check_with_scope(
code,
None,
expect![[r#"
quux Function FileId(0) 19..35 26..30 Other
2020-10-02 09:13:48 -05:00
FileId(1) 16..20 StructLiteral
2020-10-02 09:42:48 -05:00
FileId(2) 16..20 StructLiteral
"#]],
2020-01-08 15:35:58 -06:00
);
2020-10-02 09:42:48 -05:00
check_with_scope(
code,
2020-10-02 09:13:48 -05:00
Some(SearchScope::single_file(FileId(2))),
2020-10-02 09:42:48 -05:00
expect![[r#"
quux Function FileId(0) 19..35 26..30 Other
2020-10-02 09:42:48 -05:00
2020-10-02 09:13:48 -05:00
FileId(2) 16..20 StructLiteral
2020-10-02 09:42:48 -05:00
"#]],
2020-01-08 15:35:58 -06:00
);
}
2019-11-15 16:13:52 -06:00
#[test]
fn test_find_all_refs_macro_def() {
2020-10-02 09:42:48 -05:00
check(
2020-06-24 04:29:43 -05:00
r#"
#[macro_export]
2021-01-06 14:15:48 -06:00
macro_rules! m1$0 { () => (()) }
2020-06-24 04:29:43 -05:00
fn foo() {
m1();
m1();
}
"#,
2020-10-02 09:42:48 -05:00
expect![[r#"
m1 Macro FileId(0) 0..46 29..31 Other
2020-10-02 09:42:48 -05:00
2020-10-02 09:13:48 -05:00
FileId(0) 63..65 StructLiteral
FileId(0) 73..75 StructLiteral
2020-10-02 09:42:48 -05:00
"#]],
2020-01-08 15:35:58 -06:00
);
2019-11-15 16:13:52 -06:00
}
#[test]
2020-01-04 18:25:29 -06:00
fn test_basic_highlight_read_write() {
2020-10-02 09:42:48 -05:00
check(
2020-06-24 04:29:43 -05:00
r#"
fn foo() {
2021-01-06 14:15:48 -06:00
let mut i$0 = 0;
2020-06-24 04:29:43 -05:00
i = i + 1;
}
"#,
2020-10-02 09:42:48 -05:00
expect![[r#"
i Local FileId(0) 23..24 Other Write
2020-10-02 09:42:48 -05:00
2020-10-02 09:13:48 -05:00
FileId(0) 34..35 Other Write
FileId(0) 38..39 Other Read
2020-10-02 09:42:48 -05:00
"#]],
);
}
2020-01-04 18:25:29 -06:00
#[test]
fn test_basic_highlight_field_read_write() {
2020-10-02 09:42:48 -05:00
check(
2020-06-24 04:29:43 -05:00
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};
2021-01-06 14:15:48 -06:00
s.f$0 = 0;
2020-06-24 04:29:43 -05:00
}
"#,
2020-10-02 09:42:48 -05:00
expect![[r#"
f Field FileId(0) 15..21 15..16 Other
2020-10-02 09:42:48 -05:00
FileId(0) 55..56 RecordFieldExprOrPat Read
2020-10-02 09:13:48 -05:00
FileId(0) 68..69 Other Write
2020-10-02 09:42:48 -05: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-10-02 09:42:48 -05:00
check(
2020-06-24 04:29:43 -05:00
r#"
fn foo() {
2021-01-06 14:15:48 -06:00
let i$0;
2020-06-24 04:29:43 -05:00
i = 1;
}
"#,
2020-10-02 09:42:48 -05:00
expect![[r#"
i Local FileId(0) 19..20 Other
2020-10-02 09:42:48 -05:00
2020-10-02 09:13:48 -05:00
FileId(0) 26..27 Other Write
2020-10-02 09:42:48 -05:00
"#]],
2020-06-24 04:29:43 -05:00
);
2020-01-10 13:56:58 -06:00
}
#[test]
fn test_find_struct_function_refs_outside_module() {
2020-10-02 09:42:48 -05:00
check(
2020-06-24 04:29:43 -05:00
r#"
mod foo {
pub struct Foo;
2020-06-24 04:29:43 -05:00
impl Foo {
2021-01-06 14:15:48 -06:00
pub fn new$0() -> Foo { Foo }
2020-06-24 04:29:43 -05:00
}
}
2020-06-24 04:29:43 -05:00
fn main() {
let _f = foo::Foo::new();
}
"#,
2020-10-02 09:42:48 -05:00
expect![[r#"
new Function FileId(0) 54..81 61..64 Other
2020-10-02 09:42:48 -05:00
2020-10-02 09:13:48 -05:00
FileId(0) 126..129 StructLiteral
2020-10-02 09:42:48 -05:00
"#]],
);
}
2020-05-31 11:21:45 -05:00
#[test]
fn test_find_all_refs_nested_module() {
2020-10-02 09:42:48 -05:00
check(
r#"
//- /lib.rs
mod foo { mod bar; }
2020-05-31 11:21:45 -05:00
2021-01-06 14:15:48 -06:00
fn f$0() {}
2020-05-31 11:21:45 -05:00
2020-10-02 09:42:48 -05:00
//- /foo/bar.rs
use crate::f;
2020-05-31 11:21:45 -05:00
2020-10-02 09:42:48 -05:00
fn g() { f(); }
"#,
expect![[r#"
f Function FileId(0) 22..31 25..26 Other
2020-05-31 11:21:45 -05:00
2020-10-02 09:13:48 -05:00
FileId(1) 11..12 Other
FileId(1) 24..25 StructLiteral
2020-10-02 09:42:48 -05:00
"#]],
);
}
#[test]
fn test_find_all_refs_struct_pat() {
check(
r#"
struct S {
2021-01-06 14:15:48 -06:00
field$0: u8,
}
fn f(s: S) {
match s {
S { field } => {}
}
}
"#,
expect![[r#"
field Field FileId(0) 15..24 15..20 Other
FileId(0) 68..73 FieldShorthandForField Read
"#]],
);
}
#[test]
fn test_find_all_refs_enum_var_pat() {
check(
r#"
enum En {
Variant {
2021-01-06 14:15:48 -06:00
field$0: u8,
}
}
fn f(e: En) {
match e {
En::Variant { field } => {}
}
}
"#,
expect![[r#"
field Field FileId(0) 32..41 32..37 Other
FileId(0) 102..107 FieldShorthandForField Read
"#]],
);
}
#[test]
fn test_find_all_refs_enum_var_privacy() {
check(
r#"
mod m {
pub enum En {
Variant {
2021-01-06 14:15:48 -06:00
field$0: u8,
}
}
}
fn f() -> m::En {
m::En::Variant { field: 0 }
}
"#,
expect![[r#"
field Field FileId(0) 56..65 56..61 Other
FileId(0) 125..130 RecordFieldExprOrPat Read
"#]],
2020-11-28 15:55:34 -06:00
);
}
#[test]
fn test_find_self_refs() {
check(
r#"
struct Foo { bar: i32 }
impl Foo {
fn foo(self) {
2021-01-06 14:15:48 -06:00
let x = self$0.bar;
2020-11-28 15:55:34 -06:00
if true {
let _ = match () {
() => self,
};
}
}
}
"#,
expect![[r#"
self SelfParam FileId(0) 47..51 47..51 SelfKw Read
2020-11-28 15:55:34 -06:00
FileId(0) 71..75 SelfKw Read
FileId(0) 152..156 SelfKw Read
"#]],
2020-05-31 11:21:45 -05:00
);
}
2020-10-02 09:42:48 -05:00
fn check(ra_fixture: &str, expect: Expect) {
check_with_scope(ra_fixture, None, expect)
2019-03-25 15:03:32 -05:00
}
2020-01-08 15:35:58 -06:00
2020-10-02 09:13:48 -05:00
fn check_with_scope(ra_fixture: &str, search_scope: Option<SearchScope>, expect: Expect) {
2020-10-02 10:34:31 -05:00
let (analysis, pos) = fixture::position(ra_fixture);
2020-10-02 09:42:48 -05:00
let refs = analysis.find_all_refs(pos, search_scope).unwrap().unwrap();
2020-01-08 15:35:58 -06:00
2020-10-02 09:42:48 -05:00
let mut actual = String::new();
{
let decl = refs.declaration;
format_to!(actual, "{} {:?}", decl.nav.debug_render(), decl.kind);
if let Some(access) = decl.access {
format_to!(actual, " {:?}", access)
}
2020-10-02 09:42:48 -05:00
actual += "\n\n";
}
2021-01-11 17:05:07 -06:00
for FileReferences { file_id, references } in refs.references {
for r in references {
format_to!(actual, "{:?} {:?} {:?}", file_id, r.range, r.kind);
if let Some(access) = r.access {
format_to!(actual, " {:?}", access);
}
actual += "\n";
2020-10-02 09:42:48 -05:00
}
2020-01-08 15:35:58 -06:00
}
2020-10-02 09:42:48 -05:00
expect.assert_eq(&actual)
2020-01-08 15:35:58 -06:00
}
2020-12-16 14:35:15 -06:00
#[test]
fn test_find_lifetimes_function() {
check(
r#"
trait Foo<'a> {}
impl<'a> Foo<'a> for &'a () {}
2021-01-06 14:15:48 -06:00
fn foo<'a, 'b: 'a>(x: &'a$0 ()) -> &'a () where &'a (): Foo<'a> {
2020-12-16 14:35:15 -06:00
fn bar<'a>(_: &'a ()) {}
x
}
"#,
expect![[r#"
'a LifetimeParam FileId(0) 55..57 55..57 Lifetime
2020-12-16 14:35:15 -06:00
FileId(0) 63..65 Lifetime
FileId(0) 71..73 Lifetime
FileId(0) 82..84 Lifetime
FileId(0) 95..97 Lifetime
FileId(0) 106..108 Lifetime
"#]],
);
}
#[test]
fn test_find_lifetimes_type_alias() {
check(
r#"
2021-01-06 14:15:48 -06:00
type Foo<'a, T> where T: 'a$0 = &'a T;
2020-12-16 14:35:15 -06:00
"#,
expect![[r#"
'a LifetimeParam FileId(0) 9..11 9..11 Lifetime
2020-12-16 14:35:15 -06:00
FileId(0) 25..27 Lifetime
FileId(0) 31..33 Lifetime
"#]],
);
}
#[test]
fn test_find_lifetimes_trait_impl() {
check(
r#"
trait Foo<'a> {
fn foo() -> &'a ();
}
impl<'a> Foo<'a> for &'a () {
2021-01-06 14:15:48 -06:00
fn foo() -> &'a$0 () {
2020-12-16 14:35:15 -06:00
unimplemented!()
}
}
"#,
expect![[r#"
'a LifetimeParam FileId(0) 47..49 47..49 Lifetime
2020-12-16 14:35:15 -06:00
FileId(0) 55..57 Lifetime
FileId(0) 64..66 Lifetime
FileId(0) 89..91 Lifetime
"#]],
);
}
#[test]
fn test_map_range_to_original() {
check(
r#"
macro_rules! foo {($i:ident) => {$i} }
fn main() {
2021-01-06 14:15:48 -06:00
let a$0 = "test";
foo!(a);
}
"#,
expect![[r#"
a Local FileId(0) 59..60 Other
FileId(0) 80..81 Other Read
"#]],
);
}
#[test]
fn test_map_range_to_original_ref() {
check(
r#"
macro_rules! foo {($i:ident) => {$i} }
fn main() {
let a = "test";
2021-01-06 14:15:48 -06:00
foo!(a$0);
}
"#,
expect![[r#"
a Local FileId(0) 59..60 Other
FileId(0) 80..81 Other Read
"#]],
);
}
2020-12-23 10:15:01 -06:00
#[test]
fn test_find_labels() {
check(
r#"
fn foo<'a>() -> &'a () {
'a: loop {
'b: loop {
2021-01-06 14:15:48 -06:00
continue 'a$0;
2020-12-23 10:15:01 -06:00
}
break 'a;
}
}
"#,
expect![[r#"
'a Label FileId(0) 29..32 29..31 Lifetime
FileId(0) 80..82 Lifetime
FileId(0) 108..110 Lifetime
"#]],
);
}
2021-01-01 03:07:01 -06:00
#[test]
fn test_find_const_param() {
check(
r#"
2021-01-06 14:15:48 -06:00
fn foo<const FOO$0: usize>() -> usize {
2021-01-01 03:07:01 -06:00
FOO
}
"#,
expect![[r#"
FOO ConstParam FileId(0) 7..23 13..16 Other
FileId(0) 42..45 Other
"#]],
);
}
2019-01-18 02:29:09 -06:00
}