Skip to content

Commit d3c83fe

Browse files
committed
Auto merge of #30684 - tshepang:rustfmt-lexer-part2, r=nrc
2 parents 8796e01 + aa3b4c6 commit d3c83fe

File tree

2 files changed

+756
-511
lines changed

2 files changed

+756
-511
lines changed

src/libsyntax/parse/lexer/comments.rs

+61-49
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,8 @@ pub struct Comment {
4343
}
4444

4545
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("/*!")
5048
}
5149

5250
pub fn doc_comment_style(comment: &str) -> ast::AttrStyle {
@@ -64,18 +62,18 @@ pub fn strip_doc_comment_decoration(comment: &str) -> String {
6462
let mut i = 0;
6563
let mut j = lines.len();
6664
// 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 == '*') {
6966
i += 1;
7067
}
7168
while i < j && lines[i].trim().is_empty() {
7269
i += 1;
7370
}
7471
// 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 == '*') {
7977
j -= 1;
8078
}
8179
while j > i && lines[j - 1].trim().is_empty() {
@@ -85,7 +83,7 @@ pub fn strip_doc_comment_decoration(comment: &str) -> String {
8583
}
8684

8785
/// 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> {
8987
let mut i = usize::MAX;
9088
let mut can_trim = true;
9189
let mut first = true;
@@ -114,9 +112,9 @@ pub fn strip_doc_comment_decoration(comment: &str) -> String {
114112
}
115113

116114
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()
120118
} else {
121119
lines
122120
}
@@ -132,9 +130,9 @@ pub fn strip_doc_comment_decoration(comment: &str) -> String {
132130

133131
if comment.starts_with("/*") {
134132
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>>();
138136

139137
let lines = vertical_trim(lines);
140138
let lines = horizontal_trim(lines);
@@ -154,8 +152,7 @@ fn push_blank_line_comment(rdr: &StringReader, comments: &mut Vec<Comment>) {
154152
});
155153
}
156154

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>) {
159156
while is_whitespace(rdr.curr) && !rdr.is_eof() {
160157
if rdr.col == CharPos(0) && rdr.curr_is('\n') {
161158
push_blank_line_comment(rdr, &mut *comments);
@@ -165,19 +162,21 @@ fn consume_whitespace_counting_blank_lines(rdr: &mut StringReader,
165162
}
166163

167164

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,
169167
comments: &mut Vec<Comment>) {
170168
debug!(">>> shebang comment");
171169
let p = rdr.last_pos;
172170
debug!("<<< shebang comment");
173171
comments.push(Comment {
174172
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,
177175
});
178176
}
179177

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,
181180
comments: &mut Vec<Comment>) {
182181
debug!(">>> line comments");
183182
let p = rdr.last_pos;
@@ -197,7 +196,7 @@ fn read_line_comments(rdr: &mut StringReader, code_to_the_left: bool,
197196
comments.push(Comment {
198197
style: if code_to_the_left { Trailing } else { Isolated },
199198
lines: lines,
200-
pos: p
199+
pos: p,
201200
});
202201
}
203202
}
@@ -220,8 +219,7 @@ fn all_whitespace(s: &str, col: CharPos) -> Option<usize> {
220219
return Some(cursor);
221220
}
222221

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) {
225223
let len = s.len();
226224
let s1 = match all_whitespace(&s[..], col) {
227225
Some(col) => {
@@ -239,7 +237,7 @@ fn trim_whitespace_prefix_and_push_line(lines: &mut Vec<String> ,
239237

240238
fn read_block_comment(rdr: &mut StringReader,
241239
code_to_the_left: bool,
242-
comments: &mut Vec<Comment> ) {
240+
comments: &mut Vec<Comment>) {
243241
debug!(">>> block comment");
244242
let p = rdr.last_pos;
245243
let mut lines: Vec<String> = Vec::new();
@@ -261,7 +259,7 @@ fn read_block_comment(rdr: &mut StringReader,
261259
rdr.bump();
262260
}
263261
if is_block_doc_comment(&curr_line[..]) {
264-
return
262+
return;
265263
}
266264
assert!(!curr_line.contains('\n'));
267265
lines.push(curr_line);
@@ -273,9 +271,7 @@ fn read_block_comment(rdr: &mut StringReader,
273271
panic!(rdr.fatal("unterminated block comment"));
274272
}
275273
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);
279275
curr_line = String::new();
280276
rdr.bump();
281277
} else {
@@ -291,38 +287,46 @@ fn read_block_comment(rdr: &mut StringReader,
291287
rdr.bump();
292288
curr_line.push('/');
293289
level -= 1;
294-
} else { rdr.bump(); }
290+
} else {
291+
rdr.bump();
292+
}
295293
}
296294
}
297295
}
298296
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);
302298
}
303299
}
304300

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+
};
306306
rdr.consume_non_eol_whitespace();
307307
if !rdr.is_eof() && !rdr.curr_is('\n') && lines.len() == 1 {
308308
style = Mixed;
309309
}
310310
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+
});
312316
}
313317

