@@ -12,13 +12,15 @@ use oxc_span::Span;
12
12
#[ derive( Debug , Clone , Copy ) ]
13
13
pub struct Comment {
14
14
pub kind : CommentKind ,
15
- pub end : u32 ,
15
+ /// The span of the comment text (without leading/trailing delimiters).
16
+ pub span : Span ,
16
17
}
17
18
18
19
impl Comment {
19
20
#[ inline]
20
- pub fn new ( end : u32 , kind : CommentKind ) -> Self {
21
- Self { kind, end }
21
+ pub fn new ( start : u32 , end : u32 , kind : CommentKind ) -> Self {
22
+ let span = Span :: new ( start, end) ;
23
+ Self { kind, span }
22
24
}
23
25
}
24
26
@@ -41,7 +43,7 @@ impl CommentKind {
41
43
}
42
44
43
45
/// Sorted set of unique trivia comments, in ascending order by starting position.
44
- pub type SortedComments = Box < [ ( u32 , Comment ) ] > ;
46
+ pub type SortedComments = Box < [ Comment ] > ;
45
47
46
48
#[ derive( Debug , Clone , Default ) ]
47
49
pub struct Trivias ( Arc < TriviasImpl > ) ;
@@ -51,7 +53,7 @@ pub struct TriviasImpl {
51
53
/// Unique comments, ordered by increasing span-start.
52
54
comments : SortedComments ,
53
55
54
- irregular_whitespaces : Vec < Span > ,
56
+ irregular_whitespaces : Box < [ Span ] > ,
55
57
}
56
58
57
59
impl Deref for Trivias {
@@ -65,11 +67,14 @@ impl Deref for Trivias {
65
67
66
68
impl Trivias {
67
69
pub fn new ( comments : SortedComments , irregular_whitespaces : Vec < Span > ) -> Trivias {
68
- Self ( Arc :: new ( TriviasImpl { comments, irregular_whitespaces } ) )
70
+ Self ( Arc :: new ( TriviasImpl {
71
+ comments,
72
+ irregular_whitespaces : irregular_whitespaces. into_boxed_slice ( ) ,
73
+ } ) )
69
74
}
70
75
71
- pub fn comments ( & self ) -> impl Iterator < Item = ( CommentKind , Span ) > + ' _ {
72
- self . comments . iter ( ) . map ( | ( start , comment ) | ( comment . kind , Span :: new ( * start , comment . end ) ) )
76
+ pub fn comments ( & self ) -> impl Iterator < Item = & Comment > {
77
+ self . comments . iter ( )
73
78
}
74
79
75
80
pub fn comments_range < R > ( & self , range : R ) -> CommentsRange < ' _ >
@@ -83,21 +88,21 @@ impl Trivias {
83
88
self . comments_range ( span. start ..span. end ) . count ( ) > 0
84
89
}
85
90
86
- pub fn irregular_whitespaces ( & self ) -> & Vec < Span > {
91
+ pub fn irregular_whitespaces ( & self ) -> & [ Span ] {
87
92
& self . irregular_whitespaces
88
93
}
89
94
}
90
95
91
96
/// Double-ended iterator over a range of comments, by starting position.
92
97
pub struct CommentsRange < ' a > {
93
- comments : & ' a [ ( u32 , Comment ) ] ,
98
+ comments : & ' a [ Comment ] ,
94
99
range : ( Bound < u32 > , Bound < u32 > ) ,
95
100
current_start : usize ,
96
101
current_end : usize ,
97
102
}
98
103
99
104
impl < ' a > CommentsRange < ' a > {
100
- fn new ( comments : & ' a [ ( u32 , Comment ) ] , start : Bound < u32 > , end : Bound < u32 > ) -> Self {
105
+ fn new ( comments : & ' a [ Comment ] , start : Bound < u32 > , end : Bound < u32 > ) -> Self {
101
106
// Directly skip all comments that are already known to start
102
107
// outside the requested range.
103
108
let partition_start = {
@@ -106,15 +111,15 @@ impl<'a> CommentsRange<'a> {
106
111
Bound :: Included ( x) => x,
107
112
Bound :: Excluded ( x) => x. saturating_add ( 1 ) ,
108
113
} ;
109
- comments. partition_point ( |( start , _ ) | * start < range_start)
114
+ comments. partition_point ( |comment| comment . span . start < range_start)
110
115
} ;
111
116
let partition_end = {
112
117
let range_end = match end {
113
118
Bound :: Unbounded => u32:: MAX ,
114
119
Bound :: Included ( x) => x,
115
120
Bound :: Excluded ( x) => x. saturating_sub ( 1 ) ,
116
121
} ;
117
- comments. partition_point ( |( start , _ ) | * start <= range_end)
122
+ comments. partition_point ( |comment| comment . span . start <= range_end)
118
123
} ;
119
124
Self {
120
125
comments,
@@ -126,14 +131,14 @@ impl<'a> CommentsRange<'a> {
126
131
}
127
132
128
133
impl < ' c > Iterator for CommentsRange < ' c > {
129
- type Item = ( & ' c u32 , & ' c Comment ) ;
134
+ type Item = & ' c Comment ;
130
135
131
136
fn next ( & mut self ) -> Option < Self :: Item > {
132
137
if self . current_start < self . current_end {
133
- for ( start , comment) in & self . comments [ self . current_start ..self . current_end ] {
138
+ for comment in & self . comments [ self . current_start ..self . current_end ] {
134
139
self . current_start = self . current_start . saturating_add ( 1 ) ;
135
- if self . range . contains ( start) {
136
- return Some ( ( start , comment) ) ;
140
+ if self . range . contains ( & comment . span . start ) {
141
+ return Some ( comment) ;
137
142
}
138
143
}
139
144
}
@@ -149,11 +154,10 @@ impl<'c> Iterator for CommentsRange<'c> {
149
154
impl < ' c > DoubleEndedIterator for CommentsRange < ' c > {
150
155
fn next_back ( & mut self ) -> Option < Self :: Item > {
151
156
if self . current_start < self . current_end {
152
- for ( start, comment) in self . comments [ self . current_start ..self . current_end ] . iter ( ) . rev ( )
153
- {
157
+ for comment in self . comments [ self . current_start ..self . current_end ] . iter ( ) . rev ( ) {
154
158
self . current_end = self . current_end . saturating_sub ( 1 ) ;
155
- if self . range . contains ( start) {
156
- return Some ( ( start , comment) ) ;
159
+ if self . range . contains ( & comment . span . start ) {
160
+ return Some ( comment) ;
157
161
}
158
162
}
159
163
}
@@ -170,11 +174,11 @@ mod test {
170
174
#[ test]
171
175
fn test_comments_range ( ) {
172
176
let comments: SortedComments = vec ! [
173
- ( 0 , Comment { end : 4 , kind: CommentKind :: SingleLine } ) ,
174
- ( 5 , Comment { end : 9 , kind: CommentKind :: SingleLine } ) ,
175
- ( 10 , Comment { end : 13 , kind: CommentKind :: SingleLine } ) ,
176
- ( 14 , Comment { end : 17 , kind: CommentKind :: SingleLine } ) ,
177
- ( 18 , Comment { end : 23 , kind: CommentKind :: SingleLine } ) ,
177
+ Comment { span : Span :: new ( 0 , 4 ) , kind: CommentKind :: SingleLine } ,
178
+ Comment { span : Span :: new ( 5 , 9 ) , kind: CommentKind :: SingleLine } ,
179
+ Comment { span : Span :: new ( 10 , 13 ) , kind: CommentKind :: SingleLine } ,
180
+ Comment { span : Span :: new ( 14 , 17 ) , kind: CommentKind :: SingleLine } ,
181
+ Comment { span : Span :: new ( 18 , 23 ) , kind: CommentKind :: SingleLine } ,
178
182
]
179
183
. into_boxed_slice ( ) ;
180
184
let full_len = comments. len ( ) ;
0 commit comments