radian-software / apheleia

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

How to add formatter that always returns positive exit code on changes (e.g. `docformatter) #266

Closed meliache closed 7 months ago

meliache commented 7 months ago

I tried setting up docformatter as a formatter for formatting python docstrings. By default it outputs a diff but with --in-place it fixes the file.

But it didn't work for me because in both modes of operation returns a non-zero error code when it found a style mistake, even if it successfully fixed it with --in-place. Can I somehow suppress the error. I tried adding to apheleia-formatters the element

(docformatter "docformatter" "--in-place" inplace "||" "true")

But this || didn't work due to shell escaping in Emacs and probably make-process doesn't allow for pipes anyway.

Anyway, I my temorary solution was just to write a bash script wrapper that adds docformatter and suppresses errors, but that solution isn't nice...

raxod502 commented 7 months ago

We do this for the phpcs formatter:

https://github.com/radian-software/apheleia/blob/8f512dba3ee2eabae0dbfd8289b89d54c132cb87/scripts/formatters/apheleia-phpcs#L1-L6

If you add a script to that directory then it will be automatically available on Apheleia's $PATH.

The advantage of this approach is that you can identify the specific nonzero exit codes that are to be ignored, but other presumably unexpected errors such as interrupts will not be treated as success.

meliache commented 7 months ago

I had totally missed the scripts directory. So turns out docformatter is really not the only formatter with this behavior and that there's no easy way to solve this purely via apheleia-formatters customization.

That wasn't the main part of the issue, but could we add docformatter then? Maybe I should then just do the PR