@@ -43,10 +43,8 @@ pub struct Comment {
43
43
}
44
44
45
45
pub fn is_doc_comment ( s : & str ) -> bool {
46
- ( s. starts_with ( "///" ) && super :: is_doc_comment ( s) ) ||
47
- s. starts_with ( "//!" ) ||
48
- ( s. starts_with ( "/**" ) && is_block_doc_comment ( s) ) ||
49
- s. starts_with ( "/*!" )
46
+ ( s. starts_with ( "///" ) && super :: is_doc_comment ( s) ) || s. starts_with ( "//!" ) ||
47
+ ( s. starts_with ( "/**" ) && is_block_doc_comment ( s) ) || s. starts_with ( "/*!" )
50
48
}
51
49
52
50
pub fn doc_comment_style ( comment : & str ) -> ast:: AttrStyle {
@@ -64,18 +62,18 @@ pub fn strip_doc_comment_decoration(comment: &str) -> String {
64
62
let mut i = 0 ;
65
63
let mut j = lines. len ( ) ;
66
64
// first line of all-stars should be omitted
67
- if !lines. is_empty ( ) &&
68
- lines[ 0 ] . chars ( ) . all ( |c| c == '*' ) {
65
+ if !lines. is_empty ( ) && lines[ 0 ] . chars ( ) . all ( |c| c == '*' ) {
69
66
i += 1 ;
70
67
}
71
68
while i < j && lines[ i] . trim ( ) . is_empty ( ) {
72
69
i += 1 ;
73
70
}
74
71
// like the first, a last line of all stars should be omitted
75
- if j > i && lines[ j - 1 ]
76
- . chars ( )
77
- . skip ( 1 )
78
- . all ( |c| c == '*' ) {
72
+ if j > i &&
73
+ lines[ j - 1 ]
74
+ . chars ( )
75
+ . skip ( 1 )
76
+ . all ( |c| c == '*' ) {
79
77
j -= 1 ;
80
78
}
81
79
while j > i && lines[ j - 1 ] . trim ( ) . is_empty ( ) {
@@ -85,7 +83,7 @@ pub fn strip_doc_comment_decoration(comment: &str) -> String {
85
83
}
86
84
87
85
/// remove a "[ \t]*\*" block from each line, if possible
88
- fn horizontal_trim ( lines : Vec < String > ) -> Vec < String > {
86
+ fn horizontal_trim ( lines : Vec < String > ) -> Vec < String > {
89
87
let mut i = usize:: MAX ;
90
88
let mut can_trim = true ;
91
89
let mut first = true ;
@@ -114,9 +112,9 @@ pub fn strip_doc_comment_decoration(comment: &str) -> String {
114
112
}
115
113
116
114
if can_trim {
117
- lines. iter ( ) . map ( |line| {
118
- ( & line[ i + 1 ..line. len ( ) ] ) . to_string ( )
119
- } ) . collect ( )
115
+ lines. iter ( )
116
+ . map ( |line| ( & line[ i + 1 ..line. len ( ) ] ) . to_string ( ) )
117
+ . collect ( )
120
118
} else {
121
119
lines
122
120
}
@@ -132,9 +130,9 @@ pub fn strip_doc_comment_decoration(comment: &str) -> String {
132
130
133
131
if comment. starts_with ( "/*" ) {
134
132
let lines = comment[ 3 ..comment. len ( ) - 2 ]
135
- . lines ( )
136
- . map ( |s| s. to_string ( ) )
137
- . collect :: < Vec < String > > ( ) ;
133
+ . lines ( )
134
+ . map ( |s| s. to_string ( ) )
135
+ . collect :: < Vec < String > > ( ) ;
138
136
139
137
let lines = vertical_trim ( lines) ;
140
138
let lines = horizontal_trim ( lines) ;
@@ -154,8 +152,7 @@ fn push_blank_line_comment(rdr: &StringReader, comments: &mut Vec<Comment>) {
154
152
} ) ;
155
153
}
156
154
157
- fn consume_whitespace_counting_blank_lines ( rdr : & mut StringReader ,
158
- comments : & mut Vec < Comment > ) {
155
+ fn consume_whitespace_counting_blank_lines ( rdr : & mut StringReader , comments : & mut Vec < Comment > ) {
159
156
while is_whitespace ( rdr. curr ) && !rdr. is_eof ( ) {
160
157
if rdr. col == CharPos ( 0 ) && rdr. curr_is ( '\n' ) {
161
158
push_blank_line_comment ( rdr, & mut * comments) ;
@@ -165,19 +162,25 @@ fn consume_whitespace_counting_blank_lines(rdr: &mut StringReader,
165
162
}
166
163
167
164
168
- fn read_shebang_comment ( rdr : & mut StringReader , code_to_the_left : bool ,
165
+ fn read_shebang_comment ( rdr : & mut StringReader ,
166
+ code_to_the_left : bool ,
169
167
comments : & mut Vec < Comment > ) {
170
168
debug ! ( ">>> shebang comment" ) ;
171
169
let p = rdr. last_pos ;
172
170
debug ! ( "<<< shebang comment" ) ;
173
171
comments. push ( Comment {
174
- style : if code_to_the_left { Trailing } else { Isolated } ,
175
- lines : vec ! ( rdr. read_one_line_comment( ) ) ,
176
- pos : p
172
+ style : if code_to_the_left {
173
+ Trailing
174
+ } else {
175
+ Isolated
176
+ } ,
177
+ lines : vec ! [ rdr. read_one_line_comment( ) ] ,
178
+ pos : p,
177
179
} ) ;
178
180
}
179
181
180
- fn read_line_comments ( rdr : & mut StringReader , code_to_the_left : bool ,
182
+ fn read_line_comments ( rdr : & mut StringReader ,
183
+ code_to_the_left : bool ,
181
184
comments : & mut Vec < Comment > ) {
182
185
debug ! ( ">>> line comments" ) ;
183
186
let p = rdr. last_pos ;
@@ -195,9 +198,13 @@ fn read_line_comments(rdr: &mut StringReader, code_to_the_left: bool,
195
198
debug ! ( "<<< line comments" ) ;
196
199
if !lines. is_empty ( ) {
197
200
comments. push ( Comment {
198
- style : if code_to_the_left { Trailing } else { Isolated } ,
201
+ style : if code_to_the_left {
202
+ Trailing
203
+ } else {
204
+ Isolated
205
+ } ,
199
206
lines : lines,
200
- pos : p
207
+ pos : p,
201
208
} ) ;
202
209
}
203
210
}
@@ -220,8 +227,7 @@ fn all_whitespace(s: &str, col: CharPos) -> Option<usize> {
220
227
return Some ( cursor) ;
221
228
}
222
229
223
- fn trim_whitespace_prefix_and_push_line ( lines : & mut Vec < String > ,
224
- s : String , col : CharPos ) {
230
+ fn trim_whitespace_prefix_and_push_line ( lines : & mut Vec < String > , s : String , col : CharPos ) {
225
231
let len = s. len ( ) ;
226
232
let s1 = match all_whitespace ( & s[ ..] , col) {
227
233
Some ( col) => {
@@ -239,7 +245,7 @@ fn trim_whitespace_prefix_and_push_line(lines: &mut Vec<String> ,
239
245
240
246
fn read_block_comment ( rdr : & mut StringReader ,
241
247
code_to_the_left : bool ,
242
- comments : & mut Vec < Comment > ) {
248
+ comments : & mut Vec < Comment > ) {
243
249
debug ! ( ">>> block comment" ) ;
244
250
let p = rdr. last_pos ;
245
251
let mut lines: Vec < String > = Vec :: new ( ) ;
@@ -261,7 +267,7 @@ fn read_block_comment(rdr: &mut StringReader,
261
267
rdr. bump ( ) ;
262
268
}
263
269
if is_block_doc_comment ( & curr_line[ ..] ) {
264
- return
270
+ return ;
265
271
}
266
272
assert ! ( !curr_line. contains( '\n' ) ) ;
267
273
lines. push ( curr_line) ;
@@ -273,9 +279,7 @@ fn read_block_comment(rdr: &mut StringReader,
273
279
panic ! ( rdr. fatal( "unterminated block comment" ) ) ;
274
280
}
275
281
if rdr. curr_is ( '\n' ) {
276
- trim_whitespace_prefix_and_push_line ( & mut lines,
277
- curr_line,
278
- col) ;
282
+ trim_whitespace_prefix_and_push_line ( & mut lines, curr_line, col) ;
279
283
curr_line = String :: new ( ) ;
280
284
rdr. bump ( ) ;
281
285
} else {
@@ -291,38 +295,46 @@ fn read_block_comment(rdr: &mut StringReader,
291
295
rdr. bump ( ) ;
292
296
curr_line. push ( '/' ) ;
293
297
level -= 1 ;
294
- } else { rdr. bump ( ) ; }
298
+ } else {
299
+ rdr. bump ( ) ;
300
+ }
295
301
}
296
302
}
297
303
}
298
304
if !curr_line. is_empty ( ) {
299
- trim_whitespace_prefix_and_push_line ( & mut lines,
300
- curr_line,
301
- col) ;
305
+ trim_whitespace_prefix_and_push_line ( & mut lines, curr_line, col) ;
302
306
}
303
307
}
304
308
305
- let mut style = if code_to_the_left { Trailing } else { Isolated } ;
309
+ let mut style = if code_to_the_left {
310
+ Trailing
311
+ } else {
312
+ Isolated
313
+ } ;
306
314
rdr. consume_non_eol_whitespace ( ) ;
307
315
if !rdr. is_eof ( ) && !rdr. curr_is ( '\n' ) && lines. len ( ) == 1 {
308
316
style = Mixed ;
309
317
}
310
318
debug ! ( "<<< block comment" ) ;
311
- comments. push ( Comment { style : style, lines : lines, pos : p} ) ;
319
+ comments. push ( Comment {
320
+ style : style,
321
+ lines : lines,
322
+ pos : p,
323
+ } ) ;
312
324
}
313
325
314
326
315
- fn consume_comment ( rdr : & mut StringReader ,
316
- code_to_the_left : bool ,
317
- comments : & mut Vec < Comment > ) {
327
+ fn consume_comment ( rdr : & mut StringReader , code_to_the_left : bool , comments : & mut Vec < Comment > ) {
318
328
debug ! ( ">>> consume comment" ) ;
319
329
if rdr. curr_is ( '/' ) && rdr. nextch_is ( '/' ) {
320
330
read_line_comments ( rdr, code_to_the_left, comments) ;
321
331
} else if rdr. curr_is ( '/' ) && rdr. nextch_is ( '*' ) {
322
332
read_block_comment ( rdr, code_to_the_left, comments) ;
323
333
} else if rdr. curr_is ( '#' ) && rdr. nextch_is ( '!' ) {
324
334
read_shebang_comment ( rdr, code_to_the_left, comments) ;
325
- } else { panic ! ( ) ; }
335
+ } else {
336
+ panic ! ( ) ;
337
+ }
326
338
debug ! ( "<<< consume comment" ) ;
327
339
}
328
340
@@ -337,7 +349,7 @@ pub struct Literal {
337
349
pub fn gather_comments_and_literals ( span_diagnostic : & errors:: Handler ,
338
350
path : String ,
339
351
srdr : & mut Read )
340
- -> ( Vec < Comment > , Vec < Literal > ) {
352
+ -> ( Vec < Comment > , Vec < Literal > ) {
341
353
let mut src = Vec :: new ( ) ;
342
354
srdr. read_to_end ( & mut src) . unwrap ( ) ;
343
355
let src = String :: from_utf8 ( src) . unwrap ( ) ;
@@ -366,12 +378,15 @@ pub fn gather_comments_and_literals(span_diagnostic: &errors::Handler,
366
378
367
379
let bstart = rdr. last_pos ;
368
380
rdr. next_token ( ) ;
369
- //discard, and look ahead; we're working with internal state
381
+ // discard, and look ahead; we're working with internal state
370
382
let TokenAndSpan { tok, sp } = rdr. peek ( ) ;
371
383
if tok. is_lit ( ) {
372
384
rdr. with_str_from ( bstart, |s| {
373
385
debug ! ( "tok lit: {}" , s) ;
374
- literals. push ( Literal { lit : s. to_string ( ) , pos : sp. lo } ) ;
386
+ literals. push ( Literal {
387
+ lit : s. to_string ( ) ,
388
+ pos : sp. lo ,
389
+ } ) ;
375
390
} )
376
391
} else {
377
392
debug ! ( "tok: {}" , pprust:: token_to_string( & tok) ) ;
@@ -386,31 +401,36 @@ pub fn gather_comments_and_literals(span_diagnostic: &errors::Handler,
386
401
mod tests {
387
402
use super :: * ;
388
403
389
- #[ test] fn test_block_doc_comment_1 ( ) {
404
+ #[ test]
405
+ fn test_block_doc_comment_1 ( ) {
390
406
let comment = "/**\n * Test \n ** Test\n * Test\n */" ;
391
407
let stripped = strip_doc_comment_decoration ( comment) ;
392
408
assert_eq ! ( stripped, " Test \n * Test\n Test" ) ;
393
409
}
394
410
395
- #[ test] fn test_block_doc_comment_2 ( ) {
411
+ #[ test]
412
+ fn test_block_doc_comment_2 ( ) {
396
413
let comment = "/**\n * Test\n * Test\n */" ;
397
414
let stripped = strip_doc_comment_decoration ( comment) ;
398
415
assert_eq ! ( stripped, " Test\n Test" ) ;
399
416
}
400
417
401
- #[ test] fn test_block_doc_comment_3 ( ) {
418
+ #[ test]
419
+ fn test_block_doc_comment_3 ( ) {
402
420
let comment = "/**\n let a: *i32;\n *a = 5;\n */" ;
403
421
let stripped = strip_doc_comment_decoration ( comment) ;
404
422
assert_eq ! ( stripped, " let a: *i32;\n *a = 5;" ) ;
405
423
}
406
424
407
- #[ test] fn test_block_doc_comment_4 ( ) {
425
+ #[ test]
426
+ fn test_block_doc_comment_4 ( ) {
408
427
let comment = "/*******************\n test\n *********************/" ;
409
428
let stripped = strip_doc_comment_decoration ( comment) ;
410
429
assert_eq ! ( stripped, " test" ) ;
411
430
}
412
431
413
- #[ test] fn test_line_doc_comment ( ) {
432
+ #[ test]
433
+ fn test_line_doc_comment ( ) {
414
434
let stripped = strip_doc_comment_decoration ( "/// test" ) ;
415
435
assert_eq ! ( stripped, " test" ) ;
416
436
let stripped = strip_doc_comment_decoration ( "///! test" ) ;
0 commit comments