Skip to content

Commit 092d6be

Browse files
committed
Implement reformat of tuple litterals
1 parent ecd9557 commit 092d6be

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/expr.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,40 @@ impl<'a> FmtVisitor<'a> {
194194
format!("{}: {}", name, expr)
195195
}
196196

197+
fn rewrite_tuple_lit(&mut self, items: &[ptr::P<ast::Expr>], width: usize, offset: usize)
198+
-> String {
199+
// opening paren
200+
let indent = offset + 1;
201+
// Only last line has width-1 as budget, other may take max_width
202+
let item_strs: Vec<_> =
203+
items.iter()
204+
.enumerate()
205+
.map(|(i, item)| self.rewrite_expr(
206+
item,
207+
// for last line, -2 is for indent + ")", for other lines, -1 is for comma
208+
if i == items.len() - 1 { width - 2 } else { config!(max_width) - indent - 1 },
209+
indent))
210+
.collect();
211+
let tactics = if item_strs.iter().any(|s| s.contains('\n')) {
212+
ListTactic::Vertical
213+
} else {
214+
ListTactic::HorizontalVertical
215+
};
216+
// FIXME handle comments
217+
let item_strs: Vec<_> = item_strs.into_iter().map(|s| (s, String::new())).collect();
218+
let fmt = ListFormatting {
219+
tactic: tactics,
220+
separator: ",",
221+
trailing_separator: SeparatorTactic::Never,
222+
indent: indent,
223+
h_width: width - 2,
224+
v_width: width - 2,
225+
};
226+
let item_str = write_list(&item_strs, &fmt);
227+
format!("({})", item_str)
228+
}
229+
230+
197231
pub fn rewrite_expr(&mut self, expr: &ast::Expr, width: usize, offset: usize) -> String {
198232
match expr.node {
199233
ast::Expr_::ExprLit(ref l) => {
@@ -219,6 +253,9 @@ impl<'a> FmtVisitor<'a> {
219253
width,
220254
offset);
221255
}
256+
ast::Expr_::ExprTup(ref items) => {
257+
return self.rewrite_tuple_lit(items, width, offset);
258+
}
222259
_ => {}
223260
}
224261

tests/idem/tuple.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Test tuple litterals
2+
3+
fn foo() {
4+
let a = (a, a, a, a, a);
5+
let aaaaaaaaaaaaaaaa = (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaa, aaaaaaaaaa);
6+
let aaaaaaaaaaaaaaaaaaaaaa = (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
7+
aaaaaaaaaaaaaaaaaaaaaaaaa,
8+
aaaa);
9+
}

0 commit comments

Comments
 (0)