]> git.lizzy.rs Git - rust.git/blobdiff - src/items.rs
Merge pull request #3266 from wada314/fix-2973
[rust.git] / src / items.rs
index 19caafbfb828c3587eb24cc3b5e81efeb905ca0b..8298ffee7fa4363e7866b5048e117b8194e9097f 100644 (file)
@@ -94,10 +94,16 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
 
             if let Some(ref ty) = self.ty {
                 let separator = type_annotation_separator(context.config);
-                let indent = shape.indent + last_line_width(&result) + separator.len();
-                // 1 = ;
-                let budget = shape.width.checked_sub(indent.width() + 1)?;
-                let rewrite = ty.rewrite(context, Shape::legacy(budget, indent))?;
+                let ty_shape = if pat_str.contains('\n') {
+                    shape.with_max_width(context.config)
+                } else {
+                    shape
+                }
+                .offset_left(last_line_width(&result) + separator.len())?
+                // 2 = ` =`
+                .sub_width(2)?;
+
+                let rewrite = ty.rewrite(context, ty_shape)?;
 
                 infix.push_str(separator);
                 infix.push_str(&rewrite);
@@ -458,7 +464,8 @@ pub fn visit_enum(
                 BracePos::Auto
             },
             self.block_indent,
-            mk_sp(span.lo(), body_start),
+            // make a span that starts right after `enum Foo`
+            mk_sp(ident.span.hi(), body_start),
             last_line_width(&enum_header),
         )
         .unwrap();
@@ -1180,7 +1187,8 @@ fn format_unit_struct(context: &RewriteContext, p: &StructParts, offset: Indent)
             context.config.brace_style(),
             BracePos::None,
             offset,
-            mk_sp(generics.span.lo(), hi),
+            // make a span that starts right after `struct Foo`
+            mk_sp(p.ident.span.hi(), hi),
             last_line_width(&header_str),
         )?
     } else {
@@ -1202,7 +1210,7 @@ pub fn format_struct_struct(
     let header_str = struct_parts.format_header(context);
     result.push_str(&header_str);
 
-    let header_hi = span.lo() + BytePos(header_str.len() as u32);
+    let header_hi = struct_parts.ident.span.hi();
     let body_lo = context.snippet_provider.span_after(span, "{");
 
     let generics_str = match struct_parts.generics {
@@ -1216,6 +1224,7 @@ pub fn format_struct_struct(
                 BracePos::Auto
             },
             offset,
+            // make a span that starts right after `struct Foo`
             mk_sp(header_hi, body_lo),
             last_line_width(&result),
         )?,
@@ -1240,7 +1249,7 @@ pub fn format_struct_struct(
     {
         result.push('\n');
         result.push_str(&offset.to_string(context.config));
-        result.push_str(generics_str.trim_left());
+        result.push_str(generics_str.trim_start());
     } else {
         result.push_str(&generics_str);
     }
@@ -1259,7 +1268,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,
     )?;
@@ -1484,7 +1493,7 @@ fn rewrite_type_item<R: Rewrite>(
         result.push_str(suffix);
     } else {
         result.push_str(&indent.to_string_with_newline(context.config));
-        result.push_str(suffix.trim_left());
+        result.push_str(suffix.trim_start());
     }
 
     // 1 = ";"
@@ -1592,7 +1601,7 @@ pub fn rewrite_struct_field(
     for _ in 0..lhs_offset {
         spacing.push(' ');
     }
-    // In this extreme case we will be missing a space betweeen an attribute and a field.
+    // In this extreme case we will be missing a space between an attribute and a field.
     if prefix.is_empty() && !attrs_str.is_empty() && attrs_extendable && spacing.is_empty() {
         spacing.push(' ');
     }
@@ -1610,7 +1619,7 @@ pub fn rewrite_struct_field(
     let field_str = rewrite_assign_rhs(context, prefix, &*field.ty, shape)?;
     // Remove a leading white-space from `rewrite_assign_rhs()` when rewriting a tuple struct.
     let field_str = if is_prefix_empty {
-        field_str.trim_left()
+        field_str.trim_start()
     } else {
         &field_str
     };
@@ -1977,7 +1986,7 @@ fn rewrite_fn_base(
     let snuggle_angle_bracket = generics_str
         .lines()
         .last()
-        .map_or(false, |l| l.trim_left().len() == 1);
+        .map_or(false, |l| l.trim_start().len() == 1);
 
     // Note that the width and indent don't really matter, we'll re-layout the
     // return type later anyway.