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::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");

View File

@ -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(' ');
}

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
#[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() {