about summary refs log tree commit diff
path: root/src/lists.rs
diff options
context:
space:
mode:
authormwiczer <wiczerm@gmail.com>2015-10-07 19:23:07 -0400
committermwiczer <wiczerm@gmail.com>2015-10-08 17:22:57 -0400
commit5162282b6060349a672419fb86cdb2a46b59d113 (patch)
treead21dc82debc3ffddad10f3b790a9c49a2f1d539 /src/lists.rs
parent8880c0e5d3963c2ec75e22e70421d7185e746c7c (diff)
Support pre- and post-comments for enums
Use lists to format enum variants rather than special formatting.
Add tests for enums mostly around block comments.
Diffstat (limited to 'src/lists.rs')
-rw-r--r--src/lists.rs19
1 files changed, 16 insertions, 3 deletions
diff --git a/src/lists.rs b/src/lists.rs
index 6f464805e4b..35705611f1e 100644
--- a/src/lists.rs
+++ b/src/lists.rs
@@ -208,7 +208,18 @@ pub fn write_list<'b, I, T>(items: I, formatting: &ListFormatting<'b>) -> Option
         } else {
             0
         };
-        let item_width = inner_item.len() + item_sep_len;
+
+        // Item string may be multi-line. Its length (used for block comment alignment)
+        // Should be only the length of the last line.
+        let item_last_line = if item.is_multiline() {
+            inner_item.lines().last().unwrap_or("")
+        } else {
+            inner_item.as_ref()
+        };
+        let mut item_last_line_width = item_last_line.len() + item_sep_len;
+        if item_last_line.starts_with(indent_str) {
+            item_last_line_width -= indent_str.len();
+        }
 
         match tactic {
             DefinitiveListTactic::Horizontal if !first => {
@@ -284,10 +295,12 @@ pub fn write_list<'b, I, T>(items: I, formatting: &ListFormatting<'b>) -> Option
 
         if tactic == DefinitiveListTactic::Vertical && item.post_comment.is_some() {
             // 1 = space between item and comment.
-            let width = formatting.width.checked_sub(item_width + 1).unwrap_or(1);
+            let width = formatting.width.checked_sub(item_last_line_width + 1).unwrap_or(1);
             let mut offset = formatting.indent;
-            offset.alignment += item_width + 1;
+            offset.alignment += item_last_line_width + 1;
             let comment = item.post_comment.as_ref().unwrap();
+
+            debug!("Width = {}, offset = {:?}", width, offset);
             // Use block-style only for the last item or multiline comments.
             let block_style = !formatting.ends_with_newline && last ||
                               comment.trim().contains('\n') ||