Always use to_node for TypeAnnotation::As

This commit is contained in:
Joshua Warner 2024-12-19 11:13:57 -05:00
parent 5b93e834f1
commit dbbd097708
No known key found for this signature in database
GPG key ID: 89AD497003F93FDD
7 changed files with 197 additions and 43 deletions

View file

@ -1,6 +1,6 @@
use crate::{
collection::{fmt_collection, Braces},
expr::{format_spaces, merge_spaces_conservative},
expr::merge_spaces_conservative,
node::{parens_around_node, Node, NodeSequenceBuilder, Nodify, Sp},
pattern::pattern_lift_spaces_after,
pattern::snakify_camel_ident,
@ -370,36 +370,40 @@ fn fmt_ty_ann(
fmt_ext(ext, buf, indent);
}
TypeAnnotation::As(lhs, spaces, TypeHeader { name, vars }) => {
let write_parens = parens == Parens::InAsPattern || parens == Parens::InApply;
TypeAnnotation::As(..) => {
me.item
.to_node(buf.text.bump(), parens)
.item
.format(buf, indent);
// let write_parens = parens == Parens::InAsPattern || parens == Parens::InApply;
buf.indent(indent);
if write_parens {
buf.push('(')
}
// buf.indent(indent);
// if write_parens {
// buf.push('(')
// }
let lhs_indent = buf.cur_line_indent();
lhs.value
.format_with_options(buf, Parens::InAsPattern, Newlines::No, indent);
buf.spaces(1);
format_spaces(buf, spaces, newlines, indent);
buf.indent(lhs_indent + INDENT);
buf.push_str("as");
buf.spaces(1);
buf.push_str(name.value);
for var in *vars {
buf.spaces(1);
var.value.format_with_options(
buf,
Parens::NotNeeded,
Newlines::No,
lhs_indent + INDENT,
);
}
// let lhs_indent = buf.cur_line_indent();
// lhs.value
// .format_with_options(buf, Parens::InAsPattern, Newlines::No, indent);
// buf.spaces(1);
// format_spaces(buf, spaces, newlines, indent);
// buf.indent(lhs_indent + INDENT);
// buf.push_str("as");
// buf.spaces(1);
// buf.push_str(name.value);
// for var in *vars {
// buf.spaces(1);
// var.value.format_with_options(
// buf,
// Parens::NotNeeded,
// Newlines::No,
// lhs_indent + INDENT,
// );
// }
if write_parens {
buf.push(')')
}
// if write_parens {
// buf.push(')')
// }
}
TypeAnnotation::Where(annot, implements_clauses) => {
@ -523,7 +527,11 @@ impl<'a> Nodify<'a> for Tag<'a> {
Spaces {
before: &[],
item: Node::Sequence(arena.alloc(first), new_args.into_bump_slice()),
item: Node::Sequence {
first: arena.alloc(first),
extra_indent_for_rest: true,
rest: new_args.into_bump_slice(),
},
after: last_after,
}
}
@ -735,7 +743,7 @@ where
{
let first = Node::Literal(name);
let mut b = NodeSequenceBuilder::new(arena, first, 2);
let mut b = NodeSequenceBuilder::new(arena, first, 2, false);
b.push(Sp::with_space(sp), Node::Literal(sep));
@ -1386,7 +1394,7 @@ impl<'a> Nodify<'a> for TypeAnnotation<'a> {
let left = left.value.to_node(arena, Parens::InAsPattern);
let right = right.to_node(arena, Parens::InAsPattern);
let before_as = merge_spaces(arena, left.after, sp);
let mut b = NodeSequenceBuilder::new(arena, left.item, 2);
let mut b = NodeSequenceBuilder::new(arena, left.item, 2, true);
b.push(Sp::with_space(before_as), Node::Literal("as"));
b.push(Sp::with_space(right.before), right.item);

View file

@ -4,7 +4,7 @@ use roc_parse::ast::{CommentOrNewline, Pattern, Spaces, TypeAnnotation};
use crate::{
annotation::{Formattable, Newlines, Parens},
collection::Braces,
spaces::fmt_spaces,
spaces::{fmt_spaces, INDENT},
Buf,
};
@ -48,7 +48,11 @@ impl<'a> From<&'a [CommentOrNewline<'a>]> for Sp<'a> {
#[derive(Copy, Clone, Debug)]
pub enum Node<'a> {
Literal(&'a str),
Sequence(&'a Node<'a>, &'a [(Sp<'a>, Node<'a>)]),
Sequence {
first: &'a Node<'a>,
extra_indent_for_rest: bool,
rest: &'a [(Sp<'a>, Node<'a>)],
},
DelimitedSequence(Braces, &'a [(Sp<'a>, Node<'a>)], Sp<'a>),
// Temporary! TODO: translate these into proper Node elements
@ -107,7 +111,11 @@ impl<'a> Formattable for Node<'a> {
.iter()
.any(|(sp, l)| l.is_multiline() || !sp.comments.is_empty())
}
Node::Sequence(first, rest) => {
Node::Sequence {
first,
extra_indent_for_rest: _,
rest,
} => {
first.is_multiline()
|| rest
.iter()
@ -134,12 +142,23 @@ impl<'a> Formattable for Node<'a> {
buf.indent(indent);
buf.push(braces.end());
}
Node::Sequence(first, rest) => {
Node::Sequence {
first,
extra_indent_for_rest,
rest,
} => {
buf.indent(indent);
let cur_indent = buf.cur_line_indent();
first.format_with_options(buf, parens, newlines, indent);
let next_indent = if *extra_indent_for_rest {
cur_indent + INDENT
} else {
indent
};
for (sp, l) in *rest {
fmt_sp(buf, *sp, indent);
l.format_with_options(buf, parens, newlines, indent);
fmt_sp(buf, *sp, next_indent);
l.format_with_options(buf, parens, newlines, next_indent);
}
}
Node::Literal(text) => {
@ -158,13 +177,20 @@ impl<'a> Formattable for Node<'a> {
pub struct NodeSequenceBuilder<'a> {
first: Node<'a>,
extra_indent_for_rest: bool,
rest: Vec<'a, (Sp<'a>, Node<'a>)>,
}
impl<'a> NodeSequenceBuilder<'a> {
pub fn new(arena: &'a Bump, first: Node<'a>, capacity: usize) -> Self {
pub fn new(
arena: &'a Bump,
first: Node<'a>,
capacity: usize,
extra_indent_for_rest: bool,
) -> Self {
Self {
first,
extra_indent_for_rest,
rest: Vec::with_capacity_in(capacity, arena),
}
}
@ -174,9 +200,10 @@ impl<'a> NodeSequenceBuilder<'a> {
}
pub fn build(self) -> Node<'a> {
Node::Sequence(
self.rest.bump().alloc(self.first),
self.rest.into_bump_slice(),
)
Node::Sequence {
first: self.rest.bump().alloc(self.first),
extra_indent_for_rest: self.extra_indent_for_rest,
rest: self.rest.into_bump_slice(),
}
}
}

View file

@ -554,7 +554,7 @@ pub fn pattern_apply_to_node<'b, 'a: 'b>(
args: &[Loc<Pattern<'a>>],
) -> Spaces<'b, Node<'b>> {
let func_lifted = pattern_lift_spaces(arena, &func);
let mut b = NodeSequenceBuilder::new(arena, Node::Pattern(func_lifted.item), args.len());
let mut b = NodeSequenceBuilder::new(arena, Node::Pattern(func_lifted.item), args.len(), true);
let mut last_after = func_lifted.after;