@@ -41,6 +41,12 @@ pub struct AnnotationConfig {
41
41
pub annotate_references : bool ,
42
42
pub annotate_method_references : bool ,
43
43
pub annotate_enum_variant_references : bool ,
44
+ pub annotation_location : AnnotationLocation ,
45
+ }
46
+
47
+ pub enum AnnotationLocation {
48
+ AboveName ,
49
+ AboveWholeItem ,
44
50
}
45
51
46
52
pub ( crate ) fn annotations (
@@ -65,10 +71,10 @@ pub(crate) fn annotations(
65
71
visit_file_defs ( & Semantics :: new ( db) , file_id, & mut |def| {
66
72
let range = match def {
67
73
Definition :: Const ( konst) if config. annotate_references => {
68
- konst. source ( db) . and_then ( |node| name_range ( db, node, file_id) )
74
+ konst. source ( db) . and_then ( |node| name_range ( db, config , node, file_id) )
69
75
}
70
76
Definition :: Trait ( trait_) if config. annotate_references || config. annotate_impls => {
71
- trait_. source ( db) . and_then ( |node| name_range ( db, node, file_id) )
77
+ trait_. source ( db) . and_then ( |node| name_range ( db, config , node, file_id) )
72
78
}
73
79
Definition :: Adt ( adt) => match adt {
74
80
hir:: Adt :: Enum ( enum_) => {
@@ -77,7 +83,9 @@ pub(crate) fn annotations(
77
83
. variants ( db)
78
84
. into_iter ( )
79
85
. map ( |variant| {
80
- variant. source ( db) . and_then ( |node| name_range ( db, node, file_id) )
86
+ variant
87
+ . source ( db)
88
+ . and_then ( |node| name_range ( db, config, node, file_id) )
81
89
} )
82
90
. flatten ( )
83
91
. for_each ( |range| {
@@ -88,14 +96,14 @@ pub(crate) fn annotations(
88
96
} )
89
97
}
90
98
if config. annotate_references || config. annotate_impls {
91
- enum_. source ( db) . and_then ( |node| name_range ( db, node, file_id) )
99
+ enum_. source ( db) . and_then ( |node| name_range ( db, config , node, file_id) )
92
100
} else {
93
101
None
94
102
}
95
103
}
96
104
_ => {
97
105
if config. annotate_references || config. annotate_impls {
98
- adt. source ( db) . and_then ( |node| name_range ( db, node, file_id) )
106
+ adt. source ( db) . and_then ( |node| name_range ( db, config , node, file_id) )
99
107
} else {
100
108
None
101
109
}
@@ -113,6 +121,7 @@ pub(crate) fn annotations(
113
121
annotations
114
122
. push ( Annotation { range, kind : AnnotationKind :: HasImpls { file_id, data : None } } ) ;
115
123
}
124
+
116
125
if config. annotate_references {
117
126
annotations. push ( Annotation {
118
127
range,
@@ -122,12 +131,18 @@ pub(crate) fn annotations(
122
131
123
132
fn name_range < T : HasName > (
124
133
db : & RootDatabase ,
134
+ config : & AnnotationConfig ,
125
135
node : InFile < T > ,
126
136
source_file_id : FileId ,
127
137
) -> Option < TextRange > {
128
138
if let Some ( InFile { file_id, value } ) = node. original_ast_node ( db) {
129
139
if file_id == source_file_id. into ( ) {
130
- return value. name ( ) . map ( |it| it. syntax ( ) . text_range ( ) ) ;
140
+ return match config. annotation_location {
141
+ AnnotationLocation :: AboveName => {
142
+ value. name ( ) . map ( |name| name. syntax ( ) . text_range ( ) )
143
+ }
144
+ AnnotationLocation :: AboveWholeItem => Some ( value. syntax ( ) . text_range ( ) ) ,
145
+ } ;
131
146
}
132
147
}
133
148
None
@@ -188,21 +203,23 @@ mod tests {
188
203
189
204
use crate :: { fixture, Annotation , AnnotationConfig } ;
190
205
191
- fn check ( ra_fixture : & str , expect : Expect ) {
206
+ use super :: AnnotationLocation ;
207
+
208
+ const DEFAULT_CONFIG : AnnotationConfig = AnnotationConfig {
209
+ binary_target : true ,
210
+ annotate_runnables : true ,
211
+ annotate_impls : true ,
212
+ annotate_references : true ,
213
+ annotate_method_references : true ,
214
+ annotate_enum_variant_references : true ,
215
+ annotation_location : AnnotationLocation :: AboveName ,
216
+ } ;
217
+
218
+ fn check ( ra_fixture : & str , expect : Expect , config : & AnnotationConfig ) {
192
219
let ( analysis, file_id) = fixture:: file ( ra_fixture) ;
193
220
194
221
let annotations: Vec < Annotation > = analysis
195
- . annotations (
196
- & AnnotationConfig {
197
- binary_target : true ,
198
- annotate_runnables : true ,
199
- annotate_impls : true ,
200
- annotate_references : true ,
201
- annotate_method_references : true ,
202
- annotate_enum_variant_references : true ,
203
- } ,
204
- file_id,
205
- )
222
+ . annotations ( config, file_id)
206
223
. unwrap ( )
207
224
. into_iter ( )
208
225
. map ( |annotation| analysis. resolve_annotation ( annotation) . unwrap ( ) )
@@ -286,6 +303,7 @@ fn main() {
286
303
},
287
304
]
288
305
"# ] ] ,
306
+ & DEFAULT_CONFIG ,
289
307
) ;
290
308
}
291
309
@@ -362,6 +380,7 @@ fn main() {
362
380
},
363
381
]
364
382
"# ] ] ,
383
+ & DEFAULT_CONFIG ,
365
384
) ;
366
385
}
367
386
@@ -497,6 +516,7 @@ fn main() {
497
516
},
498
517
]
499
518
"# ] ] ,
519
+ & DEFAULT_CONFIG ,
500
520
) ;
501
521
}
502
522
@@ -540,6 +560,7 @@ fn main() {}
540
560
},
541
561
]
542
562
"# ] ] ,
563
+ & DEFAULT_CONFIG ,
543
564
) ;
544
565
}
545
566
@@ -654,6 +675,7 @@ fn main() {
654
675
},
655
676
]
656
677
"# ] ] ,
678
+ & DEFAULT_CONFIG ,
657
679
) ;
658
680
}
659
681
@@ -750,6 +772,7 @@ mod tests {
750
772
},
751
773
]
752
774
"# ] ] ,
775
+ & DEFAULT_CONFIG ,
753
776
) ;
754
777
}
755
778
@@ -765,6 +788,7 @@ struct Foo;
765
788
expect ! [ [ r#"
766
789
[]
767
790
"# ] ] ,
791
+ & DEFAULT_CONFIG ,
768
792
) ;
769
793
}
770
794
@@ -784,6 +808,46 @@ m!();
784
808
expect ! [ [ r#"
785
809
[]
786
810
"# ] ] ,
811
+ & DEFAULT_CONFIG ,
812
+ ) ;
813
+ }
814
+
815
+ #[ test]
816
+ fn test_annotations_appear_above_whole_item_when_configured_to_do_so ( ) {
817
+ check (
818
+ r#"
819
+ /// This is a struct named Foo, obviously.
820
+ #[derive(Clone)]
821
+ struct Foo;
822
+ "# ,
823
+ expect ! [ [ r#"
824
+ [
825
+ Annotation {
826
+ range: 0..71,
827
+ kind: HasImpls {
828
+ file_id: FileId(
829
+ 0,
830
+ ),
831
+ data: Some(
832
+ [],
833
+ ),
834
+ },
835
+ },
836
+ Annotation {
837
+ range: 0..71,
838
+ kind: HasReferences {
839
+ file_id: FileId(
840
+ 0,
841
+ ),
842
+ data: None,
843
+ },
844
+ },
845
+ ]
846
+ "# ] ] ,
847
+ & AnnotationConfig {
848
+ annotation_location : AnnotationLocation :: AboveWholeItem ,
849
+ ..DEFAULT_CONFIG
850
+ } ,
787
851
) ;
788
852
}
789
853
}
0 commit comments