diff --git a/src/expr.rs b/src/expr.rs index 8dbe5c6d1dd..e0a76308b44 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -56,11 +56,24 @@ impl Rewrite for ast::Expr { ast::Expr_::ExprTup(ref items) => { rewrite_tuple_lit(context, items, self.span, width, offset) } - ast::Expr_::ExprLoop(ref block, _) => { + ast::Expr_::ExprWhile(ref subexpr, ref block, label) => { + let label_string = rewrite_label(label); + // 6 = "while " + // 2 = " {" + let expr_width = width - 6 - 2 - label_string.len(); + let expr_offset = offset + 6 + label_string.len(); + + subexpr.rewrite(context, expr_width, expr_offset).and_then(|expr_string| { + // FIXME: this drops any comment between "loop" and the block. + block.rewrite(context, width, offset).map(|result| { + format!("{}while {} {}", rewrite_label(label), expr_string, result) + }) + }) + } + ast::Expr_::ExprLoop(ref block, label) => { // FIXME: this drops any comment between "loop" and the block. - // TODO: format label block.rewrite(context, width, offset).map(|result| { - format!("loop {}", result) + format!("{}loop {}", rewrite_label(label), result) }) } _ => context.codemap.span_to_snippet(self.span).ok() @@ -88,6 +101,13 @@ impl Rewrite for ast::Block { } } +fn rewrite_label(label: Option) -> String { + match label { + Some(ident) => format!("{}: ", ident.as_str()), + None => "".to_owned() + } +} + fn rewrite_string_lit(context: &RewriteContext, s: &str, span: Span, diff --git a/tests/source/loop.rs b/tests/source/loop.rs index ad5ef93521d..143a3a23760 100644 --- a/tests/source/loop.rs +++ b/tests/source/loop.rs @@ -5,7 +5,12 @@ fn main() { let x = loop { do_forever(); }; - loop { + 'label : loop { // Just comments } + + 'a: while loooooooooooooooooooooooooooooooooong_variable_name + another_value > some_other_value{} + + while aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa > bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb { + } } diff --git a/tests/target/loop.rs b/tests/target/loop.rs index 3eb506eb6ed..cec8b2bff61 100644 --- a/tests/target/loop.rs +++ b/tests/target/loop.rs @@ -8,7 +8,14 @@ fn main() { do_forever(); }; - loop { + 'label: loop { // Just comments } + + 'a: while loooooooooooooooooooooooooooooooooong_variable_name + another_value > + some_other_value { + } + + while aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa > bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb { + } }