Make unit assignment optional for suffixed defs

This commit is contained in:
Luke Boswell 2024-03-23 14:40:28 +11:00
parent 237bd942ed
commit 42fdcb7ff1
No known key found for this signature in database
GPG key ID: F6DB3C9DB47377B0
15 changed files with 208 additions and 7 deletions

View file

@ -12,5 +12,6 @@ roc_collections = { path = "../collections" }
roc_module = { path = "../module" }
roc_parse = { path = "../parse" }
roc_region = { path = "../region" }
roc_error_macros = { path = "../../error_macros" }
bumpalo.workspace = true

View file

@ -357,9 +357,19 @@ pub fn fmt_defs(buf: &mut Buf, defs: &Defs, indent: u16) {
}
pub fn fmt_body<'a>(buf: &mut Buf, pattern: &'a Pattern<'a>, body: &'a Expr<'a>, indent: u16) {
pattern.format_with_options(buf, Parens::InApply, Newlines::No, indent);
buf.indent(indent);
buf.push_str(" =");
// Check if this is an assignment into the unit/empty record, format as a Statement
let is_statement = if let Pattern::RecordDestructure(collection) = pattern {
collection.is_empty()
} else {
false
};
// Don't format the `{} =` for defs with this pattern
if !is_statement {
pattern.format_with_options(buf, Parens::InApply, Newlines::No, indent);
buf.indent(indent);
buf.push_str(" =");
}
if body.is_multiline() {
match body {

View file

@ -2,6 +2,7 @@ use crate::annotation::{Formattable, Newlines, Parens};
use crate::expr::{fmt_str_literal, format_sq_literal};
use crate::spaces::{fmt_comments_only, fmt_spaces, NewlineAt, INDENT};
use crate::Buf;
use roc_error_macros::internal_error;
use roc_parse::ast::{Base, CommentOrNewline, Pattern, PatternAs};
pub fn fmt_pattern<'a>(buf: &mut Buf, pattern: &'a Pattern<'a>, indent: u16, parens: Parens) {
@ -81,6 +82,7 @@ impl<'a> Formattable for Pattern<'a> {
Pattern::Tuple(patterns) | Pattern::List(patterns) => {
patterns.iter().any(|p| p.is_multiline())
}
Pattern::Stmt(_) => true,
}
}
@ -279,6 +281,10 @@ impl<'a> Formattable for Pattern<'a> {
buf.push_str(ident);
}
// Statement e.g. Suffixed with optional `{}=`
// e.g. `Stdout.line! "Hello World"`
Stmt(_) => internal_error!("should be desugared in parser into alternate pattern"),
}
}
}

View file

@ -837,6 +837,7 @@ impl<'a> RemoveSpaces<'a> for Pattern<'a> {
opt_pattern_as
.map(|(_, pattern_as)| ([].as_ref(), pattern_as.remove_spaces(arena))),
),
Pattern::Stmt(a) => Pattern::Stmt(a),
}
}
}