]> git.lizzy.rs Git - rust.git/commitdiff
fix alignment of a struct's fields with the visual style
authorStéphane Campinas <stephane.campinas@gmail.com>
Sun, 4 Nov 2018 09:51:38 +0000 (10:51 +0100)
committerStéphane Campinas <stephane.campinas@gmail.com>
Sun, 4 Nov 2018 22:41:21 +0000 (23:41 +0100)
- rewrite_with_alignment was called from the expr module with the wrong
  shape that missed the extra offset needed for the visual style
- rewrite_with_alignment was indenting the given shape although that
  should have been the caller's responsability

src/expr.rs
src/items.rs
src/vertical.rs
tests/source/alignment_2633/block_style.rs [new file with mode: 0644]
tests/source/alignment_2633/visual_style.rs [new file with mode: 0644]
tests/target/alignment_2633/block_style.rs [new file with mode: 0644]
tests/target/alignment_2633/horizontal_tactic.rs [new file with mode: 0644]
tests/target/alignment_2633/visual_style.rs [new file with mode: 0644]
tests/target/issue-2633.rs [deleted file]

index fbbc592e547228c2b1175bd008a7013ff8ea5017..08b9ad3079c767358a9fc9888af4eb420146291b 100644 (file)
@@ -1565,7 +1565,7 @@ enum StructLitField<'a> {
         rewrite_with_alignment(
             fields,
             context,
-            shape,
+            v_shape,
             mk_sp(body_lo, span.hi()),
             one_line_width,
         )?
index 66fdaf50e5aaa00add34da5f0f2d488aea395095..7c6219a4e0489d75013e0250429e92a6e69ff903 100644 (file)
@@ -1265,7 +1265,7 @@ pub fn format_struct_struct(
     let items_str = rewrite_with_alignment(
         fields,
         context,
-        Shape::indented(offset, context.config).sub_width(1)?,
+        Shape::indented(offset.block_indent(context.config), context.config).sub_width(1)?,
         mk_sp(body_lo, span.hi()),
         one_line_budget,
     )?;
index dbb3e732d6268477a186f478316c8c9020c50c52..0364db763f9d69a38bba11dbf9ae0c1c9d3392d4 100644 (file)
@@ -172,16 +172,13 @@ pub fn rewrite_with_alignment<T: AlignedItem>(
     } else {
         let rest_span = mk_sp(init_last_pos, span.hi());
         let rest_str = rewrite_with_alignment(rest, context, shape, rest_span, one_line_width)?;
-        Some(
-            result
-                + spaces
-                + "\n"
-                + &shape
-                    .indent
-                    .block_indent(context.config)
-                    .to_string(context.config)
-                + &rest_str,
-        )
+        Some(format!(
+            "{}{}\n{}{}",
+            result,
+            spaces,
+            &shape.indent.to_string(context.config),
+            &rest_str
+        ))
     }
 }
 
@@ -217,9 +214,8 @@ fn rewrite_aligned_items_inner<T: AlignedItem>(
     offset: Indent,
     one_line_width: usize,
 ) -> Option<String> {
-    let item_indent = offset.block_indent(context.config);
     // 1 = ","
-    let item_shape = Shape::indented(item_indent, context.config).sub_width(1)?;
+    let item_shape = Shape::indented(offset, context.config).sub_width(1)?;
     let (mut field_prefix_max_width, field_prefix_min_width) =
         struct_field_prefix_max_min_width(context, fields, item_shape);
     let max_diff = field_prefix_max_width.saturating_sub(field_prefix_min_width);
diff --git a/tests/source/alignment_2633/block_style.rs b/tests/source/alignment_2633/block_style.rs
new file mode 100644 (file)
index 0000000..77fb291
--- /dev/null
@@ -0,0 +1,8 @@
+// rustfmt-struct_field_align_threshold: 50
+
+fn func() {
+    Ok(ServerInformation { name:         unwrap_message_string(items.get(0)),
+           vendor: unwrap_message_string(items.get(1)),
+           version: unwrap_message_string(items.get(2)),
+           spec_version: unwrap_message_string(items.get(3)), });
+}
diff --git a/tests/source/alignment_2633/visual_style.rs b/tests/source/alignment_2633/visual_style.rs
new file mode 100644 (file)
index 0000000..f34cc62
--- /dev/null
@@ -0,0 +1,9 @@
+// rustfmt-struct_field_align_threshold: 50
+// rustfmt-indent_style: Visual
+
+fn func() {
+    Ok(ServerInformation { name:         unwrap_message_string(items.get(0)),
+           vendor: unwrap_message_string(items.get(1)),
+           version: unwrap_message_string(items.get(2)),
+           spec_version: unwrap_message_string(items.get(3)), });
+}
diff --git a/tests/target/alignment_2633/block_style.rs b/tests/target/alignment_2633/block_style.rs
new file mode 100644 (file)
index 0000000..f13e8a8
--- /dev/null
@@ -0,0 +1,10 @@
+// rustfmt-struct_field_align_threshold: 50
+
+fn func() {
+    Ok(ServerInformation {
+        name:         unwrap_message_string(items.get(0)),
+        vendor:       unwrap_message_string(items.get(1)),
+        version:      unwrap_message_string(items.get(2)),
+        spec_version: unwrap_message_string(items.get(3)),
+    });
+}
diff --git a/tests/target/alignment_2633/horizontal_tactic.rs b/tests/target/alignment_2633/horizontal_tactic.rs
new file mode 100644 (file)
index 0000000..a381945
--- /dev/null
@@ -0,0 +1,13 @@
+// rustfmt-struct_field_align_threshold: 5
+
+#[derive(Fail, Debug, Clone)]
+pub enum BuildError {
+    LineTooLong { length: usize, limit: usize },
+    DisallowedByte { b: u8, pos: usize },
+    ContainsNewLine { pos: usize },
+}
+
+enum Foo {
+    A { a: usize, bbbbb: () },
+    B { a: (), bbbbb: () },
+}
diff --git a/tests/target/alignment_2633/visual_style.rs b/tests/target/alignment_2633/visual_style.rs
new file mode 100644 (file)
index 0000000..7d21b59
--- /dev/null
@@ -0,0 +1,9 @@
+// rustfmt-struct_field_align_threshold: 50
+// rustfmt-indent_style: Visual
+
+fn func() {
+    Ok(ServerInformation { name:         unwrap_message_string(items.get(0)),
+                           vendor:       unwrap_message_string(items.get(1)),
+                           version:      unwrap_message_string(items.get(2)),
+                           spec_version: unwrap_message_string(items.get(3)), });
+}
diff --git a/tests/target/issue-2633.rs b/tests/target/issue-2633.rs
deleted file mode 100644 (file)
index a381945..0000000
+++ /dev/null
@@ -1,13 +0,0 @@
-// rustfmt-struct_field_align_threshold: 5
-
-#[derive(Fail, Debug, Clone)]
-pub enum BuildError {
-    LineTooLong { length: usize, limit: usize },
-    DisallowedByte { b: u8, pos: usize },
-    ContainsNewLine { pos: usize },
-}
-
-enum Foo {
-    A { a: usize, bbbbb: () },
-    B { a: (), bbbbb: () },
-}