Add fmt test to make sure formatting works for all parser test cases

This commit is contained in:
Joshua Warner 2022-11-11 15:56:50 -05:00
parent 492eb31a3d
commit 1efd7260de
No known key found for this signature in database
GPG key ID: 89AD497003F93FDD
5 changed files with 70 additions and 23 deletions

View file

@ -326,9 +326,6 @@ impl<'a> Formattable for Expr<'a> {
Record(fields) => {
fmt_record(buf, None, *fields, indent);
}
Tuple(_fields) => {
todo!("format tuple");
}
RecordUpdate { update, fields } => {
fmt_record(buf, Some(*update), *fields, indent);
}
@ -386,6 +383,7 @@ impl<'a> Formattable for Expr<'a> {
fmt_if(buf, branches, final_else, self.is_multiline(), indent);
}
When(loc_condition, branches) => fmt_when(buf, loc_condition, branches, indent),
Tuple(items) => fmt_collection(buf, indent, Braces::Round, *items, Newlines::No),
List(items) => fmt_collection(buf, indent, Braces::Square, *items, Newlines::No),
BinOps(lefts, right) => fmt_binops(buf, lefts, right, false, parens, indent),
UnaryOp(sub_expr, unary_op) => {
@ -421,7 +419,10 @@ impl<'a> Formattable for Expr<'a> {
buf.push('.');
buf.push_str(key);
}
MalformedIdent(_, _) => {}
MalformedIdent(str, _) => {
buf.indent(indent);
buf.push_str(str)
}
MalformedClosure => {}
PrecedenceConflict { .. } => {}
}
@ -505,10 +506,10 @@ fn push_op(buf: &mut Buf, op: BinOp) {
pub fn fmt_str_literal<'buf>(buf: &mut Buf<'buf>, literal: StrLiteral, indent: u16) {
use roc_parse::ast::StrLiteral::*;
buf.indent(indent);
buf.push('"');
match literal {
PlainLine(string) => {
buf.indent(indent);
buf.push('"');
// When a PlainLine contains '\n' or '"', format as a block string
if string.contains('"') || string.contains('\n') {
buf.push_str("\"\"");
@ -523,15 +524,21 @@ pub fn fmt_str_literal<'buf>(buf: &mut Buf<'buf>, literal: StrLiteral, indent: u
} else {
buf.push_str_allow_spaces(string);
};
buf.push('"');
}
Line(segments) => {
buf.indent(indent);
buf.push('"');
for seg in segments.iter() {
format_str_segment(seg, buf, 0)
}
buf.push('"');
}
Block(lines) => {
// Block strings will always be formatted with """ on new lines
buf.push_str("\"\"");
buf.ensure_ends_with_newline();
buf.indent(indent);
buf.push_str("\"\"\"");
buf.newline();
for segments in lines.iter() {
@ -543,10 +550,9 @@ pub fn fmt_str_literal<'buf>(buf: &mut Buf<'buf>, literal: StrLiteral, indent: u
buf.newline();
}
buf.indent(indent);
buf.push_str("\"\"");
buf.push_str("\"\"\"");
}
}
buf.push('"');
}
fn fmt_binops<'a, 'buf>(