Skip to content

Commit

Permalink
Format attributes with paths
Browse files Browse the repository at this point in the history
  • Loading branch information
nrc committed May 14, 2018
1 parent 634ca02 commit 7eb8bdb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use expr::rewrite_literal;
use lists::{itemize_list, write_list, ListFormatting};
use rewrite::{Rewrite, RewriteContext};
use shape::Shape;
use types::{rewrite_path, PathContext};
use utils::{count_newlines, mk_sp};

/// Returns attributes on the given statement.
Expand Down Expand Up @@ -200,17 +201,19 @@ fn allow_mixed_tactic_for_nested_metaitem_list(list: &[ast::NestedMetaItem]) ->
impl Rewrite for ast::MetaItem {
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
Some(match self.node {
ast::MetaItemKind::Word => String::from(&*self.name().as_str()),
ast::MetaItemKind::Word => {
rewrite_path(context, PathContext::Type, None, &self.ident, shape)?
}
ast::MetaItemKind::List(ref list) => {
let name = self.name().as_str();
let path = rewrite_path(context, PathContext::Type, None, &self.ident, shape)?;
let item_shape = match context.config.indent_style() {
IndentStyle::Block => shape
.block_indent(context.config.tab_spaces())
.with_max_width(context.config),
// 1 = `(`, 2 = `]` and `)`
IndentStyle::Visual => shape
.visual_indent(0)
.shrink_left(name.len() + 1)
.shrink_left(path.len() + 1)
.and_then(|s| s.sub_width(2))?,
};
let items = itemize_list(
Expand Down Expand Up @@ -248,21 +251,21 @@ impl Rewrite for ast::MetaItem {
};
let item_str = write_list(&item_vec, &fmt)?;
// 3 = "()" and "]"
let one_line_budget = shape.offset_left(name.len())?.sub_width(3)?.width;
let one_line_budget = shape.offset_left(path.len())?.sub_width(3)?.width;
if context.config.indent_style() == IndentStyle::Visual
|| (!item_str.contains('\n') && item_str.len() <= one_line_budget)
{
format!("{}({})", name, item_str)
format!("{}({})", path, item_str)
} else {
let indent = shape.indent.to_string_with_newline(context.config);
let nested_indent = item_shape.indent.to_string_with_newline(context.config);
format!("{}({}{}{})", name, nested_indent, item_str, indent)
format!("{}({}{}{})", path, nested_indent, item_str, indent)
}
}
ast::MetaItemKind::NameValue(ref literal) => {
let name = self.name().as_str();
let path = rewrite_path(context, PathContext::Type, None, &self.ident, shape)?;
// 3 = ` = `
let lit_shape = shape.shrink_left(name.len() + 3)?;
let lit_shape = shape.shrink_left(path.len() + 3)?;
// `rewrite_literal` returns `None` when `literal` exceeds max
// width. Since a literal is basically unformattable unless it
// is a string literal (and only if `format_strings` is set),
Expand All @@ -271,7 +274,7 @@ impl Rewrite for ast::MetaItem {
// See #2479 for example.
let value = rewrite_literal(context, literal, lit_shape)
.unwrap_or_else(|| context.snippet(literal.span).to_owned());
format!("{} = {}", name, value)
format!("{} = {}", path, value)
}
})
}
Expand Down
7 changes: 7 additions & 0 deletions tests/source/attrib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,10 @@ struct A { #[doc = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// #2647
#[cfg(feature = "this_line_is_101_characters_long_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")]
pub fn foo() {}

// path attrs
#[clippy::bar]
#[clippy::bar=foo]
#[clippy::bar(a, b, c)]
pub fn foo() {}

6 changes: 6 additions & 0 deletions tests/target/attrib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,9 @@ struct A {
feature = "this_line_is_101_characters_long_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
)]
pub fn foo() {}

// path attrs
#[clippy::bar]
#[clippy::bar=foo]
#[clippy::bar(a, b, c)]
pub fn foo() {}

0 comments on commit 7eb8bdb

Please sign in to comment.