diff options
Diffstat (limited to 'crates/ide/src/annotations.rs')
-rw-r--r-- | crates/ide/src/annotations.rs | 100 |
1 files changed, 48 insertions, 52 deletions
diff --git a/crates/ide/src/annotations.rs b/crates/ide/src/annotations.rs index 5ebe7fd0e..8d68dce05 100644 --- a/crates/ide/src/annotations.rs +++ b/crates/ide/src/annotations.rs | |||
@@ -5,7 +5,7 @@ use ide_db::{ | |||
5 | helpers::visit_file_defs, | 5 | helpers::visit_file_defs, |
6 | RootDatabase, | 6 | RootDatabase, |
7 | }; | 7 | }; |
8 | use syntax::{ast::NameOwner, AstNode, TextRange, TextSize}; | 8 | use syntax::{ast::NameOwner, AstNode, TextRange}; |
9 | 9 | ||
10 | use crate::{ | 10 | use crate::{ |
11 | fn_references::find_all_methods, | 11 | fn_references::find_all_methods, |
@@ -58,7 +58,7 @@ pub(crate) fn annotations( | |||
58 | } | 58 | } |
59 | 59 | ||
60 | let action = runnable.action(); | 60 | let action = runnable.action(); |
61 | let range = runnable.nav.full_range; | 61 | let range = runnable.nav.focus_or_full_range(); |
62 | 62 | ||
63 | if config.run { | 63 | if config.run { |
64 | annotations.push(Annotation { | 64 | annotations.push(Annotation { |
@@ -80,26 +80,26 @@ pub(crate) fn annotations( | |||
80 | 80 | ||
81 | visit_file_defs(&Semantics::new(db), file_id, &mut |def| match def { | 81 | visit_file_defs(&Semantics::new(db), file_id, &mut |def| match def { |
82 | Either::Left(def) => { | 82 | Either::Left(def) => { |
83 | let node = match def { | 83 | let range = match def { |
84 | hir::ModuleDef::Const(konst) => { | 84 | hir::ModuleDef::Const(konst) => { |
85 | konst.source(db).and_then(|node| range_and_position_of(&node, file_id)) | 85 | konst.source(db).and_then(|node| name_range(&node, file_id)) |
86 | } | 86 | } |
87 | hir::ModuleDef::Trait(trait_) => { | 87 | hir::ModuleDef::Trait(trait_) => { |
88 | trait_.source(db).and_then(|node| range_and_position_of(&node, file_id)) | 88 | trait_.source(db).and_then(|node| name_range(&node, file_id)) |
89 | } | 89 | } |
90 | hir::ModuleDef::Adt(hir::Adt::Struct(strukt)) => { | 90 | hir::ModuleDef::Adt(hir::Adt::Struct(strukt)) => { |
91 | strukt.source(db).and_then(|node| range_and_position_of(&node, file_id)) | 91 | strukt.source(db).and_then(|node| name_range(&node, file_id)) |
92 | } | 92 | } |
93 | hir::ModuleDef::Adt(hir::Adt::Enum(enum_)) => { | 93 | hir::ModuleDef::Adt(hir::Adt::Enum(enum_)) => { |
94 | enum_.source(db).and_then(|node| range_and_position_of(&node, file_id)) | 94 | enum_.source(db).and_then(|node| name_range(&node, file_id)) |
95 | } | 95 | } |
96 | hir::ModuleDef::Adt(hir::Adt::Union(union)) => { | 96 | hir::ModuleDef::Adt(hir::Adt::Union(union)) => { |
97 | union.source(db).and_then(|node| range_and_position_of(&node, file_id)) | 97 | union.source(db).and_then(|node| name_range(&node, file_id)) |
98 | } | 98 | } |
99 | _ => None, | 99 | _ => None, |
100 | }; | 100 | }; |
101 | let (offset, range) = match node { | 101 | let (range, offset) = match range { |
102 | Some(node) => node, | 102 | Some(range) => (range, range.start()), |
103 | None => return, | 103 | None => return, |
104 | }; | 104 | }; |
105 | 105 | ||
@@ -122,18 +122,12 @@ pub(crate) fn annotations( | |||
122 | }); | 122 | }); |
123 | } | 123 | } |
124 | 124 | ||
125 | fn range_and_position_of<T: NameOwner>( | 125 | fn name_range<T: NameOwner>(node: &InFile<T>, file_id: FileId) -> Option<TextRange> { |
126 | node: &InFile<T>, | 126 | if node.file_id == file_id.into() { |
127 | file_id: FileId, | 127 | node.value.name().map(|it| it.syntax().text_range()) |
128 | ) -> Option<(TextSize, TextRange)> { | 128 | } else { |
129 | if node.file_id != file_id.into() { | ||
130 | // Node is outside the file we are adding annotations to (e.g. macros). | 129 | // Node is outside the file we are adding annotations to (e.g. macros). |
131 | None | 130 | None |
132 | } else { | ||
133 | Some(( | ||
134 | node.value.name()?.syntax().text_range().start(), | ||
135 | node.value.syntax().text_range(), | ||
136 | )) | ||
137 | } | 131 | } |
138 | } | 132 | } |
139 | } | 133 | } |
@@ -141,13 +135,15 @@ pub(crate) fn annotations( | |||
141 | }); | 135 | }); |
142 | 136 | ||
143 | if config.annotate_method_references { | 137 | if config.annotate_method_references { |
144 | annotations.extend(find_all_methods(db, file_id).into_iter().map(|method| Annotation { | 138 | annotations.extend(find_all_methods(db, file_id).into_iter().map( |
145 | range: method.range, | 139 | |FileRange { file_id, range }| Annotation { |
146 | kind: AnnotationKind::HasReferences { | 140 | range, |
147 | position: FilePosition { file_id, offset: method.range.start() }, | 141 | kind: AnnotationKind::HasReferences { |
148 | data: None, | 142 | position: FilePosition { file_id, offset: range.start() }, |
143 | data: None, | ||
144 | }, | ||
149 | }, | 145 | }, |
150 | })); | 146 | )); |
151 | } | 147 | } |
152 | 148 | ||
153 | annotations | 149 | annotations |
@@ -228,7 +224,7 @@ fn main() { | |||
228 | expect![[r#" | 224 | expect![[r#" |
229 | [ | 225 | [ |
230 | Annotation { | 226 | Annotation { |
231 | range: 50..85, | 227 | range: 53..57, |
232 | kind: Runnable { | 228 | kind: Runnable { |
233 | debug: false, | 229 | debug: false, |
234 | runnable: Runnable { | 230 | runnable: Runnable { |
@@ -247,7 +243,7 @@ fn main() { | |||
247 | }, | 243 | }, |
248 | }, | 244 | }, |
249 | Annotation { | 245 | Annotation { |
250 | range: 50..85, | 246 | range: 53..57, |
251 | kind: Runnable { | 247 | kind: Runnable { |
252 | debug: true, | 248 | debug: true, |
253 | runnable: Runnable { | 249 | runnable: Runnable { |
@@ -266,7 +262,7 @@ fn main() { | |||
266 | }, | 262 | }, |
267 | }, | 263 | }, |
268 | Annotation { | 264 | Annotation { |
269 | range: 0..22, | 265 | range: 6..10, |
270 | kind: HasReferences { | 266 | kind: HasReferences { |
271 | position: FilePosition { | 267 | position: FilePosition { |
272 | file_id: FileId( | 268 | file_id: FileId( |
@@ -287,7 +283,7 @@ fn main() { | |||
287 | }, | 283 | }, |
288 | }, | 284 | }, |
289 | Annotation { | 285 | Annotation { |
290 | range: 24..48, | 286 | range: 30..36, |
291 | kind: HasReferences { | 287 | kind: HasReferences { |
292 | position: FilePosition { | 288 | position: FilePosition { |
293 | file_id: FileId( | 289 | file_id: FileId( |
@@ -332,7 +328,7 @@ fn main() { | |||
332 | expect![[r#" | 328 | expect![[r#" |
333 | [ | 329 | [ |
334 | Annotation { | 330 | Annotation { |
335 | range: 14..48, | 331 | range: 17..21, |
336 | kind: Runnable { | 332 | kind: Runnable { |
337 | debug: false, | 333 | debug: false, |
338 | runnable: Runnable { | 334 | runnable: Runnable { |
@@ -351,7 +347,7 @@ fn main() { | |||
351 | }, | 347 | }, |
352 | }, | 348 | }, |
353 | Annotation { | 349 | Annotation { |
354 | range: 14..48, | 350 | range: 17..21, |
355 | kind: Runnable { | 351 | kind: Runnable { |
356 | debug: true, | 352 | debug: true, |
357 | runnable: Runnable { | 353 | runnable: Runnable { |
@@ -370,7 +366,7 @@ fn main() { | |||
370 | }, | 366 | }, |
371 | }, | 367 | }, |
372 | Annotation { | 368 | Annotation { |
373 | range: 0..12, | 369 | range: 7..11, |
374 | kind: HasImpls { | 370 | kind: HasImpls { |
375 | position: FilePosition { | 371 | position: FilePosition { |
376 | file_id: FileId( | 372 | file_id: FileId( |
@@ -384,7 +380,7 @@ fn main() { | |||
384 | }, | 380 | }, |
385 | }, | 381 | }, |
386 | Annotation { | 382 | Annotation { |
387 | range: 0..12, | 383 | range: 7..11, |
388 | kind: HasReferences { | 384 | kind: HasReferences { |
389 | position: FilePosition { | 385 | position: FilePosition { |
390 | file_id: FileId( | 386 | file_id: FileId( |
@@ -440,7 +436,7 @@ fn main() { | |||
440 | expect![[r#" | 436 | expect![[r#" |
441 | [ | 437 | [ |
442 | Annotation { | 438 | Annotation { |
443 | range: 66..100, | 439 | range: 69..73, |
444 | kind: Runnable { | 440 | kind: Runnable { |
445 | debug: false, | 441 | debug: false, |
446 | runnable: Runnable { | 442 | runnable: Runnable { |
@@ -459,7 +455,7 @@ fn main() { | |||
459 | }, | 455 | }, |
460 | }, | 456 | }, |
461 | Annotation { | 457 | Annotation { |
462 | range: 66..100, | 458 | range: 69..73, |
463 | kind: Runnable { | 459 | kind: Runnable { |
464 | debug: true, | 460 | debug: true, |
465 | runnable: Runnable { | 461 | runnable: Runnable { |
@@ -478,7 +474,7 @@ fn main() { | |||
478 | }, | 474 | }, |
479 | }, | 475 | }, |
480 | Annotation { | 476 | Annotation { |
481 | range: 0..12, | 477 | range: 7..11, |
482 | kind: HasImpls { | 478 | kind: HasImpls { |
483 | position: FilePosition { | 479 | position: FilePosition { |
484 | file_id: FileId( | 480 | file_id: FileId( |
@@ -502,7 +498,7 @@ fn main() { | |||
502 | }, | 498 | }, |
503 | }, | 499 | }, |
504 | Annotation { | 500 | Annotation { |
505 | range: 0..12, | 501 | range: 7..11, |
506 | kind: HasReferences { | 502 | kind: HasReferences { |
507 | position: FilePosition { | 503 | position: FilePosition { |
508 | file_id: FileId( | 504 | file_id: FileId( |
@@ -529,7 +525,7 @@ fn main() { | |||
529 | }, | 525 | }, |
530 | }, | 526 | }, |
531 | Annotation { | 527 | Annotation { |
532 | range: 14..34, | 528 | range: 20..31, |
533 | kind: HasImpls { | 529 | kind: HasImpls { |
534 | position: FilePosition { | 530 | position: FilePosition { |
535 | file_id: FileId( | 531 | file_id: FileId( |
@@ -553,7 +549,7 @@ fn main() { | |||
553 | }, | 549 | }, |
554 | }, | 550 | }, |
555 | Annotation { | 551 | Annotation { |
556 | range: 14..34, | 552 | range: 20..31, |
557 | kind: HasReferences { | 553 | kind: HasReferences { |
558 | position: FilePosition { | 554 | position: FilePosition { |
559 | file_id: FileId( | 555 | file_id: FileId( |
@@ -601,7 +597,7 @@ fn main() {} | |||
601 | expect![[r#" | 597 | expect![[r#" |
602 | [ | 598 | [ |
603 | Annotation { | 599 | Annotation { |
604 | range: 0..12, | 600 | range: 3..7, |
605 | kind: Runnable { | 601 | kind: Runnable { |
606 | debug: false, | 602 | debug: false, |
607 | runnable: Runnable { | 603 | runnable: Runnable { |
@@ -620,7 +616,7 @@ fn main() {} | |||
620 | }, | 616 | }, |
621 | }, | 617 | }, |
622 | Annotation { | 618 | Annotation { |
623 | range: 0..12, | 619 | range: 3..7, |
624 | kind: Runnable { | 620 | kind: Runnable { |
625 | debug: true, | 621 | debug: true, |
626 | runnable: Runnable { | 622 | runnable: Runnable { |
@@ -674,7 +670,7 @@ fn main() { | |||
674 | expect![[r#" | 670 | expect![[r#" |
675 | [ | 671 | [ |
676 | Annotation { | 672 | Annotation { |
677 | range: 58..95, | 673 | range: 61..65, |
678 | kind: Runnable { | 674 | kind: Runnable { |
679 | debug: false, | 675 | debug: false, |
680 | runnable: Runnable { | 676 | runnable: Runnable { |
@@ -693,7 +689,7 @@ fn main() { | |||
693 | }, | 689 | }, |
694 | }, | 690 | }, |
695 | Annotation { | 691 | Annotation { |
696 | range: 58..95, | 692 | range: 61..65, |
697 | kind: Runnable { | 693 | kind: Runnable { |
698 | debug: true, | 694 | debug: true, |
699 | runnable: Runnable { | 695 | runnable: Runnable { |
@@ -712,7 +708,7 @@ fn main() { | |||
712 | }, | 708 | }, |
713 | }, | 709 | }, |
714 | Annotation { | 710 | Annotation { |
715 | range: 0..12, | 711 | range: 7..11, |
716 | kind: HasImpls { | 712 | kind: HasImpls { |
717 | position: FilePosition { | 713 | position: FilePosition { |
718 | file_id: FileId( | 714 | file_id: FileId( |
@@ -736,7 +732,7 @@ fn main() { | |||
736 | }, | 732 | }, |
737 | }, | 733 | }, |
738 | Annotation { | 734 | Annotation { |
739 | range: 0..12, | 735 | range: 7..11, |
740 | kind: HasReferences { | 736 | kind: HasReferences { |
741 | position: FilePosition { | 737 | position: FilePosition { |
742 | file_id: FileId( | 738 | file_id: FileId( |
@@ -816,7 +812,7 @@ mod tests { | |||
816 | expect![[r#" | 812 | expect![[r#" |
817 | [ | 813 | [ |
818 | Annotation { | 814 | Annotation { |
819 | range: 0..12, | 815 | range: 3..7, |
820 | kind: Runnable { | 816 | kind: Runnable { |
821 | debug: false, | 817 | debug: false, |
822 | runnable: Runnable { | 818 | runnable: Runnable { |
@@ -835,7 +831,7 @@ mod tests { | |||
835 | }, | 831 | }, |
836 | }, | 832 | }, |
837 | Annotation { | 833 | Annotation { |
838 | range: 0..12, | 834 | range: 3..7, |
839 | kind: Runnable { | 835 | kind: Runnable { |
840 | debug: true, | 836 | debug: true, |
841 | runnable: Runnable { | 837 | runnable: Runnable { |
@@ -854,7 +850,7 @@ mod tests { | |||
854 | }, | 850 | }, |
855 | }, | 851 | }, |
856 | Annotation { | 852 | Annotation { |
857 | range: 14..64, | 853 | range: 18..23, |
858 | kind: Runnable { | 854 | kind: Runnable { |
859 | debug: false, | 855 | debug: false, |
860 | runnable: Runnable { | 856 | runnable: Runnable { |
@@ -875,7 +871,7 @@ mod tests { | |||
875 | }, | 871 | }, |
876 | }, | 872 | }, |
877 | Annotation { | 873 | Annotation { |
878 | range: 14..64, | 874 | range: 18..23, |
879 | kind: Runnable { | 875 | kind: Runnable { |
880 | debug: true, | 876 | debug: true, |
881 | runnable: Runnable { | 877 | runnable: Runnable { |
@@ -896,7 +892,7 @@ mod tests { | |||
896 | }, | 892 | }, |
897 | }, | 893 | }, |
898 | Annotation { | 894 | Annotation { |
899 | range: 30..62, | 895 | range: 45..57, |
900 | kind: Runnable { | 896 | kind: Runnable { |
901 | debug: false, | 897 | debug: false, |
902 | runnable: Runnable { | 898 | runnable: Runnable { |
@@ -922,7 +918,7 @@ mod tests { | |||
922 | }, | 918 | }, |
923 | }, | 919 | }, |
924 | Annotation { | 920 | Annotation { |
925 | range: 30..62, | 921 | range: 45..57, |
926 | kind: Runnable { | 922 | kind: Runnable { |
927 | debug: true, | 923 | debug: true, |
928 | runnable: Runnable { | 924 | runnable: Runnable { |