mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-02 16:21:11 +00:00
format multline litera list with trailing comma
This commit is contained in:
parent
83aa5c1642
commit
3d0a5aa89a
1 changed files with 54 additions and 64 deletions
|
@ -402,94 +402,84 @@ fn fmt_bin_op<'a>(
|
||||||
pub fn fmt_list<'a>(
|
pub fn fmt_list<'a>(
|
||||||
buf: &mut String<'a>,
|
buf: &mut String<'a>,
|
||||||
loc_items: &[&Located<Expr<'a>>],
|
loc_items: &[&Located<Expr<'a>>],
|
||||||
_final_comments: &'a [CommentOrNewline<'a>],
|
final_comments: &'a [CommentOrNewline<'a>],
|
||||||
indent: u16,
|
indent: u16,
|
||||||
) {
|
) {
|
||||||
buf.push('[');
|
if loc_items.is_empty() && final_comments.iter().all(|c| c.is_newline()) {
|
||||||
|
buf.push_str("[]");
|
||||||
let mut iter = loc_items.iter().peekable();
|
|
||||||
|
|
||||||
let is_multiline = loc_items.iter().any(|item| (&item.value).is_multiline());
|
|
||||||
|
|
||||||
let item_indent = if is_multiline {
|
|
||||||
indent + INDENT
|
|
||||||
} else {
|
} else {
|
||||||
indent
|
buf.push('[');
|
||||||
};
|
let is_multiline = loc_items.iter().any(|item| (&item.value).is_multiline());
|
||||||
|
|
||||||
while let Some(item) = iter.next() {
|
|
||||||
if is_multiline {
|
if is_multiline {
|
||||||
match &item.value {
|
let item_indent = indent + INDENT;
|
||||||
Expr::SpaceBefore(expr_below, spaces_above_expr) => {
|
for item in loc_items.iter() {
|
||||||
newline(buf, item_indent);
|
match &item.value {
|
||||||
fmt_comments_only(
|
// TODO?? These SpaceAfter/SpaceBefore litany seems overcomplicated
|
||||||
buf,
|
// Can we simplify this?
|
||||||
spaces_above_expr.iter(),
|
Expr::SpaceBefore(expr_below, spaces_above_expr) => {
|
||||||
NewlineAt::Bottom,
|
newline(buf, item_indent);
|
||||||
item_indent,
|
fmt_comments_only(
|
||||||
);
|
buf,
|
||||||
|
spaces_above_expr.iter(),
|
||||||
|
NewlineAt::Bottom,
|
||||||
|
item_indent,
|
||||||
|
);
|
||||||
|
|
||||||
match &expr_below {
|
match &expr_below {
|
||||||
Expr::SpaceAfter(expr_above, spaces_below_expr) => {
|
Expr::SpaceAfter(expr_above, spaces_below_expr) => {
|
||||||
expr_above.format(buf, item_indent);
|
expr_above.format(buf, item_indent);
|
||||||
|
|
||||||
if iter.peek().is_some() {
|
|
||||||
buf.push(',');
|
buf.push(',');
|
||||||
}
|
|
||||||
|
|
||||||
fmt_comments_only(
|
fmt_comments_only(
|
||||||
buf,
|
buf,
|
||||||
spaces_below_expr.iter(),
|
spaces_below_expr.iter(),
|
||||||
NewlineAt::Top,
|
NewlineAt::Top,
|
||||||
item_indent,
|
item_indent,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
expr_below.format(buf, item_indent);
|
expr_below.format(buf, item_indent);
|
||||||
if iter.peek().is_some() {
|
|
||||||
buf.push(',');
|
buf.push(',');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Expr::SpaceAfter(sub_expr, spaces) => {
|
Expr::SpaceAfter(sub_expr, spaces) => {
|
||||||
newline(buf, item_indent);
|
newline(buf, item_indent);
|
||||||
|
|
||||||
sub_expr.format(buf, item_indent);
|
sub_expr.format(buf, item_indent);
|
||||||
|
|
||||||
if iter.peek().is_some() {
|
|
||||||
buf.push(',');
|
buf.push(',');
|
||||||
|
|
||||||
|
fmt_comments_only(buf, spaces.iter(), NewlineAt::Top, item_indent);
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt_comments_only(buf, spaces.iter(), NewlineAt::Top, item_indent);
|
_ => {
|
||||||
}
|
newline(buf, item_indent);
|
||||||
|
item.format_with_options(
|
||||||
_ => {
|
buf,
|
||||||
newline(buf, item_indent);
|
Parens::NotNeeded,
|
||||||
item.format_with_options(buf, Parens::NotNeeded, Newlines::Yes, item_indent);
|
Newlines::Yes,
|
||||||
if iter.peek().is_some() {
|
item_indent,
|
||||||
|
);
|
||||||
buf.push(',');
|
buf.push(',');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
newline(buf, indent);
|
||||||
|
buf.push(']');
|
||||||
} else {
|
} else {
|
||||||
buf.push(' ');
|
// is_multiline == false
|
||||||
item.format_with_options(buf, Parens::NotNeeded, Newlines::Yes, item_indent);
|
let mut iter = loc_items.iter().peekable();
|
||||||
if iter.peek().is_some() {
|
while let Some(item) = iter.next() {
|
||||||
buf.push(',');
|
buf.push(' ');
|
||||||
|
item.format_with_options(buf, Parens::NotNeeded, Newlines::Yes, indent);
|
||||||
|
if iter.peek().is_some() {
|
||||||
|
buf.push(',');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
buf.push_str(" ]");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if is_multiline {
|
|
||||||
newline(buf, indent);
|
|
||||||
}
|
|
||||||
|
|
||||||
if !loc_items.is_empty() && !is_multiline {
|
|
||||||
buf.push(' ');
|
|
||||||
}
|
|
||||||
buf.push(']');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn empty_line_before_expr<'a>(expr: &'a Expr<'a>) -> bool {
|
pub fn empty_line_before_expr<'a>(expr: &'a Expr<'a>) -> bool {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue