keep indent state in fmt Buf

This commit is contained in:
Joshua Warner 2021-12-02 16:28:57 -08:00
parent e872d00b13
commit e3f2f8d9a4
11 changed files with 313 additions and 182 deletions

View file

@ -1,14 +1,9 @@
use crate::annotation::{Formattable, Newlines, Parens};
use crate::spaces::{fmt_comments_only, fmt_spaces, NewlineAt};
use bumpalo::collections::String;
use crate::Buf;
use roc_parse::ast::{Base, Pattern};
pub fn fmt_pattern<'a>(
buf: &mut String<'a>,
pattern: &'a Pattern<'a>,
indent: u16,
parens: Parens,
) {
pub fn fmt_pattern<'a>(buf: &mut Buf<'a>, pattern: &'a Pattern<'a>, indent: u16, parens: Parens) {
pattern.format_with_options(buf, parens, Newlines::No, indent);
}
@ -44,7 +39,7 @@ impl<'a> Formattable<'a> for Pattern<'a> {
fn format_with_options(
&self,
buf: &mut String<'a>,
buf: &mut Buf<'a>,
parens: Parens,
newlines: Newlines,
indent: u16,
@ -52,11 +47,16 @@ impl<'a> Formattable<'a> for Pattern<'a> {
use self::Pattern::*;
match self {
Identifier(string) => buf.push_str(string),
Identifier(string) => {
buf.indent(indent);
buf.push_str(string)
}
GlobalTag(name) | PrivateTag(name) => {
buf.indent(indent);
buf.push_str(name);
}
Apply(loc_pattern, loc_arg_patterns) => {
buf.indent(indent);
// Sometimes, an Apply pattern needs parens around it.
// In particular when an Apply's argument is itself an Apply (> 0) arguments
let parens = !loc_arg_patterns.is_empty() && parens == Parens::InApply;
@ -77,6 +77,7 @@ impl<'a> Formattable<'a> for Pattern<'a> {
}
}
RecordDestructure(loc_patterns) => {
buf.indent(indent);
buf.push_str("{ ");
let mut it = loc_patterns.iter().peekable();
@ -93,23 +94,29 @@ impl<'a> Formattable<'a> for Pattern<'a> {
}
RequiredField(name, loc_pattern) => {
buf.indent(indent);
buf.push_str(name);
buf.push_str(": ");
loc_pattern.format(buf, indent);
}
OptionalField(name, loc_pattern) => {
buf.indent(indent);
buf.push_str(name);
buf.push_str(" ? ");
loc_pattern.format(buf, indent);
}
NumLiteral(string) => buf.push_str(string),
NumLiteral(string) => {
buf.indent(indent);
buf.push_str(string);
}
NonBase10Literal {
base,
string,
is_negative,
} => {
buf.indent(indent);
if *is_negative {
buf.push('-');
}
@ -123,11 +130,15 @@ impl<'a> Formattable<'a> for Pattern<'a> {
buf.push_str(string);
}
FloatLiteral(string) => buf.push_str(string),
FloatLiteral(string) => {
buf.indent(indent);
buf.push_str(string);
}
StrLiteral(literal) => {
todo!("Format string literal: {:?}", literal);
}
Underscore(name) => {
buf.indent(indent);
buf.push('_');
buf.push_str(name);
}
@ -152,8 +163,12 @@ impl<'a> Formattable<'a> for Pattern<'a> {
}
// Malformed
Malformed(string) | MalformedIdent(string, _) => buf.push_str(string),
Malformed(string) | MalformedIdent(string, _) => {
buf.indent(indent);
buf.push_str(string);
}
QualifiedIdentifier { module_name, ident } => {
buf.indent(indent);
if !module_name.is_empty() {
buf.push_str(module_name);
buf.push('.');