about summary refs log tree commit diff
path: root/src/expr.rs
diff options
context:
space:
mode:
authorKevin Yeh <kevinyeah@utexas.edu>2015-11-19 02:08:17 -0600
committerKevin Yeh <kevinyeah@utexas.edu>2015-11-19 14:23:56 -0600
commitfbd1398c925fb51d72c3764133e1d35679cb53c2 (patch)
treeb710f03908c3c3e9af2b01071638cd9a4a9e33de /src/expr.rs
parent22837b07483c91f8fbda2bd1df3e2bb83af5c48d (diff)
Implement Rewrite for ast::Stmt
Diffstat (limited to 'src/expr.rs')
-rw-r--r--src/expr.rs30
1 files changed, 29 insertions, 1 deletions
diff --git a/src/expr.rs b/src/expr.rs
index 7616ef0c328..5ec12876c4b 100644
--- a/src/expr.rs
+++ b/src/expr.rs
@@ -17,7 +17,8 @@ use rewrite::{Rewrite, RewriteContext};
 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 @@ impl Rewrite for ast::Block {
     }
 }
 
+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>,