]> git.lizzy.rs Git - rust.git/blobdiff - src/expr.rs
Merge pull request #128 from marcusklaas/subexpr
[rust.git] / src / expr.rs
index da706c4afa748f3c1e70cfc377ffa2c5c4c76c8d..33a82da0f29ff4c9e8cfcfc7c535cd41cb8bd16c 100644 (file)
 use lists::{write_list, itemize_list, ListFormatting, SeparatorTactic, ListTactic};
 use string::{StringFormat, rewrite_string};
 use utils::{span_after, make_indent};
+use visitor::FmtVisitor;
 
 use syntax::{ast, ptr};
-use syntax::codemap::{Pos, Span, BytePos};
+use syntax::codemap::{Pos, Span, BytePos, mk_sp};
 use syntax::parse::token;
 use syntax::print::pprust;
+use syntax::visit::Visitor;
 
 impl Rewrite for ast::Expr {
     fn rewrite(&self, context: &RewriteContext, width: usize, offset: usize) -> Option<String> {
@@ -53,11 +55,38 @@ fn rewrite(&self, context: &RewriteContext, width: usize, offset: usize) -> Opti
             ast::Expr_::ExprTup(ref items) => {
                 rewrite_tuple_lit(context, items, self.span, width, offset)
             }
+            ast::Expr_::ExprLoop(ref block, _) => {
+                // FIXME: this drops any comment between "loop" and the block.
+                // TODO: format label
+                block.rewrite(context, width, offset).map(|result| {
+                    format!("loop {}", result)
+                })
+            }
             _ => context.codemap.span_to_snippet(self.span).ok()
         }
     }
 }
 
+impl Rewrite for ast::Block {
+    fn rewrite(&self, context: &RewriteContext, _: usize, _: usize) -> Option<String> {
+        let mut visitor = FmtVisitor::from_codemap(context.codemap, context.config);
+        visitor.last_pos = self.span.lo;
+        visitor.block_indent = context.block_indent;
+
+        visitor.visit_block(self);
+
+        // Push text between last block item and end of block
+        let snippet = visitor.snippet(mk_sp(visitor.last_pos, self.span.hi));
+        visitor.changes.push_str_span(self.span, &snippet);
+
+        // Stringify visitor
+        let file_name = context.codemap.span_to_filename(self.span);
+        let string_buffer = visitor.changes.get(&file_name);
+
+        Some(string_buffer.to_string())
+    }
+}
+
 fn rewrite_string_lit(context: &RewriteContext,
                       s: &str,
                       span: Span,
@@ -239,7 +268,13 @@ fn rewrite_tuple_lit(context: &RewriteContext,
                      width: usize,
                      offset: usize)
                      -> Option<String> {
+    debug!("rewrite_tuple_lit: width: {}, offset: {}", width, offset);
     let indent = offset + 1;
+    // In case of length 1, need a trailing comma
+    if items.len() == 1 {
+        // 3 = "(" + ",)"
+        return items[0].rewrite(context, width - 3, indent).map(|s| format!("({},)", s));
+    }
 
     let items = itemize_list(context.codemap,
                              Vec::new(),
@@ -249,23 +284,16 @@ fn rewrite_tuple_lit(context: &RewriteContext,
                              |item| item.span.lo,
                              |item| item.span.hi,
                              |item| item.rewrite(context,
-                                                 context.config.max_width - indent - 2,
+                                                 context.config.max_width - indent - 1,
                                                  indent)
                                         .unwrap_or(context.codemap.span_to_snippet(item.span)
                                                                   .unwrap()),
                              span.lo + BytePos(1), // Remove parens
                              span.hi - BytePos(1));
 
-    // In case of length 1, need a trailing comma
-    let trailing_separator_tactic = if items.len() == 1 {
-        SeparatorTactic::Always
-    } else {
-        SeparatorTactic::Never
-    };
-
     let fmt = ListFormatting { tactic: ListTactic::HorizontalVertical,
                                separator: ",",
-                               trailing_separator: trailing_separator_tactic,
+                               trailing_separator: SeparatorTactic::Never,
                                indent: indent,
                                h_width: width - 2,
                                v_width: width - 2,