-
Notifications
You must be signed in to change notification settings - Fork 901
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
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, | ||
|
@@ -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 | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) - | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) => { | ||
|
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")]; |
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"), | ||
]; |
There was a problem hiding this comment.
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