Multiline list and record def

This commit is contained in:
Chad Stearns 2020-01-20 23:05:34 -05:00
parent d0c02af6ca
commit bc38405826
3 changed files with 124 additions and 35 deletions

View File

@ -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::pattern::fmt_pattern;
use crate::fmt::spaces::fmt_spaces; use crate::fmt::spaces::{fmt_spaces, newline, INDENT};
use crate::parse::ast::Def; use crate::parse::ast::{Def, Expr};
use bumpalo::collections::String; use bumpalo::collections::String;
pub fn fmt_def<'a>(buf: &mut String<'a>, def: &'a Def<'a>, indent: u16) { 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"), Annotation(_, _) => panic!("TODO have format_def support Annotation"),
Body(loc_pattern, loc_expr) => { Body(loc_pattern, loc_expr) => {
fmt_pattern(buf, &loc_pattern.value, indent, true, false); fmt_pattern(buf, &loc_pattern.value, indent, true, false);
buf.push_str(" = "); buf.push_str(" =");
fmt_expr(buf, &loc_expr.value, indent, false, true); 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) => { TypedDef(_loc_pattern, _loc_annotation, _loc_expr) => {
panic!("TODO support Annotation in TypedDef"); panic!("TODO support Annotation in TypedDef");

View File

@ -665,7 +665,7 @@ pub fn fmt_record<'a>(
} }
if is_multiline { if is_multiline {
buf.push('\n'); newline(buf, indent)
} else if !loc_fields.is_empty() { } else if !loc_fields.is_empty() {
buf.push(' '); buf.push(' ');
} }

View File

@ -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 // RECORD LITERALS
#[test] #[test]
@ -909,35 +974,45 @@ mod test_format {
)); ));
} }
// #[test] #[test]
// fn multi_line_list_def() { fn multi_line_record_def() {
// expr_formats_same(indoc!( // expr_formats_same(indoc!(
// r#" // r#"
// scores = // pos =
// [ // {
// 5, // x: 4,
// 10 // y: 11,
// ] // z: 16
// // }
// scores //
// "# // pos
// )); // "#
// } // ));
//
// #[test] expr_formats_to(
// fn multi_line_record_def() { indoc!(
// expr_formats_same(indoc!( r#"
// r#" pos = {
// pos = x: 5,
// { y: 10
// x: 5, }
// x: 10
// } pos
// "#
// pos ),
// "# indoc!(
// )); r#"
// } pos =
{
x: 5,
y: 10
}
pos
"#
),
);
}
#[test] #[test]
fn two_fields_center_newline() { fn two_fields_center_newline() {