Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't treat as a tagged tuple if there are newlines just after the first atom element #83

Merged
merged 1 commit into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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