Skip to content

Commit

Permalink
remove duplicated space when max_attrs_per_line set
Browse files Browse the repository at this point in the history
in self-closing tag
fix #2
  • Loading branch information
g-plane committed Nov 10, 2023
1 parent 604d8f5 commit 759d1fe
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 16 deletions.
33 changes: 19 additions & 14 deletions markup_fmt/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,26 @@ impl<'s> DocGen<'s> for Element<'s> {
}));

let attrs = if let Some(max) = ctx.options.max_attrs_per_line {
Doc::line_or_space()
.concat(itertools::intersperse(
self.attrs.chunks(max.into()).map(|chunk| {
Doc::list(
itertools::intersperse(
chunk.iter().map(|attr| attr.doc(ctx)),
Doc::line_or_space(),
)
.collect(),
// fix #2
if self.attrs.is_empty() {
Doc::line_or_nil()
} else {
Doc::line_or_space()
}
.concat(itertools::intersperse(
self.attrs.chunks(max.into()).map(|chunk| {
Doc::list(
itertools::intersperse(
chunk.iter().map(|attr| attr.doc(ctx)),
Doc::line_or_space(),
)
.group()
}),
Doc::hard_line(),
))
.nest_with_ctx(ctx)
.collect(),
)
.group()
}),
Doc::hard_line(),
))
.nest_with_ctx(ctx)
} else {
Doc::list(
self.attrs
Expand Down
4 changes: 4 additions & 0 deletions markup_fmt/tests/fmt/html/self-closing/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@

[mathml-disabled]
"mathml.selfClosing" = false

[with-max-attrs-per-line]
"html.normal.selfClosing" = true
maxAttrsPerLine = 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: markup_fmt/tests/fmt.rs
---
<div />
<div />

<input>
<input />

<circle></circle>
<circle />

<abs></abs>
<abs />

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
source: markup_fmt/tests/fmt.rs
---
<template >
<template>
<div data-a="1">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
source: markup_fmt/tests/fmt.rs
---
<template >
<template>
<div data-a="1">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</div>
Expand Down

0 comments on commit 759d1fe

Please sign in to comment.