Fix formatting bug with multline function type alias

This commit is contained in:
Joshua Warner 2024-12-31 19:52:23 -05:00
parent 275b2afb55
commit 835c246c56
No known key found for this signature in database
GPG key ID: 89AD497003F93FDD
12 changed files with 129 additions and 40 deletions

View file

@ -1108,7 +1108,12 @@ impl<'a> Nodify<'a> for TypeAnnotation<'a> {
.to_node(arena)
.add_parens(arena, Parens::InCollection)
});
maybe_add_ext(arena, coll, ext)
maybe_add_ext(
arena,
coll,
ext,
fields.is_empty() && fields.final_comments().is_empty(),
)
}
TypeAnnotation::TagUnion { ext, tags } => {
let coll =
@ -1117,7 +1122,12 @@ impl<'a> Nodify<'a> for TypeAnnotation<'a> {
.to_node(arena)
.add_parens(arena, Parens::InCollection)
});
maybe_add_ext(arena, coll, ext)
maybe_add_ext(
arena,
coll,
ext,
tags.is_empty() && tags.final_comments().is_empty(),
)
}
TypeAnnotation::Tuple { elems, ext } => {
let coll = collection_to_node(arena, Braces::Round, false, elems, |is_first, e| {
@ -1128,7 +1138,12 @@ impl<'a> Nodify<'a> for TypeAnnotation<'a> {
v.add_parens(arena, Parens::InCollection)
}
});
maybe_add_ext(arena, coll, ext)
maybe_add_ext(
arena,
coll,
ext,
elems.is_empty() && elems.final_comments().is_empty(),
)
}
TypeAnnotation::Where(annot, implements_clauses) => {
let mut items = Vec::with_capacity_in(implements_clauses.len() + 2, arena);
@ -1138,6 +1153,8 @@ impl<'a> Nodify<'a> for TypeAnnotation<'a> {
.to_node(arena)
.add_parens(arena, Parens::NotNeeded);
let mut needs_indent = annot.needs_indent || !annot.after.is_empty();
items.push(Item {
comma_before: false,
before: annot.after,
@ -1159,6 +1176,8 @@ impl<'a> Nodify<'a> for TypeAnnotation<'a> {
space: true,
node: node.node,
});
needs_indent |= node.node.is_multiline() || !before.is_empty();
}
NodeInfo {
@ -1171,7 +1190,7 @@ impl<'a> Nodify<'a> for TypeAnnotation<'a> {
indent_rest: false,
},
after: last_after,
needs_indent: true,
needs_indent,
prec: Prec::Term,
}
}
@ -1353,6 +1372,7 @@ fn maybe_add_ext<'a>(
arena: &'a Bump,
delim: Node<'a>,
ext: &Option<&'a Loc<TypeAnnotation<'a>>>,
needs_indent: bool,
) -> NodeInfo<'a> {
if let Some(ext) = ext {
let ext = ext.value.to_node(arena).add_ty_ext_parens(arena);
@ -1366,7 +1386,7 @@ fn maybe_add_ext<'a>(
before: &[],
node: item,
after: ext.after,
needs_indent: false,
needs_indent,
prec: Prec::Term,
}
} else {
@ -1374,7 +1394,7 @@ fn maybe_add_ext<'a>(
before: &[],
node: delim,
after: &[],
needs_indent: false,
needs_indent,
prec: Prec::Term,
}
}

View file

@ -876,36 +876,30 @@ fn fmt_general_def<L: Formattable>(
lhs.format_with_options(buf, lhs_parens, Newlines::Yes, indent);
buf.indent(indent);
if rhs.is_multiline() {
buf.spaces(1);
buf.push_str(sep);
buf.spaces(1);
buf.spaces(1);
buf.push_str(sep);
buf.spaces(1);
let rhs_lifted = ann_lift_spaces(buf.text.bump(), rhs);
let rhs = rhs.to_node(buf.text.bump());
if ty_is_outdentable(&rhs_lifted.item) && rhs_lifted.before.iter().all(|s| s.is_newline()) {
rhs_lifted
.item
if rhs.node.is_multiline() || !rhs.before.is_empty() || !rhs.after.is_empty() {
if rhs.node.is_multiline() && !rhs.needs_indent && rhs.before.iter().all(|s| s.is_newline())
{
rhs.node
.format_with_options(buf, Parens::NotNeeded, Newlines::No, indent);
} else {
buf.ensure_ends_with_newline();
fmt_comments_only(
buf,
rhs_lifted.before.iter(),
NewlineAt::Bottom,
indent + INDENT,
);
rhs_lifted
.item
fmt_comments_only(buf, rhs.before.iter(), NewlineAt::Bottom, indent + INDENT);
rhs.node
.format_with_options(buf, Parens::NotNeeded, newlines, indent + INDENT);
}
fmt_comments_only(buf, rhs_lifted.after.iter(), NewlineAt::Bottom, indent);
} else {
buf.spaces(1);
buf.push_str(sep);
buf.spaces(1);
rhs.format_with_options(buf, Parens::NotNeeded, Newlines::No, indent);
fmt_comments_only(buf, rhs.before.iter(), NewlineAt::Bottom, indent);
rhs.node
.format_with_options(buf, Parens::NotNeeded, Newlines::No, indent);
}
fmt_comments_only(buf, rhs.after.iter(), NewlineAt::Bottom, indent);
}
fn fmt_dbg_in_def<'a>(buf: &mut Buf, condition: &'a Loc<Expr<'a>>, _: bool, indent: u16) {

View file

@ -123,8 +123,13 @@ pub struct Item<'a> {
}
impl<'a> Item<'a> {
fn is_multiline(&self) -> bool {
self.newline || !self.before.is_empty() || self.node.is_multiline()
fn is_multiline(&self, allow_newlines: bool) -> bool {
let has_newlines = if allow_newlines {
!self.before.is_empty()
} else {
self.before.iter().any(|c| c.is_comment())
};
self.newline || has_newlines || self.node.is_multiline()
}
}
@ -336,11 +341,11 @@ impl<'a> Formattable for Node<'a> {
} => after.is_multiline() || items.iter().any(|item| item.is_multiline()),
Node::CommaSequence {
allow_blank_lines: _,
allow_newlines: _,
allow_newlines,
indent_rest: _,
first,
rest,
} => first.is_multiline() || rest.iter().any(|item| item.is_multiline()),
} => first.is_multiline() || rest.iter().any(|item| item.is_multiline(*allow_newlines)),
Node::Literal(_) => false,
Node::TypeAnnotation(type_annotation) => type_annotation.is_multiline(),
Node::Pattern(pat) => pat.is_multiline(),