Skip to content

Commit b473c2b

Browse files
committed
Format loops
1 parent 04cf309 commit b473c2b

File tree

6 files changed

+67
-2
lines changed

6 files changed

+67
-2
lines changed

src/changes.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ impl<'a> ChangeSet<'a> {
9999
self.push_str(&file_name, text)
100100
}
101101

102+
// Fetch the output buffer for the given file name.
103+
// Panics on unknown files.
104+
pub fn get(&mut self, file_name: &str) -> &StringBuffer {
105+
self.file_map.get(file_name).unwrap()
106+
}
107+
108+
// Fetch a mutable reference to the output buffer for the given file name.
109+
// Panics on unknown files.
102110
pub fn get_mut(&mut self, file_name: &str) -> &mut StringBuffer {
103111
self.file_map.get_mut(file_name).unwrap()
104112
}

src/expr.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ use rewrite::{Rewrite, RewriteContext};
1212
use lists::{write_list, itemize_list, ListFormatting, SeparatorTactic, ListTactic};
1313
use string::{StringFormat, rewrite_string};
1414
use utils::{span_after, make_indent};
15+
use visitor::FmtVisitor;
1516

1617
use syntax::{ast, ptr};
17-
use syntax::codemap::{Pos, Span, BytePos};
18+
use syntax::codemap::{Pos, Span, BytePos, mk_sp};
1819
use syntax::parse::token;
1920
use syntax::print::pprust;
21+
use syntax::visit::Visitor;
2022

2123
impl Rewrite for ast::Expr {
2224
fn rewrite(&self, context: &RewriteContext, width: usize, offset: usize) -> Option<String> {
@@ -53,11 +55,38 @@ impl Rewrite for ast::Expr {
5355
ast::Expr_::ExprTup(ref items) => {
5456
rewrite_tuple_lit(context, items, self.span, width, offset)
5557
}
58+
ast::Expr_::ExprLoop(ref block, _) => {
59+
// FIXME: this drops any comment between "loop" and the block.
60+
// TODO: format label
61+
block.rewrite(context, width, offset).map(|result| {
62+
format!("loop {}", result)
63+
})
64+
}
5665
_ => context.codemap.span_to_snippet(self.span).ok()
5766
}
5867
}
5968
}
6069

70+
impl Rewrite for ast::Block {
71+
fn rewrite(&self, context: &RewriteContext, _: usize, _: usize) -> Option<String> {
72+
let mut visitor = FmtVisitor::from_codemap(context.codemap, context.config);
73+
visitor.last_pos = self.span.lo;
74+
visitor.block_indent = context.block_indent;
75+
76+
visitor.visit_block(self);
77+
78+
// Push text between last block item and end of block
79+
let snippet = visitor.snippet(mk_sp(visitor.last_pos, self.span.hi));
80+
visitor.changes.push_str_span(self.span, &snippet);
81+
82+
// Stringify visitor
83+
let file_name = context.codemap.span_to_filename(self.span);
84+
let string_buffer = visitor.changes.get(&file_name);
85+
86+
Some(string_buffer.to_string())
87+
}
88+
}
89+
6190
fn rewrite_string_lit(context: &RewriteContext,
6291
s: &str,
6392
span: Span,

src/rewrite.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ pub trait Rewrite {
2828
pub struct RewriteContext<'a> {
2929
pub codemap: &'a CodeMap,
3030
pub config: &'a Config,
31+
pub block_indent: usize,
3132
}

src/visitor.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
3838
self.codemap.lookup_char_pos(ex.span.hi));
3939
self.format_missing(ex.span.lo);
4040
let offset = self.changes.cur_offset_span(ex.span);
41-
let context = RewriteContext { codemap: self.codemap, config: self.config };
41+
let context = RewriteContext { codemap: self.codemap,
42+
config: self.config,
43+
block_indent: self.block_indent, };
4244
let rewrite = ex.rewrite(&context, self.config.max_width - offset, offset);
4345

4446
if let Some(new_str) = rewrite {

tests/source/loop.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
fn main() {
3+
loop
4+
{ return some_val;}
5+
6+
let x = loop { do_forever(); };
7+
8+
loop {
9+
// Just comments
10+
}
11+
}

tests/target/loop.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
fn main() {
3+
loop {
4+
return some_val;
5+
}
6+
7+
let x = loop {
8+
do_forever();
9+
};
10+
11+
loop {
12+
// Just comments
13+
}
14+
}

0 commit comments

Comments
 (0)