diff options
| author | Gaëtan Cassiers <gaetan.cassiers@gmail.com> | 2015-05-25 22:14:05 +0200 |
|---|---|---|
| committer | Gaëtan Cassiers <gaetan.cassiers@gmail.com> | 2015-05-28 21:30:28 +0200 |
| commit | 092d6be3683192bdbcd780d950817c622f44b62e (patch) | |
| tree | de467b66eaf4d18ac6961e368d5d27ecd835644c /src/expr.rs | |
| parent | ecd95574952bc0900c99b30395ec7950a0f8f864 (diff) | |
Implement reformat of tuple litterals
Diffstat (limited to 'src/expr.rs')
| -rw-r--r-- | src/expr.rs | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/expr.rs b/src/expr.rs index 4b32301fe91..e731abde4e7 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -194,6 +194,40 @@ 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; + // 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, + // for last line, -2 is for indent + ")", for other lines, -1 is for comma + if i == items.len() - 1 { width - 2 } else { config!(max_width) - indent - 1 }, + 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) => { @@ -219,6 +253,9 @@ impl<'a> FmtVisitor<'a> { width, offset); } + ast::Expr_::ExprTup(ref items) => { + return self.rewrite_tuple_lit(items, width, offset); + } _ => {} } |
