Don't reformat strings if we don't have to.

Specifically if no line exceeds the allowed width and we aren't moving the string to a new offset
This commit is contained in:
Nick Cameron 2016-01-28 19:14:08 +13:00
parent fb17a44584
commit 02302d2800
6 changed files with 69 additions and 5 deletions

View File

@ -334,7 +334,8 @@ create_config! {
chain_indent: BlockIndentStyle, BlockIndentStyle::Visual, "Indentation of chain";
reorder_imports: bool, false, "Reorder import statements alphabetically";
single_line_if_else: bool, false, "Put else on same line as closing brace for if statements";
format_strings: bool, true, "Format string literals, or leave as is";
format_strings: bool, true, "Format string literals where necessary";
force_format_strings: bool, false, "Always format string literals";
chains_overflow_last: bool, true, "Allow last call in method chain to break the line";
take_source_hints: bool, true, "Retain some formatting characteristics from the source code";
hard_tabs: bool, false, "Use tab characters for indentation, spaces for alignment";

View File

@ -1182,8 +1182,15 @@ fn rewrite_string_lit(context: &RewriteContext,
width: usize,
offset: Indent)
-> Option<String> {
if !context.config.format_strings {
return Some(context.snippet(span));
let string_lit = context.snippet(span);
if !context.config.format_strings && !context.config.force_format_strings {
return Some(string_lit);
}
if !context.config.force_format_strings &&
!string_requires_rewrite(context, span, &string_lit, width, offset) {
return Some(string_lit);
}
let fmt = StringFormat {
@ -1197,12 +1204,37 @@ fn rewrite_string_lit(context: &RewriteContext,
config: context.config,
};
let string_lit = context.snippet(span);
let str_lit = &string_lit[1..string_lit.len() - 1]; // Remove the quote characters.
// Remove the quote characters.
let str_lit = &string_lit[1..string_lit.len() - 1];
rewrite_string(str_lit, &fmt)
}
fn string_requires_rewrite(context: &RewriteContext,
span: Span,
string: &str,
width: usize,
offset: Indent)
-> bool {
if context.codemap.lookup_char_pos(span.lo).col.0 != offset.width() {
return true;
}
for (i, line) in string.lines().enumerate() {
if i == 0 {
if line.len() > width {
return true;
}
} else {
if line.len() > width + offset.width() {
return true;
}
}
}
false
}
pub fn rewrite_call<R>(context: &RewriteContext,
callee: &R,
args: &[ptr::P<ast::Expr>],

View File

@ -0,0 +1,14 @@
fn main() -> &'static str {
let too_many_lines = "H\
e\
l\
l\
o";
let leave_me = "sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss\
s
jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj";
// Crappy formatting :-(
let change_me = "ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss\
s
jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj";
}

View File

@ -1,3 +1,4 @@
// rustfmt-force_format_strings: true
// Long string literals
fn main() -> &'static str {

View File

@ -0,0 +1,15 @@
fn main() -> &'static str {
let too_many_lines = "H\
e\
l\
l\
o";
let leave_me = "sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss\
s
jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj";
// Crappy formatting :-(
let change_me = "sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
\
jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj\
j";
}

View File

@ -1,3 +1,4 @@
// rustfmt-force_format_strings: true
// Long string literals
fn main() -> &'static str {