X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fattr.rs;h=033f3767ecdcf3e50ac9fdb092f94b5c6d2c8473;hb=cbd568083d87a90dfe5ab0e90f404454946c9f20;hp=f4b84e51a30b741972a45bf2e09145d6fd56dac6;hpb=0d60f6715df7bf7bff875b6492f06702564c25f2;p=rust.git diff --git a/src/attr.rs b/src/attr.rs index f4b84e51a30..033f3767ecd 100644 --- a/src/attr.rs +++ b/src/attr.rs @@ -10,17 +10,17 @@ //! 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> { +fn get_derive_spans(attr: &ast::Attribute) -> Option> { attr.meta_item_list().map(|meta_item_list| { meta_item_list .iter() @@ -216,55 +216,21 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { } 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)?; @@ -350,13 +316,34 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { 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()) + } } } }