Move TypeAnnotation::Apply to to_node

This commit is contained in:
Joshua Warner 2024-12-19 17:36:20 -05:00
parent f169329c0e
commit 13ec99c16b
No known key found for this signature in database
GPG key ID: 89AD497003F93FDD
2 changed files with 46 additions and 54 deletions

View file

@ -222,8 +222,6 @@ fn fmt_ty_ann(
) { ) {
let me = ann_lift_spaces(buf.text.bump(), me); let me = ann_lift_spaces(buf.text.bump(), me);
let self_is_multiline = me.item.is_multiline();
if !me.before.is_empty() { if !me.before.is_empty() {
buf.ensure_ends_with_newline(); buf.ensure_ends_with_newline();
fmt_comments_only(buf, me.before.iter(), NewlineAt::Bottom, indent); fmt_comments_only(buf, me.before.iter(), NewlineAt::Bottom, indent);
@ -1214,59 +1212,43 @@ impl<'a> Nodify<'a> for TypeAnnotation<'a> {
{ {
match self { match self {
TypeAnnotation::Apply(module, func, args) => { TypeAnnotation::Apply(module, func, args) => {
if args.is_empty() { let first = if module.is_empty() {
return Spaces { Node::Literal(func)
item: Node::TypeAnnotation(*self),
before: &[],
after: &[],
};
}
let mut new_args = Vec::with_capacity_in(args.len(), arena);
if !args.is_empty() {
for arg in args.iter().take(args.len() - 1) {
let lifted = ann_lift_spaces(arena, &arg.value);
new_args.push(Loc::at(arg.region, lower(arena, lifted)));
}
}
let after = if let Some(last) = args.last() {
let lifted = ann_lift_spaces(arena, &last.value);
if lifted.before.is_empty() {
new_args.push(Loc::at(last.region, lifted.item));
} else {
new_args.push(Loc::at(
last.region,
TypeAnnotation::SpaceBefore(arena.alloc(lifted.item), lifted.before),
));
}
lifted.after
} else { } else {
&[] Node::Literal(arena.alloc_str(&format!("{}.{}", module, func)))
}; };
let item = Node::TypeAnnotation(TypeAnnotation::Apply( let mut last_after: &[CommentOrNewline<'_>] = &[];
module, let mut rest = Vec::with_capacity_in(args.len(), arena);
func,
new_args.into_bump_slice(),
));
if parens == Parens::InApply { for arg in *args {
parens_around_node( let lifted = arg.value.to_node(arena, Parens::InApply);
arena, let before = merge_spaces_conservative(arena, last_after, lifted.before);
Spaces { last_after = lifted.after;
before: &[], rest.push(Item {
item, before,
after, comma: false,
}, newline: false,
true, space: true,
) node: lifted.item,
});
}
let item = Spaces {
before: &[],
item: Node::CommaSequence {
allow_blank_lines: false,
indent_rest: true,
first: arena.alloc(first),
rest: rest.into_bump_slice(),
},
after: last_after,
};
if parens == Parens::InApply && !args.is_empty() {
parens_around_node(arena, item, false)
} else { } else {
Spaces { item
before: &[],
item,
after,
}
} }
} }
TypeAnnotation::SpaceBefore(expr, spaces) => { TypeAnnotation::SpaceBefore(expr, spaces) => {
@ -1335,6 +1317,7 @@ impl<'a> Nodify<'a> for TypeAnnotation<'a> {
before: first_node.before, before: first_node.before,
item: Node::CommaSequence { item: Node::CommaSequence {
allow_blank_lines: false, allow_blank_lines: false,
indent_rest: false,
first: arena.alloc(first_node.item), first: arena.alloc(first_node.item),
rest: rest_nodes.into_bump_slice(), rest: rest_nodes.into_bump_slice(),
}, },
@ -1346,7 +1329,7 @@ impl<'a> Nodify<'a> for TypeAnnotation<'a> {
|| parens == Parens::InAsPattern || parens == Parens::InAsPattern
|| parens == Parens::InFunctionType || parens == Parens::InFunctionType
{ {
parens_around_node(arena, inner, true) parens_around_node(arena, item, false)
} else { } else {
item item
} }
@ -1375,7 +1358,7 @@ impl<'a> Nodify<'a> for TypeAnnotation<'a> {
let item = Spaces::item(Node::Literal(text)); let item = Spaces::item(Node::Literal(text));
if *text == "implements" { if *text == "implements" {
parens_around_node(arena, item) parens_around_node(arena, item, false)
} else { } else {
item item
} }

View file

@ -56,6 +56,7 @@ pub enum Node<'a> {
DelimitedSequence(Braces, &'a [(Sp<'a>, Node<'a>)], Sp<'a>), DelimitedSequence(Braces, &'a [(Sp<'a>, Node<'a>)], Sp<'a>),
CommaSequence { CommaSequence {
allow_blank_lines: bool, allow_blank_lines: bool,
indent_rest: bool,
first: &'a Node<'a>, first: &'a Node<'a>,
rest: &'a [Item<'a>], rest: &'a [Item<'a>],
}, },
@ -160,6 +161,7 @@ impl<'a> Formattable for Node<'a> {
} }
Node::CommaSequence { Node::CommaSequence {
allow_blank_lines: _, allow_blank_lines: _,
indent_rest: _,
first, first,
rest, rest,
} => first.is_multiline() || rest.iter().any(|item| item.is_multiline()), } => first.is_multiline() || rest.iter().any(|item| item.is_multiline()),
@ -205,10 +207,16 @@ impl<'a> Formattable for Node<'a> {
} }
Node::CommaSequence { Node::CommaSequence {
allow_blank_lines, allow_blank_lines,
indent_rest,
first, first,
rest, rest,
} => { } => {
buf.indent(indent); buf.indent(indent);
let inner_indent = if *indent_rest {
indent + INDENT
} else {
indent
};
first.format_with_options(buf, parens, newlines, indent); first.format_with_options(buf, parens, newlines, indent);
for item in *rest { for item in *rest {
@ -218,14 +226,15 @@ impl<'a> Formattable for Node<'a> {
if *allow_blank_lines { if *allow_blank_lines {
fmt_spaces(buf, item.before.iter(), indent); fmt_spaces(buf, item.before.iter(), indent);
} else { } else {
fmt_spaces_no_blank_lines(buf, item.before.iter(), indent); fmt_spaces_no_blank_lines(buf, item.before.iter(), inner_indent);
} }
if item.newline { if item.newline {
buf.ensure_ends_with_newline(); buf.ensure_ends_with_newline();
} else if item.space { } else if item.space {
buf.ensure_ends_with_whitespace(); buf.ensure_ends_with_whitespace();
} }
item.node.format_with_options(buf, parens, newlines, indent); item.node
.format_with_options(buf, parens, newlines, inner_indent);
} }
} }
Node::Literal(text) => { Node::Literal(text) => {