about summary refs log tree commit diff
path: root/src/items.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/items.rs')
-rw-r--r--src/items.rs110
1 files changed, 42 insertions, 68 deletions
diff --git a/src/items.rs b/src/items.rs
index c8054b8d01f..e6b401d2ff3 100644
--- a/src/items.rs
+++ b/src/items.rs
@@ -26,85 +26,59 @@ use syntax::codemap::{Span, BytePos, mk_sp};
 use syntax::print::pprust;
 use syntax::parse::token;
 
-impl<'a> FmtVisitor<'a> {
-    pub fn visit_let(&mut self, local: &ast::Local, span: Span) {
-        self.format_missing_with_indent(span.lo);
-
-        // New scope so we drop the borrow of self (context) in time to mutably
-        // borrow self to mutate its buffer.
-        let result = {
-            let context = self.get_context();
-            let mut result = "let ".to_owned();
-            let pattern_offset = self.block_indent + result.len();
-            // 1 = ;
-            let pattern_width = match self.config
-                                          .max_width
-                                          .checked_sub(pattern_offset.width() + 1) {
-                Some(width) => width,
-                None => return,
-            };
+// Statements of the form
+// let pat: ty = init;
+impl Rewrite for ast::Local {
+    fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
+        let mut result = "let ".to_owned();
+        let pattern_offset = offset + result.len();
+        // 1 = ;
+        let pattern_width = try_opt!(width.checked_sub(pattern_offset.width() + 1));
 
-            match local.pat.rewrite(&context, pattern_width, pattern_offset) {
-                Some(ref pat_string) => result.push_str(pat_string),
-                None => return,
-            }
+        let pat_str = try_opt!(self.pat.rewrite(&context, pattern_width, pattern_offset));
+        result.push_str(&pat_str);
 
-            // String that is placed within the assignment pattern and expression.
-            let infix = {
-                let mut infix = String::new();
-
-                if let Some(ref ty) = local.ty {
-                    // 2 = ": ".len()
-                    // 1 = ;
-                    let offset = self.block_indent + result.len() + 2;
-                    let width = match self.config.max_width.checked_sub(offset.width() + 1) {
-                        Some(w) => w,
-                        None => return,
-                    };
-                    let rewrite = ty.rewrite(&self.get_context(), width, offset);
-
-                    match rewrite {
-                        Some(result) => {
-                            infix.push_str(": ");
-                            infix.push_str(&result);
-                        }
-                        None => return,
-                    }
-                }
+        // String that is placed within the assignment pattern and expression.
+        let infix = {
+            let mut infix = String::new();
 
-                if local.init.is_some() {
-                    infix.push_str(" =");
-                }
+            if let Some(ref ty) = self.ty {
+                // 2 = ": ".len()
+                // 1 = ;
+                let indent = offset + last_line_width(&result) + 2;
+                let budget = try_opt!(width.checked_sub(indent.width() + 1));
+                let rewrite = try_opt!(ty.rewrite(context, budget, indent));
 
-                infix
-            };
+                infix.push_str(": ");
+                infix.push_str(&rewrite);
+            }
 
-            result.push_str(&infix);
+            if self.init.is_some() {
+                infix.push_str(" =");
+            }
 
-            if let Some(ref ex) = local.init {
-                let max_width = self.config.max_width.checked_sub(context.block_indent.width() + 1);
-                let max_width = match max_width {
-                    Some(width) => width,
-                    None => return,
-                };
+            infix
+        };
 
-                // 1 = trailing semicolon;
-                let rhs = rewrite_assign_rhs(&context, result, ex, max_width, context.block_indent);
+        result.push_str(&infix);
 
-                match rhs {
-                    Some(s) => s,
-                    None => return,
-                }
-            } else {
-                result
-            }
-        };
+        if let Some(ref ex) = self.init {
+            let budget = try_opt!(width.checked_sub(context.block_indent.width() + 1));
 
-        self.buffer.push_str(&result);
-        self.buffer.push_str(";");
-        self.last_pos = span.hi;
+            // 1 = trailing semicolon;
+            result = try_opt!(rewrite_assign_rhs(&context,
+                                                 result,
+                                                 ex,
+                                                 budget,
+                                                 context.block_indent));
+        }
+
+        result.push(';');
+        Some(result)
     }
+}
 
+impl<'a> FmtVisitor<'a> {
     pub fn format_foreign_mod(&mut self, fm: &ast::ForeignMod, span: Span) {
         self.buffer.push_str("extern ");