Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format const array as block if remaining width is less than half #751

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 47 additions & 3 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,39 @@ pub fn rewrite_array<'a, I>(expr_iter: I,
Some(format!("[{}]", list_str))
}

pub fn rewrite_array_block<'a, I>(context: &RewriteContext,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment for this function please

expr_iter: I,
span: Span,
offset: Indent)
-> Option<String>
where I: Iterator<Item = &'a ast::Expr>
{
let item_indent = offset.block_indent(context.config);
// 1 = ","
let item_width = try_opt!(context.config.max_width.checked_sub(item_indent.width() + 1));
let items = itemize_list(context.codemap,
expr_iter,
"]",
|item| item.span.lo,
|item| item.span.hi,
|item| item.rewrite(context, item_width, item_indent),
span.lo,
span.hi);
let fmt = ListFormatting {
tactic: DefinitiveListTactic::Vertical,
separator: ",",
trailing_separator: SeparatorTactic::Always,
indent: item_indent,
width: item_width,
ends_with_newline: true,
config: context.config,
};
Some(format!("[\n{}{}\n{}]",
item_indent.to_string(context.config),
try_opt!(write_list(items, &fmt)),
offset.to_string(context.config)))
}

// This functions is pretty messy because of the wrapping and unwrapping of
// expressions into and from blocks. See rust issue #27872.
fn rewrite_closure(capture: ast::CaptureClause,
Expand Down Expand Up @@ -1582,7 +1615,7 @@ fn rewrite_assignment(context: &RewriteContext,
try_opt!(lhs.rewrite(context, max_width, offset)),
operator_str);

rewrite_assign_rhs(&context, lhs_str, rhs, width, offset)
rewrite_assign_rhs(&context, lhs_str, rhs, width, offset, false)
}

// The left hand side must contain everything up to, and including, the
Expand All @@ -1591,7 +1624,8 @@ pub fn rewrite_assign_rhs<S: Into<String>>(context: &RewriteContext,
lhs: S,
ex: &ast::Expr,
width: usize,
offset: Indent)
offset: Indent,
try_block: bool)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use a new enum instead of a bool here please? It makes the caller code easier to read.

-> Option<String> {
let mut result = lhs.into();
let last_line_width = last_line_width(&result) -
Expand All @@ -1602,7 +1636,17 @@ pub fn rewrite_assign_rhs<S: Into<String>>(context: &RewriteContext,
};
// 1 = space between operator and rhs.
let max_width = try_opt!(width.checked_sub(last_line_width + 1));
let rhs = ex.rewrite(&context, max_width, offset + last_line_width + 1);
let mut rhs = ex.rewrite(&context, max_width, offset + last_line_width + 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems this rewrite is wasted, could we only do it if we don't do the block rewrite?


if let ast::ExprVec(ref exprs) = ex.node {
if try_block && max_width < context.config.max_width / 2 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than use max_width / 2, could you add an option for the limit, we do this for the function call and struct max widths, see https://github.com/rust-lang-nursery/rustfmt/blob/master/src/config.rs#L268

let span = mk_sp(span_after(ex.span, "[", context.codemap), ex.span.hi);
rhs = rewrite_array_block(context,
exprs.iter().map(|e| &**e),
span,
context.block_indent);
}
}

match rhs {
Some(new_str) => {
Expand Down
11 changes: 9 additions & 2 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ impl Rewrite for ast::Local {
result,
ex,
budget,
context.block_indent));
context.block_indent,
false));
}

result.push(';');
Expand Down Expand Up @@ -874,7 +875,13 @@ pub fn rewrite_static(prefix: &str,

// 1 = ;
let remaining_width = context.config.max_width - context.block_indent.width() - 1;
rewrite_assign_rhs(context, lhs, expr, remaining_width, context.block_indent).map(|s| s + ";")
let result = rewrite_assign_rhs(context,
lhs,
expr,
remaining_width,
context.block_indent,
true);
result.map(|s| s + ";")
}

impl Rewrite for ast::FunctionRetTy {
Expand Down
10 changes: 10 additions & 0 deletions tests/source/static-block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const TRAIT_METHODS: [(&'static str, usize, SelfKind, OutType, &'static str); 30] = [("add",
2,
ValueSelf,
AnyType,
"std::ops::Add"),
("sub",
2,
ValueSelf,
AnyType,
"std::ops::Sub")];
4 changes: 4 additions & 0 deletions tests/target/static-block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const TRAIT_METHODS: [(&'static str, usize, SelfKind, OutType, &'static str); 30] = [
("add", 2, ValueSelf, AnyType, "std::ops::Add"),
("sub", 2, ValueSelf, AnyType, "std::ops::Sub"),
];