2022-03-28 15:03:21 +02:00
|
|
|
#+title: :editor format
|
|
|
|
#+subtitle: Standardize your ugly code
|
|
|
|
#+created: July 26, 2020
|
|
|
|
#+since: 21.12.0
|
2020-07-25 23:24:38 -04:00
|
|
|
|
2021-10-16 01:22:41 +02:00
|
|
|
* Description :unfold:
|
2022-11-19 16:43:31 +00:00
|
|
|
Code style is something that's hotly debated since the beginning of time.
|
2020-07-25 23:24:38 -04:00
|
|
|
|
2022-11-19 16:43:31 +00:00
|
|
|
Tabs or spaces?
|
|
|
|
2-width or 4-width indentation?
|
|
|
|
|
|
|
|
Which is right? Doom doesn't care, but we will try and make it easy for you to
|
|
|
|
format code within the safety of Emacs.
|
|
|
|
|
|
|
|
At present, the module wraps [[https://github.com/radian-software/apheleia/][apheleia]], which includes some more detail on the
|
|
|
|
internals of the package; but the long and short of it is on-save your code will
|
|
|
|
be formatted and returned to the buffer using
|
|
|
|
[[https://tools.ietf.org/doc/tcllib/html/rcs.html#section4][RCS patching]].
|
2020-07-25 23:24:38 -04:00
|
|
|
|
|
|
|
** Maintainers
|
2022-11-19 16:43:31 +00:00
|
|
|
- [[doom-user:][@elken]]
|
|
|
|
|
|
|
|
[[doom-contrib-maintainer:][Become a maintainer?]]
|
2020-07-25 23:24:38 -04:00
|
|
|
|
2021-10-16 01:22:41 +02:00
|
|
|
** Module flags
|
|
|
|
- +onsave ::
|
|
|
|
Enable reformatting of a buffer when it is saved. See
|
2022-11-19 16:43:31 +00:00
|
|
|
[[var:+format-on-save-disabled-modes]] to control what major modes to (or not to)
|
2020-07-25 23:24:38 -04:00
|
|
|
format on save.
|
|
|
|
|
2021-10-16 01:22:41 +02:00
|
|
|
** Packages
|
2022-11-19 16:43:31 +00:00
|
|
|
- [[doom-package:apheleia]]
|
2020-07-25 23:24:38 -04:00
|
|
|
|
|
|
|
** Hacks
|
2022-11-19 16:43:31 +00:00
|
|
|
As of writing this, apheleia doesn't /yet/ support regions or similar kinds of
|
|
|
|
buffers, so there are a couple of hacks to attempt to rectify this.
|
2020-07-25 23:24:38 -04:00
|
|
|
|
2023-09-15 16:18:27 +01:00
|
|
|
For the most part, things should work as expected. However, because the
|
|
|
|
formatting occurs on an isolated version of the buffer; lisp/scheme or similarly
|
|
|
|
indentation-based languages may produce poor results.
|
|
|
|
|
2021-10-16 01:22:41 +02:00
|
|
|
** TODO Changelog
|
|
|
|
# This section will be machine generated. Don't edit it by hand.
|
|
|
|
/This module does not have a changelog yet./
|
|
|
|
|
|
|
|
* Installation
|
|
|
|
[[id:01cffea4-3329-45e2-a892-95a384ab2338][Enable this module in your ~doom!~ block.]]
|
|
|
|
|
|
|
|
This module has no direct requirements, but each language will need one of their
|
|
|
|
supported formatter programs in order for this to work. In their absence,
|
2022-11-19 16:43:31 +00:00
|
|
|
[[doom-package:apheleia]] will fail silently.
|
2021-10-16 01:22:41 +02:00
|
|
|
|
2022-11-19 16:43:31 +00:00
|
|
|
To see if a particular mode has a configured formatter, check for the mode in
|
|
|
|
[[var:apheleia-mode-alist]] which corresponds to the list of formatters defined in
|
|
|
|
[[var:apheleia-formatters]]
|
|
|
|
|
|
|
|
* Usage
|
|
|
|
** With +onsave
|
|
|
|
When this flag is enabled, you shouldn't need to do anything other than write
|
|
|
|
code and save it.
|
|
|
|
|
|
|
|
** Without +onsave
|
|
|
|
Without the flag, formatting will only occur when either =apheleia-format-buffer=
|
|
|
|
or =+format/buffer= is called.
|
2021-10-16 01:22:41 +02:00
|
|
|
|
2022-11-19 16:43:31 +00:00
|
|
|
* Configuration
|
|
|
|
|
|
|
|
Detailed configuration can be found [[https://github.com/radian-software/apheleia/#user-guide][upstream]], but for most purposes here we
|
|
|
|
provide a simple macro that looks like the below:
|
2020-07-25 23:24:38 -04:00
|
|
|
|
2021-10-16 01:22:41 +02:00
|
|
|
#+begin_src emacs-lisp
|
2022-11-19 16:43:31 +00:00
|
|
|
(set-formatter! 'unique-name '("command" "line" "here") :modes '(name-of-major-mode))
|
2021-10-16 01:22:41 +02:00
|
|
|
#+end_src
|
2020-07-25 23:24:38 -04:00
|
|
|
|
2023-09-14 16:44:43 +01:00
|
|
|
If you're trying to override a formatter that has previously been defined by
|
|
|
|
Doom, you will need to ensure that the call in your config is contained within
|
|
|
|
an =after!= form, eg below to override Clojure's with =zprint=:
|
|
|
|
|
|
|
|
#+begin_src emacs-lisp
|
|
|
|
(after! clojure-mode
|
|
|
|
(set-formatter! 'zprint '("zprint" "-") :modes '(clojure-mode)))
|
|
|
|
#+end_src
|
|
|
|
|
2022-11-19 16:43:31 +00:00
|
|
|
There are a few bonus symbols that apheleia uses (for example =npx= will be
|
|
|
|
replaced by a correct path to npx) which are all documented in the link above.
|
|
|
|
|
|
|
|
** Disabling formatters
|
|
|
|
*** Permanently
|
|
|
|
To permanently disable a particular formatter with no provided alternative
|
|
|
|
|
|
|
|
#+begin_src emacs-lisp
|
|
|
|
(setq apheleia-formatters (delq (assoc 'csharpier apheleia-formatters) apheleia-formatters))
|
|
|
|
#+end_src
|
|
|
|
|
|
|
|
*** Per-buffer
|
|
|
|
If you want to save without formatting, this is done by first passing the
|
|
|
|
universal argument thus; =SPC u SPC f s= for evil users, =C-u C-x C-s= for non-evil
|
|
|
|
users.
|
|
|
|
|
|
|
|
If you want to save more than a handful of time, you can set
|
|
|
|
[[var:apheleia-inhibit]] to disable even if =apheleia-global-mode= is on.
|
|
|
|
|
|
|
|
*** Onsave only
|
|
|
|
This behaviour is controlled via [[var:+format-on-save-disabled-modes]] thus;
|
|
|
|
|
2021-10-16 01:22:41 +02:00
|
|
|
#+begin_src emacs-lisp
|
2022-11-19 16:43:31 +00:00
|
|
|
(setq +format-on-save-disabled-modes
|
|
|
|
'(emacs-lisp-mode ; elisp's mechanisms are good enough
|
|
|
|
sql-mode ; sqlformat is currently broken
|
|
|
|
tex-mode ; latexindent is broken
|
|
|
|
latex-mode))
|
2021-10-16 01:22:41 +02:00
|
|
|
#+end_src
|
2020-07-25 23:24:38 -04:00
|
|
|
|
2022-11-19 16:43:31 +00:00
|
|
|
In this case, =emacs-lisp-mode=, =sql-mode=, =tex-mode= and =latex-mode= will not be
|
|
|
|
formatted on save, but can still be formatted by manually invoking the commands
|
|
|
|
=apheleia-format-buffer= or =+format/buffer=.
|
|
|
|
|
2020-07-25 23:24:38 -04:00
|
|
|
** Disabling the LSP formatter
|
|
|
|
If you are in a buffer with ~lsp-mode~ enabled and a server that supports
|
2022-09-26 02:19:42 +08:00
|
|
|
=textDocument/formatting=, it will be used instead of [[doom-package:format-all]]'s formatter.
|
2020-07-25 23:24:38 -04:00
|
|
|
|
|
|
|
+ To disable this behavior universally use: ~(setq +format-with-lsp nil)~
|
2021-10-16 01:22:41 +02:00
|
|
|
+ To disable this behavior in one mode: ~(setq-hook! 'python-mode-hook
|
|
|
|
+format-with-lsp nil)~
|
2020-07-25 23:24:38 -04:00
|
|
|
|
2022-11-19 16:43:31 +00:00
|
|
|
** Selecting a specific formatter for a particular buffer
|
2020-07-25 23:24:38 -04:00
|
|
|
Set the buffer-local variable ~+format-with~ to the name of the formatter to
|
|
|
|
use. e.g.
|
2021-10-16 01:22:41 +02:00
|
|
|
#+begin_src emacs-lisp
|
2022-11-19 16:43:31 +00:00
|
|
|
;; Overrides `apheleia-mode-alist`
|
2020-07-25 23:24:38 -04:00
|
|
|
(setq-hook! 'python-mode-hook +format-with 'html-tidy)
|
|
|
|
|
2022-11-19 16:43:31 +00:00
|
|
|
;; Or set it to `nil' to fallback to `apheleia-mode-alist`
|
|
|
|
(setq-hook! 'python-mode-hook +format-with nil)
|
2021-10-16 01:22:41 +02:00
|
|
|
#+end_src
|
2020-07-25 23:24:38 -04:00
|
|
|
|
|
|
|
Formatters are referred to by the name they were defined with. They can be
|
2022-11-19 16:43:31 +00:00
|
|
|
looked up in the ~apheleia-mode-alist~ hash table.
|
2020-07-25 23:24:38 -04:00
|
|
|
|
|
|
|
* Troubleshooting
|
2022-11-19 16:43:31 +00:00
|
|
|
There are a few fail-safes apheleia has to prevent accidental code wipe,
|
|
|
|
included silently failing if the command errors or doesn't exist.
|
|
|
|
|
|
|
|
Check that the command you've specified runs fine in a terminal first before
|
|
|
|
reporting this as an issue.
|
|
|
|
|
|
|
|
If any errors are reported from the command, run =apheleia-goto-error= to jump to
|
|
|
|
the error buffer and handle any problems raised there.
|
|
|
|
|
|
|
|
Any issues specific to apheleia should most often be reported upstream [[https://github.com/radian-software/apheleia/issues][here]].
|
2021-10-16 01:22:41 +02:00
|
|
|
|
|
|
|
* Frequently asked questions
|
|
|
|
/This module has no FAQs yet./ [[doom-suggest-faq:][Ask one?]]
|
|
|
|
|
|
|
|
* TODO Appendix
|
|
|
|
#+begin_quote
|
|
|
|
🔨 This module has no appendix yet. [[doom-contrib-module:][Write one?]]
|
|
|
|
#+end_quote
|