]> git.lizzy.rs Git - rust.git/blobdiff - src/attr.rs
Fixup formatting
[rust.git] / src / attr.rs
index deaf2be46b80c1cdf357c8c31cd18e3903ce16fe..033f3767ecdcf3e50ac9fdb092f94b5c6d2c8473 100644 (file)
 
 //! Format attributes and meta items.
 
-use comment::{contains_comment, rewrite_doc_comment};
+use comment::{contains_comment, rewrite_doc_comment, CommentStyle};
 use config::lists::*;
 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};
 
@@ -48,7 +48,7 @@ fn is_derive(attr: &ast::Attribute) -> bool {
 }
 
 /// Returns the arguments of `#[derive(...)]`.
-fn get_derive_spans<'a>(attr: &ast::Attribute) -> Option<Vec<Span>> {
+fn get_derive_spans(attr: &ast::Attribute) -> Option<Vec<Span>> {
     attr.meta_item_list().map(|meta_item_list| {
         meta_item_list
             .iter()
@@ -216,58 +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 snippet = context.snippet(self.span);
-                // 2 = )] (this might go wrong if there is whitespace between the brackets, but
-                // it's close enough).
-                let snippet = snippet[..snippet.len() - 2].trim();
-                let trailing_comma = if snippet.ends_with(',') { "," } 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)?;
@@ -353,13 +316,34 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
             if contains_comment(snippet) {
                 return Some(snippet.to_owned());
             }
-            // 1 = `[`
-            let shape = shape.offset_left(prefix.len() + 1)?;
-            Some(
-                self.meta()
-                    .and_then(|meta| meta.rewrite(context, shape))
-                    .map_or_else(|| snippet.to_owned(), |rw| format!("{}[{}]", prefix, rw)),
-            )
+
+            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.rewrite(context, shape)
+                        .map_or_else(|| snippet.to_owned(), |rw| format!("{}[{}]", prefix, rw)),
+                )
+            } else {
+                Some(snippet.to_owned())
+            }
         }
     }
 }