Skip to content

Commit

Permalink
Merge pull request #83 from sile/non-tagged-tuples
Browse files Browse the repository at this point in the history
Don't treat as a tagged tuple if there are newlines just after the first atom element
  • Loading branch information
sile authored Sep 28, 2022
2 parents a0298fb + f09c597 commit 3f0ddad
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
6 changes: 6 additions & 0 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ impl<T: Format> Format for Box<T> {
}
}

impl<T: Format> Format for &T {
fn format(&self, fmt: &mut Formatter) {
(**self).format(fmt);
}
}

impl<A: Format, B: Format> Format for (A, B) {
fn format(&self, fmt: &mut Formatter) {
self.0.format(fmt);
Expand Down
51 changes: 40 additions & 11 deletions src/items/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,18 +211,29 @@ impl<T: Format, D: Format> Format for NonEmptyItems<T, D> {

impl<T: Format, D: Format> NonEmptyItems<T, D> {
fn format_items(&self, fmt: &mut Formatter) {
fmt.with_scoped_indent(|fmt| {
fmt.set_indent(fmt.column());
format_non_empty_items(fmt, self.items().iter(), self.delimiters.iter());
}
}

fn format_non_empty_items<T, D>(
fmt: &mut Formatter,
mut items: impl Iterator<Item = T>,
delimiters: impl Iterator<Item = D>,
) where
T: Format,
D: Format,
{
fmt.with_scoped_indent(|fmt| {
fmt.set_indent(fmt.column());

let item = self.items().first().expect("unreachable");
let item = items.next().expect("unreachable");
item.format(fmt);
for (item, delimiter) in items.zip(delimiters) {
delimiter.format(fmt);
fmt.write_newline();
item.format(fmt);
for (item, delimiter) in self.items.iter().skip(1).zip(self.delimiters.iter()) {
delimiter.format(fmt);
fmt.write_newline();
item.format(fmt);
}
});
}
}
});
}

#[derive(Debug, Clone, Span, Parse, Format)]
Expand Down Expand Up @@ -253,6 +264,10 @@ impl<T, D> MaybePackedItems<T, D> {
pub(crate) fn items(&self) -> &[T] {
self.0.items()
}

fn delimiters(&self) -> &[D] {
self.0.delimiters()
}
}

impl<T: Format, D: Format> MaybePackedItems<T, D> {
Expand Down Expand Up @@ -366,13 +381,27 @@ impl<T: Element + Format> Format for TupleLike<T> {
fn format(&self, fmt: &mut Formatter) {
self.open.format(fmt);
if let Some(tag) = self.tag.get() {
if fmt.has_newline_until(&self.items) {
// Not a tagged tuple
let items_indent = fmt.column();
format_non_empty_items(
fmt,
std::iter::once(Either::A(&tag.0))
.chain(self.items.items().iter().map(Either::B)),
std::iter::once(Either::A(&tag.1))
.chain(self.items.delimiters().iter().map(Either::B)),
);
fmt.set_next_comment_indent(items_indent);
self.close.format(fmt);
return;
}

tag.format(fmt);
fmt.write_space();
}

let items_indent = fmt.column();
self.items.format(fmt);

fmt.set_next_comment_indent(items_indent);
self.close.format(fmt);
}
Expand Down
5 changes: 5 additions & 0 deletions src/items/expressions/tuples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ mod tests {
{3, 4, 5},
6,
{7, 8, 9}}"},
indoc::indoc! {"
{error,
{Foo, Bar,
Baz},
qux}"},
];
for text in texts {
crate::assert_format!(text, Expr);
Expand Down

0 comments on commit 3f0ddad

Please sign in to comment.