@@ -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,21 @@ 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
172
style : if code_to_the_left { Trailing } else { Isolated } ,
175
- lines : vec ! ( rdr. read_one_line_comment( ) ) ,
176
- pos : p
173
+ lines : vec ! [ rdr. read_one_line_comment( ) ] ,
174
+ pos : p,
177
175
} ) ;
178
176
}
179
177
180
- fn read_line_comments ( rdr : & mut StringReader , code_to_the_left : bool ,
178
+ fn read_line_comments ( rdr : & mut StringReader ,
179
+ code_to_the_left : bool ,
181
180
comments : & mut Vec < Comment > ) {
182
181
debug ! ( ">>> line comments" ) ;
183
182
let p = rdr. last_pos ;
@@ -197,7 +196,7 @@ fn read_line_comments(rdr: &mut StringReader, code_to_the_left: bool,
197
196
comments. push ( Comment {
198
197
style : if code_to_the_left { Trailing } else { Isolated } ,
199
198
lines : lines,
200
- pos : p
199
+ pos : p,
201
200
} ) ;
202
201
}
203
202
}
@@ -220,8 +219,7 @@ fn all_whitespace(s: &str, col: CharPos) -> Option<usize> {
220
219
return Some ( cursor) ;
221
220
}
222
221
223
- fn trim_whitespace_prefix_and_push_line ( lines : & mut Vec < String > ,
224
- s : String , col : CharPos ) {
222
+ fn trim_whitespace_prefix_and_push_line ( lines : & mut Vec < String > , s : String , col : CharPos ) {
225
223
let len = s. len ( ) ;
226
224
let s1 = match all_whitespace ( & s[ ..] , col) {
227
225
Some ( col) => {
@@ -239,7 +237,7 @@ fn trim_whitespace_prefix_and_push_line(lines: &mut Vec<String> ,
239
237
240
238
fn read_block_comment ( rdr : & mut StringReader ,
241
239
code_to_the_left : bool ,
242
- comments : & mut Vec < Comment > ) {
240
+ comments : & mut Vec < Comment > ) {
243
241
debug ! ( ">>> block comment" ) ;
244
242
let p = rdr. last_pos ;
245
243
let mut lines: Vec < String > = Vec :: new ( ) ;
@@ -261,7 +259,7 @@ fn read_block_comment(rdr: &mut StringReader,
261
259
rdr. bump ( ) ;
262
260
}
263
261
if is_block_doc_comment ( & curr_line[ ..] ) {
264
- return
262
+ return ;
265
263
}
266
264
assert ! ( !curr_line. contains( '\n' ) ) ;
267
265
lines. push ( curr_line) ;
@@ -273,9 +271,7 @@ fn read_block_comment(rdr: &mut StringReader,
273
271
panic ! ( rdr. fatal( "unterminated block comment" ) ) ;
274
272
}
275
273
if rdr. curr_is ( '\n' ) {
276
- trim_whitespace_prefix_and_push_line ( & mut lines,
277
- curr_line,
278
- col) ;
274
+ trim_whitespace_prefix_and_push_line ( & mut lines, curr_line, col) ;
279
275
curr_line = String :: new ( ) ;
280
276
rdr. bump ( ) ;
281
277
} else {
@@ -291,38 +287,46 @@ fn read_block_comment(rdr: &mut StringReader,
291
287
rdr. bump ( ) ;
292
288
curr_line. push ( '/' ) ;
293
289
level -= 1 ;
294
- } else { rdr. bump ( ) ; }
290
+ } else {
291
+ rdr. bump ( ) ;
292
+ }
295
293
}
296
294
}
297
295
}
298
296
if !curr_line. is_empty ( ) {
299
- trim_whitespace_prefix_and_push_line ( & mut lines,
300
- curr_line,
301
- col) ;
297
+ trim_whitespace_prefix_and_push_line ( & mut lines, curr_line, col) ;
302
298
}
303
299
}
304
300
305
- let mut style = if code_to_the_left { Trailing } else { Isolated } ;
301
+ let mut style = if code_to_the_left {
302
+ Trailing
303
+ } else {
304
+ Isolated
305
+ } ;
306
306
rdr. consume_non_eol_whitespace ( ) ;
307
307
if !rdr. is_eof ( ) && !rdr. curr_is ( '\n' ) && lines. len ( ) == 1 {
308
308
style = Mixed ;
309
309
}
310
310
debug ! ( "<<< block comment" ) ;
311
- comments. push ( Comment { style : style, lines : lines, pos : p} ) ;
311
+ comments. push ( Comment {
312
+ style : style,
313
+ lines : lines,
314
+ pos : p,
315
+ } ) ;
312
316
}
313
317
314
318
315
- fn consume_comment ( rdr : & mut StringReader ,
316
- code_to_the_left : bool ,
317
- comments : & mut Vec < Comment > ) {
319
+ fn consume_comment ( rdr : & mut StringReader , code_to_the_left : bool , comments : & mut Vec < Comment > ) {
318
320
debug ! ( ">>> consume comment" ) ;
319
321
if rdr. curr_is ( '/' ) && rdr. nextch_is ( '/' ) {
320
322
read_line_comments ( rdr, code_to_the_left, comments) ;
321
323
} else if rdr. curr_is ( '/' ) && rdr. nextch_is ( '*' ) {
322
324
read_block_comment ( rdr, code_to_the_left, comments) ;
323
325
} else if rdr. curr_is ( '#' ) && rdr. nextch_is ( '!' ) {
324
326
read_shebang_comment ( rdr, code_to_the_left, comments) ;
325
- } else { panic ! ( ) ; }
327
+ } else {
328
+ panic ! ( ) ;
329
+ }
326
330
debug ! ( "<<< consume comment" ) ;
327
331
}
328
332
@@ -337,7 +341,7 @@ pub struct Literal {
337
341
pub fn gather_comments_and_literals ( span_diagnostic : & errors:: Handler ,
338
342
path : String ,
339
343
srdr : & mut Read )
340
- -> ( Vec < Comment > , Vec < Literal > ) {
344
+ -> ( Vec < Comment > , Vec < Literal > ) {
341
345
let mut src = Vec :: new ( ) ;
342
346
srdr. read_to_end ( & mut src) . unwrap ( ) ;
343
347
let src = String :: from_utf8 ( src) . unwrap ( ) ;
@@ -366,12 +370,15 @@ pub fn gather_comments_and_literals(span_diagnostic: &errors::Handler,
366
370
367
371
let bstart = rdr. last_pos ;
368
372
rdr. next_token ( ) ;
369
- //discard, and look ahead; we're working with internal state
373
+ // discard, and look ahead; we're working with internal state
370
374
let TokenAndSpan { tok, sp } = rdr. peek ( ) ;
371
375
if tok. is_lit ( ) {
372
376
rdr. with_str_from ( bstart, |s| {
373
377
debug ! ( "tok lit: {}" , s) ;
374
- literals. push ( Literal { lit : s. to_string ( ) , pos : sp. lo } ) ;
378
+ literals. push ( Literal {
379
+ lit : s. to_string ( ) ,
380
+ pos : sp. lo ,
381
+ } ) ;
375
382
} )
376
383
} else {
377
384
debug ! ( "tok: {}" , pprust:: token_to_string( & tok) ) ;
@@ -386,31 +393,36 @@ pub fn gather_comments_and_literals(span_diagnostic: &errors::Handler,
386
393
mod tests {
387
394
use super :: * ;
388
395
389
- #[ test] fn test_block_doc_comment_1 ( ) {
396
+ #[ test]
397
+ fn test_block_doc_comment_1 ( ) {
390
398
let comment = "/**\n * Test \n ** Test\n * Test\n */" ;
391
399
let stripped = strip_doc_comment_decoration ( comment) ;
392
400
assert_eq ! ( stripped, " Test \n * Test\n Test" ) ;
393
401
}
394
402
395
- #[ test] fn test_block_doc_comment_2 ( ) {
403
+ #[ test]
404
+ fn test_block_doc_comment_2 ( ) {
396
405
let comment = "/**\n * Test\n * Test\n */" ;
397
406
let stripped = strip_doc_comment_decoration ( comment) ;
398
407
assert_eq ! ( stripped, " Test\n Test" ) ;
399
408
}
400
409
401
- #[ test] fn test_block_doc_comment_3 ( ) {
410
+ #[ test]
411
+ fn test_block_doc_comment_3 ( ) {
402
412
let comment = "/**\n let a: *i32;\n *a = 5;\n */" ;
403
413
let stripped = strip_doc_comment_decoration ( comment) ;
404
414
assert_eq ! ( stripped, " let a: *i32;\n *a = 5;" ) ;
405
415
}
406
416
407
- #[ test] fn test_block_doc_comment_4 ( ) {
417
+ #[ test]
418
+ fn test_block_doc_comment_4 ( ) {
408
419
let comment = "/*******************\n test\n *********************/" ;
409
420
let stripped = strip_doc_comment_decoration ( comment) ;
410
421
assert_eq ! ( stripped, " test" ) ;
411
422
}
412
423
413
- #[ test] fn test_line_doc_comment ( ) {
424
+ #[ test]
425
+ fn test_line_doc_comment ( ) {
414
426
let stripped = strip_doc_comment_decoration ( "/// test" ) ;
415
427
assert_eq ! ( stripped, " test" ) ;
416
428
let stripped = strip_doc_comment_decoration ( "///! test" ) ;
0 commit comments