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

@ -8,6 +8,7 @@ use crate::{
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub enum Braces {
Round,
Square,
Curly,
}
@ -22,11 +23,13 @@ pub fn fmt_collection<'a, 'buf, T: ExtractSpaces<'a> + Formattable>(
<T as ExtractSpaces<'a>>::Item: Formattable,
{
let start = match braces {
Braces::Round => '(',
Braces::Curly => '{',
Braces::Square => '[',
};
let end = match braces {
Braces::Round => ')',
Braces::Curly => '}',
Braces::Square => ']',
};

View file

@ -134,7 +134,7 @@ impl<'a> Formattable for TypeDef<'a> {
if !self.is_multiline() {
debug_assert_eq!(members.len(), 1);
buf.push_str(" ");
buf.spaces(1);
members[0].format_with_options(
buf,
Parens::NotNeeded,

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>(

View file

@ -109,7 +109,7 @@ impl<'a> Buf<'a> {
if self.spaces_to_flush > 0 {
self.flush_spaces();
self.newline();
} else if !self.text.ends_with('\n') {
} else if !self.text.ends_with('\n') && !self.text.is_empty() {
self.newline()
}
}