]> git.lizzy.rs Git - rust.git/blobdiff - src/attr.rs
Fixup formatting
[rust.git] / src / attr.rs
index c7ee633ec4edd556181bbb467a994bd6ee35f9d6..033f3767ecdcf3e50ac9fdb092f94b5c6d2c8473 100644 (file)
 use config::IndentStyle;
 use expr::rewrite_literal;
 use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
+use overflow;
 use rewrite::{Rewrite, RewriteContext};
 use shape::Shape;
 use types::{rewrite_path, PathContext};
 use utils::{count_newlines, mk_sp};
 
-use std::borrow::Cow;
 use syntax::ast;
 use syntax::source_map::{BytePos, Span, DUMMY_SP};
 
@@ -216,55 +216,21 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
             }
             ast::MetaItemKind::List(ref list) => {
                 let path = rewrite_path(context, PathContext::Type, None, &self.ident, shape)?;
-
-                let has_comma = ::expr::span_ends_with_comma(context, self.span);
-                let trailing_comma = if has_comma { "," } else { "" };
-                let combine = list.len() == 1 && match list[0].node {
-                    ast::NestedMetaItemKind::Literal(..) => false,
-                    ast::NestedMetaItemKind::MetaItem(ref inner_meta_item) => {
-                        match inner_meta_item.node {
-                            ast::MetaItemKind::List(..) => rewrite_path(
-                                context,
-                                PathContext::Type,
-                                None,
-                                &inner_meta_item.ident,
-                                shape,
-                            ).map_or(false, |s| s.len() + path.len() + 2 <= shape.width),
-                            _ => false,
-                        }
-                    }
-                };
-
-                let argument_shape = argument_shape(
-                    path.len() + 1,
-                    2 + trailing_comma.len(),
-                    combine,
-                    shape,
+                let has_trailing_comma = ::expr::span_ends_with_comma(context, self.span);
+                overflow::rewrite_with_parens(
                     context,
-                )?;
-                let item_str = format_arg_list(
+                    &path,
                     list.iter(),
-                    |nested_meta_item| nested_meta_item.span.lo(),
-                    |nested_meta_item| nested_meta_item.span.hi(),
-                    |nested_meta_item| nested_meta_item.rewrite(context, argument_shape),
+                    // 1 = "]"
+                    shape.sub_width(1)?,
                     self.span,
-                    context,
-                    argument_shape,
-                    // 3 = "()" and "]"
-                    shape
-                        .offset_left(path.len())?
-                        .sub_width(3 + trailing_comma.len())?,
-                    Some(context.config.width_heuristics().fn_call_width),
-                    combine,
-                )?;
-
-                let indent = if item_str.starts_with('\n') {
-                    shape.indent.to_string_with_newline(context.config)
-                } else {
-                    Cow::Borrowed("")
-                };
-
-                format!("{}({}{}{})", path, item_str, trailing_comma, indent)
+                    context.config.width_heuristics().attr_fn_like_width,
+                    Some(if has_trailing_comma {
+                        SeparatorTactic::Always
+                    } else {
+                        SeparatorTactic::Never
+                    }),
+                )?
             }
             ast::MetaItemKind::NameValue(ref literal) => {
                 let path = rewrite_path(context, PathContext::Type, None, &self.ident, shape)?;
@@ -351,35 +317,33 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                 return Some(snippet.to_owned());
             }
 
-            let meta = self.meta();
-
-            // This attribute is possibly a doc attribute needing normalization to a doc comment
-            if context.config.normalize_doc_attributes() {
-                if let Some(ref meta) = meta {
-                    if meta.check_name("doc") {
-                        if let Some(ref literal) = meta.value_str() {
-                            let comment_style = match self.style {
-                                ast::AttrStyle::Inner => CommentStyle::Doc,
-                                ast::AttrStyle::Outer => CommentStyle::TripleSlash,
-                            };
-
-                            let doc_comment = format!("{}{}", comment_style.opener(), literal);
-                            return rewrite_doc_comment(
-                                &doc_comment,
-                                shape.comment(context.config),
-                                context.config,
-                            );
-                        }
+            if let Some(ref meta) = self.meta() {
+                // This attribute is possibly a doc attribute needing normalization to a doc comment
+                if context.config.normalize_doc_attributes() && meta.check_name("doc") {
+                    if let Some(ref literal) = meta.value_str() {
+                        let comment_style = match self.style {
+                            ast::AttrStyle::Inner => CommentStyle::Doc,
+                            ast::AttrStyle::Outer => CommentStyle::TripleSlash,
+                        };
+
+                        let doc_comment = format!("{}{}", comment_style.opener(), literal);
+                        return rewrite_doc_comment(
+                            &doc_comment,
+                            shape.comment(context.config),
+                            context.config,
+                        );
                     }
                 }
-            }
 
-            // 1 = `[`
-            let shape = shape.offset_left(prefix.len() + 1)?;
-            Some(
-                meta.and_then(|meta| meta.rewrite(context, shape))
-                    .map_or_else(|| snippet.to_owned(), |rw| format!("{}[{}]", prefix, rw)),
-            )
+                // 1 = `[`
+                let shape = shape.offset_left(prefix.len() + 1)?;
+                Some(
+                    meta.rewrite(context, shape)
+                        .map_or_else(|| snippet.to_owned(), |rw| format!("{}[{}]", prefix, rw)),
+                )
+            } else {
+                Some(snippet.to_owned())
+            }
         }
     }
 }