314318

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>) {
318320
debug!(">>> consume comment");
319321
if rdr.curr_is('/') && rdr.nextch_is('/') {
320322
read_line_comments(rdr, code_to_the_left, comments);
321323
} else if rdr.curr_is('/') && rdr.nextch_is('*') {
322324
read_block_comment(rdr, code_to_the_left, comments);
323325
} else if rdr.curr_is('#') && rdr.nextch_is('!') {
324326
read_shebang_comment(rdr, code_to_the_left, comments);
325-
} else { panic!(); }
327+
} else {
328+
panic!();
329+
}
326330
debug!("<<< consume comment");
327331
}
328332

@@ -337,7 +341,7 @@ pub struct Literal {
337341
pub fn gather_comments_and_literals(span_diagnostic: &errors::Handler,
338342
path: String,
339343
srdr: &mut Read)
340-
-> (Vec<Comment>, Vec<Literal>) {
344+
-> (Vec<Comment>, Vec<Literal>) {
341345
let mut src = Vec::new();
342346
srdr.read_to_end(&mut src).unwrap();
343347
let src = String::from_utf8(src).unwrap();
@@ -366,12 +370,15 @@ pub fn gather_comments_and_literals(span_diagnostic: &errors::Handler,
366370

367371
let bstart = rdr.last_pos;
368372
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
370374
let TokenAndSpan { tok, sp } = rdr.peek();
371375
if tok.is_lit() {
372376
rdr.with_str_from(bstart, |s| {
373377
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+
});
375382
})
376383
} else {
377384
debug!("tok: {}", pprust::token_to_string(&tok));
@@ -386,31 +393,36 @@ pub fn gather_comments_and_literals(span_diagnostic: &errors::Handler,
386393
mod tests {
387394
use super::*;
388395

389-
#[test] fn test_block_doc_comment_1() {
396+
#[test]
397+
fn test_block_doc_comment_1() {
390398
let comment = "/**\n * Test \n ** Test\n * Test\n*/";
391399
let stripped = strip_doc_comment_decoration(comment);
392400
assert_eq!(stripped, " Test \n* Test\n Test");
393401
}
394402

395-
#[test] fn test_block_doc_comment_2() {
403+
#[test]
404+
fn test_block_doc_comment_2() {
396405
let comment = "/**\n * Test\n * Test\n*/";
397406
let stripped = strip_doc_comment_decoration(comment);
398407
assert_eq!(stripped, " Test\n Test");
399408
}
400409

401-
#[test] fn test_block_doc_comment_3() {
410+
#[test]
411+
fn test_block_doc_comment_3() {
402412
let comment = "/**\n let a: *i32;\n *a = 5;\n*/";
403413
let stripped = strip_doc_comment_decoration(comment);
404414
assert_eq!(stripped, " let a: *i32;\n *a = 5;");
405415
}
406416

407-
#[test] fn test_block_doc_comment_4() {
417+
#[test]
418+
fn test_block_doc_comment_4() {
408419
let comment = "/*******************\n test\n *********************/";
409420
let stripped = strip_doc_comment_decoration(comment);
410421
assert_eq!(stripped, " test");
411422
}
412423

413-
#[test] fn test_line_doc_comment() {
424+
#[test]
425+
fn test_line_doc_comment() {
414426
let stripped = strip_doc_comment_decoration("/// test");
415427
assert_eq!(stripped, " test");
416428
let stripped = strip_doc_comment_decoration("///! test");

0 commit comments

Comments
 (0)