Skip to content

Commit 8c8a32d

Browse files
committed
Move logic of tuple formatting into lists.rs
This allows re-use (at least for tuple type), maybe for arg lists
1 parent 9da7be9 commit 8c8a32d

File tree

2 files changed

+67
-46
lines changed

2 files changed

+67
-46
lines changed

src/expr.rs

Lines changed: 15 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use visitor::FmtVisitor;
1212
use utils::*;
13-
use lists::{write_list, ListFormatting, SeparatorTactic, ListTactic};
13+
use lists::{write_list, ListFormatting, SeparatorTactic, ListTactic, rewrite_tuple};
1414

1515
use syntax::{ast, ptr};
1616
use syntax::codemap::{Pos, Span};
@@ -21,7 +21,7 @@ use MIN_STRING;
2121

2222
impl<'a> FmtVisitor<'a> {
2323
// TODO NEEDS TESTS
24-
fn rewrite_string_lit(&mut self, s: &str, span: Span, width: usize, offset: usize) -> String {
24+
fn rewrite_string_lit(&self, s: &str, span: Span, width: usize, offset: usize) -> String {
2525
// FIXME I bet this stomps unicode escapes in the source string
2626

2727
// Check if there is anything to fix: we always try to fixup multi-line
@@ -84,7 +84,7 @@ impl<'a> FmtVisitor<'a> {
8484
result
8585
}
8686

87-
fn rewrite_call(&mut self,
87+
fn rewrite_call(&self,
8888
callee: &ast::Expr,
8989
args: &[ptr::P<ast::Expr>],
9090
width: usize,
@@ -121,7 +121,7 @@ impl<'a> FmtVisitor<'a> {
121121
format!("{}({})", callee_str, args_str)
122122
}
123123

124-
fn rewrite_paren(&mut self, subexpr: &ast::Expr, width: usize, offset: usize) -> String {
124+
fn rewrite_paren(&self, subexpr: &ast::Expr, width: usize, offset: usize) -> String {
125125
debug!("rewrite_paren, width: {}, offset: {}", width, offset);
126126
// 1 is for opening paren, 2 is for opening+closing, we want to keep the closing
127127
// paren on the same line as the subexpr
@@ -130,7 +130,7 @@ impl<'a> FmtVisitor<'a> {
130130
format!("({})", subexpr_str)
131131
}
132132

133-
fn rewrite_struct_lit(&mut self,
133+
fn rewrite_struct_lit(&self,
134134
path: &ast::Path,
135135
fields: &[ast::Field],
136136
base: Option<&ast::Expr>,
@@ -176,53 +176,20 @@ impl<'a> FmtVisitor<'a> {
176176
// }
177177
}
178178

179-
fn rewrite_field(&mut self, field: &ast::Field, width: usize, offset: usize) -> String {
179+
fn rewrite_field(&self, field: &ast::Field, width: usize, offset: usize) -> String {
180180
let name = &token::get_ident(field.ident.node);
181181
let overhead = name.len() + 2;
182182
let expr = self.rewrite_expr(&field.expr, width - overhead, offset + overhead);
183183
format!("{}: {}", name, expr)
184184
}
185185

186-
fn rewrite_tuple_lit(&mut self, items: &[ptr::P<ast::Expr>], width: usize, offset: usize)
187-
-> String {
188-
// opening paren
189-
let indent = offset + 1;
190-
// In case of length 1, need a trailing comma
191-
if items.len() == 1 {
192-
return format!("({},)", self.rewrite_expr(&*items[0], width - 3, indent));
193-
}
194-
// Only last line has width-1 as budget, other may take max_width
195-
let item_strs: Vec<_> =
196-
items.iter()
197-
.enumerate()
198-
.map(|(i, item)| self.rewrite_expr(
199-
item,
200-
// last line : given width (minus "("+")"), other lines : max_width
201-
// (minus "("+","))
202-
if i == items.len() - 1 { width - 2 } else { config!(max_width) - indent - 2 },
203-
indent))
204-
.collect();
205-
let tactics = if item_strs.iter().any(|s| s.contains('\n')) {
206-
ListTactic::Vertical
207-
} else {
208-
ListTactic::HorizontalVertical
209-
};
210-
// FIXME handle comments
211-
let item_strs: Vec<_> = item_strs.into_iter().map(|s| (s, String::new())).collect();
212-
let fmt = ListFormatting {
213-
tactic: tactics,
214-
separator: ",",
215-
trailing_separator: SeparatorTactic::Never,
216-
indent: indent,
217-
h_width: width - 2,
218-
v_width: width - 2,
219-
};
220-
let item_str = write_list(&item_strs, &fmt);
221-
format!("({})", item_str)
186+
fn rewrite_tuple_lit(&self, items: &[ptr::P<ast::Expr>], width: usize, offset: usize)
187+
-> Option<String>
188+
{
189+
rewrite_tuple(items, width, offset, &|exp, w, o| self.rewrite_expr(exp, w, o))
222190
}
223191

224-
225-
pub fn rewrite_expr(&mut self, expr: &ast::Expr, width: usize, offset: usize) -> String {
192+
pub fn rewrite_expr(&self, expr: &ast::Expr, width: usize, offset: usize) -> String {
226193
match expr.node {
227194
ast::Expr_::ExprLit(ref l) => {
228195
match l.node {
@@ -248,7 +215,10 @@ impl<'a> FmtVisitor<'a> {
248215
offset);
249216
}
250217
ast::Expr_::ExprTup(ref items) => {
251-
return self.rewrite_tuple_lit(items, width, offset);
218+
match self.rewrite_tuple_lit(items, width, offset) {
219+
Some(s) => { return s; }
220+
None => {}
221+
}
252222
}
253223
_ => {}
254224
}

src/lists.rs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use utils::make_indent;
1212
use rustc_serialize::{Decodable, Decoder};
13+
use std::borrow::Borrow;
1314

1415
#[derive(Eq, PartialEq, Debug, Copy, Clone)]
1516
pub enum ListTactic {
@@ -163,7 +164,7 @@ fn needs_trailing_separator(separator_tactic: SeparatorTactic, list_tactic: List
163164
}
164165
}
165166

166-
fn calculate_width(items:&[(String, String)]) -> usize {
167+
fn calculate_width(items: &[(String, String)]) -> usize {
167168
let missed_width = items.iter().map(|&(_, ref s)| {
168169
let text_len = s.trim().len();
169170
if text_len > 0 {
@@ -176,3 +177,53 @@ fn calculate_width(items:&[(String, String)]) -> usize {
176177
let item_width = items.iter().map(|&(ref s, _)| s.len()).fold(0, |a, l| a + l);
177178
missed_width + item_width
178179
}
180+
181+
182+
/// Format a tuple within given offset and width
183+
pub fn rewrite_tuple<Item, ItemPtr>(items: &[ItemPtr],
184+
width: usize,
185+
offset: usize,
186+
rewriter: &Fn(&Item, usize, usize) -> String)
187+
-> Option<String>
188+
where ItemPtr: Borrow<Item>
189+
{
190+
debug!("rewrite_tuple: width: {}, offset: {}", width, offset);
191+
// opening paren
192+
let indent = offset + 1;
193+
// In case of length 1, need a trailing comma
194+
if items.len() == 1 {
195+
// 3 = "(" + ",)"
196+
if width <= 3 {
197+
return None;
198+
} else {
199+
return Some(format!("({},)", rewriter(items[0].borrow(), width - 3, indent)));
200+
}
201+
}
202+
// 2 = "(" + ","
203+
if width <= 2 {
204+
return None;
205+
}
206+
// Only las line has width - 1 budget, other may take max_width
207+
// FIXME handle comments
208+
let item_strs: Vec<_> =
209+
items.iter()
210+
.enumerate()
211+
.map(|(i, item)| (rewriter(item.borrow(),
212+
// last line : width - "(" - ")"
213+
// other lines : max_widht - "(" - ","
214+
if i == items.len() - 1 {
215+
width - 2
216+
} else {
217+
config!(max_width) - indent - 2
218+
},
219+
indent),
220+
String::new()))
221+
.collect();
222+
let fmt = ListFormatting { tactic: ListTactic::HorizontalVertical,
223+
separator: ",",
224+
trailing_separator: SeparatorTactic::Never,
225+
indent: indent,
226+
h_width: width - 2,
227+
v_width: width - 2, };
228+
Some(format!("({})", write_list(&item_strs, &fmt)))
229+
}

0 commit comments

Comments
 (0)