djlint / djLint

✨ HTML Template Linter and Formatter. Django - Jinja - Nunjucks - Handlebars - GoLang
https://djLint.com
GNU General Public License v3.0
664 stars 81 forks source link

[BUG] [Formatter] {% set var_name = { ... } %} multiline object property indentation don't seem to work properly #836

Open asolopovas opened 3 months ago

asolopovas commented 3 months ago

System Info

Issue

Is it normal to have closing } %} be hanging in the middle of the line?

{#  16 ... #}   <a class="{{ itemLinkClass | default("block-link") }}"
                   href="{{ item.link }}"
                   title="Link to {{ item.title }}">
                   {#  Property Indentation from parent   24 ...    #}
                    {% set image_params = {
                                            image: item.image,
                                            imageClass: imageClass,
                                            caption: settings.pictureTag ? item.title : false,
                                            captionClass: captionClass,
                                            lazy: loop.first ? settings.firstLazy : true
                                        } %}
                    {% include "image-block.twig" with  image_params %}
                </a>

isn't it looking better this way?

{#  16 ... #}   <a class="{{ itemLinkClass | default("block-link") }}"
                   href="{{ item.link }}"
                   title="Link to {{ item.title }}">
                    {# Property Indentation from parent 4 ... #}
                    {% set image_params = {
                        image: item.image,
                        imageClass: imageClass,
                        caption: settings.pictureTag ? item.title : false,
                        captionClass: captionClass,
                        lazy: loop.first ? settings.firstLazy : true
                    } %}
                    {% include "image-block.twig" with  image_params %}
                </a>

Please note that I indented the anchor tag <a specifically by 16 spaces to demonstrate that spacing added to indent properties is not event depending on how far the parent is indented, further indentation of parent nesting, results in further indentation of all of the object. In first example above object properties got indented by 24 spaces from line above {% set.

If tag is located at root indentation like bellow:

<a class="{{ itemLinkClass | default("block-link") }}"
   href="{{ item.link }}"
   title="Link to {{ item.title }}">
    {# Property Indentation from Parent 8 ... #}
    {# Closing Tag %} Indentation 8 ... #}
    {% set image_params = {
            image: item.image,
            imageClass: imageClass,
            caption: settings.pictureTag ? item.title : false,
            captionClass: captionClass,
            lazy: loop.first ? settings.firstLazy : true
         } %}
    {% include "image-block.twig" with  image_params %}
</a>

it indents object properties by 8 spaces instead of required indentation of 4

<a class="{{ itemLinkClass | default("block-link") }}"
   href="{{ item.link }}"
   title="Link to {{ item.title }}">
   {# Property Indentation from Parent 4 ... #}
   {# Closing Tag %} Indentation 0 ... #}
    {% set image_params = {
        image: item.image,
        imageClass: imageClass,
        caption: settings.pictureTag ? item.title : false,
        captionClass: captionClass,
        lazy: loop.first ? settings.firstLazy : true
    } %}
    {% include "image-block.twig" with  image_params %}
</a>
anentropic commented 2 months ago

I have some examples like:

        {% set item_kwargs = {
                  "label": label,
                  "disabled": item.disabled,
                  "selected": item.selected,
                  "two_line": two_line,
                  "callback": callback,
                  "item_attrs": item_attrs,
                  "first_unselected": (_parent and loop.first and none_selected)
                } %}
        {% if _checkboxes_name %}
          {% do item_kwargs.update({
          "checkbox_kwargs": {
          "name": _checkboxes_name,
          "value": item.value,
          "id": _checkboxes_name + "--id-" + loop.index|str,
          }
          }) %}

the indentation djlint is enforcing here seems quick wacky and inconsistent