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

@ -19,19 +19,40 @@ pub struct Buf<'a> {
newlines_to_flush: usize,
beginning_of_line: bool,
line_indent: u16,
flags: MigrationFlags,
}
#[derive(Debug, Copy, Clone)]
pub struct MigrationFlags {
pub(crate) snakify: bool,
}
impl MigrationFlags {
pub fn new(snakify: bool) -> Self {
MigrationFlags { snakify }
}
pub fn at_least_one_active(&self) -> bool {
self.snakify
}
}
impl<'a> Buf<'a> {
pub fn new_in(arena: &'a Bump) -> Buf<'a> {
pub fn new_in(arena: &'a Bump, flags: MigrationFlags) -> Buf<'a> {
Buf {
text: String::new_in(arena),
line_indent: 0,
spaces_to_flush: 0,
newlines_to_flush: 0,
beginning_of_line: true,
flags,
}
}
pub fn flags(&self) -> MigrationFlags {
self.flags
}
pub fn as_str(&'a self) -> &'a str {
self.text.as_str()
}