mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-12 20:16:49 +00:00
Format while loops, including labels
This commit is contained in:
parent
979d0c9756
commit
e47e91013e
26
src/expr.rs
26
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<ast::Ident>) -> String {
|
||||
match label {
|
||||
Some(ident) => format!("{}: ", ident.as_str()),
|
||||
None => "".to_owned()
|
||||
}
|
||||
}
|
||||
|
||||
fn rewrite_string_lit(context: &RewriteContext,
|
||||
s: &str,
|
||||
span: Span,
|
||||
|
@ -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 {
|
||||
}
|
||||
}
|
||||
|
@ -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 {
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user