Move MigrationFlags from an argument on format* to a field on Buf

That has a few advantages:

* We avoid a bunch of extra parameter-passing "noise", since the vast majority of formatting code doesn't need to care about this setting beyond just passing it to nested format calls.
* It aligns really well with the "global" nature of this setting, and makes it impossible to have bugs where e.g. one callsite forgets to pass the correct value to it's children - which would lead to parts of the tree not being migrated. If this is truly a global setting on Buf, that simply can't happen.
This commit is contained in:
Joshua Warner 2024-12-04 17:43:11 -08:00
parent 0274d9b997
commit 120e9be550
No known key found for this signature in database
GPG key ID: 89AD497003F93FDD
15 changed files with 390 additions and 812 deletions

View file

@ -1,5 +1,5 @@
use bumpalo::Bump;
use roc_fmt::{annotation::Formattable, annotation::MigrationFlags, header::fmt_header};
use roc_fmt::{annotation::Formattable, header::fmt_header, MigrationFlags};
use roc_parse::{
ast::{Defs, Expr, FullAst, Header, Malformed, SpacesBefore},
header::parse_module_defs,
@ -82,26 +82,26 @@ pub enum Output<'a> {
impl<'a> Output<'a> {
pub fn format(&self) -> InputOwned {
let arena = Bump::new();
let mut buf = Buf::new_in(&arena);
let flags = MigrationFlags::new(false);
let mut buf = Buf::new_in(&arena, flags);
match self {
Output::Header(header) => {
fmt_header(&mut buf, header, &flags);
fmt_header(&mut buf, header);
buf.fmt_end_of_file();
InputOwned::Header(buf.as_str().to_string())
}
Output::ModuleDefs(defs) => {
defs.format(&mut buf, &flags, 0);
defs.format(&mut buf, 0);
buf.fmt_end_of_file();
InputOwned::ModuleDefs(buf.as_str().to_string())
}
Output::Expr(expr) => {
expr.format(&mut buf, &flags, 0);
expr.format(&mut buf, 0);
InputOwned::Expr(buf.as_str().to_string())
}
Output::Full(full) => {
fmt_header(&mut buf, &full.header, &flags);
full.defs.format(&mut buf, &flags, 0);
fmt_header(&mut buf, &full.header);
full.defs.format(&mut buf, 0);
buf.fmt_end_of_file();
InputOwned::Full(buf.as_str().to_string())
}