mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-29 23:04:49 +00:00
use Parens type in pattern formatting
This commit is contained in:
parent
6046d8ee8e
commit
e18d3bbfe4
5 changed files with 86 additions and 30 deletions
|
@ -1,3 +1,4 @@
|
|||
use crate::annotation::Parens;
|
||||
use crate::spaces::{fmt_comments_only, fmt_spaces};
|
||||
use bumpalo::collections::String;
|
||||
use roc_parse::ast::{Base, Pattern};
|
||||
|
@ -6,7 +7,7 @@ pub fn fmt_pattern<'a>(
|
|||
buf: &mut String<'a>,
|
||||
pattern: &'a Pattern<'a>,
|
||||
indent: u16,
|
||||
apply_needs_parens: bool,
|
||||
parens: Parens,
|
||||
only_comments: bool,
|
||||
) {
|
||||
use self::Pattern::*;
|
||||
|
@ -17,34 +18,48 @@ pub fn fmt_pattern<'a>(
|
|||
buf.push_str(name);
|
||||
}
|
||||
Apply(loc_pattern, loc_arg_patterns) => {
|
||||
if apply_needs_parens {
|
||||
// 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;
|
||||
|
||||
if parens {
|
||||
buf.push('(');
|
||||
}
|
||||
|
||||
fmt_pattern(buf, &loc_pattern.value, indent, true, only_comments);
|
||||
fmt_pattern(
|
||||
buf,
|
||||
&loc_pattern.value,
|
||||
indent,
|
||||
Parens::InApply,
|
||||
only_comments,
|
||||
);
|
||||
|
||||
for loc_arg in loc_arg_patterns.iter() {
|
||||
buf.push(' ');
|
||||
fmt_pattern(buf, &loc_arg.value, indent, true, only_comments);
|
||||
fmt_pattern(buf, &loc_arg.value, indent, Parens::InApply, only_comments);
|
||||
}
|
||||
|
||||
if apply_needs_parens {
|
||||
if parens {
|
||||
buf.push(')');
|
||||
}
|
||||
}
|
||||
RecordDestructure(loc_patterns) => {
|
||||
buf.push_str("{ ");
|
||||
|
||||
let mut is_first = true;
|
||||
let mut it = loc_patterns.iter().peekable();
|
||||
|
||||
for loc_pattern in *loc_patterns {
|
||||
if is_first {
|
||||
is_first = false;
|
||||
} else {
|
||||
while let Some(loc_pattern) = it.next() {
|
||||
fmt_pattern(
|
||||
buf,
|
||||
&loc_pattern.value,
|
||||
indent,
|
||||
Parens::NotNeeded,
|
||||
only_comments,
|
||||
);
|
||||
|
||||
if it.peek().is_some() {
|
||||
buf.push_str(", ");
|
||||
}
|
||||
|
||||
fmt_pattern(buf, &loc_pattern.value, indent, true, only_comments);
|
||||
}
|
||||
|
||||
buf.push_str(" }");
|
||||
|
@ -53,7 +68,13 @@ pub fn fmt_pattern<'a>(
|
|||
RecordField(name, loc_pattern) => {
|
||||
buf.push_str(name);
|
||||
buf.push_str(": ");
|
||||
fmt_pattern(buf, &loc_pattern.value, indent, true, only_comments);
|
||||
fmt_pattern(
|
||||
buf,
|
||||
&loc_pattern.value,
|
||||
indent,
|
||||
Parens::NotNeeded,
|
||||
only_comments,
|
||||
);
|
||||
}
|
||||
|
||||
NumLiteral(string) => buf.push_str(string),
|
||||
|
@ -91,10 +112,10 @@ pub fn fmt_pattern<'a>(
|
|||
} else {
|
||||
fmt_spaces(buf, spaces.iter(), indent);
|
||||
}
|
||||
fmt_pattern(buf, sub_pattern, indent, apply_needs_parens, only_comments);
|
||||
fmt_pattern(buf, sub_pattern, indent, parens, only_comments);
|
||||
}
|
||||
SpaceAfter(sub_pattern, spaces) => {
|
||||
fmt_pattern(buf, sub_pattern, indent, apply_needs_parens, only_comments);
|
||||
fmt_pattern(buf, sub_pattern, indent, parens, only_comments);
|
||||
if only_comments {
|
||||
fmt_comments_only(buf, spaces.iter(), indent)
|
||||
} else {
|
||||
|
@ -103,7 +124,7 @@ pub fn fmt_pattern<'a>(
|
|||
}
|
||||
|
||||
Nested(sub_pattern) => {
|
||||
fmt_pattern(buf, sub_pattern, indent, apply_needs_parens, only_comments);
|
||||
fmt_pattern(buf, sub_pattern, indent, parens, only_comments);
|
||||
}
|
||||
|
||||
// Malformed
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue