about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorStéphane Campinas <stephane.campinas@gmail.com>2019-02-07 00:05:05 +0100
committerStéphane Campinas <stephane.campinas@gmail.com>2019-02-07 00:05:05 +0100
commit813aa79567cba6b0e2ed8d1126e11e83c8d450fd (patch)
tree091d96a70e853aca1a4911b5caef84339ec083ae /src
parentecde43e06b6ee52b49ce2cc96c35f9db7a1b4058 (diff)
fix formatting of strings within a macro
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs7
-rw-r--r--src/macros.rs6
-rw-r--r--src/utils.rs13
3 files changed, 18 insertions, 8 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 6fc3dc85945..35f0cba26a8 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -50,10 +50,11 @@ use crate::comment::LineClasses;
 use crate::formatting::{FormatErrorMap, FormattingError, ReportedErrors, SourceFile};
 use crate::issues::Issue;
 use crate::shape::Indent;
+use crate::utils::indent_next_line;
 
 pub use crate::config::{
     load_config, CliOptions, Color, Config, Edition, EmitMode, FileLines, FileName, NewlineStyle,
-    Range, Verbosity,
+    Range, Verbosity, Version,
 };
 
 #[macro_use]
@@ -438,7 +439,7 @@ fn format_code_block(code_snippet: &str, config: &Config) -> Option<FormattedSni
             }
             result.push_str(&line);
             result.push('\n');
-            need_indent = !kind.is_string() || line.ends_with('\\');
+            need_indent = indent_next_line(kind, &line, config);
         }
         result.push('}');
         result
@@ -499,7 +500,7 @@ fn format_code_block(code_snippet: &str, config: &Config) -> Option<FormattedSni
             line
         };
         result.push_str(trimmed_line);
-        is_indented = !kind.is_string() || line.ends_with('\\');
+        is_indented = indent_next_line(kind, line, config);
     }
     Some(FormattedSnippet {
         snippet: result,
diff --git a/src/macros.rs b/src/macros.rs
index 51cf9db53e8..9973a0f9953 100644
--- a/src/macros.rs
+++ b/src/macros.rs
@@ -43,8 +43,8 @@ use crate::shape::{Indent, Shape};
 use crate::source_map::SpanUtils;
 use crate::spanned::Spanned;
 use crate::utils::{
-    format_visibility, is_empty_line, mk_sp, remove_trailing_white_spaces, rewrite_ident,
-    trim_left_preserve_layout, wrap_str, NodeIdExt,
+    format_visibility, indent_next_line, is_empty_line, mk_sp, remove_trailing_white_spaces,
+    rewrite_ident, trim_left_preserve_layout, wrap_str, NodeIdExt,
 };
 use crate::visitor::FmtVisitor;
 
@@ -1303,7 +1303,7 @@ impl MacroBranch {
                     {
                         s += &indent_str;
                     }
-                    (s + l + "\n", !kind.is_string() || l.ends_with('\\'))
+                    (s + l + "\n", indent_next_line(kind, &l, &config))
                 },
             )
             .0;
diff --git a/src/utils.rs b/src/utils.rs
index 06f1ca2d8d8..4a26fc4c5dd 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -526,8 +526,10 @@ pub fn trim_left_preserve_layout(orig: &str, indent: Indent, config: &Config) ->
                 Some(get_prefix_space_width(config, &line))
             };
 
-            let new_veto_trim_value = (kind.is_string()
-                || (config.version() == Version::Two && kind.is_commented_string()))
+            // just InString{Commented} in order to allow the start of a string to be indented
+            let new_veto_trim_value = (kind == FullCodeCharKind::InString
+                || (config.version() == Version::Two
+                    && kind == FullCodeCharKind::InStringCommented))
                 && !line.ends_with('\\');
             let line = if veto_trim || new_veto_trim_value {
                 veto_trim = new_veto_trim_value;
@@ -574,6 +576,13 @@ pub fn trim_left_preserve_layout(orig: &str, indent: Indent, config: &Config) ->
     )
 }
 
+/// Based on the given line, determine if the next line can be indented or not.
+/// This allows to preserve the indentation of multi-line literals.
+pub fn indent_next_line(kind: FullCodeCharKind, line: &str, config: &Config) -> bool {
+    !(kind.is_string() || (config.version() == Version::Two && kind.is_commented_string()))
+        || line.ends_with('\\')
+}
+
 pub fn is_empty_line(s: &str) -> bool {
     s.is_empty() || s.chars().all(char::is_whitespace)
 }