diff options
Diffstat (limited to 'crates/ide/src/references.rs')
-rw-r--r-- | crates/ide/src/references.rs | 696 |
1 files changed, 696 insertions, 0 deletions
diff --git a/crates/ide/src/references.rs b/crates/ide/src/references.rs new file mode 100644 index 000000000..722c8f406 --- /dev/null +++ b/crates/ide/src/references.rs | |||
@@ -0,0 +1,696 @@ | |||
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 futher 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. | ||
11 | |||
12 | mod rename; | ||
13 | |||
14 | use hir::Semantics; | ||
15 | use ide_db::{ | ||
16 | defs::{classify_name, classify_name_ref, Definition}, | ||
17 | search::SearchScope, | ||
18 | RootDatabase, | ||
19 | }; | ||
20 | use syntax::{ | ||
21 | algo::find_node_at_offset, | ||
22 | ast::{self, NameOwner}, | ||
23 | AstNode, SyntaxKind, SyntaxNode, TextRange, TokenAtOffset, | ||
24 | }; | ||
25 | |||
26 | use crate::{display::TryToNav, FilePosition, FileRange, NavigationTarget, RangeInfo}; | ||
27 | |||
28 | pub(crate) use self::rename::rename; | ||
29 | |||
30 | pub use ide_db::search::{Reference, ReferenceAccess, ReferenceKind}; | ||
31 | |||
32 | #[derive(Debug, Clone)] | ||
33 | pub struct ReferenceSearchResult { | ||
34 | declaration: Declaration, | ||
35 | references: Vec<Reference>, | ||
36 | } | ||
37 | |||
38 | #[derive(Debug, Clone)] | ||
39 | pub struct Declaration { | ||
40 | pub nav: NavigationTarget, | ||
41 | pub kind: ReferenceKind, | ||
42 | pub access: Option<ReferenceAccess>, | ||
43 | } | ||
44 | |||
45 | impl ReferenceSearchResult { | ||
46 | pub fn declaration(&self) -> &Declaration { | ||
47 | &self.declaration | ||
48 | } | ||
49 | |||
50 | pub fn decl_target(&self) -> &NavigationTarget { | ||
51 | &self.declaration.nav | ||
52 | } | ||
53 | |||
54 | pub fn references(&self) -> &[Reference] { | ||
55 | &self.references | ||
56 | } | ||
57 | |||
58 | /// Total number of references | ||
59 | /// At least 1 since all valid references should | ||
60 | /// Have a declaration | ||
61 | pub fn len(&self) -> usize { | ||
62 | self.references.len() + 1 | ||
63 | } | ||
64 | } | ||
65 | |||
66 | // allow turning ReferenceSearchResult into an iterator | ||
67 | // over References | ||
68 | impl IntoIterator for ReferenceSearchResult { | ||
69 | type Item = Reference; | ||
70 | type IntoIter = std::vec::IntoIter<Reference>; | ||
71 | |||
72 | fn into_iter(mut self) -> Self::IntoIter { | ||
73 | let mut v = Vec::with_capacity(self.len()); | ||
74 | v.push(Reference { | ||
75 | file_range: FileRange { | ||
76 | file_id: self.declaration.nav.file_id, | ||
77 | range: self.declaration.nav.focus_or_full_range(), | ||
78 | }, | ||
79 | kind: self.declaration.kind, | ||
80 | access: self.declaration.access, | ||
81 | }); | ||
82 | v.append(&mut self.references); | ||
83 | v.into_iter() | ||
84 | } | ||
85 | } | ||
86 | |||
87 | pub(crate) fn find_all_refs( | ||
88 | sema: &Semantics<RootDatabase>, | ||
89 | position: FilePosition, | ||
90 | search_scope: Option<SearchScope>, | ||
91 | ) -> Option<RangeInfo<ReferenceSearchResult>> { | ||
92 | let _p = profile::span("find_all_refs"); | ||
93 | let syntax = sema.parse(position.file_id).syntax().clone(); | ||
94 | |||
95 | let (opt_name, search_kind) = if let Some(name) = | ||
96 | get_struct_def_name_for_struct_literal_search(&sema, &syntax, position) | ||
97 | { | ||
98 | (Some(name), ReferenceKind::StructLiteral) | ||
99 | } else { | ||
100 | ( | ||
101 | sema.find_node_at_offset_with_descend::<ast::Name>(&syntax, position.offset), | ||
102 | ReferenceKind::Other, | ||
103 | ) | ||
104 | }; | ||
105 | |||
106 | let RangeInfo { range, info: def } = find_name(&sema, &syntax, position, opt_name)?; | ||
107 | |||
108 | let references = def | ||
109 | .usages(sema) | ||
110 | .set_scope(search_scope) | ||
111 | .all() | ||
112 | .into_iter() | ||
113 | .filter(|r| search_kind == ReferenceKind::Other || search_kind == r.kind) | ||
114 | .collect(); | ||
115 | |||
116 | let decl_range = def.try_to_nav(sema.db)?.focus_or_full_range(); | ||
117 | |||
118 | let declaration = Declaration { | ||
119 | nav: def.try_to_nav(sema.db)?, | ||
120 | kind: ReferenceKind::Other, | ||
121 | access: decl_access(&def, &syntax, decl_range), | ||
122 | }; | ||
123 | |||
124 | Some(RangeInfo::new(range, ReferenceSearchResult { declaration, references })) | ||
125 | } | ||
126 | |||
127 | fn find_name( | ||
128 | sema: &Semantics<RootDatabase>, | ||
129 | syntax: &SyntaxNode, | ||
130 | position: FilePosition, | ||
131 | opt_name: Option<ast::Name>, | ||
132 | ) -> Option<RangeInfo<Definition>> { | ||
133 | if let Some(name) = opt_name { | ||
134 | let def = classify_name(sema, &name)?.definition(sema.db); | ||
135 | let range = name.syntax().text_range(); | ||
136 | return Some(RangeInfo::new(range, def)); | ||
137 | } | ||
138 | let name_ref = | ||
139 | sema.find_node_at_offset_with_descend::<ast::NameRef>(&syntax, position.offset)?; | ||
140 | let def = classify_name_ref(sema, &name_ref)?.definition(sema.db); | ||
141 | let range = name_ref.syntax().text_range(); | ||
142 | Some(RangeInfo::new(range, def)) | ||
143 | } | ||
144 | |||
145 | fn decl_access(def: &Definition, syntax: &SyntaxNode, range: TextRange) -> Option<ReferenceAccess> { | ||
146 | match def { | ||
147 | Definition::Local(_) | Definition::Field(_) => {} | ||
148 | _ => return None, | ||
149 | }; | ||
150 | |||
151 | let stmt = find_node_at_offset::<ast::LetStmt>(syntax, range.start())?; | ||
152 | if stmt.initializer().is_some() { | ||
153 | let pat = stmt.pat()?; | ||
154 | if let ast::Pat::IdentPat(it) = pat { | ||
155 | if it.mut_token().is_some() { | ||
156 | return Some(ReferenceAccess::Write); | ||
157 | } | ||
158 | } | ||
159 | } | ||
160 | |||
161 | None | ||
162 | } | ||
163 | |||
164 | fn get_struct_def_name_for_struct_literal_search( | ||
165 | sema: &Semantics<RootDatabase>, | ||
166 | syntax: &SyntaxNode, | ||
167 | position: FilePosition, | ||
168 | ) -> Option<ast::Name> { | ||
169 | if let TokenAtOffset::Between(ref left, ref right) = syntax.token_at_offset(position.offset) { | ||
170 | if right.kind() != SyntaxKind::L_CURLY && right.kind() != SyntaxKind::L_PAREN { | ||
171 | return None; | ||
172 | } | ||
173 | if let Some(name) = | ||
174 | sema.find_node_at_offset_with_descend::<ast::Name>(&syntax, left.text_range().start()) | ||
175 | { | ||
176 | return name.syntax().ancestors().find_map(ast::Struct::cast).and_then(|l| l.name()); | ||
177 | } | ||
178 | if sema | ||
179 | .find_node_at_offset_with_descend::<ast::GenericParamList>( | ||
180 | &syntax, | ||
181 | left.text_range().start(), | ||
182 | ) | ||
183 | .is_some() | ||
184 | { | ||
185 | return left.ancestors().find_map(ast::Struct::cast).and_then(|l| l.name()); | ||
186 | } | ||
187 | } | ||
188 | None | ||
189 | } | ||
190 | |||
191 | #[cfg(test)] | ||
192 | mod tests { | ||
193 | use crate::{ | ||
194 | mock_analysis::{analysis_and_position, MockAnalysis}, | ||
195 | Declaration, Reference, ReferenceSearchResult, SearchScope, | ||
196 | }; | ||
197 | |||
198 | #[test] | ||
199 | fn test_struct_literal_after_space() { | ||
200 | let refs = get_all_refs( | ||
201 | r#" | ||
202 | struct Foo <|>{ | ||
203 | a: i32, | ||
204 | } | ||
205 | impl Foo { | ||
206 | fn f() -> i32 { 42 } | ||
207 | } | ||
208 | fn main() { | ||
209 | let f: Foo; | ||
210 | f = Foo {a: Foo::f()}; | ||
211 | } | ||
212 | "#, | ||
213 | ); | ||
214 | check_result( | ||
215 | refs, | ||
216 | "Foo STRUCT FileId(1) 0..26 7..10 Other", | ||
217 | &["FileId(1) 101..104 StructLiteral"], | ||
218 | ); | ||
219 | } | ||
220 | |||
221 | #[test] | ||
222 | fn test_struct_literal_before_space() { | ||
223 | let refs = get_all_refs( | ||
224 | r#" | ||
225 | struct Foo<|> {} | ||
226 | fn main() { | ||
227 | let f: Foo; | ||
228 | f = Foo {}; | ||
229 | } | ||
230 | "#, | ||
231 | ); | ||
232 | check_result( | ||
233 | refs, | ||
234 | "Foo STRUCT FileId(1) 0..13 7..10 Other", | ||
235 | &["FileId(1) 41..44 Other", "FileId(1) 54..57 StructLiteral"], | ||
236 | ); | ||
237 | } | ||
238 | |||
239 | #[test] | ||
240 | fn test_struct_literal_with_generic_type() { | ||
241 | let refs = get_all_refs( | ||
242 | r#" | ||
243 | struct Foo<T> <|>{} | ||
244 | fn main() { | ||
245 | let f: Foo::<i32>; | ||
246 | f = Foo {}; | ||
247 | } | ||
248 | "#, | ||
249 | ); | ||
250 | check_result( | ||
251 | refs, | ||
252 | "Foo STRUCT FileId(1) 0..16 7..10 Other", | ||
253 | &["FileId(1) 64..67 StructLiteral"], | ||
254 | ); | ||
255 | } | ||
256 | |||
257 | #[test] | ||
258 | fn test_struct_literal_for_tuple() { | ||
259 | let refs = get_all_refs( | ||
260 | r#" | ||
261 | struct Foo<|>(i32); | ||
262 | |||
263 | fn main() { | ||
264 | let f: Foo; | ||
265 | f = Foo(1); | ||
266 | } | ||
267 | "#, | ||
268 | ); | ||
269 | check_result( | ||
270 | refs, | ||
271 | "Foo STRUCT FileId(1) 0..16 7..10 Other", | ||
272 | &["FileId(1) 54..57 StructLiteral"], | ||
273 | ); | ||
274 | } | ||
275 | |||
276 | #[test] | ||
277 | fn test_find_all_refs_for_local() { | ||
278 | let refs = get_all_refs( | ||
279 | r#" | ||
280 | fn main() { | ||
281 | let mut i = 1; | ||
282 | let j = 1; | ||
283 | i = i<|> + j; | ||
284 | |||
285 | { | ||
286 | i = 0; | ||
287 | } | ||
288 | |||
289 | i = 5; | ||
290 | }"#, | ||
291 | ); | ||
292 | check_result( | ||
293 | refs, | ||
294 | "i IDENT_PAT FileId(1) 24..25 Other Write", | ||
295 | &[ | ||
296 | "FileId(1) 50..51 Other Write", | ||
297 | "FileId(1) 54..55 Other Read", | ||
298 | "FileId(1) 76..77 Other Write", | ||
299 | "FileId(1) 94..95 Other Write", | ||
300 | ], | ||
301 | ); | ||
302 | } | ||
303 | |||
304 | #[test] | ||
305 | fn search_filters_by_range() { | ||
306 | let refs = get_all_refs( | ||
307 | r#" | ||
308 | fn foo() { | ||
309 | let spam<|> = 92; | ||
310 | spam + spam | ||
311 | } | ||
312 | fn bar() { | ||
313 | let spam = 92; | ||
314 | spam + spam | ||
315 | } | ||
316 | "#, | ||
317 | ); | ||
318 | check_result( | ||
319 | refs, | ||
320 | "spam IDENT_PAT FileId(1) 19..23 Other", | ||
321 | &["FileId(1) 34..38 Other Read", "FileId(1) 41..45 Other Read"], | ||
322 | ); | ||
323 | } | ||
324 | |||
325 | #[test] | ||
326 | fn test_find_all_refs_for_param_inside() { | ||
327 | let refs = get_all_refs( | ||
328 | r#" | ||
329 | fn foo(i : u32) -> u32 { | ||
330 | i<|> | ||
331 | } | ||
332 | "#, | ||
333 | ); | ||
334 | check_result(refs, "i IDENT_PAT FileId(1) 7..8 Other", &["FileId(1) 29..30 Other Read"]); | ||
335 | } | ||
336 | |||
337 | #[test] | ||
338 | fn test_find_all_refs_for_fn_param() { | ||
339 | let refs = get_all_refs( | ||
340 | r#" | ||
341 | fn foo(i<|> : u32) -> u32 { | ||
342 | i | ||
343 | } | ||
344 | "#, | ||
345 | ); | ||
346 | check_result(refs, "i IDENT_PAT FileId(1) 7..8 Other", &["FileId(1) 29..30 Other Read"]); | ||
347 | } | ||
348 | |||
349 | #[test] | ||
350 | fn test_find_all_refs_field_name() { | ||
351 | let refs = get_all_refs( | ||
352 | r#" | ||
353 | //- /lib.rs | ||
354 | struct Foo { | ||
355 | pub spam<|>: u32, | ||
356 | } | ||
357 | |||
358 | fn main(s: Foo) { | ||
359 | let f = s.spam; | ||
360 | } | ||
361 | "#, | ||
362 | ); | ||
363 | check_result( | ||
364 | refs, | ||
365 | "spam RECORD_FIELD FileId(1) 17..30 21..25 Other", | ||
366 | &["FileId(1) 67..71 Other Read"], | ||
367 | ); | ||
368 | } | ||
369 | |||
370 | #[test] | ||
371 | fn test_find_all_refs_impl_item_name() { | ||
372 | let refs = get_all_refs( | ||
373 | r#" | ||
374 | struct Foo; | ||
375 | impl Foo { | ||
376 | fn f<|>(&self) { } | ||
377 | } | ||
378 | "#, | ||
379 | ); | ||
380 | check_result(refs, "f FN FileId(1) 27..43 30..31 Other", &[]); | ||
381 | } | ||
382 | |||
383 | #[test] | ||
384 | fn test_find_all_refs_enum_var_name() { | ||
385 | let refs = get_all_refs( | ||
386 | r#" | ||
387 | enum Foo { | ||
388 | A, | ||
389 | B<|>, | ||
390 | C, | ||
391 | } | ||
392 | "#, | ||
393 | ); | ||
394 | check_result(refs, "B VARIANT FileId(1) 22..23 22..23 Other", &[]); | ||
395 | } | ||
396 | |||
397 | #[test] | ||
398 | fn test_find_all_refs_two_modules() { | ||
399 | let (analysis, pos) = analysis_and_position( | ||
400 | r#" | ||
401 | //- /lib.rs | ||
402 | pub mod foo; | ||
403 | pub mod bar; | ||
404 | |||
405 | fn f() { | ||
406 | let i = foo::Foo { n: 5 }; | ||
407 | } | ||
408 | |||
409 | //- /foo.rs | ||
410 | use crate::bar; | ||
411 | |||
412 | pub struct Foo { | ||
413 | pub n: u32, | ||
414 | } | ||
415 | |||
416 | fn f() { | ||
417 | let i = bar::Bar { n: 5 }; | ||
418 | } | ||
419 | |||
420 | //- /bar.rs | ||
421 | use crate::foo; | ||
422 | |||
423 | pub struct Bar { | ||
424 | pub n: u32, | ||
425 | } | ||
426 | |||
427 | fn f() { | ||
428 | let i = foo::Foo<|> { n: 5 }; | ||
429 | } | ||
430 | "#, | ||
431 | ); | ||
432 | let refs = analysis.find_all_refs(pos, None).unwrap().unwrap(); | ||
433 | check_result( | ||
434 | refs, | ||
435 | "Foo STRUCT FileId(2) 17..51 28..31 Other", | ||
436 | &["FileId(1) 53..56 StructLiteral", "FileId(3) 79..82 StructLiteral"], | ||
437 | ); | ||
438 | } | ||
439 | |||
440 | // `mod foo;` is not in the results because `foo` is an `ast::Name`. | ||
441 | // So, there are two references: the first one is a definition of the `foo` module, | ||
442 | // which is the whole `foo.rs`, and the second one is in `use foo::Foo`. | ||
443 | #[test] | ||
444 | fn test_find_all_refs_decl_module() { | ||
445 | let (analysis, pos) = analysis_and_position( | ||
446 | r#" | ||
447 | //- /lib.rs | ||
448 | mod foo<|>; | ||
449 | |||
450 | use foo::Foo; | ||
451 | |||
452 | fn f() { | ||
453 | let i = Foo { n: 5 }; | ||
454 | } | ||
455 | |||
456 | //- /foo.rs | ||
457 | pub struct Foo { | ||
458 | pub n: u32, | ||
459 | } | ||
460 | "#, | ||
461 | ); | ||
462 | let refs = analysis.find_all_refs(pos, None).unwrap().unwrap(); | ||
463 | check_result(refs, "foo SOURCE_FILE FileId(2) 0..35 Other", &["FileId(1) 14..17 Other"]); | ||
464 | } | ||
465 | |||
466 | #[test] | ||
467 | fn test_find_all_refs_super_mod_vis() { | ||
468 | let (analysis, pos) = analysis_and_position( | ||
469 | r#" | ||
470 | //- /lib.rs | ||
471 | mod foo; | ||
472 | |||
473 | //- /foo.rs | ||
474 | mod some; | ||
475 | use some::Foo; | ||
476 | |||
477 | fn f() { | ||
478 | let i = Foo { n: 5 }; | ||
479 | } | ||
480 | |||
481 | //- /foo/some.rs | ||
482 | pub(super) struct Foo<|> { | ||
483 | pub n: u32, | ||
484 | } | ||
485 | "#, | ||
486 | ); | ||
487 | let refs = analysis.find_all_refs(pos, None).unwrap().unwrap(); | ||
488 | check_result( | ||
489 | refs, | ||
490 | "Foo STRUCT FileId(3) 0..41 18..21 Other", | ||
491 | &["FileId(2) 20..23 Other", "FileId(2) 47..50 StructLiteral"], | ||
492 | ); | ||
493 | } | ||
494 | |||
495 | #[test] | ||
496 | fn test_find_all_refs_with_scope() { | ||
497 | let code = r#" | ||
498 | //- /lib.rs | ||
499 | mod foo; | ||
500 | mod bar; | ||
501 | |||
502 | pub fn quux<|>() {} | ||
503 | |||
504 | //- /foo.rs | ||
505 | fn f() { super::quux(); } | ||
506 | |||
507 | //- /bar.rs | ||
508 | fn f() { super::quux(); } | ||
509 | "#; | ||
510 | |||
511 | let (mock, pos) = MockAnalysis::with_files_and_position(code); | ||
512 | let bar = mock.id_of("/bar.rs"); | ||
513 | let analysis = mock.analysis(); | ||
514 | |||
515 | let refs = analysis.find_all_refs(pos, None).unwrap().unwrap(); | ||
516 | check_result( | ||
517 | refs, | ||
518 | "quux FN FileId(1) 19..35 26..30 Other", | ||
519 | &["FileId(2) 16..20 StructLiteral", "FileId(3) 16..20 StructLiteral"], | ||
520 | ); | ||
521 | |||
522 | let refs = | ||
523 | analysis.find_all_refs(pos, Some(SearchScope::single_file(bar))).unwrap().unwrap(); | ||
524 | check_result( | ||
525 | refs, | ||
526 | "quux FN FileId(1) 19..35 26..30 Other", | ||
527 | &["FileId(3) 16..20 StructLiteral"], | ||
528 | ); | ||
529 | } | ||
530 | |||
531 | #[test] | ||
532 | fn test_find_all_refs_macro_def() { | ||
533 | let refs = get_all_refs( | ||
534 | r#" | ||
535 | #[macro_export] | ||
536 | macro_rules! m1<|> { () => (()) } | ||
537 | |||
538 | fn foo() { | ||
539 | m1(); | ||
540 | m1(); | ||
541 | } | ||
542 | "#, | ||
543 | ); | ||
544 | check_result( | ||
545 | refs, | ||
546 | "m1 MACRO_CALL FileId(1) 0..46 29..31 Other", | ||
547 | &["FileId(1) 63..65 StructLiteral", "FileId(1) 73..75 StructLiteral"], | ||
548 | ); | ||
549 | } | ||
550 | |||
551 | #[test] | ||
552 | fn test_basic_highlight_read_write() { | ||
553 | let refs = get_all_refs( | ||
554 | r#" | ||
555 | fn foo() { | ||
556 | let mut i<|> = 0; | ||
557 | i = i + 1; | ||
558 | } | ||
559 | "#, | ||
560 | ); | ||
561 | check_result( | ||
562 | refs, | ||
563 | "i IDENT_PAT FileId(1) 23..24 Other Write", | ||
564 | &["FileId(1) 34..35 Other Write", "FileId(1) 38..39 Other Read"], | ||
565 | ); | ||
566 | } | ||
567 | |||
568 | #[test] | ||
569 | fn test_basic_highlight_field_read_write() { | ||
570 | let refs = get_all_refs( | ||
571 | r#" | ||
572 | struct S { | ||
573 | f: u32, | ||
574 | } | ||
575 | |||
576 | fn foo() { | ||
577 | let mut s = S{f: 0}; | ||
578 | s.f<|> = 0; | ||
579 | } | ||
580 | "#, | ||
581 | ); | ||
582 | check_result( | ||
583 | refs, | ||
584 | "f RECORD_FIELD FileId(1) 15..21 15..16 Other", | ||
585 | &["FileId(1) 55..56 Other Read", "FileId(1) 68..69 Other Write"], | ||
586 | ); | ||
587 | } | ||
588 | |||
589 | #[test] | ||
590 | fn test_basic_highlight_decl_no_write() { | ||
591 | let refs = get_all_refs( | ||
592 | r#" | ||
593 | fn foo() { | ||
594 | let i<|>; | ||
595 | i = 1; | ||
596 | } | ||
597 | "#, | ||
598 | ); | ||
599 | check_result(refs, "i IDENT_PAT FileId(1) 19..20 Other", &["FileId(1) 26..27 Other Write"]); | ||
600 | } | ||
601 | |||
602 | #[test] | ||
603 | fn test_find_struct_function_refs_outside_module() { | ||
604 | let refs = get_all_refs( | ||
605 | r#" | ||
606 | mod foo { | ||
607 | pub struct Foo; | ||
608 | |||
609 | impl Foo { | ||
610 | pub fn new<|>() -> Foo { | ||
611 | Foo | ||
612 | } | ||
613 | } | ||
614 | } | ||
615 | |||
616 | fn main() { | ||
617 | let _f = foo::Foo::new(); | ||
618 | } | ||
619 | "#, | ||
620 | ); | ||
621 | check_result( | ||
622 | refs, | ||
623 | "new FN FileId(1) 54..101 61..64 Other", | ||
624 | &["FileId(1) 146..149 StructLiteral"], | ||
625 | ); | ||
626 | } | ||
627 | |||
628 | #[test] | ||
629 | fn test_find_all_refs_nested_module() { | ||
630 | let code = r#" | ||
631 | //- /lib.rs | ||
632 | mod foo { | ||
633 | mod bar; | ||
634 | } | ||
635 | |||
636 | fn f<|>() {} | ||
637 | |||
638 | //- /foo/bar.rs | ||
639 | use crate::f; | ||
640 | |||
641 | fn g() { | ||
642 | f(); | ||
643 | } | ||
644 | "#; | ||
645 | |||
646 | let (analysis, pos) = analysis_and_position(code); | ||
647 | let refs = analysis.find_all_refs(pos, None).unwrap().unwrap(); | ||
648 | check_result( | ||
649 | refs, | ||
650 | "f FN FileId(1) 26..35 29..30 Other", | ||
651 | &["FileId(2) 11..12 Other", "FileId(2) 28..29 StructLiteral"], | ||
652 | ); | ||
653 | } | ||
654 | |||
655 | fn get_all_refs(ra_fixture: &str) -> ReferenceSearchResult { | ||
656 | let (analysis, position) = analysis_and_position(ra_fixture); | ||
657 | analysis.find_all_refs(position, None).unwrap().unwrap() | ||
658 | } | ||
659 | |||
660 | fn check_result(res: ReferenceSearchResult, expected_decl: &str, expected_refs: &[&str]) { | ||
661 | res.declaration().assert_match(expected_decl); | ||
662 | assert_eq!(res.references.len(), expected_refs.len()); | ||
663 | res.references() | ||
664 | .iter() | ||
665 | .enumerate() | ||
666 | .for_each(|(i, r)| ref_assert_match(r, expected_refs[i])); | ||
667 | } | ||
668 | |||
669 | impl Declaration { | ||
670 | fn debug_render(&self) -> String { | ||
671 | let mut s = format!("{} {:?}", self.nav.debug_render(), self.kind); | ||
672 | if let Some(access) = self.access { | ||
673 | s.push_str(&format!(" {:?}", access)); | ||
674 | } | ||
675 | s | ||
676 | } | ||
677 | |||
678 | fn assert_match(&self, expected: &str) { | ||
679 | let actual = self.debug_render(); | ||
680 | test_utils::assert_eq_text!(expected.trim(), actual.trim(),); | ||
681 | } | ||
682 | } | ||
683 | |||
684 | fn ref_debug_render(r: &Reference) -> String { | ||
685 | let mut s = format!("{:?} {:?} {:?}", r.file_range.file_id, r.file_range.range, r.kind); | ||
686 | if let Some(access) = r.access { | ||
687 | s.push_str(&format!(" {:?}", access)); | ||
688 | } | ||
689 | s | ||
690 | } | ||
691 | |||
692 | fn ref_assert_match(r: &Reference, expected: &str) { | ||
693 | let actual = ref_debug_render(r); | ||
694 | test_utils::assert_eq_text!(expected.trim(), actual.trim(),); | ||
695 | } | ||
696 | } | ||