mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-23 20:34:06 +00:00
Merge pull request #2925 from scampi/issue539
discard trailing blank comments
This commit is contained in:
commit
8573df602a
@ -96,21 +96,6 @@ impl<'a> CommentStyle<'a> {
|
|||||||
pub fn to_str_tuplet(&self) -> (&'a str, &'a str, &'a str) {
|
pub fn to_str_tuplet(&self) -> (&'a str, &'a str, &'a str) {
|
||||||
(self.opener(), self.closer(), self.line_start())
|
(self.opener(), self.closer(), self.line_start())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn line_with_same_comment_style(&self, line: &str, normalize_comments: bool) -> bool {
|
|
||||||
match *self {
|
|
||||||
CommentStyle::DoubleSlash | CommentStyle::TripleSlash | CommentStyle::Doc => {
|
|
||||||
line.trim_left().starts_with(self.line_start().trim_left())
|
|
||||||
|| comment_style(line, normalize_comments) == *self
|
|
||||||
}
|
|
||||||
CommentStyle::DoubleBullet | CommentStyle::SingleBullet | CommentStyle::Exclamation => {
|
|
||||||
line.trim_left().starts_with(self.closer().trim_left())
|
|
||||||
|| line.trim_left().starts_with(self.line_start().trim_left())
|
|
||||||
|| comment_style(line, normalize_comments) == *self
|
|
||||||
}
|
|
||||||
CommentStyle::Custom(opener) => line.trim_left().starts_with(opener.trim_right()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn comment_style(orig: &str, normalize_comments: bool) -> CommentStyle {
|
fn comment_style(orig: &str, normalize_comments: bool) -> CommentStyle {
|
||||||
@ -273,19 +258,56 @@ fn identify_comment(
|
|||||||
is_doc_comment: bool,
|
is_doc_comment: bool,
|
||||||
) -> Option<String> {
|
) -> Option<String> {
|
||||||
let style = comment_style(orig, false);
|
let style = comment_style(orig, false);
|
||||||
let first_group = orig
|
let mut first_group_ending = 0;
|
||||||
.lines()
|
|
||||||
.take_while(|l| style.line_with_same_comment_style(l, false))
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join("\n");
|
|
||||||
let rest = orig
|
|
||||||
.lines()
|
|
||||||
.skip(first_group.lines().count())
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join("\n");
|
|
||||||
|
|
||||||
|
fn compute_len(orig: &str, line: &str) -> usize {
|
||||||
|
if orig.len() > line.len() {
|
||||||
|
if orig.as_bytes()[line.len()] == b'\r' {
|
||||||
|
line.len() + 2
|
||||||
|
} else {
|
||||||
|
line.len() + 1
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
line.len()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
match style {
|
||||||
|
CommentStyle::DoubleSlash | CommentStyle::TripleSlash | CommentStyle::Doc => {
|
||||||
|
let line_start = style.line_start().trim_left();
|
||||||
|
for line in orig.lines() {
|
||||||
|
if line.trim_left().starts_with(line_start) || comment_style(line, false) == style {
|
||||||
|
first_group_ending += compute_len(&orig[first_group_ending..], line);
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
CommentStyle::Custom(opener) => {
|
||||||
|
let trimmed_opener = opener.trim_right();
|
||||||
|
for line in orig.lines() {
|
||||||
|
if line.trim_left().starts_with(trimmed_opener) {
|
||||||
|
first_group_ending += compute_len(&orig[first_group_ending..], line);
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// for a block comment, search for the closing symbol
|
||||||
|
CommentStyle::DoubleBullet | CommentStyle::SingleBullet | CommentStyle::Exclamation => {
|
||||||
|
let closer = style.closer().trim_left();
|
||||||
|
for line in orig.lines() {
|
||||||
|
first_group_ending += compute_len(&orig[first_group_ending..], line);
|
||||||
|
if line.trim_left().ends_with(closer) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let (first_group, rest) = orig.split_at(first_group_ending);
|
||||||
let first_group_str = rewrite_comment_inner(
|
let first_group_str = rewrite_comment_inner(
|
||||||
&first_group,
|
first_group,
|
||||||
block_style,
|
block_style,
|
||||||
style,
|
style,
|
||||||
shape,
|
shape,
|
||||||
@ -295,7 +317,7 @@ fn identify_comment(
|
|||||||
if rest.is_empty() {
|
if rest.is_empty() {
|
||||||
Some(first_group_str)
|
Some(first_group_str)
|
||||||
} else {
|
} else {
|
||||||
identify_comment(&rest, block_style, shape, config, is_doc_comment).map(|rest_str| {
|
identify_comment(rest, block_style, shape, config, is_doc_comment).map(|rest_str| {
|
||||||
format!(
|
format!(
|
||||||
"{}\n{}{}",
|
"{}\n{}{}",
|
||||||
first_group_str,
|
first_group_str,
|
||||||
@ -427,8 +449,12 @@ fn rewrite_comment_inner(
|
|||||||
}
|
}
|
||||||
} else if is_prev_line_multi_line && !line.is_empty() {
|
} else if is_prev_line_multi_line && !line.is_empty() {
|
||||||
result.push(' ')
|
result.push(' ')
|
||||||
} else if is_last && !closer.is_empty() && line.is_empty() {
|
} else if is_last && line.is_empty() {
|
||||||
result.push_str(&indent_str);
|
// trailing blank lines are unwanted
|
||||||
|
if !closer.is_empty() {
|
||||||
|
result.push_str(&indent_str);
|
||||||
|
}
|
||||||
|
break;
|
||||||
} else {
|
} else {
|
||||||
result.push_str(&comment_line_separator);
|
result.push_str(&comment_line_separator);
|
||||||
if !has_leading_whitespace && result.ends_with(' ') {
|
if !has_leading_whitespace && result.ends_with(' ') {
|
||||||
|
@ -87,13 +87,13 @@ fn format_project<T: FormatHandler>(
|
|||||||
if (config.skip_children() && path != main_file) || config.ignore().skip_file(&path) {
|
if (config.skip_children() && path != main_file) || config.ignore().skip_file(&path) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
should_emit_verbose(!input_is_stdin, config, || println!("Formatting {}", path));
|
should_emit_verbose(input_is_stdin, config, || println!("Formatting {}", path));
|
||||||
let is_root = path == main_file;
|
let is_root = path == main_file;
|
||||||
context.format_file(path, module, is_root)?;
|
context.format_file(path, module, is_root)?;
|
||||||
}
|
}
|
||||||
timer = timer.done_formatting();
|
timer = timer.done_formatting();
|
||||||
|
|
||||||
should_emit_verbose(!input_is_stdin, config, || {
|
should_emit_verbose(input_is_stdin, config, || {
|
||||||
println!(
|
println!(
|
||||||
"Spent {0:.3} secs in the parsing phase, and {1:.3} secs in the formatting phase",
|
"Spent {0:.3} secs in the parsing phase, and {1:.3} secs in the formatting phase",
|
||||||
timer.get_parse_time(),
|
timer.get_parse_time(),
|
||||||
@ -611,7 +611,7 @@ fn parse_crate(
|
|||||||
// Note that if you see this message and want more information,
|
// Note that if you see this message and want more information,
|
||||||
// then run the `parse_crate_mod` function above without
|
// then run the `parse_crate_mod` function above without
|
||||||
// `catch_unwind` so rustfmt panics and you can get a backtrace.
|
// `catch_unwind` so rustfmt panics and you can get a backtrace.
|
||||||
should_emit_verbose(!input_is_stdin, config, || {
|
should_emit_verbose(input_is_stdin, config, || {
|
||||||
println!("The Rust parser panicked")
|
println!("The Rust parser panicked")
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
5
tests/source/issue-539.rs
Normal file
5
tests/source/issue-539.rs
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
// rustfmt-normalize_comments: true
|
||||||
|
/*
|
||||||
|
FIXME (#3300): Should allow items to be anonymous. Right now
|
||||||
|
we just use dummy names for anon items.
|
||||||
|
*/
|
5
tests/source/issue-683.rs
Normal file
5
tests/source/issue-683.rs
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
// rustfmt-normalize_comments: true
|
||||||
|
/*
|
||||||
|
* FIXME (#3300): Should allow items to be anonymous. Right now
|
||||||
|
* we just use dummy names for anon items.
|
||||||
|
*/
|
@ -8,5 +8,4 @@
|
|||||||
/// # #![cfg_attr(not(dox), no_std)]
|
/// # #![cfg_attr(not(dox), no_std)]
|
||||||
/// fn foo() {}
|
/// fn foo() {}
|
||||||
/// ```
|
/// ```
|
||||||
///
|
|
||||||
fn foo() {}
|
fn foo() {}
|
||||||
|
3
tests/target/issue-539.rs
Normal file
3
tests/target/issue-539.rs
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
// rustfmt-normalize_comments: true
|
||||||
|
// FIXME (#3300): Should allow items to be anonymous. Right now
|
||||||
|
// we just use dummy names for anon items.
|
3
tests/target/issue-683.rs
Normal file
3
tests/target/issue-683.rs
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
// rustfmt-normalize_comments: true
|
||||||
|
// FIXME (#3300): Should allow items to be anonymous. Right now
|
||||||
|
// we just use dummy names for anon items.
|
Loading…
Reference in New Issue
Block a user