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);
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()
}
}
}
}
+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,
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,
|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())
}
// 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();
// 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));
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)
}
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 {
+ 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 {
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,
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;
#[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) {
#[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;
- 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
+ }
}
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(),
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
+ }
}
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.