mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-01 15:51:12 +00:00
72 lines
1.8 KiB
Rust
72 lines
1.8 KiB
Rust
use roc_parse::ast::{Collection, ExtractSpaces};
|
|
|
|
use crate::{
|
|
annotation::{Formattable, Newlines},
|
|
spaces::{fmt_comments_only, NewlineAt, INDENT},
|
|
Buf,
|
|
};
|
|
|
|
pub fn fmt_collection<'a, 'buf, T: ExtractSpaces<'a> + Formattable>(
|
|
buf: &mut Buf<'buf>,
|
|
indent: u16,
|
|
start: char,
|
|
end: char,
|
|
items: Collection<'a, T>,
|
|
newline: Newlines,
|
|
) where
|
|
<T as ExtractSpaces<'a>>::Item: Formattable,
|
|
{
|
|
if items.is_multiline() {
|
|
let braces_indent = indent;
|
|
let item_indent = braces_indent + INDENT;
|
|
if newline == Newlines::Yes {
|
|
buf.newline();
|
|
}
|
|
buf.indent(braces_indent);
|
|
buf.push(start);
|
|
|
|
for item in items.iter() {
|
|
let item = item.extract_spaces();
|
|
|
|
buf.newline();
|
|
if !item.before.is_empty() {
|
|
fmt_comments_only(buf, item.before.iter(), NewlineAt::Bottom, item_indent);
|
|
}
|
|
|
|
item.item.format(buf, item_indent);
|
|
|
|
buf.push(',');
|
|
|
|
if !item.after.is_empty() {
|
|
fmt_comments_only(buf, item.after.iter(), NewlineAt::Top, item_indent);
|
|
}
|
|
}
|
|
fmt_comments_only(
|
|
buf,
|
|
items.final_comments().iter(),
|
|
NewlineAt::Top,
|
|
item_indent,
|
|
);
|
|
buf.newline();
|
|
buf.indent(braces_indent);
|
|
} else {
|
|
// is_multiline == false
|
|
// there is no comment to add
|
|
buf.indent(indent);
|
|
buf.push(start);
|
|
let mut iter = items.iter().peekable();
|
|
while let Some(item) = iter.next() {
|
|
buf.spaces(1);
|
|
item.format(buf, indent);
|
|
if iter.peek().is_some() {
|
|
buf.push(',');
|
|
}
|
|
}
|
|
|
|
if !items.is_empty() {
|
|
buf.spaces(1);
|
|
}
|
|
}
|
|
|
|
buf.push(end);
|
|
}
|