Format while loops, including labels
This commit is contained in:
parent
979d0c9756
commit
e47e91013e
3 changed files with 37 additions and 5 deletions
26
src/expr.rs
26
src/expr.rs
|
@ -56,11 +56,24 @@ impl Rewrite for ast::Expr {
|
||||||
ast::Expr_::ExprTup(ref items) => {
|
ast::Expr_::ExprTup(ref items) => {
|
||||||
rewrite_tuple_lit(context, items, self.span, width, offset)
|
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.
|
// FIXME: this drops any comment between "loop" and the block.
|
||||||
// TODO: format label
|
|
||||||
block.rewrite(context, width, offset).map(|result| {
|
block.rewrite(context, width, offset).map(|result| {
|
||||||
format!("loop {}", result)
|
format!("{}loop {}", rewrite_label(label), result)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
_ => context.codemap.span_to_snippet(self.span).ok()
|
_ => 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,
|
fn rewrite_string_lit(context: &RewriteContext,
|
||||||
s: &str,
|
s: &str,
|
||||||
span: Span,
|
span: Span,
|
||||||
|
|
|
@ -5,7 +5,12 @@ fn main() {
|
||||||
|
|
||||||
let x = loop { do_forever(); };
|
let x = loop { do_forever(); };
|
||||||
|
|
||||||
loop {
|
'label : loop {
|
||||||
// Just comments
|
// Just comments
|
||||||
}
|
}
|
||||||
|
|
||||||
|
'a: while loooooooooooooooooooooooooooooooooong_variable_name + another_value > some_other_value{}
|
||||||
|
|
||||||
|
while aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa > bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,14 @@ fn main() {
|
||||||
do_forever();
|
do_forever();
|
||||||
};
|
};
|
||||||
|
|
||||||
loop {
|
'label: loop {
|
||||||
// Just comments
|
// Just comments
|
||||||
}
|
}
|
||||||
|
|
||||||
|
'a: while loooooooooooooooooooooooooooooooooong_variable_name + another_value >
|
||||||
|
some_other_value {
|
||||||
|
}
|
||||||
|
|
||||||
|
while aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa > bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue