Format while loops, including labels

This commit is contained in:
Marcus Klaas 2015-07-16 16:29:28 +02:00
parent 979d0c9756
commit e47e91013e
3 changed files with 37 additions and 5 deletions

View File

@ -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<ast::Ident>) -> String {
match label {
Some(ident) => format!("{}: ", ident.as_str()),
None => "".to_owned()
}
}
fn rewrite_string_lit(context: &RewriteContext,
s: &str,
span: Span,

View File

@ -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 {
}
}

View File

@ -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 {
}
}