Skip to content

Commit

Permalink
Don't try to keep all on oneline for Jinja
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownPlatypus committed Aug 14, 2024
1 parent d302d65 commit f0201ea
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
11 changes: 9 additions & 2 deletions markup_fmt/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,15 @@ impl<'s> DocGen<'s> for Element<'s> {
docs.push(Doc::text(formatted_tag_name.clone()));

match self.attrs.as_slice() {
[single_attr] if !is_whitespace_sensitive => {
// Try to avoid breaking on multiple lines for a single attribute.
[single_attr]
if !is_whitespace_sensitive
&& !matches!(
single_attr,
Attribute::JinjaTagOrBlock(..) | Attribute::VentoTagOrBlock(..)
) =>
{
// Avoid breaking on multiple lines for a single attribute in non whitespace sensitive context.
// Skip this for templating Blocks because they usually span across multiple lines.
docs.push(Doc::space());
docs.push(single_attr.doc(ctx, &state));

Expand Down
42 changes: 25 additions & 17 deletions markup_fmt/tests/fmt/jinja/attributes/single-attr.snap
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
---
source: markup_fmt/tests/fmt.rs
---
<tr {% if targeted_fooo and not foo_bar_bar_bar == mykey %}
class="collapse"{% endif %}>
<tr
{% if targeted_fooo and not foo_bar_bar_bar == mykey %}
class="collapse"{% endif %}
>
</tr>

<div {% if not line.fields|length_is:'1' %}
class="field-box{% if field.field.name %}
field-{{ field.field.name }}{% endif %}{%
if not field.is_readonly and field.errors
%}
errors{% endif %}{% if field.field.is_hidden %}
hidden{% endif %}"{% elif field.is_checkbox %}
class="checkbox-row"
{% endif %}>
<div
{% if not line.fields|length_is:'1' %}
class="field-box{% if field.field.name %}
field-{{ field.field.name }}{% endif %}{%
if not field.is_readonly and field.errors
%}
errors{% endif %}{% if field.field.is_hidden %}
hidden{% endif %}"{% elif field.is_checkbox %}
class="checkbox-row"
{% endif %}
>
</div>

<picture {% for key, val in attributes.items %}
{{ key }}="{{ val }}"
{% endfor %}>
<picture
{% for key, val in attributes.items %}
{{ key }}="{{ val }}"
{% endfor %}
>
</picture>

<div {% if field.field.causes_red_card %}data-red-card{%
elif field.field.causes_yellow_card
%}data-yellow-card{% endif %}>
<div
{% if field.field.causes_red_card %}data-red-card{%
elif field.field.causes_yellow_card
%}data-yellow-card{% endif %}
>
</div>

0 comments on commit f0201ea

Please sign in to comment.