about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorMarcus Klaas <mail@marcusklaas.nl>2015-07-19 22:25:44 +0200
committerMarcus Klaas <mail@marcusklaas.nl>2015-07-19 23:43:03 +0200
commit2fda8dd883f5de6989280884a37e408981dfc2ee (patch)
treeb925d9122f5e5d02d765ae9dc251e02a4fda4bbe /src
parentb161815fe0aace4baa7007386b783a88547d7548 (diff)
Format if-let-else expressions
Diffstat (limited to 'src')
-rw-r--r--src/expr.rs59
1 files changed, 48 insertions, 11 deletions
diff --git a/src/expr.rs b/src/expr.rs
index 89846527cff..68b780b414a 100644
--- a/src/expr.rs
+++ b/src/expr.rs
@@ -84,6 +84,16 @@ impl Rewrite for ast::Expr {
                                 cond,
                                 if_block,
                                 else_block.as_ref().map(|e| &**e),
+                                None,
+                                width,
+                                offset)
+            }
+            ast::Expr_::ExprIfLet(ref pat, ref cond, ref if_block, ref else_block) => {
+                rewrite_if_else(context,
+                                cond,
+                                if_block,
+                                else_block.as_ref().map(|e| &**e),
+                                Some(pat),
                                 width,
                                 offset)
             }
@@ -112,9 +122,16 @@ impl Rewrite for ast::Block {
     }
 }
 
+// TODO(#18): implement pattern formatting
+impl Rewrite for ast::Pat {
+    fn rewrite(&self, context: &RewriteContext, _: usize, _: usize) -> Option<String> {
+        context.codemap.span_to_snippet(self.span).ok()
+    }
+}
+
 fn rewrite_label(label: Option<ast::Ident>) -> String {
     match label {
-        Some(ident) => format!("{}: ", ident.as_str()),
+        Some(ident) => format!("{}: ", ident),
         None => "".to_owned()
     }
 }
@@ -123,23 +140,43 @@ fn rewrite_if_else(context: &RewriteContext,
                    cond: &ast::Expr,
                    if_block: &ast::Block,
                    else_block: Option<&ast::Expr>,
+                   pat: Option<&ast::Pat>,
                    width: usize,
                    offset: usize)
                    -> Option<String> {
     // FIXME: missing comments between control statements and blocks
-    let cond_string = try_opt!(cond.rewrite(context, width - 3 - 2, offset + 3));
+    // 3 = "if ", 2 = " {"
+    let pat_string = match pat {
+        Some(pat) => {
+            // 7 = "let ".len() + " = ".len()
+            // 4 = "let ".len()
+            let pat_string = try_opt!(pat.rewrite(context, width - 3 - 2 - 7, offset + 3 + 4));
+            format!("let {} = ", pat_string)
+        }
+        None => String::new()
+    };
+
+    // Consider only the last line of the pat string
+    let extra_offset = match pat_string.rfind('\n') {
+        // 1 for newline character
+        Some(idx) => pat_string.len() - idx - 1 - offset,
+        None => 3 + pat_string.len()
+    };
+
+    let cond_string = try_opt!(cond.rewrite(context,
+                                            width - extra_offset - 2,
+                                            offset + extra_offset));
     let if_block_string = try_opt!(if_block.rewrite(context, width, offset));
+    let mut result = format!("if {}{} {}", pat_string, cond_string, if_block_string);
 
-    match else_block {
-        Some(else_block) => {
-            else_block.rewrite(context, width, offset).map(|else_block_string| {
-                format!("if {} {} else {}", cond_string, if_block_string, else_block_string)
-            })
-        }
-        None => {
-            Some(format!("if {} {}", cond_string, if_block_string))
-        }
+    if let Some(else_block) = else_block {
+        let else_block_string = try_opt!(else_block.rewrite(context, width, offset));
+
+        result.push_str(" else ");
+        result.push_str(&else_block_string);
     }
+
+    Some(result)
 }
 
 fn rewrite_string_lit(context: &RewriteContext,