aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorkjeremy <[email protected]>2020-01-09 21:27:10 +0000
committerkjeremy <[email protected]>2020-01-10 20:14:19 +0000
commit49fd6a5228bfb2200611e6a9a9b24d01368bb279 (patch)
treeae4cd3555e5504369d1d4dd66224e2e760335ad3 /crates
parentcc96ddfe695c2b3d2e9f28c3c5205a83a99a8ac3 (diff)
Split Declaration out into it's own type
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_ide/src/lib.rs4
-rw-r--r--crates/ra_ide/src/references.rs100
2 files changed, 55 insertions, 49 deletions
diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs
index 837315ca7..4d8deb21c 100644
--- a/crates/ra_ide/src/lib.rs
+++ b/crates/ra_ide/src/lib.rs
@@ -75,7 +75,9 @@ pub use crate::{
75 inlay_hints::{InlayHint, InlayKind}, 75 inlay_hints::{InlayHint, InlayKind},
76 line_index::{LineCol, LineIndex}, 76 line_index::{LineCol, LineIndex},
77 line_index_utils::translate_offset_with_edit, 77 line_index_utils::translate_offset_with_edit,
78 references::{Reference, ReferenceAccess, ReferenceKind, ReferenceSearchResult, SearchScope}, 78 references::{
79 Declaration, Reference, ReferenceAccess, ReferenceKind, ReferenceSearchResult, SearchScope,
80 },
79 runnables::{Runnable, RunnableKind}, 81 runnables::{Runnable, RunnableKind},
80 source_change::{FileSystemEdit, SourceChange, SourceFileEdit}, 82 source_change::{FileSystemEdit, SourceChange, SourceFileEdit},
81 syntax_highlighting::HighlightedRange, 83 syntax_highlighting::HighlightedRange,
diff --git a/crates/ra_ide/src/references.rs b/crates/ra_ide/src/references.rs
index 5b8ed370c..2d20de1d0 100644
--- a/crates/ra_ide/src/references.rs
+++ b/crates/ra_ide/src/references.rs
@@ -37,12 +37,18 @@ pub use self::search_scope::SearchScope;
37 37
38#[derive(Debug, Clone)] 38#[derive(Debug, Clone)]
39pub struct ReferenceSearchResult { 39pub struct ReferenceSearchResult {
40 declaration: NavigationTarget, 40 declaration: Declaration,
41 declaration_kind: ReferenceKind,
42 references: Vec<Reference>, 41 references: Vec<Reference>,
43} 42}
44 43
45#[derive(Debug, Clone)] 44#[derive(Debug, Clone)]
45pub struct Declaration {
46 pub nav: NavigationTarget,
47 pub kind: ReferenceKind,
48 pub access: Option<ReferenceAccess>,
49}
50
51#[derive(Debug, Clone)]
46pub struct Reference { 52pub struct Reference {
47 pub file_range: FileRange, 53 pub file_range: FileRange,
48 pub kind: ReferenceKind, 54 pub kind: ReferenceKind,
@@ -62,10 +68,14 @@ pub enum ReferenceAccess {
62} 68}
63 69
64impl ReferenceSearchResult { 70impl ReferenceSearchResult {
65 pub fn declaration(&self) -> &NavigationTarget { 71 pub fn declaration(&self) -> &Declaration {
66 &self.declaration 72 &self.declaration
67 } 73 }
68 74
75 pub fn decl_target(&self) -> &NavigationTarget {
76 &self.declaration.nav
77 }
78
69 pub fn references(&self) -> &[Reference] { 79 pub fn references(&self) -> &[Reference] {
70 &self.references 80 &self.references
71 } 81 }
@@ -88,11 +98,11 @@ impl IntoIterator for ReferenceSearchResult {
88 let mut v = Vec::with_capacity(self.len()); 98 let mut v = Vec::with_capacity(self.len());
89 v.push(Reference { 99 v.push(Reference {
90 file_range: FileRange { 100 file_range: FileRange {
91 file_id: self.declaration.file_id(), 101 file_id: self.declaration.nav.file_id(),
92 range: self.declaration.range(), 102 range: self.declaration.nav.range(),
93 }, 103 },
94 kind: self.declaration_kind, 104 kind: self.declaration.kind,
95 access: None, 105 access: self.declaration.access,
96 }); 106 });
97 v.append(&mut self.references); 107 v.append(&mut self.references);
98 v.into_iter() 108 v.into_iter()
@@ -139,15 +149,14 @@ pub(crate) fn find_all_refs(
139 } 149 }
140 }; 150 };
141 151
152 let declaration = Declaration { nav: declaration, kind: ReferenceKind::Other, access: None };
153
142 let references = process_definition(db, def, name, search_scope) 154 let references = process_definition(db, def, name, search_scope)
143 .into_iter() 155 .into_iter()
144 .filter(|r| search_kind == ReferenceKind::Other || search_kind == r.kind) 156 .filter(|r| search_kind == ReferenceKind::Other || search_kind == r.kind)
145 .collect(); 157 .collect();
146 158
147 Some(RangeInfo::new( 159 Some(RangeInfo::new(range, ReferenceSearchResult { declaration, references }))
148 range,
149 ReferenceSearchResult { declaration, references, declaration_kind: ReferenceKind::Other },
150 ))
151} 160}
152 161
153fn find_name<'a>( 162fn find_name<'a>(
@@ -259,7 +268,7 @@ fn access_mode(kind: NameKind, name_ref: &ast::NameRef) -> Option<ReferenceAcces
259mod tests { 268mod tests {
260 use crate::{ 269 use crate::{
261 mock_analysis::{analysis_and_position, single_file_with_position, MockAnalysis}, 270 mock_analysis::{analysis_and_position, single_file_with_position, MockAnalysis},
262 Reference, ReferenceKind, ReferenceSearchResult, SearchScope, 271 Declaration, Reference, ReferenceSearchResult, SearchScope,
263 }; 272 };
264 273
265 #[test] 274 #[test]
@@ -279,8 +288,7 @@ mod tests {
279 let refs = get_all_refs(code); 288 let refs = get_all_refs(code);
280 check_result( 289 check_result(
281 refs, 290 refs,
282 "Foo STRUCT_DEF FileId(1) [5; 39) [12; 15)", 291 "Foo STRUCT_DEF FileId(1) [5; 39) [12; 15) Other",
283 ReferenceKind::Other,
284 &["FileId(1) [142; 145) StructLiteral"], 292 &["FileId(1) [142; 145) StructLiteral"],
285 ); 293 );
286 } 294 }
@@ -303,8 +311,7 @@ mod tests {
303 let refs = get_all_refs(code); 311 let refs = get_all_refs(code);
304 check_result( 312 check_result(
305 refs, 313 refs,
306 "i BIND_PAT FileId(1) [33; 34)", 314 "i BIND_PAT FileId(1) [33; 34) Other",
307 ReferenceKind::Other,
308 &[ 315 &[
309 "FileId(1) [67; 68) Other Write", 316 "FileId(1) [67; 68) Other Write",
310 "FileId(1) [71; 72) Other Read", 317 "FileId(1) [71; 72) Other Read",
@@ -324,8 +331,7 @@ mod tests {
324 let refs = get_all_refs(code); 331 let refs = get_all_refs(code);
325 check_result( 332 check_result(
326 refs, 333 refs,
327 "i BIND_PAT FileId(1) [12; 13)", 334 "i BIND_PAT FileId(1) [12; 13) Other",
328 ReferenceKind::Other,
329 &["FileId(1) [38; 39) Other Read"], 335 &["FileId(1) [38; 39) Other Read"],
330 ); 336 );
331 } 337 }
@@ -340,8 +346,7 @@ mod tests {
340 let refs = get_all_refs(code); 346 let refs = get_all_refs(code);
341 check_result( 347 check_result(
342 refs, 348 refs,
343 "i BIND_PAT FileId(1) [12; 13)", 349 "i BIND_PAT FileId(1) [12; 13) Other",
344 ReferenceKind::Other,
345 &["FileId(1) [38; 39) Other Read"], 350 &["FileId(1) [38; 39) Other Read"],
346 ); 351 );
347 } 352 }
@@ -362,8 +367,7 @@ mod tests {
362 let refs = get_all_refs(code); 367 let refs = get_all_refs(code);
363 check_result( 368 check_result(
364 refs, 369 refs,
365 "spam RECORD_FIELD_DEF FileId(1) [66; 79) [70; 74)", 370 "spam RECORD_FIELD_DEF FileId(1) [66; 79) [70; 74) Other",
366 ReferenceKind::Other,
367 &["FileId(1) [152; 156) Other Read"], 371 &["FileId(1) [152; 156) Other Read"],
368 ); 372 );
369 } 373 }
@@ -379,7 +383,7 @@ mod tests {
379 "#; 383 "#;
380 384
381 let refs = get_all_refs(code); 385 let refs = get_all_refs(code);
382 check_result(refs, "f FN_DEF FileId(1) [88; 104) [91; 92)", ReferenceKind::Other, &[]); 386 check_result(refs, "f FN_DEF FileId(1) [88; 104) [91; 92) Other", &[]);
383 } 387 }
384 388
385 #[test] 389 #[test]
@@ -394,7 +398,7 @@ mod tests {
394 "#; 398 "#;
395 399
396 let refs = get_all_refs(code); 400 let refs = get_all_refs(code);
397 check_result(refs, "B ENUM_VARIANT FileId(1) [83; 84) [83; 84)", ReferenceKind::Other, &[]); 401 check_result(refs, "B ENUM_VARIANT FileId(1) [83; 84) [83; 84) Other", &[]);
398 } 402 }
399 403
400 #[test] 404 #[test]
@@ -435,8 +439,7 @@ mod tests {
435 let refs = analysis.find_all_refs(pos, None).unwrap().unwrap(); 439 let refs = analysis.find_all_refs(pos, None).unwrap().unwrap();
436 check_result( 440 check_result(
437 refs, 441 refs,
438 "Foo STRUCT_DEF FileId(2) [16; 50) [27; 30)", 442 "Foo STRUCT_DEF FileId(2) [16; 50) [27; 30) Other",
439 ReferenceKind::Other,
440 &["FileId(1) [52; 55) StructLiteral", "FileId(3) [77; 80) StructLiteral"], 443 &["FileId(1) [52; 55) StructLiteral", "FileId(3) [77; 80) StructLiteral"],
441 ); 444 );
442 } 445 }
@@ -466,8 +469,7 @@ mod tests {
466 let refs = analysis.find_all_refs(pos, None).unwrap().unwrap(); 469 let refs = analysis.find_all_refs(pos, None).unwrap().unwrap();
467 check_result( 470 check_result(
468 refs, 471 refs,
469 "foo SOURCE_FILE FileId(2) [0; 35)", 472 "foo SOURCE_FILE FileId(2) [0; 35) Other",
470 ReferenceKind::Other,
471 &["FileId(1) [13; 16) Other"], 473 &["FileId(1) [13; 16) Other"],
472 ); 474 );
473 } 475 }
@@ -496,8 +498,7 @@ mod tests {
496 let refs = analysis.find_all_refs(pos, None).unwrap().unwrap(); 498 let refs = analysis.find_all_refs(pos, None).unwrap().unwrap();
497 check_result( 499 check_result(
498 refs, 500 refs,
499 "Foo STRUCT_DEF FileId(3) [0; 41) [18; 21)", 501 "Foo STRUCT_DEF FileId(3) [0; 41) [18; 21) Other",
500 ReferenceKind::Other,
501 &["FileId(2) [20; 23) Other", "FileId(2) [46; 49) StructLiteral"], 502 &["FileId(2) [20; 23) Other", "FileId(2) [46; 49) StructLiteral"],
502 ); 503 );
503 } 504 }
@@ -525,8 +526,7 @@ mod tests {
525 let refs = analysis.find_all_refs(pos, None).unwrap().unwrap(); 526 let refs = analysis.find_all_refs(pos, None).unwrap().unwrap();
526 check_result( 527 check_result(
527 refs, 528 refs,
528 "quux FN_DEF FileId(1) [18; 34) [25; 29)", 529 "quux FN_DEF FileId(1) [18; 34) [25; 29) Other",
529 ReferenceKind::Other,
530 &["FileId(2) [16; 20) Other", "FileId(3) [16; 20) Other"], 530 &["FileId(2) [16; 20) Other", "FileId(3) [16; 20) Other"],
531 ); 531 );
532 532
@@ -534,8 +534,7 @@ mod tests {
534 analysis.find_all_refs(pos, Some(SearchScope::single_file(bar))).unwrap().unwrap(); 534 analysis.find_all_refs(pos, Some(SearchScope::single_file(bar))).unwrap().unwrap();
535 check_result( 535 check_result(
536 refs, 536 refs,
537 "quux FN_DEF FileId(1) [18; 34) [25; 29)", 537 "quux FN_DEF FileId(1) [18; 34) [25; 29) Other",
538 ReferenceKind::Other,
539 &["FileId(3) [16; 20) Other"], 538 &["FileId(3) [16; 20) Other"],
540 ); 539 );
541 } 540 }
@@ -554,8 +553,7 @@ mod tests {
554 let refs = get_all_refs(code); 553 let refs = get_all_refs(code);
555 check_result( 554 check_result(
556 refs, 555 refs,
557 "m1 MACRO_CALL FileId(1) [9; 63) [46; 48)", 556 "m1 MACRO_CALL FileId(1) [9; 63) [46; 48) Other",
558 ReferenceKind::Other,
559 &["FileId(1) [96; 98) Other", "FileId(1) [114; 116) Other"], 557 &["FileId(1) [96; 98) Other", "FileId(1) [114; 116) Other"],
560 ); 558 );
561 } 559 }
@@ -571,8 +569,7 @@ mod tests {
571 let refs = get_all_refs(code); 569 let refs = get_all_refs(code);
572 check_result( 570 check_result(
573 refs, 571 refs,
574 "i BIND_PAT FileId(1) [36; 37)", 572 "i BIND_PAT FileId(1) [36; 37) Other",
575 ReferenceKind::Other,
576 &["FileId(1) [55; 56) Other Write", "FileId(1) [59; 60) Other Read"], 573 &["FileId(1) [55; 56) Other Write", "FileId(1) [59; 60) Other Read"],
577 ); 574 );
578 } 575 }
@@ -592,8 +589,7 @@ mod tests {
592 let refs = get_all_refs(code); 589 let refs = get_all_refs(code);
593 check_result( 590 check_result(
594 refs, 591 refs,
595 "f RECORD_FIELD_DEF FileId(1) [32; 38) [32; 33)", 592 "f RECORD_FIELD_DEF FileId(1) [32; 38) [32; 33) Other",
596 ReferenceKind::Other,
597 &["FileId(1) [96; 97) Other Read", "FileId(1) [117; 118) Other Write"], 593 &["FileId(1) [96; 97) Other Read", "FileId(1) [117; 118) Other Write"],
598 ); 594 );
599 } 595 }
@@ -603,19 +599,27 @@ mod tests {
603 analysis.find_all_refs(position, None).unwrap().unwrap() 599 analysis.find_all_refs(position, None).unwrap().unwrap()
604 } 600 }
605 601
606 fn check_result( 602 fn check_result(res: ReferenceSearchResult, expected_decl: &str, expected_refs: &[&str]) {
607 res: ReferenceSearchResult,
608 expected_decl: &str,
609 decl_kind: ReferenceKind,
610 expected_refs: &[&str],
611 ) {
612 res.declaration().assert_match(expected_decl); 603 res.declaration().assert_match(expected_decl);
613 assert_eq!(res.declaration_kind, decl_kind);
614
615 assert_eq!(res.references.len(), expected_refs.len()); 604 assert_eq!(res.references.len(), expected_refs.len());
616 res.references().iter().enumerate().for_each(|(i, r)| r.assert_match(expected_refs[i])); 605 res.references().iter().enumerate().for_each(|(i, r)| r.assert_match(expected_refs[i]));
617 } 606 }
618 607
608 impl Declaration {
609 fn debug_render(&self) -> String {
610 let mut s = format!("{} {:?}", self.nav.debug_render(), self.kind);
611 if let Some(access) = self.access {
612 s.push_str(&format!(" {:?}", access));
613 }
614 s
615 }
616
617 fn assert_match(&self, expected: &str) {
618 let actual = self.debug_render();
619 test_utils::assert_eq_text!(expected.trim(), actual.trim(),);
620 }
621 }
622
619 impl Reference { 623 impl Reference {
620 fn debug_render(&self) -> String { 624 fn debug_render(&self) -> String {
621 let mut s = format!( 625 let mut s = format!(