lassik / emacs-format-all-the-code

Auto-format source code in many languages with one command
https://melpa.org/#/format-all
MIT License
604 stars 105 forks source link

Help with setting the default formatter + it's options #239

Closed o0nd7ots closed 10 months ago

o0nd7ots commented 11 months ago

Hello,

I would like to set shfmt as my default shell formatter. I want it to format with four spaces indent and do not have an ~/.editorconfig file.

I tried doing this:

 (setq format-all-formatters
          (list
                                 '(sh-mode   "shfmt -i 4 -ci")
                                 '(bash-mode "shfmt -i 4 -ci")
                                 )
          )

but it does not seem to work.

With the .editorconfig it works but I want to keep the config local within emacs.

Can anyone help me?

lassik commented 10 months ago

Try this: (setq format-all-formatters '(("Shell" (shfmt "-i" "4" "-ci"))))

o0nd7ots commented 10 months ago

I did what you suggested:

  (use-package format-all
    :diminish
    :hook (prog-mode . format-all-mode)
    :config
    (setq format-all-formatters '(("Shell" (shfmt "-i" "4" "-ci"))))
    )

also running describe-variable format-all-formatters I get

format-all-formatters is a variable defined in ‘format-all.el’.

Its value is (("Shell" shfmt))
Local in buffer count-chars.sh; global value is nil

So I changed the setq to setq-default. Now it works fine.

Do you know why normal setq works that way?

lassik commented 10 months ago

Ah. setq must have set the buffer-local value.I'm not familiar with use-package, so I don't know which buffer that would be.

o0nd7ots commented 10 months ago

I am not an expert of use-package usage, but from what I gather :config contains the code that gets executed after the package gets loaded. I have swapped it to :init which executes the code before (regardless of if) the package gets loaded and with setq I get the same results as with :config and setq-local.

I do not know which way of doing it is better. On one hand :config deffers the code execution, so the memory is cleaner. On the other I stray from using setq-default for unknown reasons.

Could you weigh in on what solution might be better?

For use-package my sources are this use-package github issue and this emacs stackexchange thread.

Flinner commented 10 months ago
  (use-package format-all
    ;; :commands (format-all-mode)
    :defer
    :config
     (setq my/format-all-formatters '(("Verilog" verible)))
    :hook (prog-mode . format-all-mode)
          ; (format-all-mode . format-all-ensure-formatter)
          (format-all-mode . (lambda () (setq format-all-formatters my/format-all-formatters))))

I hope this example makes it clear. only change my/format-all-formatters list.

o0nd7ots commented 10 months ago

I understand what you did there but I do not understand why. What are the benefits of this?

Flinner commented 10 months ago

I haven't noticed your use of setq-default, so my answer is useless, sorry

o0nd7ots commented 10 months ago

No problem. Either way thanks for weighing in!

o0nd7ots commented 10 months ago

Since I learned the basic syntax but I still have some problems I will be moving this to another issue.