Skip to content

Implement reformat of tuple litterals #85

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

Merged
merged 3 commits into from
May 31, 2015
Merged
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
42 changes: 42 additions & 0 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,45 @@ impl<'a> FmtVisitor<'a> {
format!("{}: {}", name, expr)
}

fn rewrite_tuple_lit(&mut self, items: &[ptr::P<ast::Expr>], width: usize, offset: usize)
-> String {
// opening paren
let indent = offset + 1;
// In case of length 1, need a trailing comma
if items.len() == 1 {
return format!("({},)", self.rewrite_expr(&*items[0], width - 3, indent));
}
// Only last line has width-1 as budget, other may take max_width
let item_strs: Vec<_> =
items.iter()
.enumerate()
.map(|(i, item)| self.rewrite_expr(
item,
// last line : given width (minus "("+")"), other lines : max_width
// (minus "("+","))
if i == items.len() - 1 { width - 2 } else { config!(max_width) - indent - 2 },
indent))
.collect();
let tactics = if item_strs.iter().any(|s| s.contains('\n')) {
ListTactic::Vertical
} else {
ListTactic::HorizontalVertical
};
// FIXME handle comments
let item_strs: Vec<_> = item_strs.into_iter().map(|s| (s, String::new())).collect();
let fmt = ListFormatting {
tactic: tactics,
separator: ",",
trailing_separator: SeparatorTactic::Never,
indent: indent,
h_width: width - 2,
v_width: width - 2,
};
let item_str = write_list(&item_strs, &fmt);
format!("({})", item_str)
}


pub fn rewrite_expr(&mut self, expr: &ast::Expr, width: usize, offset: usize) -> String {
match expr.node {
ast::Expr_::ExprLit(ref l) => {
Expand All @@ -219,6 +258,9 @@ impl<'a> FmtVisitor<'a> {
width,
offset);
}
ast::Expr_::ExprTup(ref items) => {
return self.rewrite_tuple_lit(items, width, offset);
}
_ => {}
}

Expand Down
11 changes: 11 additions & 0 deletions tests/idem/tuple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Test tuple litterals

fn foo() {
let a = (a, a, a, a, a);
let aaaaaaaaaaaaaaaa = (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaa, aaaaaaaaaaaaaa);
let aaaaaaaaaaaaaaaaaaaaaa = (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaa,
aaaa);
let a = (a,);
}