mirror of
https://github.com/roc-lang/roc.git
synced 2024-09-22 16:30:04 +03:00
Multiline list and record def
This commit is contained in:
parent
d0c02af6ca
commit
bc38405826
@ -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) {
|
||||
@ -12,8 +12,22 @@ pub fn fmt_def<'a>(buf: &mut String<'a>, def: &'a Def<'a>, indent: u16) {
|
||||
Body(loc_pattern, loc_expr) => {
|
||||
fmt_pattern(buf, &loc_pattern.value, indent, true, false);
|
||||
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");
|
||||
}
|
||||
|
@ -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(' ');
|
||||
}
|
||||
|
@ -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() {
|
||||
#[test]
|
||||
fn multi_line_record_def() {
|
||||
// expr_formats_same(indoc!(
|
||||
// r#"
|
||||
// pos =
|
||||
// {
|
||||
// x: 5,
|
||||
// x: 10
|
||||
// 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() {
|
||||
|
Loading…
Reference in New Issue
Block a user