@@ -40,20 +40,34 @@ impl ModuleExtra {
40
40
self . first_comment_between ( start, end) . is_some ( )
41
41
}
42
42
43
- // TODO: this won't necessarily find the first comment, just any comment
43
+ /// Returns the first comment overlapping the given source locations (inclusive)
44
+ /// Note that the returned span covers the text of the comment, not the `//`
44
45
pub ( crate ) fn first_comment_between ( & self , start : u32 , end : u32 ) -> Option < SrcSpan > {
45
- self . comments
46
- . binary_search_by ( |comment| {
47
- if comment. end < start {
48
- Ordering :: Less
49
- } else if comment. start > end {
50
- Ordering :: Greater
51
- } else {
52
- Ordering :: Equal
53
- }
54
- } )
55
- . ok ( )
56
- . and_then ( |index| self . comments . get ( index) . copied ( ) )
46
+ let inner = |comments : & [ SrcSpan ] , start, end| {
47
+ if comments. is_empty ( ) {
48
+ return None ;
49
+ }
50
+
51
+ comments
52
+ . binary_search_by ( |comment| {
53
+ if comment. end < start {
54
+ Ordering :: Less
55
+ } else if comment. start > end {
56
+ Ordering :: Greater
57
+ } else {
58
+ Ordering :: Equal
59
+ }
60
+ } )
61
+ . ok ( )
62
+ } ;
63
+
64
+ let mut best = None ;
65
+ let mut search_list = & self . comments [ ..] ;
66
+ while let Some ( index) = inner ( search_list, start, end) {
67
+ best = self . comments . get ( index) . copied ( ) ;
68
+ search_list = & search_list[ 0 ..index] ;
69
+ }
70
+ best
57
71
}
58
72
}
59
73
@@ -130,6 +144,15 @@ mod tests {
130
144
) ) ;
131
145
}
132
146
147
+ #[ test]
148
+ fn first_comment_between_overlapping_end_of_range ( ) {
149
+ let extra = set_up_extra ( ) ;
150
+ assert ! ( matches!(
151
+ extra. first_comment_between( 35 , 45 ) ,
152
+ Some ( SrcSpan { start: 40 , end: 50 } )
153
+ ) ) ;
154
+ }
155
+
133
156
#[ test]
134
157
fn first_comment_between_at_end_of_range ( ) {
135
158
let extra = set_up_extra ( ) ;
0 commit comments