mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-30 18:53:39 +00:00
Add a more lightweight method for rewriting comments when we are not normalising
Fixes #652
This commit is contained in:
parent
d948485371
commit
0218a41d73
@ -38,9 +38,21 @@ pub fn rewrite_comment(orig: &str,
|
||||
offset: Indent,
|
||||
config: &Config)
|
||||
-> Option<String> {
|
||||
let s = orig.trim();
|
||||
// If there are lines without a starting sigil, we won't format them correctly
|
||||
// so in that case we won't even re-align (if !config.normalize_comments) and
|
||||
// we should stop now.
|
||||
let num_bare_lines = orig.lines()
|
||||
.map(|line| line.trim())
|
||||
.filter(|l| !(l.starts_with('*') || l.starts_with("//") || l.starts_with("/*")))
|
||||
.count();
|
||||
if num_bare_lines > 0 && !config.normalize_comments {
|
||||
return Some(orig.to_owned());
|
||||
}
|
||||
|
||||
if !config.normalize_comments && !config.wrap_comments {
|
||||
return light_rewrite_comment(orig, offset, config);
|
||||
}
|
||||
|
||||
// Edge case: block comments. Let's not trim their lines (for now).
|
||||
let (opener, closer, line_start) =
|
||||
if block_style {
|
||||
("/* ", " */", " * ")
|
||||
@ -74,7 +86,7 @@ pub fn rewrite_comment(orig: &str,
|
||||
};
|
||||
|
||||
let max_chars = width.checked_sub(closer.len() + opener.len()).unwrap_or(1);
|
||||
|
||||
let indent_str = offset.to_string(config);
|
||||
let fmt = StringFormat {
|
||||
opener: "",
|
||||
closer: "",
|
||||
@ -86,29 +98,17 @@ pub fn rewrite_comment(orig: &str,
|
||||
config: config,
|
||||
};
|
||||
|
||||
let indent_str = offset.to_string(config);
|
||||
let line_breaks = s.chars().filter(|&c| c == '\n').count();
|
||||
|
||||
let num_bare_lines = s.lines()
|
||||
.enumerate()
|
||||
.map(|(_, line)| line.trim())
|
||||
.filter(|l| !(l.starts_with('*') || l.starts_with("//") || l.starts_with("/*")))
|
||||
.count();
|
||||
|
||||
if num_bare_lines > 0 && !config.normalize_comments {
|
||||
return Some(orig.to_owned());
|
||||
}
|
||||
|
||||
let lines = s.lines()
|
||||
let line_breaks = orig.trim_right().chars().filter(|&c| c == '\n').count();
|
||||
let lines = orig.lines()
|
||||
.enumerate()
|
||||
.map(|(i, mut line)| {
|
||||
line = line.trim();
|
||||
// Drop old closer.
|
||||
if i == line_breaks && line.ends_with("*/") && !line.starts_with("//") {
|
||||
line = &line[..(line.len() - 2)];
|
||||
line = &line[..(line.len() - 2)].trim_right();
|
||||
}
|
||||
|
||||
line.trim_right()
|
||||
line
|
||||
})
|
||||
.map(left_trim_comment_line)
|
||||
.map(|line| if orig.starts_with("/*") && line_breaks == 0 {
|
||||
@ -150,6 +150,31 @@ pub fn rewrite_comment(orig: &str,
|
||||
Some(result)
|
||||
}
|
||||
|
||||
/// Trims whitespace and aligns to indent, but otherwise does not change comments.
|
||||
fn light_rewrite_comment(orig: &str, offset: Indent, config: &Config) -> Option<String> {
|
||||
let lines: Vec<&str> = orig.lines()
|
||||
.map(|l| {
|
||||
// This is basically just l.trim(), but in the case that a line starts
|
||||
// with `*` we want to leave one space before it, so it aligns with the
|
||||
// `*` in `/*`.
|
||||
let first_non_whitespace = l.find(|c| !char::is_whitespace(c));
|
||||
if let Some(fnw) = first_non_whitespace {
|
||||
if l.as_bytes()[fnw] == '*' as u8 && fnw > 0 {
|
||||
&l[fnw - 1..]
|
||||
} else {
|
||||
&l[fnw..]
|
||||
}
|
||||
} else {
|
||||
""
|
||||
}
|
||||
.trim_right()
|
||||
})
|
||||
.collect();
|
||||
Some(lines.join(&format!("\n{}", offset.to_string(config))))
|
||||
}
|
||||
|
||||
/// Trims comment characters and possibly a single space from the left of a string.
|
||||
/// Does not trim all whitespace.
|
||||
fn left_trim_comment_line(line: &str) -> &str {
|
||||
if line.starts_with("//! ") || line.starts_with("/// ") || line.starts_with("/*! ") ||
|
||||
line.starts_with("/** ") {
|
||||
@ -708,6 +733,7 @@ mod test {
|
||||
fn format_comments() {
|
||||
let mut config: ::config::Config = Default::default();
|
||||
config.wrap_comments = true;
|
||||
config.normalize_comments = true;
|
||||
|
||||
let comment = rewrite_comment(" //test", true, 100, Indent::new(0, 100), &config).unwrap();
|
||||
assert_eq!("/* test */", comment);
|
||||
|
@ -1 +1,2 @@
|
||||
reorder_imports = true
|
||||
normalize_comments = true
|
||||
|
@ -1,3 +1,4 @@
|
||||
// rustfmt-normalize_comments: true
|
||||
|
||||
// sadfsdfa
|
||||
//sdffsdfasdf
|
||||
|
@ -1,3 +1,4 @@
|
||||
// rustfmt-normalize_comments: true
|
||||
// rustfmt-reorder_imported_names: true
|
||||
|
||||
use path::{C,/*A*/ A, B /* B */, self /* self */};
|
||||
|
@ -1,3 +1,4 @@
|
||||
// rustfmt-normalize_comments: true
|
||||
fn main() {
|
||||
// Line Comment
|
||||
/* Block Comment */
|
||||
|
@ -1,3 +1,4 @@
|
||||
// rustfmt-normalize_comments: true
|
||||
|
||||
fn main() {
|
||||
let constellation_chan = Constellation::<layout::layout_task::LayoutTask, script::script_task::ScriptTask> ::start(
|
||||
|
@ -1,3 +1,4 @@
|
||||
// rustfmt-normalize_comments: true
|
||||
fn main() {
|
||||
let z = match x {
|
||||
"pat1" => 1,
|
||||
|
@ -1,3 +1,4 @@
|
||||
// rustfmt-normalize_comments: true
|
||||
|
||||
// sadfsdfa
|
||||
// sdffsdfasdf
|
||||
|
@ -1,3 +1,4 @@
|
||||
// rustfmt-normalize_comments: true
|
||||
// rustfmt-reorder_imported_names: true
|
||||
|
||||
use path::{self /* self */, /* A */ A, B /* B */, C};
|
||||
|
@ -1,3 +1,4 @@
|
||||
// rustfmt-normalize_comments: true
|
||||
fn main() {
|
||||
// Line Comment
|
||||
// Block Comment
|
||||
|
@ -1,3 +1,4 @@
|
||||
// rustfmt-normalize_comments: true
|
||||
|
||||
fn main() {
|
||||
let constellation_chan =
|
||||
|
@ -1,3 +1,4 @@
|
||||
// rustfmt-normalize_comments: true
|
||||
fn main() {
|
||||
let z = match x {
|
||||
"pat1" => 1,
|
||||
|
Loading…
Reference in New Issue
Block a user