]> git.lizzy.rs Git - rust.git/commitdiff
Don't reformat strings if we don't have to.
authorNick Cameron <ncameron@mozilla.com>
Thu, 28 Jan 2016 06:14:08 +0000 (19:14 +1300)
committerNick Cameron <ncameron@mozilla.com>
Thu, 28 Jan 2016 06:14:08 +0000 (19:14 +1300)
Specifically if no line exceeds the allowed width and we aren't moving the string to a new offset

src/config.rs
src/expr.rs
tests/source/string-lit-2.rs [new file with mode: 0644]
tests/source/string-lit.rs
tests/target/string-lit-2.rs [new file with mode: 0644]
tests/target/string-lit.rs

index 48b7a06e538ee4b2490a3932df1177a8454320d8..01fc86bdbf45c7c963cf0d700ce05622c5433974 100644 (file)
@@ -334,7 +334,8 @@ fn default() -> 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";
index 8e76001e731a0bd6e37dda2a8c76f0b2502581b0..b299f82a883e0c6a60b970443b8f479df450414a 100644 (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>],
diff --git a/tests/source/string-lit-2.rs b/tests/source/string-lit-2.rs
new file mode 100644 (file)
index 0000000..8ba60cc
--- /dev/null
@@ -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";
+}
index c89b798a0ea7042054a178332df0ece0953e7957..ca8aa36a90d8f63295e564ffd5b10f8c472f7ceb 100644 (file)
@@ -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 (file)
index 0000000..08e8be8
--- /dev/null
@@ -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";
+}
index b9a676c49ea7017d130a50722b2dbe4ab8246e63..e7146a84f9e97c7ba43f7d4443645d6590f38b18 100644 (file)
@@ -1,3 +1,4 @@
+// rustfmt-force_format_strings: true
 // Long string literals
 
 fn main() -> &'static str {