Specifically if no line exceeds the allowed width and we aren't moving the string to a new offset
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";
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 {
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>],
--- /dev/null
+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";
+}
+// rustfmt-force_format_strings: true
// Long string literals
fn main() -> &'static str {
--- /dev/null
+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";
+}
+// rustfmt-force_format_strings: true
// Long string literals
fn main() -> &'static str {