Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] [Formatter] #566

Closed
3 tasks done
nrnvgh opened this issue Feb 27, 2023 · 7 comments
Closed
3 tasks done

[BUG] [Formatter] #566

nrnvgh opened this issue Feb 27, 2023 · 7 comments
Labels

Comments

@nrnvgh
Copy link

nrnvgh commented Feb 27, 2023

System Info

  • OS: OSX 13.2.1
  • Python Version 3.10.2
  • djLint Version 1.19.15
  • template language: jinja

Issue

Can't stop {% for %}...{% endfor %} block from being collapsed down to a single line.

Slightly longer description

I'm attempting to use jinja2 to output YAML. I've inherited a stack of .j2 files with zero indentation for the {% %} control blocks and djlint seemed like a good tool to use to try and fix this. Because it's YAML, whitespace needs to be preserved. You may be saying to yourself "Yes, but there's a config parameter for that", however see below.

How To Reproduce

Here's my current .djlintrc:

{
    "profile": "jinja",
    "preserve_leading_space": true
}

Using preserve_leading_space is certainly acres better than not using it, however given this jinja snippet:

- parent:
    child:
      var1: {{ some_var }}
      var2: {{ another_var }}
{% if some_var > 1 %}
{% if another_var > 10 %}
{% if IGNORE is defined %}
      config:
        set:
          ignore:
{% for ignored_item in IGNORE %}
            {{ ignored_item }}: {}
{% endfor %}
{% endif %}
{% endif %}
{% endif %}

...which will render this YAML:

- parent:
    child:
      var1: 3
      var2: 12
      config:
        set:
          ignore:
            foo: {}
            bar: {}
            baz: {}

...djlint --reformat outputs this:

- parent:
    child:
      var1: {{ some_var }}
      var2: {{ another_var }}
{% if some_var > 1 %}
    {% if another_var > 10 %}
        {% if IGNORE is defined %}
      config:
        set:
          ignore:
            {% for ignored_item in IGNORE %}{{ ignored_item }}: {}{% endfor %}
        {% endif %}
    {% endif %}
{% endif %}

...which outputs this (obvs invalid) YAML:

- parent:
    child:
      var1: 3
      var2: 12
      config:
        set:
          ignore:
foo: {}bar: {}baz: {}

I did try the beast_mode branch just for grins, and that's...much worse. =)

@nrnvgh nrnvgh added 🦠 bug Something isn't working 🧽 formatter labels Feb 27, 2023
@christopherpickering
Copy link
Contributor

I did try the beast_mode branch just for grins, and that's...much worse. =)
😀 you are welcome to help make that branch ready to go 😀

@christopherpickering
Copy link
Contributor

@nrnvgh I'm thinking what you need to do is set the max line length to 0 to force everything to wrap.

Here's a pic from the demo https://djlint.com/demo/:

image

Does that come how you need?

@nrnvgh
Copy link
Author

nrnvgh commented Feb 28, 2023

Thanks for the quick response. The problem with your proposed solution is that the lines which reads...

{{ ignored_item }}: {}

...is brought back to the start of the line; the rendered YAML needs to be:

- parent:
    child:
      var1: my_var_1
      var2: my_var_2
      config:
        set:
          ignore:
            item1_to_ignore: {}
            item2_to_ignore: {}
            itemN_to_ignore: {}

...but with max_line_length set to 0, I get:

- parent:
    child:
      var1: my_var_1
      var2: my_var_2
      config:
        set:
          ignore:
item1_to_ignore: {}
item2_to_ignore: {}
itemN_to_ignore: {}

Similarly (again, with max_line_length at 0) djlint will reformat this:

      parent:
        mgmt:
          server:
{% for server in entry.servers %}
            {{ server.ip }}:
              port: {{ server.port }}
{% endfor %}

...to this:

parent:
        mgmt:
          server:
{% for server in entry.servers %}
{{ server.ip }}:
              port: {{ server.port }}
{% endfor %}

Which has both incorrectly tweaked indentation levels between parent and mgmt and the pulled-back representation for {{ server.ip }}

@christopherpickering
Copy link
Contributor

ah I see, the leading space is not preserved on template syntax. That might be the best fix for this case - changing djlint to also keep indentation on lines that start w/ a template tag... if that is possible. I'll have to check out the code closely on this one :)

@christopherpickering
Copy link
Contributor

@nrnvgh sorry for the delay on this. I should have an update out soon that will treat {{ }} tags as text and keep the leading spaces intact.

you should be able to use a config like

{
    "profile": "jinja",
    "preserve_leading_space": true
}

christopherpickering pushed a commit that referenced this issue Apr 5, 2023
## [1.19.17](v1.19.16...v1.19.17) (2023-04-05)

### Bug Fixes

* **blocktrans:** allowed closing transblocks to be indented if they have a leading space ([d667273](d667273))
* **custom tags:** fixed issue where end tag for custom block was not de-indented ([fb8bf5e](fb8bf5e)), closes [#572](#572)
* **ignored code:** fixed formatting of ignore code inside django comment blocks ([120460d](120460d)), closes [#569](#569)
* **linter:** linter will now ignore {% raw %} {% endraw %} blocks ([2a6865e](2a6865e)), closes [#539](#539)
* **max line length:** fixes issues around max line length not fully respected on indented lines ([1ec6b29](1ec6b29)), closes [#580](#580)
* **preserving space:** misc improvements for the preserve leading space option ([03ee6f0](03ee6f0)), closes [#566](#566)
@christopherpickering
Copy link
Contributor

🎉 This issue has been resolved in version 1.19.17 🎉

The release is available on:

Your semantic-release bot 📦🚀

@nrnvgh
Copy link
Author

nrnvgh commented Apr 5, 2023

Preliminary testing looks good; I'll know more in a few days when I have a chance to use it against some gnarlier templates. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants