//! 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::codemap::{BytePos, Span, DUMMY_SP};
+use syntax::source_map::{BytePos, Span, DUMMY_SP};
/// Returns attributes on the given statement.
pub fn get_attrs_from_stmt(stmt: &ast::Stmt) -> &[ast::Attribute] {
}
/// 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()
}
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)?;
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())
+ }
}
}
}