17
17
//! ```
18
18
19
19
#![ allow( dead_code) ]
20
- use rhai_rowan:: syntax:: { SyntaxElement , SyntaxKind :: * , SyntaxNode } ;
20
+ use rhai_rowan:: syntax:: {
21
+ SyntaxElement ,
22
+ SyntaxKind :: { self , * } ,
23
+ SyntaxNode ,
24
+ } ;
21
25
use rowan:: Direction ;
22
26
23
27
use crate :: {
@@ -125,20 +129,13 @@ impl<W: Write> Formatter<W> {
125
129
. siblings_with_tokens ( Direction :: Next )
126
130
. skip ( 1 )
127
131
. skip_while ( |ws| {
128
- // Skip punctuation
129
- if ws. as_token ( ) . is_some ( )
130
- && !matches ! ( ws. kind( ) , WHITESPACE | COMMENT_BLOCK | COMMENT_LINE )
131
- {
132
- return true ;
133
- }
134
-
135
132
if let Some ( token) = ws. as_token ( ) {
136
- if break_count ( token) ! = 0 {
137
- return false ;
133
+ if break_count ( token) = = 0 {
134
+ return true ;
138
135
}
139
136
}
140
137
141
- true
138
+ false
142
139
} )
143
140
. take_while ( |e| matches ! ( e. kind( ) , WHITESPACE | COMMENT_BLOCK | COMMENT_LINE ) )
144
141
. filter_map ( SyntaxElement :: into_token)
@@ -186,6 +183,100 @@ impl<W: Write> Formatter<W> {
186
183
187
184
Ok ( info)
188
185
}
186
+
187
+ /// Add comments that are before the expression.
188
+ pub ( crate ) fn comments_in_expr_before ( & mut self , expr : & SyntaxNode ) -> io:: Result < ( ) > {
189
+ let comments_before = expr
190
+ . children_with_tokens ( )
191
+ . take_while ( |t| t. as_node ( ) . is_none ( ) )
192
+ . filter_map ( SyntaxElement :: into_token)
193
+ . filter ( |t| matches ! ( t. kind( ) , COMMENT_LINE | COMMENT_BLOCK ) ) ;
194
+
195
+ let mut first = true ;
196
+ let mut hardbreak_last = false ;
197
+ for comment in comments_before {
198
+ if !first {
199
+ self . space ( ) ;
200
+ }
201
+ first = false ;
202
+ match comment. kind ( ) {
203
+ COMMENT_LINE => {
204
+ self . word ( comment. static_text ( ) . trim_end ( ) ) ?;
205
+ self . hardbreak ( ) ;
206
+ hardbreak_last = true ;
207
+ }
208
+ COMMENT_BLOCK => {
209
+ self . word ( comment. static_text ( ) . trim_end ( ) ) ?;
210
+ }
211
+ _ => unreachable ! ( ) ,
212
+ }
213
+ }
214
+
215
+ if !first && !hardbreak_last {
216
+ self . space ( ) ;
217
+ }
218
+
219
+ Ok ( ( ) )
220
+ }
221
+
222
+ /// Add comments that are before the expression.
223
+ pub ( crate ) fn comments_in_expr_after ( & mut self , expr : & SyntaxNode ) -> io:: Result < ( ) > {
224
+ let comments_before = expr
225
+ . children_with_tokens ( )
226
+ . skip_while ( |t| t. as_token ( ) . is_some ( ) )
227
+ . filter_map ( SyntaxElement :: into_token)
228
+ . filter ( |t| matches ! ( t. kind( ) , COMMENT_LINE | COMMENT_BLOCK ) ) ;
229
+
230
+ for comment in comments_before {
231
+ self . space ( ) ;
232
+ match comment. kind ( ) {
233
+ COMMENT_LINE => {
234
+ self . word ( comment. static_text ( ) . trim_end ( ) ) ?;
235
+ self . hardbreak ( ) ;
236
+ }
237
+ COMMENT_BLOCK => {
238
+ self . word ( comment. static_text ( ) . trim_end ( ) ) ?;
239
+ }
240
+ _ => unreachable ! ( ) ,
241
+ }
242
+ }
243
+
244
+ Ok ( ( ) )
245
+ }
246
+
247
+ pub ( crate ) fn comments_after_child (
248
+ & mut self ,
249
+ node : & SyntaxNode ,
250
+ kind : SyntaxKind ,
251
+ ) -> io:: Result < ( ) > {
252
+ let comments_after = node
253
+ . children_with_tokens ( )
254
+ . skip_while ( |t| t. kind ( ) != kind)
255
+ . skip_while ( |t| t. kind ( ) == kind)
256
+ . filter_map ( SyntaxElement :: into_token)
257
+ . filter ( |t| matches ! ( t. kind( ) , COMMENT_BLOCK | COMMENT_LINE ) ) ;
258
+
259
+ let mut first = true ;
260
+ for comment in comments_after {
261
+ if first {
262
+ self . space ( ) ;
263
+ }
264
+ first = false ;
265
+ match comment. kind ( ) {
266
+ COMMENT_LINE => {
267
+ self . word ( comment. static_text ( ) . trim_end ( ) ) ?;
268
+ self . hardbreak ( ) ;
269
+ }
270
+ COMMENT_BLOCK => {
271
+ self . word ( comment. static_text ( ) . trim_end ( ) ) ?;
272
+ self . space ( ) ;
273
+ }
274
+ _ => unreachable ! ( ) ,
275
+ }
276
+ }
277
+
278
+ Ok ( ( ) )
279
+ }
189
280
}
190
281
191
282
#[ derive( Default ) ]
@@ -205,3 +296,8 @@ impl CommentInfo {
205
296
}
206
297
}
207
298
}
299
+
300
+ pub ( crate ) fn comments_in_expr ( expr : & SyntaxNode ) -> bool {
301
+ expr. children_with_tokens ( )
302
+ . any ( |c| matches ! ( c. kind( ) , COMMENT_LINE | COMMENT_BLOCK ) )
303
+ }
0 commit comments