about summary refs log tree commit diff
path: root/src/items.rs
diff options
context:
space:
mode:
authortopecongiro <seuchida@gmail.com>2018-04-28 13:59:03 +0900
committertopecongiro <seuchida@gmail.com>2018-04-28 13:59:03 +0900
commit82d8dd2f4bdcd61b08c6952e4e6ebdf4db70b299 (patch)
treefb56c706c13c369fbf447977802ffef15207caec /src/items.rs
parenta9553654592fa9508c4a42b9c47a04fc3b4357d8 (diff)
Factor out a formatting routine for empty struct and tuple
Diffstat (limited to 'src/items.rs')
-rw-r--r--src/items.rs35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/items.rs b/src/items.rs
index 1924d881127..e6351a3aa15 100644
--- a/src/items.rs
+++ b/src/items.rs
@@ -1242,6 +1242,41 @@ fn get_bytepos_after_visibility(vis: &ast::Visibility, default_span: Span) -> By
     }
 }
 
+// Format tuple or struct without any fields. We need to make sure that the comments
+// inside the delimiters are preserved.
+fn format_empty_struct_or_tuple(
+    context: &RewriteContext,
+    span: Span,
+    offset: Indent,
+    result: &mut String,
+    opener: &str,
+    closer: &str,
+) {
+    // 3 = " {}" or "();"
+    let used_width = last_line_used_width(&result, offset.width()) + 3;
+    if used_width > context.config.max_width() {
+        result.push_str(&offset.to_string_with_newline(context.config))
+    }
+    result.push_str(opener);
+    match rewrite_missing_comment(span, Shape::indented(offset, context.config), context) {
+        Some(ref s) if s.is_empty() => (),
+        Some(ref s) => {
+            if !is_single_line(s) || first_line_contains_single_line_comment(s) {
+                let nested_indent_str = offset
+                    .block_indent(context.config)
+                    .to_string_with_newline(context.config);
+                result.push_str(&nested_indent_str);
+            }
+            result.push_str(s);
+            if last_line_contains_single_line_comment(s) {
+                result.push_str(&offset.to_string_with_newline(context.config));
+            }
+        }
+        None => result.push_str(context.snippet(span)),
+    }
+    result.push_str(closer);
+}
+
 fn format_tuple_struct(
     context: &RewriteContext,
     struct_parts: &StructParts,