Format if-else expressions

This commit is contained in:
Marcus Klaas 2015-07-19 23:42:54 +02:00
parent e47e91013e
commit b161815fe0
10 changed files with 117 additions and 16 deletions

View File

@ -17,9 +17,11 @@ pub fn rewrite_comment(orig: &str, block_style: bool, width: usize, offset: usiz
let s = orig.trim();
// Edge case: block comments. Let's not trim their lines (for now).
let opener = if block_style { "/* " } else { "// " };
let closer = if block_style { " */" } else { "" };
let line_start = if block_style { " * " } else { "// " };
let (opener, closer, line_start) = if block_style {
("/* ", " */", " * ")
} else {
("// ", "", "// ")
};
let max_chars = width.checked_sub(closer.len()).unwrap_or(1)
.checked_sub(opener.len()).unwrap_or(1);

View File

@ -76,6 +76,17 @@ impl Rewrite for ast::Expr {
format!("{}loop {}", rewrite_label(label), result)
})
}
ast::Expr_::ExprBlock(ref block) => {
block.rewrite(context, width, offset)
}
ast::Expr_::ExprIf(ref cond, ref if_block, ref else_block) => {
rewrite_if_else(context,
cond,
if_block,
else_block.as_ref().map(|e| &**e),
width,
offset)
}
_ => context.codemap.span_to_snippet(self.span).ok()
}
}
@ -108,6 +119,29 @@ fn rewrite_label(label: Option<ast::Ident>) -> String {
}
}
fn rewrite_if_else(context: &RewriteContext,
cond: &ast::Expr,
if_block: &ast::Block,
else_block: Option<&ast::Expr>,
width: usize,
offset: usize)
-> Option<String> {
// FIXME: missing comments between control statements and blocks
let cond_string = try_opt!(cond.rewrite(context, width - 3 - 2, offset + 3));
let if_block_string = try_opt!(if_block.rewrite(context, width, offset));
match else_block {
Some(else_block) => {
else_block.rewrite(context, width, offset).map(|else_block_string| {
format!("if {} {} else {}", cond_string, if_block_string, else_block_string)
})
}
None => {
Some(format!("if {} {}", cond_string, if_block_string))
}
}
}
fn rewrite_string_lit(context: &RewriteContext,
s: &str,
span: Span,
@ -229,6 +263,8 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext,
let field_iter = fields.into_iter().map(StructLitField::Regular)
.chain(base.into_iter().map(StructLitField::Base));
let inner_context = &RewriteContext { block_indent: indent, ..*context };
let items = itemize_list(context.codemap,
Vec::new(),
field_iter,
@ -250,13 +286,13 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext,
|item| {
match *item {
StructLitField::Regular(ref field) => {
rewrite_field(context, &field, h_budget, indent)
rewrite_field(inner_context, &field, h_budget, indent)
.unwrap_or(context.codemap.span_to_snippet(field.span)
.unwrap())
},
StructLitField::Base(ref expr) => {
// 2 = ..
expr.rewrite(context, h_budget - 2, indent + 2)
expr.rewrite(inner_context, h_budget - 2, indent + 2)
.map(|s| format!("..{}", s))
.unwrap_or(context.codemap.span_to_snippet(expr.span)
.unwrap())

View File

@ -60,7 +60,11 @@ impl<'a> FmtVisitor<'a> {
}
// 2 = ::
let path_separation_w = if path_str.len() > 0 { 2 } else { 0 };
let path_separation_w = if path_str.len() > 0 {
2
} else {
0
};
// 5 = "use " + {
let indent = path_str.len() + 5 + path_separation_w + vis.len();
@ -106,7 +110,11 @@ impl<'a> FmtVisitor<'a> {
// FIXME: Make more efficient by using a linked list? That would
// require changes to the signatures of itemize_list and write_list.
let has_self = move_self_to_front(&mut items);
let first_index = if has_self { 0 } else { 1 };
let first_index = if has_self {
0
} else {
1
};
if self.config.reorder_imports {
items[1..].sort_by(|a, b| a.item.cmp(&b.item));

View File

@ -70,7 +70,11 @@ impl fmt::Display for Issue {
IssueType::Todo => "TODO",
IssueType::Fixme => "FIXME",
};
let details = if self.missing_number { " without issue number" } else { "" };
let details = if self.missing_number {
" without issue number"
} else {
""
};
write!(fmt, "{}{}", msg, details)
}
@ -177,7 +181,7 @@ impl BadIssueSeeker {
issue: Issue,
mut part: NumberPart)
-> IssueClassification {
if ! issue.missing_number || c == '\n' {
if !issue.missing_number || c == '\n' {
return IssueClassification::Bad(issue);
} else if c == ')' {
return if let NumberPart::CloseParen = part {

View File

@ -446,7 +446,11 @@ impl<'a> FmtVisitor<'a> {
+ field.node.name.to_string().len()
+ 1; // Open paren
let comma_cost = if self.config.enum_trailing_comma { 1 } else { 0 };
let comma_cost = if self.config.enum_trailing_comma {
1
} else {
0
};
let budget = self.config.ideal_width - indent - comma_cost - 1; // 1 = )
let fmt = ListFormatting {
@ -520,7 +524,11 @@ impl<'a> FmtVisitor<'a> {
ast::StructFieldKind::UnnamedField(..) => true
};
let (opener, terminator) = if is_tuple { ("(", ")") } else { (" {", "}") };
let (opener, terminator) = if is_tuple {
("(", ")")
} else {
(" {", "}")
};
let generics_str = match generics {
Some(g) => self.format_generics(g,
@ -565,7 +573,11 @@ impl<'a> FmtVisitor<'a> {
result.push_str(&indentation);
}
let tactic = if break_line { ListTactic::Vertical } else { ListTactic::Horizontal };
let tactic = if break_line {
ListTactic::Vertical
} else {
ListTactic::Horizontal
};
// 1 = ,
let budget = self.config.ideal_width - offset + self.config.tab_spaces - 1;

View File

@ -24,7 +24,9 @@ pub fn span_after(original: Span, needle: &str, codemap: &CodeMap) -> BytePos {
#[inline]
pub fn prev_char(s: &str, mut i: usize) -> usize {
if i == 0 { return 0; }
if i == 0 {
return 0;
}
i -= 1;
while !s.is_char_boundary(i) {
@ -35,7 +37,9 @@ pub fn prev_char(s: &str, mut i: usize) -> usize {
#[inline]
pub fn next_char(s: &str, mut i: usize) -> usize {
if i >= s.len() { return s.len(); }
if i >= s.len() {
return s.len();
}
while !s.is_char_boundary(i) {
i += 1;

View File

@ -12,5 +12,16 @@ some_ridiculously_loooooooooooooooooooooong_function(10000 * 30000000000 + 40000
- 50000 * sqrt(-1),
trivial_value);
(((((((((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + a +
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaa)))))))))
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaa)))))))));
if 1 + 2 > 0 { let result = 5; result } else { 4};
if cond() {
something();
} else if different_cond() {
something_else();
} else {
// Check subformatting
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
}
}

View File

@ -22,6 +22,8 @@ fn main() {
Foo { a:Bar,
b:foo() };
Quux { x: if cond { bar(); }, y: baz() };
A {
// Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor.
first: item(),

View File

@ -13,5 +13,22 @@ fn foo() -> bool {
trivial_value);
(((((((((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
a + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
aaaaa)))))))))
aaaaa)))))))));
if 1 + 2 > 0 {
let result = 5;
result
} else {
4
};
if cond() {
something();
} else if different_cond() {
something_else();
} else {
// Check subformatting
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
}
}

View File

@ -35,6 +35,11 @@ fn main() {
Foo { a: Bar, b: foo() };
Quux { x: if cond {
bar();
},
y: baz(), };
A { // Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit
// amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante
// hendrerit. Donec et mollis dolor.