Merge pull request #1869 from topecongiro/configs-error_on_line_overflow_comments

Add error_on_line_overflow_comments config option
This commit is contained in:
Nick Cameron 2017-08-10 11:36:39 +12:00 committed by GitHub
commit e1a0d82dd7
3 changed files with 22 additions and 1 deletions

View File

@ -501,6 +501,7 @@ create_config! {
via the --file-lines option";
max_width: usize, 100, "Maximum width of each line";
error_on_line_overflow: bool, true, "Error if unable to get all lines within max_width";
error_on_line_overflow_comments: bool, true, "Error if unable to get comments within max_width";
tab_spaces: usize, 4, "Number of spaces per tab";
fn_call_width: usize, 60,
"Maximum width of the args of a function call before falling back to vertical formatting";

View File

@ -599,6 +599,8 @@ fn format_lines(text: &mut StringBuffer, name: &str, config: &Config, report: &m
let mut newline_count = 0;
let mut errors = vec![];
let mut issue_seeker = BadIssueSeeker::new(config.report_todo(), config.report_fixme());
let mut prev_char: Option<char> = None;
let mut is_comment = false;
for (c, b) in text.chars() {
if c == '\r' {
@ -626,7 +628,9 @@ fn format_lines(text: &mut StringBuffer, name: &str, config: &Config, report: &m
}
// Check for any line width errors we couldn't correct.
if config.error_on_line_overflow() && line_len > config.max_width() {
let report_error_on_line_overflow = config.error_on_line_overflow() &&
(config.error_on_line_overflow_comments() || !is_comment);
if report_error_on_line_overflow && line_len > config.max_width() {
errors.push(FormattingError {
line: cur_line,
kind: ErrorKind::LineOverflow(line_len, config.max_width()),
@ -638,6 +642,8 @@ fn format_lines(text: &mut StringBuffer, name: &str, config: &Config, report: &m
cur_line += 1;
newline_count += 1;
last_wspace = None;
prev_char = None;
is_comment = false;
} else {
newline_count = 0;
line_len += 1;
@ -645,9 +651,16 @@ fn format_lines(text: &mut StringBuffer, name: &str, config: &Config, report: &m
if last_wspace.is_none() {
last_wspace = Some(b);
}
} else if c == '/' {
match prev_char {
Some('/') => is_comment = true,
_ => (),
}
last_wspace = None;
} else {
last_wspace = None;
}
prev_char = Some(c);
}
}

View File

@ -0,0 +1,7 @@
// rustfmt-error_on_line_overflow_comments: false
// Error on line overflow comment
// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
fn main() {
// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
}