]> git.lizzy.rs Git - rust.git/blobdiff - src/expr.rs
Implement Rewrite for ast::Stmt
[rust.git] / src / expr.rs
index 7616ef0c328ea62fad01f76f20ed2c3893c49a03..5ec12876c4bbcf332084b7f4db0f073608a5febd 100644 (file)
@@ -17,7 +17,8 @@
 use lists::{write_list, itemize_list, ListFormatting, SeparatorTactic, ListTactic,
             DefinitiveListTactic, definitive_tactic, ListItem, format_fn_args};
 use string::{StringFormat, rewrite_string};
-use utils::{span_after, extra_offset, last_line_width, wrap_str, binary_search, first_line_width};
+use utils::{span_after, extra_offset, last_line_width, wrap_str, binary_search, first_line_width,
+            semicolon_for_stmt};
 use visitor::FmtVisitor;
 use config::{StructLitStyle, MultilineStyle};
 use comment::{FindUncommented, rewrite_comment, contains_comment};
@@ -475,6 +476,33 @@ fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Opt
     }
 }
 
+impl Rewrite for ast::Stmt {
+    fn rewrite(&self, context: &RewriteContext, _width: usize, offset: Indent) -> Option<String> {
+        match self.node {
+            ast::Stmt_::StmtDecl(ref decl, _) => {
+                if let ast::Decl_::DeclLocal(ref local) = decl.node {
+                    local.rewrite(context, context.config.max_width, offset)
+                } else {
+                    None
+                }
+            }
+            ast::Stmt_::StmtExpr(ref ex, _) | ast::Stmt_::StmtSemi(ref ex, _) => {
+                let suffix = if semicolon_for_stmt(self) {
+                    ";"
+                } else {
+                    ""
+                };
+
+                ex.rewrite(context,
+                           context.config.max_width - offset.width() - suffix.len(),
+                           offset)
+                  .map(|s| s + suffix)
+            }
+            ast::Stmt_::StmtMac(..) => None,
+        }
+    }
+}
+
 // Abstraction over for, while and loop expressions
 struct Loop<'a> {
     cond: Option<&'a ast::Expr>,