diff --git a/src/config.rs b/src/config.rs index 48b7a06e538..01fc86bdbf4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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"; diff --git a/src/expr.rs b/src/expr.rs index 8e76001e731..b299f82a883 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1182,8 +1182,15 @@ fn rewrite_string_lit(context: &RewriteContext, width: usize, offset: Indent) -> Option { - 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(context: &RewriteContext, callee: &R, args: &[ptr::P], diff --git a/tests/source/string-lit-2.rs b/tests/source/string-lit-2.rs new file mode 100644 index 00000000000..8ba60ccac1d --- /dev/null +++ b/tests/source/string-lit-2.rs @@ -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"; +} diff --git a/tests/source/string-lit.rs b/tests/source/string-lit.rs index c89b798a0ea..ca8aa36a90d 100644 --- a/tests/source/string-lit.rs +++ b/tests/source/string-lit.rs @@ -1,3 +1,4 @@ +// rustfmt-force_format_strings: true // Long string literals fn main() -> &'static str { diff --git a/tests/target/string-lit-2.rs b/tests/target/string-lit-2.rs new file mode 100644 index 00000000000..08e8be8186c --- /dev/null +++ b/tests/target/string-lit-2.rs @@ -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"; +} diff --git a/tests/target/string-lit.rs b/tests/target/string-lit.rs index b9a676c49ea..e7146a84f9e 100644 --- a/tests/target/string-lit.rs +++ b/tests/target/string-lit.rs @@ -1,3 +1,4 @@ +// rustfmt-force_format_strings: true // Long string literals fn main() -> &'static str {