Skip to content

Commit

Permalink
Add jinja tests and ignore jinja blocks spanning multiple lines
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownPlatypus committed Feb 3, 2025
1 parent 81ceae9 commit 816e955
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 31 deletions.
14 changes: 6 additions & 8 deletions dprint_plugin/tests/integration/biome/basic.vue.snap
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ function greet(msg: string) {
close();
'
/>
<div
:style="
{
height: '200px',
/* position: relative; */
}
"
>
<div :style="
{
height: '200px',
/* position: relative; */
}
">
</div>
</template>
14 changes: 6 additions & 8 deletions dprint_plugin/tests/integration/dprint_ts/basic.vue.snap
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ function greet(msg: string) {
close();
'
/>
<div
:style="
{
'height': '200px',
/* position: relative; */
}
"
>
<div :style="
{
'height': '200px',
/* position: relative; */
}
">
</div>
</template>
14 changes: 11 additions & 3 deletions markup_fmt/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,17 @@ 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::JinjaBlock(..)
| Attribute::JinjaTag(..)
| 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 Expand Up @@ -515,7 +524,6 @@ impl<'s> DocGen<'s> for Element<'s> {
)
.nest(ctx.indent_width)
};


if self.void_element {
docs.push(attrs);
Expand Down
23 changes: 23 additions & 0 deletions markup_fmt/tests/fmt/jinja/attributes/single-attr.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<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>

<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>
39 changes: 39 additions & 0 deletions markup_fmt/tests/fmt/jinja/attributes/single-attr.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
source: markup_fmt/tests/fmt.rs
---
<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>

<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>
23 changes: 11 additions & 12 deletions markup_fmt/tests/fmt/jinja/attributes/tag-or-block-as-attr.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: markup_fmt/tests/fmt.rs
snapshot_kind: text
---
<div
{% if colored_with_absolute_dates %}
Expand Down Expand Up @@ -33,9 +32,7 @@ snapshot_kind: text
{% endif %}
/>

<details {% if category %}
open
{% endif %}>
<details {% if category %}open{% endif %}>
<summary>Details</summary>
Something small enough to escape casual notice.
</details>
Expand Down Expand Up @@ -144,12 +141,14 @@ snapshot_kind: text
{% endif %}
{% endfor %}

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

0 comments on commit 816e955

Please sign in to comment.