-
Notifications
You must be signed in to change notification settings - Fork 13.4k
run rustfmt on syntax::parse::lexer #30684
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,10 +43,8 @@ pub struct Comment { | |
} | ||
|
||
pub fn is_doc_comment(s: &str) -> bool { | ||
(s.starts_with("///") && super::is_doc_comment(s)) || | ||
s.starts_with("//!") || | ||
(s.starts_with("/**") && is_block_doc_comment(s)) || | ||
s.starts_with("/*!") | ||
(s.starts_with("///") && super::is_doc_comment(s)) || s.starts_with("//!") || | ||
(s.starts_with("/**") && is_block_doc_comment(s)) || s.starts_with("/*!") | ||
} | ||
|
||
pub fn doc_comment_style(comment: &str) -> ast::AttrStyle { | ||
|
@@ -64,18 +62,18 @@ pub fn strip_doc_comment_decoration(comment: &str) -> String { | |
let mut i = 0; | ||
let mut j = lines.len(); | ||
// first line of all-stars should be omitted | ||
if !lines.is_empty() && | ||
lines[0].chars().all(|c| c == '*') { | ||
if !lines.is_empty() && lines[0].chars().all(|c| c == '*') { | ||
i += 1; | ||
} | ||
while i < j && lines[i].trim().is_empty() { | ||
i += 1; | ||
} | ||
// like the first, a last line of all stars should be omitted | ||
if j > i && lines[j - 1] | ||
.chars() | ||
.skip(1) | ||
.all(|c| c == '*') { | ||
if j > i && | ||
lines[j - 1] | ||
.chars() | ||
.skip(1) | ||
.all(|c| c == '*') { | ||
j -= 1; | ||
} | ||
while j > i && lines[j - 1].trim().is_empty() { | ||
|
@@ -85,7 +83,7 @@ pub fn strip_doc_comment_decoration(comment: &str) -> String { | |
} | ||
|
||
/// remove a "[ \t]*\*" block from each line, if possible | ||
fn horizontal_trim(lines: Vec<String> ) -> Vec<String> { | ||
fn horizontal_trim(lines: Vec<String>) -> Vec<String> { | ||
let mut i = usize::MAX; | ||
let mut can_trim = true; | ||
let mut first = true; | ||
|
@@ -114,9 +112,9 @@ pub fn strip_doc_comment_decoration(comment: &str) -> String { | |
} | ||
|
||
if can_trim { | ||
lines.iter().map(|line| { | ||
(&line[i + 1..line.len()]).to_string() | ||
}).collect() | ||
lines.iter() | ||
.map(|line| (&line[i + 1..line.len()]).to_string()) | ||
.collect() | ||
} else { | ||
lines | ||
} | ||
|
@@ -132,9 +130,9 @@ pub fn strip_doc_comment_decoration(comment: &str) -> String { | |
|
||
if comment.starts_with("/*") { | ||
let lines = comment[3..comment.len() - 2] | ||
.lines() | ||
.map(|s| s.to_string()) | ||
.collect::<Vec<String> >(); | ||
.lines() | ||
.map(|s| s.to_string()) | ||
.collect::<Vec<String>>(); | ||
|
||
let lines = vertical_trim(lines); | ||
let lines = horizontal_trim(lines); | ||
|
@@ -154,8 +152,7 @@ fn push_blank_line_comment(rdr: &StringReader, comments: &mut Vec<Comment>) { | |
}); | ||
} | ||
|
||
fn consume_whitespace_counting_blank_lines(rdr: &mut StringReader, | ||
comments: &mut Vec<Comment>) { | ||
fn consume_whitespace_counting_blank_lines(rdr: &mut StringReader, comments: &mut Vec<Comment>) { | ||
while is_whitespace(rdr.curr) && !rdr.is_eof() { | ||
if rdr.col == CharPos(0) && rdr.curr_is('\n') { | ||
push_blank_line_comment(rdr, &mut *comments); | ||
|
@@ -165,19 +162,25 @@ fn consume_whitespace_counting_blank_lines(rdr: &mut StringReader, | |
} | ||
|
||
|
||
fn read_shebang_comment(rdr: &mut StringReader, code_to_the_left: bool, | ||
fn read_shebang_comment(rdr: &mut StringReader, | ||
code_to_the_left: bool, | ||
comments: &mut Vec<Comment>) { | ||
debug!(">>> shebang comment"); | ||
let p = rdr.last_pos; | ||
debug!("<<< shebang comment"); | ||
comments.push(Comment { | ||
style: if code_to_the_left { Trailing } else { Isolated }, | ||
lines: vec!(rdr.read_one_line_comment()), | ||
pos: p | ||
style: if code_to_the_left { | ||
Trailing | ||
} else { | ||
Isolated | ||
}, | ||
lines: vec![rdr.read_one_line_comment()], | ||
pos: p, | ||
}); | ||
} | ||
|
||
fn read_line_comments(rdr: &mut StringReader, code_to_the_left: bool, | ||
fn read_line_comments(rdr: &mut StringReader, | ||
code_to_the_left: bool, | ||
comments: &mut Vec<Comment>) { | ||
debug!(">>> line comments"); | ||
let p = rdr.last_pos; | ||
|
@@ -195,9 +198,13 @@ fn read_line_comments(rdr: &mut StringReader, code_to_the_left: bool, | |
debug!("<<< line comments"); | ||
if !lines.is_empty() { | ||
comments.push(Comment { | ||
style: if code_to_the_left { Trailing } else { Isolated }, | ||
style: if code_to_the_left { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. |
||
Trailing | ||
} else { | ||
Isolated | ||
}, | ||
lines: lines, | ||
pos: p | ||
pos: p, | ||
}); | ||
} | ||
} | ||
|
@@ -220,8 +227,7 @@ fn all_whitespace(s: &str, col: CharPos) -> Option<usize> { | |
return Some(cursor); | ||
} | ||
|
||
fn trim_whitespace_prefix_and_push_line(lines: &mut Vec<String> , | ||
s: String, col: CharPos) { | ||
fn trim_whitespace_prefix_and_push_line(lines: &mut Vec<String>, s: String, col: CharPos) { | ||
let len = s.len(); | ||
let s1 = match all_whitespace(&s[..], col) { | ||
Some(col) => { | ||
|
@@ -239,7 +245,7 @@ fn trim_whitespace_prefix_and_push_line(lines: &mut Vec<String> , | |
|
||
fn read_block_comment(rdr: &mut StringReader, | ||
code_to_the_left: bool, | ||
comments: &mut Vec<Comment> ) { | ||
comments: &mut Vec<Comment>) { | ||
debug!(">>> block comment"); | ||
let p = rdr.last_pos; | ||
let mut lines: Vec<String> = Vec::new(); | ||
|
@@ -261,7 +267,7 @@ fn read_block_comment(rdr: &mut StringReader, | |
rdr.bump(); | ||
} | ||
if is_block_doc_comment(&curr_line[..]) { | ||
return | ||
return; | ||
} | ||
assert!(!curr_line.contains('\n')); | ||
lines.push(curr_line); | ||
|
@@ -273,9 +279,7 @@ fn read_block_comment(rdr: &mut StringReader, | |
panic!(rdr.fatal("unterminated block comment")); | ||
} | ||
if rdr.curr_is('\n') { | ||
trim_whitespace_prefix_and_push_line(&mut lines, | ||
curr_line, | ||
col); | ||
trim_whitespace_prefix_and_push_line(&mut lines, curr_line, col); | ||
curr_line = String::new(); | ||
rdr.bump(); | ||
} else { | ||
|
@@ -291,38 +295,46 @@ fn read_block_comment(rdr: &mut StringReader, | |
rdr.bump(); | ||
curr_line.push('/'); | ||
level -= 1; | ||
} else { rdr.bump(); } | ||
} else { | ||
rdr.bump(); | ||
} | ||
} | ||
} | ||
} | ||
if !curr_line.is_empty() { | ||
trim_whitespace_prefix_and_push_line(&mut lines, | ||
curr_line, | ||
col); | ||
trim_whitespace_prefix_and_push_line(&mut lines, curr_line, col); | ||
} | ||
} | ||
|
||
let mut style = if code_to_the_left { Trailing } else { Isolated }; | ||
let mut style = if code_to_the_left { | ||
Trailing | ||
} else { | ||
Isolated | ||
}; | ||
rdr.consume_non_eol_whitespace(); | ||
if !rdr.is_eof() && !rdr.curr_is('\n') && lines.len() == 1 { | ||
style = Mixed; | ||
} | ||
debug!("<<< block comment"); | ||
comments.push(Comment {style: style, lines: lines, pos: p}); | ||
comments.push(Comment { | ||
style: style, | ||
lines: lines, | ||
pos: p, | ||
}); | ||
} | ||
|
||
|
||
fn consume_comment(rdr: &mut StringReader, | ||
code_to_the_left: bool, | ||
comments: &mut Vec<Comment> ) { | ||
fn consume_comment(rdr: &mut StringReader, code_to_the_left: bool, comments: &mut Vec<Comment>) { | ||
debug!(">>> consume comment"); | ||
if rdr.curr_is('/') && rdr.nextch_is('/') { | ||
read_line_comments(rdr, code_to_the_left, comments); | ||
} else if rdr.curr_is('/') && rdr.nextch_is('*') { | ||
read_block_comment(rdr, code_to_the_left, comments); | ||
} else if rdr.curr_is('#') && rdr.nextch_is('!') { | ||
read_shebang_comment(rdr, code_to_the_left, comments); | ||
} else { panic!(); } | ||
} else { | ||
panic!(); | ||
} | ||
debug!("<<< consume comment"); | ||
} | ||
|
||
|
@@ -337,7 +349,7 @@ pub struct Literal { | |
pub fn gather_comments_and_literals(span_diagnostic: &errors::Handler, | ||
path: String, | ||
srdr: &mut Read) | ||
-> (Vec<Comment>, Vec<Literal>) { | ||
-> (Vec<Comment>, Vec<Literal>) { | ||
let mut src = Vec::new(); | ||
srdr.read_to_end(&mut src).unwrap(); | ||
let src = String::from_utf8(src).unwrap(); | ||
|
@@ -366,12 +378,15 @@ pub fn gather_comments_and_literals(span_diagnostic: &errors::Handler, | |
|
||
let bstart = rdr.last_pos; | ||
rdr.next_token(); | ||
//discard, and look ahead; we're working with internal state | ||
// discard, and look ahead; we're working with internal state | ||
let TokenAndSpan { tok, sp } = rdr.peek(); | ||
if tok.is_lit() { | ||
rdr.with_str_from(bstart, |s| { | ||
debug!("tok lit: {}", s); | ||
literals.push(Literal {lit: s.to_string(), pos: sp.lo}); | ||
literals.push(Literal { | ||
lit: s.to_string(), | ||
pos: sp.lo, | ||
}); | ||
}) | ||
} else { | ||
debug!("tok: {}", pprust::token_to_string(&tok)); | ||
|
@@ -386,31 +401,36 @@ pub fn gather_comments_and_literals(span_diagnostic: &errors::Handler, | |
mod tests { | ||
use super::*; | ||
|
||
#[test] fn test_block_doc_comment_1() { | ||
#[test] | ||
fn test_block_doc_comment_1() { | ||
let comment = "/**\n * Test \n ** Test\n * Test\n*/"; | ||
let stripped = strip_doc_comment_decoration(comment); | ||
assert_eq!(stripped, " Test \n* Test\n Test"); | ||
} | ||
|
||
#[test] fn test_block_doc_comment_2() { | ||
#[test] | ||
fn test_block_doc_comment_2() { | ||
let comment = "/**\n * Test\n * Test\n*/"; | ||
let stripped = strip_doc_comment_decoration(comment); | ||
assert_eq!(stripped, " Test\n Test"); | ||
} | ||
|
||
#[test] fn test_block_doc_comment_3() { | ||
#[test] | ||
fn test_block_doc_comment_3() { | ||
let comment = "/**\n let a: *i32;\n *a = 5;\n*/"; | ||
let stripped = strip_doc_comment_decoration(comment); | ||
assert_eq!(stripped, " let a: *i32;\n *a = 5;"); | ||
} | ||
|
||
#[test] fn test_block_doc_comment_4() { | ||
#[test] | ||
fn test_block_doc_comment_4() { | ||
let comment = "/*******************\n test\n *********************/"; | ||
let stripped = strip_doc_comment_decoration(comment); | ||
assert_eq!(stripped, " test"); | ||
} | ||
|
||
#[test] fn test_line_doc_comment() { | ||
#[test] | ||
fn test_line_doc_comment() { | ||
let stripped = strip_doc_comment_decoration("/// test"); | ||
assert_eq!(stripped, " test"); | ||
let stripped = strip_doc_comment_decoration("///! test"); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bad formatting. (Comment copied from the previous PR.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer these in separate lines... I prefer what rustfmt did.