Skip to content

Commit

Permalink
refactor tiny_pretty usage
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Apr 2, 2024
1 parent 12d71eb commit 4324a23
Showing 1 changed file with 38 additions and 54 deletions.
92 changes: 38 additions & 54 deletions markup_fmt/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,13 @@ impl<'s> DocGen<'s> for AstroExpr<'s> {
if let AstroExprChild::Template(nodes) = child {
Some(
Doc::flat_or_break(Doc::nil(), Doc::text("("))
.append(
Doc::line_or_nil()
.append(format_children_without_inserting_linebreak(
nodes,
has_two_more_non_text_children(nodes),
ctx,
))
.nest_with_ctx(ctx),
)
.append(Doc::line_or_nil())
.append(format_children_without_inserting_linebreak(
nodes,
has_two_more_non_text_children(nodes),
ctx,
))
.nest_with_ctx(ctx)
.append(Doc::line_or_nil())
.append(Doc::flat_or_break(Doc::nil(), Doc::text(")")))
.group(),
Expand All @@ -81,22 +79,20 @@ impl<'s> DocGen<'s> for AstroExpr<'s> {
});

let doc = Doc::text("{")
.append(
Doc::line_or_nil()
.concat(
formatted_script
.split(PLACEHOLDER)
.map(|script| {
if script.contains('\n') {
Doc::list(reflow_with_indent(script).collect())
} else {
Doc::text(script.to_string())
}
})
.interleave(templates),
)
.nest_with_ctx(ctx),
.append(Doc::line_or_nil())
.concat(
formatted_script
.split(PLACEHOLDER)
.map(|script| {
if script.contains('\n') {
Doc::list(reflow_with_indent(script).collect())
} else {
Doc::text(script.to_string())
}
})
.interleave(templates),
)
.nest_with_ctx(ctx)
.append(Doc::line_or_nil())
.append(Doc::text("}"));
if script.contains("//") {
Expand Down Expand Up @@ -142,11 +138,9 @@ impl<'s> DocGen<'s> for Comment<'s> {
{
if ctx.options.format_comments {
Doc::text("<!--")
.append(
Doc::line_or_space()
.concat(reflow_with_indent(self.raw.trim()))
.nest_with_ctx(ctx),
)
.append(Doc::line_or_space())
.concat(reflow_with_indent(self.raw.trim()))
.nest_with_ctx(ctx)
.append(Doc::line_or_space())
.append(Doc::text("-->"))
.group()
Expand Down Expand Up @@ -513,11 +507,9 @@ impl<'s> DocGen<'s> for JinjaComment<'s> {
{
if ctx.options.format_comments {
Doc::text("{#")
.append(
Doc::line_or_space()
.concat(reflow_with_indent(self.raw.trim()))
.nest_with_ctx(ctx),
)
.append(Doc::line_or_space())
.concat(reflow_with_indent(self.raw.trim()))
.nest_with_ctx(ctx)
.append(Doc::line_or_space())
.append(Doc::text("#}"))
.group()
Expand All @@ -535,11 +527,9 @@ impl<'s> DocGen<'s> for JinjaInterpolation<'s> {
F: for<'a> FnMut(&Path, &'a str, usize) -> Result<Cow<'a, str>, E>,
{
Doc::text("{{")
.append(
Doc::line_or_space()
.append(Doc::text(self.expr.trim()))
.nest(ctx.indent_width),
)
.append(Doc::line_or_space())
.append(Doc::text(self.expr.trim()))
.nest(ctx.indent_width)
.append(Doc::line_or_space())
.append(Doc::text("}}"))
.group()
Expand Down Expand Up @@ -928,11 +918,9 @@ impl<'s> DocGen<'s> for SvelteInterpolation<'s> {
F: for<'a> FnMut(&Path, &'a str, usize) -> Result<Cow<'a, str>, E>,
{
Doc::text("{")
.append(
Doc::line_or_nil()
.concat(reflow_with_indent(&ctx.format_expr(self.expr)))
.nest_with_ctx(ctx),
)
.append(Doc::line_or_nil())
.concat(reflow_with_indent(&ctx.format_expr(self.expr)))
.nest_with_ctx(ctx)
.append(Doc::line_or_nil())
.append(Doc::text("}"))
.group()
Expand Down Expand Up @@ -999,11 +987,9 @@ impl<'s> DocGen<'s> for VentoComment<'s> {
{
if ctx.options.format_comments {
Doc::text("{{#")
.append(
Doc::line_or_space()
.concat(reflow_with_indent(self.raw.trim()))
.nest_with_ctx(ctx),
)
.append(Doc::line_or_space())
.concat(reflow_with_indent(self.raw.trim()))
.nest_with_ctx(ctx)
.append(Doc::line_or_space())
.append(Doc::text("#}}"))
.group()
Expand Down Expand Up @@ -1309,11 +1295,9 @@ impl<'s> DocGen<'s> for VueInterpolation<'s> {
F: for<'a> FnMut(&Path, &'a str, usize) -> Result<Cow<'a, str>, E>,
{
Doc::text("{{")
.append(
Doc::line_or_space()
.concat(reflow_with_indent(&ctx.format_expr(self.expr)))
.nest_with_ctx(ctx),
)
.append(Doc::line_or_space())
.concat(reflow_with_indent(&ctx.format_expr(self.expr)))
.nest_with_ctx(ctx)
.append(Doc::line_or_space())
.append(Doc::text("}}"))
.group()
Expand Down

0 comments on commit 4324a23

Please sign in to comment.