From bc384058269ae478bb76d1a23b619d88cfb05e62 Mon Sep 17 00:00:00 2001 From: Chad Stearns Date: Mon, 20 Jan 2020 23:05:34 -0500 Subject: [PATCH] Multiline list and record def --- src/fmt/def.rs | 24 ++++++-- src/fmt/expr.rs | 2 +- tests/test_format.rs | 133 +++++++++++++++++++++++++++++++++---------- 3 files changed, 124 insertions(+), 35 deletions(-) diff --git a/src/fmt/def.rs b/src/fmt/def.rs index f6e518467e..55c236af09 100644 --- a/src/fmt/def.rs +++ b/src/fmt/def.rs @@ -1,7 +1,7 @@ -use crate::fmt::expr::fmt_expr; +use crate::fmt::expr::{fmt_expr, is_multiline_expr}; use crate::fmt::pattern::fmt_pattern; -use crate::fmt::spaces::fmt_spaces; -use crate::parse::ast::Def; +use crate::fmt::spaces::{fmt_spaces, newline, INDENT}; +use crate::parse::ast::{Def, Expr}; use bumpalo::collections::String; pub fn fmt_def<'a>(buf: &mut String<'a>, def: &'a Def<'a>, indent: u16) { @@ -11,8 +11,22 @@ pub fn fmt_def<'a>(buf: &mut String<'a>, def: &'a Def<'a>, indent: u16) { Annotation(_, _) => panic!("TODO have format_def support Annotation"), Body(loc_pattern, loc_expr) => { fmt_pattern(buf, &loc_pattern.value, indent, true, false); - buf.push_str(" = "); - fmt_expr(buf, &loc_expr.value, indent, false, true); + buf.push_str(" ="); + if is_multiline_expr(&loc_expr.value) { + match &loc_expr.value { + Expr::Record { .. } | Expr::List(_) => { + newline(buf, indent + INDENT); + fmt_expr(buf, &loc_expr.value, indent + INDENT, false, true); + } + _ => { + buf.push(' '); + fmt_expr(buf, &loc_expr.value, indent, false, true); + } + } + } else { + buf.push(' '); + fmt_expr(buf, &loc_expr.value, indent, false, true); + } } TypedDef(_loc_pattern, _loc_annotation, _loc_expr) => { panic!("TODO support Annotation in TypedDef"); diff --git a/src/fmt/expr.rs b/src/fmt/expr.rs index b7b841ab35..93f5ba08f7 100644 --- a/src/fmt/expr.rs +++ b/src/fmt/expr.rs @@ -665,7 +665,7 @@ pub fn fmt_record<'a>( } if is_multiline { - buf.push('\n'); + newline(buf, indent) } else if !loc_fields.is_empty() { buf.push(' '); } diff --git a/tests/test_format.rs b/tests/test_format.rs index 545bb382e2..83dc5dbd09 100644 --- a/tests/test_format.rs +++ b/tests/test_format.rs @@ -880,6 +880,71 @@ mod test_format { ); } + #[test] + fn multi_line_list_def() { + // expr_formats_same(indoc!( + // r#" + // r = + // [ + // 1, + // 2 + // ] + // + // r + // "# + // ) + // ); + + expr_formats_to( + indoc!( + r#" + results = [ + Ok 4, + Ok 5 + ] + + allOks results + "# + ), + indoc!( + r#" + results = + [ + Ok 4, + Ok 5 + ] + + allOks results + "# + ), + ); + + // expr_formats_to(indoc!( + // r#" + // results = + // # Lets count past 6 + // [ + // Ok 6, + // Err CountError + // ] + // + // allOks results + // "# + // ), indoc!( + // r#" + // results = + // # Lets count past 6 + // [ + // Ok 6, + // Err CountError + // ] + // + // allOks results + // "# + // ) + // ); + } + // RECORD LITERALS #[test] @@ -909,35 +974,45 @@ mod test_format { )); } - // #[test] - // fn multi_line_list_def() { - // expr_formats_same(indoc!( - // r#" - // scores = - // [ - // 5, - // 10 - // ] - // - // scores - // "# - // )); - // } - // - // #[test] - // fn multi_line_record_def() { - // expr_formats_same(indoc!( - // r#" - // pos = - // { - // x: 5, - // x: 10 - // } - // - // pos - // "# - // )); - // } + #[test] + fn multi_line_record_def() { + // expr_formats_same(indoc!( + // r#" + // pos = + // { + // x: 4, + // y: 11, + // z: 16 + // } + // + // pos + // "# + // )); + + expr_formats_to( + indoc!( + r#" + pos = { + x: 5, + y: 10 + } + + pos + "# + ), + indoc!( + r#" + pos = + { + x: 5, + y: 10 + } + + pos + "# + ), + ); + } #[test] fn two_fields_center_newline() {