Merge pull request #309 from marcusklaas/array-literals
Format array literals
This commit is contained in:
commit
4738d284f2
57
src/expr.rs
57
src/expr.rs
@ -31,12 +31,20 @@ use syntax::visit::Visitor;
|
||||
impl Rewrite for ast::Expr {
|
||||
fn rewrite(&self, context: &RewriteContext, width: usize, offset: usize) -> Option<String> {
|
||||
match self.node {
|
||||
ast::Expr_::ExprVec(ref expr_vec) => {
|
||||
rewrite_array(expr_vec.iter().map(|e| &**e), self.span, context, width, offset)
|
||||
}
|
||||
ast::Expr_::ExprLit(ref l) => {
|
||||
match l.node {
|
||||
ast::Lit_::LitStr(_, ast::StrStyle::CookedStr) => {
|
||||
rewrite_string_lit(context, l.span, width, offset)
|
||||
}
|
||||
_ => Some(context.snippet(self.span)),
|
||||
_ => {
|
||||
wrap_str(context.snippet(self.span),
|
||||
context.config.max_width,
|
||||
width,
|
||||
offset)
|
||||
}
|
||||
}
|
||||
}
|
||||
ast::Expr_::ExprCall(ref callee, ref args) => {
|
||||
@ -152,6 +160,53 @@ impl Rewrite for ast::Expr {
|
||||
}
|
||||
}
|
||||
|
||||
fn rewrite_array<'a, I>(expr_iter: I,
|
||||
span: Span,
|
||||
context: &RewriteContext,
|
||||
width: usize,
|
||||
offset: usize)
|
||||
-> Option<String>
|
||||
where I: Iterator<Item = &'a ast::Expr> + ExactSizeIterator
|
||||
{
|
||||
// 2 for brackets;
|
||||
let max_item_width = try_opt!(width.checked_sub(2));
|
||||
let items = itemize_list(context.codemap,
|
||||
expr_iter,
|
||||
"]",
|
||||
|item| item.span.lo,
|
||||
|item| item.span.hi,
|
||||
// 1 = [
|
||||
// FIXME(#133): itemize_list doesn't support
|
||||
// rewrite failure. This may not be its
|
||||
// responsibility, but that of write_list.
|
||||
|item| {
|
||||
item.rewrite(context, max_item_width, offset + 1)
|
||||
.unwrap_or_else(|| context.snippet(item.span))
|
||||
},
|
||||
span_after(span, "[", context.codemap),
|
||||
span.hi)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let tactic = if items.iter().any(|li| li.item.len() > 10 || li.is_multiline()) {
|
||||
ListTactic::HorizontalVertical
|
||||
} else {
|
||||
ListTactic::Mixed
|
||||
};
|
||||
|
||||
let fmt = ListFormatting {
|
||||
tactic: tactic,
|
||||
separator: ",",
|
||||
trailing_separator: SeparatorTactic::Never,
|
||||
indent: offset + 1,
|
||||
h_width: max_item_width,
|
||||
v_width: max_item_width,
|
||||
ends_with_newline: false,
|
||||
};
|
||||
let list_str = try_opt!(write_list(&items, &fmt));
|
||||
|
||||
Some(format!("[{}]", list_str))
|
||||
}
|
||||
|
||||
// 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,
|
||||
|
@ -129,3 +129,47 @@ fn issue184(source: &str) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn arrays() {
|
||||
let x = [0,
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
5,
|
||||
6,
|
||||
7,
|
||||
8,
|
||||
9,
|
||||
0,
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
5,
|
||||
6,
|
||||
7,
|
||||
8,
|
||||
9,
|
||||
0,
|
||||
7,
|
||||
8,
|
||||
9,
|
||||
0,
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
5,
|
||||
6,
|
||||
7,
|
||||
8,
|
||||
9,
|
||||
0];
|
||||
|
||||
let y = [/* comment */ 1, 2 /* post comment */, 3];
|
||||
|
||||
let z = [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, yyyyyyyyyyyyyyyyyyyyyyyyyyy, zzzzzzzzzzzzzzzzzz, q];
|
||||
|
||||
[ 1 + 3, 4 , 5, 6, 7, 7, fncall::<Vec<_>>(3-1)]
|
||||
}
|
||||
|
@ -166,3 +166,20 @@ fn issue184(source: &str) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn arrays() {
|
||||
let x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 7, 8, 9, 0, 1, 2, 3, 4,
|
||||
5, 6, 7, 8, 9, 0];
|
||||
|
||||
let y = [// comment
|
||||
1,
|
||||
2, // post comment
|
||||
3];
|
||||
|
||||
let z = [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
|
||||
yyyyyyyyyyyyyyyyyyyyyyyyyyy,
|
||||
zzzzzzzzzzzzzzzzzz,
|
||||
q];
|
||||
|
||||
[1 + 3, 4, 5, 6, 7, 7, fncall::<Vec<_>>(3 - 1)]
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user