1 //! Format attributes and meta items.
4 use rustc_ast::HasAttrs;
5 use rustc_span::{symbol::sym, Span, Symbol};
7 use self::doc_comment::DocCommentFormatter;
8 use crate::comment::{contains_comment, rewrite_doc_comment, CommentStyle};
9 use crate::config::lists::*;
10 use crate::config::IndentStyle;
11 use crate::expr::rewrite_literal;
12 use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
14 use crate::rewrite::{Rewrite, RewriteContext};
15 use crate::shape::Shape;
16 use crate::source_map::SpanUtils;
17 use crate::types::{rewrite_path, PathContext};
18 use crate::utils::{count_newlines, mk_sp};
22 pub(crate) fn contains_name(attrs: &[ast::Attribute], name: Symbol) -> bool {
23 attrs.iter().any(|attr| attr.has_name(name))
26 pub(crate) fn first_attr_value_str_by_name(
27 attrs: &[ast::Attribute],
32 .find(|attr| attr.has_name(name))
33 .and_then(|attr| attr.value_str())
36 /// Returns attributes on the given statement.
37 pub(crate) fn get_attrs_from_stmt(stmt: &ast::Stmt) -> &[ast::Attribute] {
41 pub(crate) fn get_span_without_attrs(stmt: &ast::Stmt) -> Span {
43 ast::StmtKind::Local(ref local) => local.span,
44 ast::StmtKind::Item(ref item) => item.span,
45 ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => expr.span,
46 ast::StmtKind::MacCall(ref mac_stmt) => mac_stmt.mac.span(),
47 ast::StmtKind::Empty => stmt.span,
51 /// Returns attributes that are within `outer_span`.
52 pub(crate) fn filter_inline_attrs(attrs: &[ast::Attribute], outer_span: Span) -> ast::AttrVec {
55 .filter(|a| outer_span.lo() <= a.span.lo() && a.span.hi() <= outer_span.hi())
60 fn is_derive(attr: &ast::Attribute) -> bool {
61 attr.has_name(sym::derive)
64 // The shape of the arguments to a function-like attribute.
70 context: &RewriteContext<'_>,
72 match context.config.indent_style() {
73 IndentStyle::Block => {
75 shape.offset_left(left)
79 .block_indent(context.config.tab_spaces())
80 .with_max_width(context.config),
84 IndentStyle::Visual => shape
87 .and_then(|s| s.sub_width(right)),
92 derives: &[ast::Attribute],
94 context: &RewriteContext<'_>,
96 // Collect all items from all attributes
97 let all_items = derives
100 // Parse the derive items and extract the span for each item; if any
101 // attribute is not parseable, none of the attributes will be
103 let item_spans = attr.meta_item_list().map(|meta_item_list| {
106 .map(|nested_meta_item| nested_meta_item.span())
109 let items = itemize_list(
110 context.snippet_provider,
116 |span| Some(context.snippet(*span).to_owned()),
117 // We update derive attribute spans to start after the opening '('
118 // This helps us focus parsing to just what's inside #[derive(...)]
119 context.snippet_provider.span_after(attr.span, "("),
126 // Fail if any attribute failed.
127 .collect::<Option<Vec<_>>>()?
128 // Collect the results into a single, flat, Vec.
131 .collect::<Vec<_>>();
133 // Collect formatting parameters.
134 let prefix = attr_prefix(&derives[0]);
135 let argument_shape = argument_shape(
136 "[derive()]".len() + prefix.len(),
142 let one_line_shape = shape
143 .offset_left("[derive()]".len() + prefix.len())?
144 .sub_width("()]".len())?;
145 let one_line_budget = one_line_shape.width;
147 let tactic = definitive_tactic(
149 ListTactic::HorizontalVertical,
151 argument_shape.width,
153 let trailing_separator = match context.config.indent_style() {
154 // We always add the trailing comma and remove it if it is not needed.
155 IndentStyle::Block => SeparatorTactic::Always,
156 IndentStyle::Visual => SeparatorTactic::Never,
159 // Format the collection of items.
160 let fmt = ListFormatting::new(argument_shape, context.config)
162 .trailing_separator(trailing_separator)
163 .ends_with_newline(false);
164 let item_str = write_list(&all_items, &fmt)?;
166 debug!("item_str: '{}'", item_str);
168 // Determine if the result will be nested, i.e. if we're using the block
169 // indent style and either the items are on multiple lines or we've exceeded
170 // our budget to fit on a single line.
171 let nested = context.config.indent_style() == IndentStyle::Block
172 && (item_str.contains('\n') || item_str.len() > one_line_budget);
174 // Format the final result.
175 let mut result = String::with_capacity(128);
176 result.push_str(prefix);
177 result.push_str("[derive(");
179 let nested_indent = argument_shape.indent.to_string_with_newline(context.config);
180 result.push_str(&nested_indent);
181 result.push_str(&item_str);
182 result.push_str(&shape.indent.to_string_with_newline(context.config));
183 } else if let SeparatorTactic::Always = context.config.trailing_comma() {
184 // Retain the trailing comma.
185 result.push_str(&item_str);
186 } else if item_str.ends_with(',') {
187 // Remove the trailing comma.
188 result.push_str(&item_str[..item_str.len() - 1]);
190 result.push_str(&item_str);
192 result.push_str(")]");
197 /// Returns the first group of attributes that fills the given predicate.
198 /// We consider two doc comments are in different group if they are separated by normal comments.
199 fn take_while_with_pred<'a, P>(
200 context: &RewriteContext<'_>,
201 attrs: &'a [ast::Attribute],
203 ) -> &'a [ast::Attribute]
205 P: Fn(&ast::Attribute) -> bool,
208 let mut iter = attrs.iter().peekable();
210 while let Some(attr) = iter.next() {
216 if let Some(next_attr) = iter.peek() {
217 // Extract comments between two attributes.
218 let span_between_attr = mk_sp(attr.span.hi(), next_attr.span.lo());
219 let snippet = context.snippet(span_between_attr);
220 if count_newlines(snippet) >= 2 || snippet.contains('/') {
229 /// Rewrite the any doc comments which come before any other attributes.
230 fn rewrite_initial_doc_comments(
231 context: &RewriteContext<'_>,
232 attrs: &[ast::Attribute],
234 ) -> Option<(usize, Option<String>)> {
235 if attrs.is_empty() {
236 return Some((0, None));
238 // Rewrite doc comments
239 let sugared_docs = take_while_with_pred(context, attrs, |a| a.is_doc_comment());
240 if !sugared_docs.is_empty() {
241 let snippet = sugared_docs
243 .map(|a| context.snippet(a.span))
248 Some(rewrite_doc_comment(
250 shape.comment(context.config),
259 impl Rewrite for ast::NestedMetaItem {
260 fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
262 ast::NestedMetaItem::MetaItem(ref meta_item) => meta_item.rewrite(context, shape),
263 ast::NestedMetaItem::Lit(ref l) => {
264 rewrite_literal(context, l.as_token_lit(), l.span, shape)
270 fn has_newlines_before_after_comment(comment: &str) -> (&str, &str) {
271 // Look at before and after comment and see if there are any empty lines.
272 let comment_begin = comment.find('/');
273 let len = comment_begin.unwrap_or_else(|| comment.len());
274 let mlb = count_newlines(&comment[..len]) > 1;
275 let mla = if comment_begin.is_none() {
281 .take_while(|c| c.is_whitespace())
282 .filter(|&c| c == '\n')
286 (if mlb { "\n" } else { "" }, if mla { "\n" } else { "" })
289 impl Rewrite for ast::MetaItem {
290 fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
291 Some(match self.kind {
292 ast::MetaItemKind::Word => {
293 rewrite_path(context, PathContext::Type, &None, &self.path, shape)?
295 ast::MetaItemKind::List(ref list) => {
296 let path = rewrite_path(context, PathContext::Type, &None, &self.path, shape)?;
297 let has_trailing_comma = crate::expr::span_ends_with_comma(context, self.span);
298 overflow::rewrite_with_parens(
305 context.config.attr_fn_like_width(),
306 Some(if has_trailing_comma {
307 SeparatorTactic::Always
309 SeparatorTactic::Never
313 ast::MetaItemKind::NameValue(ref lit) => {
314 let path = rewrite_path(context, PathContext::Type, &None, &self.path, shape)?;
316 let lit_shape = shape.shrink_left(path.len() + 3)?;
317 // `rewrite_literal` returns `None` when `lit` exceeds max
318 // width. Since a literal is basically unformattable unless it
319 // is a string literal (and only if `format_strings` is set),
320 // we might be better off ignoring the fact that the attribute
321 // is longer than the max width and continue on formatting.
322 // See #2479 for example.
323 let value = rewrite_literal(context, lit.as_token_lit(), lit.span, lit_shape)
324 .unwrap_or_else(|| context.snippet(lit.span).to_owned());
325 format!("{} = {}", path, value)
331 impl Rewrite for ast::Attribute {
332 fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
333 let snippet = context.snippet(self.span);
334 if self.is_doc_comment() {
335 rewrite_doc_comment(snippet, shape.comment(context.config), context.config)
337 let should_skip = self
339 .map(|s| context.skip_context.skip_attribute(s.name.as_str()))
341 let prefix = attr_prefix(self);
343 if should_skip || contains_comment(snippet) {
344 return Some(snippet.to_owned());
347 if let Some(ref meta) = self.meta() {
348 // This attribute is possibly a doc attribute needing normalization to a doc comment
349 if context.config.normalize_doc_attributes() && meta.has_name(sym::doc) {
350 if let Some(ref literal) = meta.value_str() {
351 let comment_style = match self.style {
352 ast::AttrStyle::Inner => CommentStyle::Doc,
353 ast::AttrStyle::Outer => CommentStyle::TripleSlash,
356 let literal_str = literal.as_str();
357 let doc_comment_formatter =
358 DocCommentFormatter::new(literal_str, comment_style);
359 let doc_comment = format!("{}", doc_comment_formatter);
360 return rewrite_doc_comment(
362 shape.comment(context.config),
369 let shape = shape.offset_left(prefix.len() + 1)?;
371 meta.rewrite(context, shape)
372 .map_or_else(|| snippet.to_owned(), |rw| format!("{}[{}]", prefix, rw)),
375 Some(snippet.to_owned())
381 impl Rewrite for [ast::Attribute] {
382 fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
384 return Some(String::new());
387 // The current remaining attributes.
388 let mut attrs = self;
389 let mut result = String::new();
391 // Determine if the source text is annotated with `#[rustfmt::skip::attributes(derive)]`
392 // or `#![rustfmt::skip::attributes(derive)]`
393 let skip_derives = context.skip_context.skip_attribute("derive");
395 // This is not just a simple map because we need to handle doc comments
396 // (where we take as many doc comment attributes as possible) and possibly
397 // merging derives into a single attribute.
399 if attrs.is_empty() {
403 // Handle doc comments.
404 let (doc_comment_len, doc_comment_str) =
405 rewrite_initial_doc_comments(context, attrs, shape)?;
406 if doc_comment_len > 0 {
407 let doc_comment_str = doc_comment_str.expect("doc comments, but no result");
408 result.push_str(&doc_comment_str);
410 let missing_span = attrs
411 .get(doc_comment_len)
412 .map(|next| mk_sp(attrs[doc_comment_len - 1].span.hi(), next.span.lo()));
413 if let Some(missing_span) = missing_span {
414 let snippet = context.snippet(missing_span);
415 let (mla, mlb) = has_newlines_before_after_comment(snippet);
416 let comment = crate::comment::recover_missing_comment_in_span(
418 shape.with_max_width(context.config),
422 let comment = if comment.is_empty() {
425 format!("{}{}\n{}", mla, comment, mlb)
427 result.push_str(&comment);
428 result.push_str(&shape.indent.to_string(context.config));
431 attrs = &attrs[doc_comment_len..];
436 // Handle derives if we will merge them.
437 if !skip_derives && context.config.merge_derives() && is_derive(&attrs[0]) {
438 let derives = take_while_with_pred(context, attrs, is_derive);
439 let derive_str = format_derive(derives, shape, context)?;
440 result.push_str(&derive_str);
442 let missing_span = attrs
444 .map(|next| mk_sp(attrs[derives.len() - 1].span.hi(), next.span.lo()));
445 if let Some(missing_span) = missing_span {
446 let comment = crate::comment::recover_missing_comment_in_span(
448 shape.with_max_width(context.config),
452 result.push_str(&comment);
453 if let Some(next) = attrs.get(derives.len()) {
454 if next.is_doc_comment() {
455 let snippet = context.snippet(missing_span);
456 let (_, mlb) = has_newlines_before_after_comment(snippet);
457 result.push_str(mlb);
461 result.push_str(&shape.indent.to_string(context.config));
464 attrs = &attrs[derives.len()..];
469 // If we get here, then we have a regular attribute, just handle one
472 let formatted_attr = attrs[0].rewrite(context, shape)?;
473 result.push_str(&formatted_attr);
475 let missing_span = attrs
477 .map(|next| mk_sp(attrs[0].span.hi(), next.span.lo()));
478 if let Some(missing_span) = missing_span {
479 let comment = crate::comment::recover_missing_comment_in_span(
481 shape.with_max_width(context.config),
485 result.push_str(&comment);
486 if let Some(next) = attrs.get(1) {
487 if next.is_doc_comment() {
488 let snippet = context.snippet(missing_span);
489 let (_, mlb) = has_newlines_before_after_comment(snippet);
490 result.push_str(mlb);
494 result.push_str(&shape.indent.to_string(context.config));
502 fn attr_prefix(attr: &ast::Attribute) -> &'static str {
504 ast::AttrStyle::Inner => "#!",
505 ast::AttrStyle::Outer => "#",
509 pub(crate) trait MetaVisitor<'ast> {
510 fn visit_meta_item(&mut self, meta_item: &'ast ast::MetaItem) {
511 match meta_item.kind {
512 ast::MetaItemKind::Word => self.visit_meta_word(meta_item),
513 ast::MetaItemKind::List(ref list) => self.visit_meta_list(meta_item, list),
514 ast::MetaItemKind::NameValue(ref lit) => self.visit_meta_name_value(meta_item, lit),
520 _meta_item: &'ast ast::MetaItem,
521 list: &'ast [ast::NestedMetaItem],
524 self.visit_nested_meta_item(nm);
528 fn visit_meta_word(&mut self, _meta_item: &'ast ast::MetaItem) {}
530 fn visit_meta_name_value(
532 _meta_item: &'ast ast::MetaItem,
533 _lit: &'ast ast::MetaItemLit,
537 fn visit_nested_meta_item(&mut self, nm: &'ast ast::NestedMetaItem) {
539 ast::NestedMetaItem::MetaItem(ref meta_item) => self.visit_meta_item(meta_item),
540 ast::NestedMetaItem::Lit(ref lit) => self.visit_meta_item_lit(lit),
544 fn visit_meta_item_lit(&mut self, _lit: &'ast ast::MetaItemLit) {}