radian-software / apheleia

🌷 Run code formatter on buffer contents without moving point, using RCS patches and dynamic programming.
MIT License
523 stars 74 forks source link

Wrong behavior of `apheleia-format-buffer` for `jinja2-mode` buffers #220

Closed zzantares closed 7 months ago

zzantares commented 9 months ago

Please note that running apheleia-format-buffer in a *.j2 file (for example an Ansible template file) makes the code unreadable, it removes all line breaks and whitespaces from the file, kind of like a minifier, I've disabled it for jinja2-mode, however, IMHO if a formatter isn't defined for some mode then the buffer contents shouldn't be changed at all.

It is possible that this happens with other modes for which there's no formatter defined, but I haven't tested this hypothesis.

raxod502 commented 9 months ago

This doesn't happen for me. If there is no formatter defined for a buffer, then apheleia-format-buffer (or format-on-save) will silently do nothing.

What major mode are you using? Is it possible that it is something extended from yaml-mode, and thus Prettier is being used?

https://github.com/radian-software/apheleia/blob/791346cd3a331b7a6b949c69e7680f8dc389d3a3/apheleia.el#L128-L130

zzantares commented 8 months ago

What major mode are you using?

I'm using Emacs Doom and it uses this http://github.com/paradoxxxzero/jinja2-mode

Is it possible that it is something extended from yaml-mode, and thus Prettier is being used?

You're not too far off, in jinja2-mode evaluating (derived-mode-p 'html-mode) is non-nil, and prettier is being called to format the buffer as HTML.

I'm able to reproduce what I see in Emacs when invoking prettier directly in the terminal as prettier --parser html sample-file.j2 with these contents (I have prettier v2.8.8):

{%
  set zone= [
    {
      'name':"WAN1",
      'member':'port2',
    },
    {
      'name':"WAN2",
      'member':'port3',
    },
    {
      'name':"HUB1",
      'member':'HUB1-VPN1',
    },
    {
      'name':"HUB1",
      'member':'HUB1-VPN2',
    },
    {
      'name':"HUB2",
      'member':'HUB2-VPN1',
    },
    {
      'name':"HUB2",
      'member':'HUB2-VPN2',
    },
  ]
%}

config system global
set hostname {{branch_id}}
end

{# Config SDWAN Zone and Zone Member #}
config system sdwan
    set status enable
    config zone
    {%- set exclude_zone = [] %}
    {%- for item in zone if item.name not in exclude_zone  %} 
        {{ exclude_zone.append(item.name) or ""}}
        edit {{ item.name }}
        next
    {% endfor %}
    end

    config members
    {%- for i in zone %}
      edit {{ loop.index }}
          set interface {{ i.member }}
          set zone {{ i.name }}               
      next

    {% endfor %}
    end

Now, I'm not sure if there's anything that apheleia could do to improve the situation; and I guess we can't really blame prettier on having this behavior if invoked with non-HTML code :thinking:.

raxod502 commented 8 months ago

We could always disable the formatter by default in jinja2-mode, even if it's enabled in html-mode. Try https://github.com/radian-software/apheleia/pull/238.

zzantares commented 7 months ago

Apologies for the late response, I tried the changes in #238 it works great 👍🏼.

Thank you very much for taking the time to look into this!

raxod502 commented 7 months ago

Cool, merged.