support optional suffied last def

This commit is contained in:
Luke Boswell 2024-04-12 14:39:12 +10:00
parent 4625926486
commit c32fa5b600
No known key found for this signature in database
GPG key ID: F6DB3C9DB47377B0
17 changed files with 603 additions and 269 deletions

View file

@ -9,8 +9,8 @@ use crate::spaces::{
use crate::Buf;
use roc_module::called_via::{self, BinOp};
use roc_parse::ast::{
AssignedField, Base, Collection, CommentOrNewline, Expr, ExtractSpaces, Pattern,
RecordBuilderField, WhenBranch,
is_loc_expr_suffixed, AssignedField, Base, Collection, CommentOrNewline, Expr, ExtractSpaces,
Pattern, RecordBuilderField, WhenBranch,
};
use roc_parse::ast::{StrLiteral, StrSegment};
use roc_parse::ident::Accessor;
@ -48,6 +48,7 @@ impl<'a> Formattable for Expr<'a> {
| Tag(_)
| OpaqueRef(_)
| IngestedFile(_, _)
| EmptyDefsFinal()
| Crash => false,
// These expressions always have newlines
@ -427,6 +428,9 @@ impl<'a> Formattable for Expr<'a> {
indent,
);
}
EmptyDefsFinal() => {
// no need to print anything
}
_ => {
buf.ensure_ends_with_newline();
buf.indent(indent);
@ -443,6 +447,9 @@ impl<'a> Formattable for Expr<'a> {
buf.push(')');
}
}
EmptyDefsFinal() => {
// no need to print anything
}
Expect(condition, continuation) => {
fmt_expect(buf, condition, continuation, self.is_multiline(), indent);
}
@ -782,14 +789,32 @@ fn fmt_binops<'a>(
|| loc_right_side.value.is_multiline()
|| lefts.iter().any(|(expr, _)| expr.value.is_multiline());
let is_any_lefts_suffixed = lefts.iter().any(|(left, _)| is_loc_expr_suffixed(left));
let is_right_suffixed = is_loc_expr_suffixed(loc_right_side);
let is_any_suffixed = is_any_lefts_suffixed || is_right_suffixed;
let mut is_first = false;
let mut adjusted_indent = indent;
if is_any_suffixed {
// we only want to indent the remaining lines if this is a suffixed expression.
is_first = true;
}
for (loc_left_side, loc_binop) in lefts {
let binop = loc_binop.value;
loc_left_side.format_with_options(buf, Parens::InOperator, Newlines::No, indent);
loc_left_side.format_with_options(buf, Parens::InOperator, Newlines::No, adjusted_indent);
if is_first {
// indent the remaining lines, but only if the expression is suffixed.
is_first = false;
adjusted_indent = indent + 4;
}
if is_multiline {
buf.ensure_ends_with_newline();
buf.indent(indent);
buf.indent(adjusted_indent);
} else {
buf.spaces(1);
}
@ -799,7 +824,7 @@ fn fmt_binops<'a>(
buf.spaces(1);
}
loc_right_side.format_with_options(buf, Parens::InOperator, Newlines::Yes, indent);
loc_right_side.format_with_options(buf, Parens::InOperator, Newlines::Yes, adjusted_indent);
}
fn format_spaces(buf: &mut Buf, spaces: &[CommentOrNewline], newlines: Newlines, indent: u16) {

View file

@ -669,6 +669,7 @@ impl<'a> RemoveSpaces<'a> for StrSegment<'a> {
impl<'a> RemoveSpaces<'a> for Expr<'a> {
fn remove_spaces(&self, arena: &'a Bump) -> Self {
match *self {
Expr::EmptyDefsFinal() => Expr::EmptyDefsFinal(),
Expr::Float(a) => Expr::Float(a),
Expr::Num(a) => Expr::Num(a),
Expr::NonBase10Int {