mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-04 12:18:19 +00:00
parse and format pattern as
This commit is contained in:
parent
ea53a50447
commit
e9196f3c0b
13 changed files with 189 additions and 7 deletions
|
@ -69,7 +69,11 @@ impl<'a> Buf<'a> {
|
|||
}
|
||||
|
||||
pub fn push_str_allow_spaces(&mut self, s: &str) {
|
||||
debug_assert!(!self.beginning_of_line);
|
||||
debug_assert!(
|
||||
!self.beginning_of_line,
|
||||
"push_str: `{s}` with text:\n{}",
|
||||
self.text
|
||||
);
|
||||
|
||||
self.flush_spaces();
|
||||
|
||||
|
@ -77,8 +81,13 @@ impl<'a> Buf<'a> {
|
|||
}
|
||||
|
||||
pub fn push_str(&mut self, s: &str) {
|
||||
debug_assert!(!self.beginning_of_line);
|
||||
debug_assert!(!s.contains('\n') && !s.ends_with(' '));
|
||||
debug_assert!(
|
||||
!self.beginning_of_line,
|
||||
"push_str: `{s}` with text:\n{}",
|
||||
self.text
|
||||
);
|
||||
debug_assert!(!s.contains('\n'));
|
||||
debug_assert!(!s.ends_with(' '));
|
||||
|
||||
if !s.is_empty() {
|
||||
self.flush_spaces();
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use crate::annotation::{Formattable, Newlines, Parens};
|
||||
use crate::expr::{fmt_str_literal, format_sq_literal};
|
||||
use crate::spaces::{fmt_comments_only, fmt_spaces, NewlineAt};
|
||||
use crate::spaces::{fmt_comments_only, fmt_spaces, NewlineAt, INDENT};
|
||||
use crate::Buf;
|
||||
use roc_parse::ast::{Base, CommentOrNewline, Pattern};
|
||||
|
||||
|
@ -28,6 +28,10 @@ impl<'a> Formattable for Pattern<'a> {
|
|||
|
||||
Pattern::OptionalField(_, expr) => expr.is_multiline(),
|
||||
|
||||
Pattern::As(pattern, spaces, _identifier) => {
|
||||
pattern.is_multiline() || spaces.iter().any(|s| s.is_comment())
|
||||
}
|
||||
|
||||
Pattern::Identifier(_)
|
||||
| Pattern::Tag(_)
|
||||
| Pattern::OpaqueRef(_)
|
||||
|
@ -199,6 +203,27 @@ impl<'a> Formattable for Pattern<'a> {
|
|||
buf.push_str("..");
|
||||
}
|
||||
|
||||
As(pattern, spaces, identifier) => {
|
||||
fmt_pattern(buf, &pattern.value, indent, parens);
|
||||
|
||||
let as_indent = indent + INDENT;
|
||||
|
||||
buf.indent(as_indent);
|
||||
|
||||
if !buf.ends_with_space() {
|
||||
buf.spaces(1);
|
||||
}
|
||||
|
||||
buf.push_str("as");
|
||||
buf.spaces(1);
|
||||
|
||||
// these spaces "belong" to the identifier, which can never be multiline
|
||||
fmt_comments_only(buf, spaces.iter(), NewlineAt::Bottom, indent);
|
||||
|
||||
buf.indent(as_indent);
|
||||
buf.push_str(identifier.value);
|
||||
}
|
||||
|
||||
// Space
|
||||
SpaceBefore(sub_pattern, spaces) => {
|
||||
if !sub_pattern.is_multiline() {
|
||||
|
|
|
@ -766,6 +766,11 @@ impl<'a> RemoveSpaces<'a> for Pattern<'a> {
|
|||
Pattern::OptionalField(a, b) => {
|
||||
Pattern::OptionalField(a, arena.alloc(b.remove_spaces(arena)))
|
||||
}
|
||||
Pattern::As(pattern, spaces, identifier) => Pattern::As(
|
||||
arena.alloc(pattern.remove_spaces(arena)),
|
||||
spaces,
|
||||
identifier,
|
||||
),
|
||||
Pattern::NumLiteral(a) => Pattern::NumLiteral(a),
|
||||
Pattern::NonBase10Literal {
|
||||
string,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue