Skip to content

Commit

Permalink
fix formatting comments
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Nov 3, 2023
1 parent 1e31b28 commit 11e86bb
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 21 deletions.
34 changes: 13 additions & 21 deletions markup_fmt/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl<'s> DocGen<'s> for Comment<'s> {
Doc::text("<!--")
.append(
Doc::line_or_space()
.concat(reflow(self.raw.trim()))
.concat(reflow_with_indent(self.raw.trim()))
.nest_with_ctx(ctx),
)
.append(Doc::line_or_space())
Expand Down Expand Up @@ -223,7 +223,7 @@ impl<'s> DocGen<'s> for Element<'s> {
})
.unwrap_or("js"),
);
let doc = Doc::hard_line().concat(reflow_raw_with_indent(formatted.trim()));
let doc = Doc::hard_line().concat(reflow_with_indent(formatted.trim()));
docs.push(
if ctx.script_indent() {
doc.nest_with_ctx(ctx)
Expand All @@ -249,7 +249,7 @@ impl<'s> DocGen<'s> for Element<'s> {
})
.unwrap_or("css"),
);
let doc = Doc::hard_line().concat(reflow_raw_with_indent(formatted.trim()));
let doc = Doc::hard_line().concat(reflow_with_indent(formatted.trim()));
docs.push(
if ctx.style_indent() {
doc.nest_with_ctx(ctx)
Expand Down Expand Up @@ -364,7 +364,7 @@ impl<'s> DocGen<'s> for JinjaComment<'s> {
Doc::text("{#")
.append(
Doc::line_or_space()
.concat(reflow(self.raw.trim()))
.concat(reflow_with_indent(self.raw.trim()))
.nest_with_ctx(ctx),
)
.append(Doc::line_or_space())
Expand Down Expand Up @@ -440,12 +440,12 @@ impl<'s> DocGen<'s> for NativeAttribute<'s> {
&& self.name == formatted_expr =>
{
Doc::text("{")
.concat(reflow_raw_with_indent(&formatted_expr))
.concat(reflow_with_indent(&formatted_expr))
.append(Doc::text("}"))
}
_ => Doc::text(self.name.to_owned())
.append(Doc::text("={"))
.concat(reflow_raw_with_indent(&formatted_expr))
.concat(reflow_with_indent(&formatted_expr))
.append(Doc::text("}")),
};
} else {
Expand Down Expand Up @@ -560,7 +560,7 @@ impl<'s> DocGen<'s> for SvelteAttribute<'s> {
{
let expr_code = ctx.format_expr(self.expr);
let expr = Doc::text("{")
.concat(reflow_raw_with_indent(&expr_code))
.concat(reflow_with_indent(&expr_code))
.append(Doc::text("}"));
if let Some(name) = self.name {
match name.split_once(':') {
Expand Down Expand Up @@ -756,7 +756,7 @@ impl<'s> DocGen<'s> for SvelteInterpolation<'s> {
Doc::text("{")
.append(
Doc::line_or_nil()
.concat(reflow_raw_with_indent(&ctx.format_expr(self.expr)))
.concat(reflow_with_indent(&ctx.format_expr(self.expr)))
.nest_with_ctx(ctx),
)
.append(Doc::line_or_nil())
Expand Down Expand Up @@ -933,7 +933,7 @@ impl<'s> DocGen<'s> for VueInterpolation<'s> {
Doc::text("{{")
.append(
Doc::line_or_space()
.concat(reflow_raw_with_indent(&ctx.format_expr(self.expr)))
.concat(reflow_with_indent(&ctx.format_expr(self.expr)))
.nest_with_ctx(ctx),
)
.append(Doc::line_or_space())
Expand All @@ -942,14 +942,6 @@ impl<'s> DocGen<'s> for VueInterpolation<'s> {
}
}

fn reflow(s: &str) -> impl Iterator<Item = Doc<'static>> + '_ {
itertools::intersperse(
s.split('\n')
.map(|s| Doc::text(s.strip_suffix('\r').unwrap_or(s).to_owned())),
Doc::hard_line(),
)
}

fn reflow_raw(s: &str) -> impl Iterator<Item = Doc<'_>> {
itertools::intersperse(
s.split('\n')
Expand All @@ -958,15 +950,15 @@ fn reflow_raw(s: &str) -> impl Iterator<Item = Doc<'_>> {
)
}

fn reflow_raw_owned(s: &str) -> impl Iterator<Item = Doc<'static>> + '_ {
fn reflow_owned(s: &str) -> impl Iterator<Item = Doc<'static>> + '_ {
itertools::intersperse(
s.split('\n')
.map(|s| Doc::text(s.strip_suffix('\r').unwrap_or(s).to_owned())),
Doc::empty_line(),
)
}

fn reflow_raw_with_indent(s: &str) -> impl Iterator<Item = Doc<'static>> + '_ {
fn reflow_with_indent(s: &str) -> impl Iterator<Item = Doc<'static>> + '_ {
let indent = s
.lines()
.skip(if s.starts_with([' ', '\t']) { 0 } else { 1 })
Expand Down Expand Up @@ -1086,9 +1078,9 @@ fn format_attr_value(
quote
.clone()
.append(if indent {
Doc::list(reflow_raw_with_indent(value).collect())
Doc::list(reflow_with_indent(value).collect())
} else {
Doc::list(reflow_raw_owned(value).collect())
Doc::list(reflow_owned(value).collect())
})
.append(quote)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ source: markup_fmt/tests/fmt.rs
<!--
This is a very very very very very very very very very very very very long comment
-->

<!--
note: commented-out because we no longer use this
<div>
...
</div>
-->
</body>
</html>

6 changes: 6 additions & 0 deletions markup_fmt/tests/fmt/html/format-comments/comments.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,11 @@
<!-- This is a comment -->
<!--This is a very very very very very very very very very very very very long comment-->
<!-- This is a very very very very very very very very very very very very long comment -->

<!-- note: commented-out because we no longer use this
<div>
...
</div>
-->
</body>
</html>
6 changes: 6 additions & 0 deletions markup_fmt/tests/fmt/jinja/comments/fixture.disabled.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ source: markup_fmt/tests/fmt.rs
{# This is a comment #}
{#This is a very very very very very very very very very very very very long comment#}
{# This is a very very very very very very very very very very very very long comment #}

{# note: commented-out template because we no longer use this
{% for user in users %}
...
{% endfor %}
#}
</body>
</html>

7 changes: 7 additions & 0 deletions markup_fmt/tests/fmt/jinja/comments/fixture.enabled.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ source: markup_fmt/tests/fmt.rs
{#
This is a very very very very very very very very very very very very long comment
#}

{#
note: commented-out template because we no longer use this
{% for user in users %}
...
{% endfor %}
#}
</body>
</html>

6 changes: 6 additions & 0 deletions markup_fmt/tests/fmt/jinja/comments/fixture.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,11 @@
{# This is a comment #}
{#This is a very very very very very very very very very very very very long comment#}
{# This is a very very very very very very very very very very very very long comment #}

{# note: commented-out template because we no longer use this
{% for user in users %}
...
{% endfor %}
#}
</body>
</html>

0 comments on commit 11e86bb

Please sign in to comment.