1 //! This module implements a reference search.
2 //! First, the element at the cursor position must be either an `ast::Name`
3 //! or `ast::NameRef`. If it's a `ast::NameRef`, at the classification step we
4 //! try to resolve the direct tree parent of this element, otherwise we
5 //! already have a definition and just need to get its HIR together with
6 //! some information that is needed for further steps of searching.
7 //! After that, we collect files that might contain references and look
8 //! for text occurrences of the identifier. If there's an `ast::NameRef`
9 //! at the index that the match starts at and its tree parent is
10 //! resolved to the search element definition, we get a reference.
12 use hir::{PathResolution, Semantics};
15 defs::{Definition, NameClass, NameRefClass},
16 search::{ReferenceAccess, SearchScope},
19 use rustc_hash::FxHashMap;
21 algo::find_node_at_offset,
22 ast::{self, NameOwner},
23 match_ast, AstNode, SyntaxNode, TextRange, TextSize, T,
26 use crate::{display::TryToNav, FilePosition, NavigationTarget};
28 #[derive(Debug, Clone)]
29 pub struct ReferenceSearchResult {
30 pub declaration: Option<Declaration>,
31 pub references: FxHashMap<FileId, Vec<(TextRange, Option<ReferenceAccess>)>>,
34 #[derive(Debug, Clone)]
35 pub struct Declaration {
36 pub nav: NavigationTarget,
37 pub access: Option<ReferenceAccess>,
40 // Feature: Find All References
42 // Shows all references of the item at the cursor location
45 // | Editor | Shortcut
47 // | VS Code | kbd:[Shift+Alt+F12]
50 // image::https://user-images.githubusercontent.com/48062697/113020670-b7c34f00-917a-11eb-8003-370ac5f2b3cb.gif[]
51 pub(crate) fn find_all_refs(
52 sema: &Semantics<RootDatabase>,
53 position: FilePosition,
54 search_scope: Option<SearchScope>,
55 ) -> Option<ReferenceSearchResult> {
56 let _p = profile::span("find_all_refs");
57 let syntax = sema.parse(position.file_id).syntax().clone();
59 let (def, is_literal_search) =
60 if let Some(name) = get_name_of_item_declaration(&syntax, position) {
61 (NameClass::classify(sema, &name)?.referenced_or_defined(), true)
63 (find_def(sema, &syntax, position.offset)?, false)
66 let mut usages = def.usages(sema).set_scope(search_scope).include_self_refs().all();
67 if is_literal_search {
68 // filter for constructor-literals
69 let refs = usages.references.values_mut();
71 Definition::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Enum(enum_))) => {
73 it.retain(|reference| {
77 .map_or(false, |name_ref| is_enum_lit_name_ref(sema, enum_, name_ref))
80 usages.references.retain(|_, it| !it.is_empty());
82 Definition::ModuleDef(hir::ModuleDef::Adt(_) | hir::ModuleDef::Variant(_)) => {
84 it.retain(|reference| {
85 reference.name.as_name_ref().map_or(false, is_lit_name_ref)
88 usages.references.retain(|_, it| !it.is_empty());
93 let declaration = match def {
94 Definition::ModuleDef(hir::ModuleDef::Module(module)) => {
95 Some(NavigationTarget::from_module_to_decl(sema.db, module))
97 def => def.try_to_nav(sema.db),
100 let decl_range = nav.focus_or_full_range();
101 Declaration { nav, access: decl_access(&def, &syntax, decl_range) }
103 let references = usages
105 .map(|(file_id, refs)| {
106 (file_id, refs.into_iter().map(|file_ref| (file_ref.range, file_ref.access)).collect())
110 Some(ReferenceSearchResult { declaration, references })
113 pub(crate) fn find_def(
114 sema: &Semantics<RootDatabase>,
117 ) -> Option<Definition> {
118 let def = match sema.find_node_at_offset_with_descend(syntax, offset)? {
119 ast::NameLike::NameRef(name_ref) => NameRefClass::classify(sema, &name_ref)?.referenced(),
120 ast::NameLike::Name(name) => NameClass::classify(sema, &name)?.referenced_or_defined(),
121 ast::NameLike::Lifetime(lifetime) => NameRefClass::classify_lifetime(sema, &lifetime)
122 .map(|class| class.referenced())
124 NameClass::classify_lifetime(sema, &lifetime)
125 .map(|class| class.referenced_or_defined())
131 pub(crate) fn decl_access(
135 ) -> Option<ReferenceAccess> {
137 Definition::Local(_) | Definition::Field(_) => {}
141 let stmt = find_node_at_offset::<ast::LetStmt>(syntax, range.start())?;
142 if stmt.initializer().is_some() {
143 let pat = stmt.pat()?;
144 if let ast::Pat::IdentPat(it) = pat {
145 if it.mut_token().is_some() {
146 return Some(ReferenceAccess::Write);
154 fn get_name_of_item_declaration(syntax: &SyntaxNode, position: FilePosition) -> Option<ast::Name> {
155 let token = syntax.token_at_offset(position.offset).right_biased()?;
156 let token_parent = token.parent()?;
157 let kind = token.kind();
159 ast::Struct::cast(token_parent)
160 .filter(|struct_| struct_.field_list().is_none())
161 .and_then(|struct_| struct_.name())
162 } else if kind == T!['{'] {
165 ast::RecordFieldList(rfl) => match_ast! {
166 match (rfl.syntax().parent()?) {
167 ast::Variant(it) => it.name(),
168 ast::Struct(it) => it.name(),
169 ast::Union(it) => it.name(),
173 ast::VariantList(vl) => ast::Enum::cast(vl.syntax().parent()?)?.name(),
177 } else if kind == T!['('] {
178 let tfl = ast::TupleFieldList::cast(token_parent)?;
180 match (tfl.syntax().parent()?) {
181 ast::Variant(it) => it.name(),
182 ast::Struct(it) => it.name(),
191 fn is_enum_lit_name_ref(
192 sema: &Semantics<RootDatabase>,
194 name_ref: &ast::NameRef,
196 let path_is_variant_of_enum = |path: ast::Path| {
198 sema.resolve_path(&path),
199 Some(PathResolution::Def(hir::ModuleDef::Variant(variant)))
200 if variant.parent_enum(sema.db) == enum_
206 .find_map(|ancestor| {
209 ast::PathExpr(path_expr) => path_expr.path().map(path_is_variant_of_enum),
210 ast::RecordExpr(record_expr) => record_expr.path().map(path_is_variant_of_enum),
218 fn path_ends_with(path: Option<ast::Path>, name_ref: &ast::NameRef) -> bool {
219 path.and_then(|path| path.segment())
220 .and_then(|segment| segment.name_ref())
221 .map_or(false, |segment| segment == *name_ref)
224 fn is_lit_name_ref(name_ref: &ast::NameRef) -> bool {
225 name_ref.syntax().ancestors().find_map(|ancestor| {
228 ast::PathExpr(path_expr) => Some(path_ends_with(path_expr.path(), name_ref)),
229 ast::RecordExpr(record_expr) => Some(path_ends_with(record_expr.path(), name_ref)),
238 use expect_test::{expect, Expect};
239 use ide_db::base_db::FileId;
242 use crate::{fixture, SearchScope};
245 fn test_struct_literal_after_space() {
256 f = Foo {a: Foo::f()};
260 Foo Struct FileId(0) 0..26 7..10
268 fn test_struct_literal_before_space() {
278 Foo Struct FileId(0) 0..13 7..10
287 fn test_struct_literal_with_generic_type() {
297 Foo Struct FileId(0) 0..16 7..10
305 fn test_struct_literal_for_tuple() {
316 Foo Struct FileId(0) 0..16 7..10
324 fn test_struct_literal_for_union() {
337 Foo Union FileId(0) 0..24 6..9
345 fn test_enum_after_space() {
361 Foo Enum FileId(0) 0..37 5..8
371 fn test_variant_record_after_space() {
381 f = Foo::A { n: 92 };
385 A Variant FileId(0) 15..27 15..16
392 fn test_variant_tuple_before_paren() {
406 A Variant FileId(0) 15..21 15..16
414 fn test_enum_before_space() {
427 Foo Enum FileId(0) 0..26 5..8
436 fn test_enum_with_generic_type() {
449 Foo Enum FileId(0) 0..32 5..8
457 fn test_enum_for_tuple() {
470 Foo Enum FileId(0) 0..33 5..8
478 fn test_find_all_refs_for_local() {
493 i Local FileId(0) 20..25 24..25 Write
495 FileId(0) 50..51 Write
496 FileId(0) 54..55 Read
497 FileId(0) 76..77 Write
498 FileId(0) 94..95 Write
504 fn search_filters_by_range() {
517 spam Local FileId(0) 19..23 19..23
519 FileId(0) 34..38 Read
520 FileId(0) 41..45 Read
526 fn test_find_all_refs_for_param_inside() {
529 fn foo(i : u32) -> u32 { i$0 }
532 i ValueParam FileId(0) 7..8 7..8
534 FileId(0) 25..26 Read
540 fn test_find_all_refs_for_fn_param() {
543 fn foo(i$0 : u32) -> u32 { i }
546 i ValueParam FileId(0) 7..8 7..8
548 FileId(0) 25..26 Read
554 fn test_find_all_refs_field_name() {
567 spam Field FileId(0) 17..30 21..25
569 FileId(0) 67..71 Read
575 fn test_find_all_refs_impl_item_name() {
584 f Function FileId(0) 27..43 30..31
591 fn test_find_all_refs_enum_var_name() {
601 B Variant FileId(0) 22..23 22..23
608 fn test_find_all_refs_enum_var_field() {
618 field Field FileId(0) 26..35 26..31
625 fn test_find_all_refs_two_modules() {
633 let i = foo::Foo { n: 5 };
644 let i = bar::Bar { n: 5 };
655 let i = foo::Foo$0 { n: 5 };
659 Foo Struct FileId(1) 17..51 28..31
668 fn test_find_all_refs_decl_module() {
677 let i = Foo { n: 5 };
686 foo Module FileId(0) 0..8 4..7
694 fn test_find_all_refs_decl_module_on_self() {
704 foo Module FileId(0) 0..8 4..7
712 fn test_find_all_refs_decl_module_on_self_crate_root() {
719 Module FileId(0) 0..10
726 fn test_find_all_refs_super_mod_vis() {
737 let i = Foo { n: 5 };
741 pub(super) struct Foo$0 {
746 Foo Struct FileId(2) 0..41 18..21
755 fn test_find_all_refs_with_scope() {
764 fn f() { super::quux(); }
767 fn f() { super::quux(); }
774 quux Function FileId(0) 19..35 26..30
783 Some(SearchScope::single_file(FileId(2))),
785 quux Function FileId(0) 19..35 26..30
793 fn test_find_all_refs_macro_def() {
797 macro_rules! m1$0 { () => (()) }
805 m1 Macro FileId(0) 0..46 29..31
814 fn test_basic_highlight_read_write() {
823 i Local FileId(0) 19..24 23..24 Write
825 FileId(0) 34..35 Write
826 FileId(0) 38..39 Read
832 fn test_basic_highlight_field_read_write() {
845 f Field FileId(0) 15..21 15..16
847 FileId(0) 55..56 Read
848 FileId(0) 68..69 Write
854 fn test_basic_highlight_decl_no_write() {
863 i Local FileId(0) 19..20 19..20
865 FileId(0) 26..27 Write
871 fn test_find_struct_function_refs_outside_module() {
878 pub fn new$0() -> Foo { Foo }
883 let _f = foo::Foo::new();
887 new Function FileId(0) 54..81 61..64
895 fn test_find_all_refs_nested_module() {
909 f Function FileId(0) 22..31 25..26
918 fn test_find_all_refs_struct_pat() {
932 field Field FileId(0) 15..24 15..20
934 FileId(0) 68..73 Read
940 fn test_find_all_refs_enum_var_pat() {
951 En::Variant { field } => {}
956 field Field FileId(0) 32..41 32..37
958 FileId(0) 102..107 Read
964 fn test_find_all_refs_enum_var_privacy() {
976 m::En::Variant { field: 0 }
980 field Field FileId(0) 56..65 56..61
982 FileId(0) 125..130 Read
988 fn test_find_self_refs() {
991 struct Foo { bar: i32 }
1005 self SelfParam FileId(0) 47..51 47..51
1007 FileId(0) 71..75 Read
1008 FileId(0) 152..156 Read
1014 fn test_find_self_refs_decl() {
1017 struct Foo { bar: i32 }
1026 self SelfParam FileId(0) 47..51 47..51
1028 FileId(0) 63..67 Read
1033 fn check(ra_fixture: &str, expect: Expect) {
1034 check_with_scope(ra_fixture, None, expect)
1037 fn check_with_scope(ra_fixture: &str, search_scope: Option<SearchScope>, expect: Expect) {
1038 let (analysis, pos) = fixture::position(ra_fixture);
1039 let refs = analysis.find_all_refs(pos, search_scope).unwrap().unwrap();
1041 let mut actual = String::new();
1042 if let Some(decl) = refs.declaration {
1043 format_to!(actual, "{}", decl.nav.debug_render());
1044 if let Some(access) = decl.access {
1045 format_to!(actual, " {:?}", access)
1050 for (file_id, references) in refs.references {
1051 for (range, access) in references {
1052 format_to!(actual, "{:?} {:?}", file_id, range);
1053 if let Some(access) = access {
1054 format_to!(actual, " {:?}", access);
1059 expect.assert_eq(actual.trim_start())
1063 fn test_find_lifetimes_function() {
1067 impl<'a> Foo<'a> for &'a () {}
1068 fn foo<'a, 'b: 'a>(x: &'a$0 ()) -> &'a () where &'a (): Foo<'a> {
1069 fn bar<'a>(_: &'a ()) {}
1074 'a LifetimeParam FileId(0) 55..57 55..57
1086 fn test_find_lifetimes_type_alias() {
1089 type Foo<'a, T> where T: 'a$0 = &'a T;
1092 'a LifetimeParam FileId(0) 9..11 9..11
1101 fn test_find_lifetimes_trait_impl() {
1107 impl<'a> Foo<'a> for &'a () {
1108 fn foo() -> &'a$0 () {
1114 'a LifetimeParam FileId(0) 47..49 47..49
1124 fn test_map_range_to_original() {
1127 macro_rules! foo {($i:ident) => {$i} }
1134 a Local FileId(0) 59..60 59..60
1136 FileId(0) 80..81 Read
1142 fn test_map_range_to_original_ref() {
1145 macro_rules! foo {($i:ident) => {$i} }
1152 a Local FileId(0) 59..60 59..60
1154 FileId(0) 80..81 Read
1160 fn test_find_labels() {
1163 fn foo<'a>() -> &'a () {
1173 'a Label FileId(0) 29..32 29..31
1182 fn test_find_const_param() {
1185 fn foo<const FOO$0: usize>() -> usize {
1190 FOO ConstParam FileId(0) 7..23 13..16
1201 trait Foo$0 where Self: {}
1206 Foo Trait FileId(0) 0..24 6..9
1214 fn test_trait_self() {
1217 trait Foo where Self$0 {
1224 Self TypeParam FileId(0) 6..9 6..9
1238 impl Foo where Self: {
1243 Foo Struct FileId(0) 0..11 7..10
1254 impl Foo where Self: {
1259 impl Impl FileId(0) 13..57 18..21
1268 fn test_self_variant_with_payload() {
1276 Self::Bar$0() => (),
1283 Bar Variant FileId(0) 11..16 11..14
1291 fn test_attr_differs_from_fn_with_same_name() {
1300 test Function FileId(0) 0..33 11..15
1308 fn test_const_in_pattern() {
1311 const A$0: i32 = 42;
1322 A Const FileId(0) 0..18 6..7
1333 fn test_primitives() {
1336 fn foo(_: bool) -> bo$0ol { true }
1346 fn test_transitive() {
1349 //- /level3.rs new_source_root: crate:level3
1351 //- /level2.rs new_source_root: crate:level2 deps:level3
1352 pub use level3::Foo;
1353 //- /level1.rs new_source_root: crate:level1 deps:level2
1354 pub use level2::Foo;
1355 //- /level0.rs new_source_root: crate:level0 deps:level1
1356 pub use level1::Foo;
1359 Foo Struct FileId(0) 0..15 11..14
1369 fn test_decl_macro_references() {
1372 //- /lib.rs crate:lib
1380 macro_rules! foo$0 {
1381 () => {struct Foo;};
1385 //- /other.rs crate:other deps:lib new_source_root:
1389 foo Macro FileId(1) 0..61 29..32
1399 fn macro_doesnt_reference_attribute_on_call() {
1406 #[proc_macro_test::attr_noop]
1411 m Macro FileId(0) 0..32 13..14