2018-08-22 02:15:44 +02:00
|
|
|
;;; editor/format/config.el -*- lexical-binding: t; -*-
|
|
|
|
|
2024-07-06 19:54:55 -04:00
|
|
|
(defcustom +format-on-save-disabled-modes
|
2022-11-19 16:41:16 +00:00
|
|
|
'(sql-mode ; sqlformat is currently broken
|
|
|
|
tex-mode ; latexindent is broken
|
|
|
|
latex-mode
|
|
|
|
org-msg-edit-mode) ; doesn't need a formatter
|
2023-11-16 12:37:46 -05:00
|
|
|
"A list of major modes in which to not reformat the buffer upon saving.
|
2022-11-19 16:41:16 +00:00
|
|
|
|
2024-07-06 19:54:55 -04:00
|
|
|
If it is t, it is disabled in all modes, the same as if the +onsave flag wasn't
|
|
|
|
used at all.
|
|
|
|
If nil, formatting is enabled in all modes."
|
|
|
|
:type '(list symbol))
|
2018-09-18 13:14:39 -04:00
|
|
|
|
2024-07-06 19:54:55 -04:00
|
|
|
(defcustom +format-with-lsp t
|
2020-05-21 01:20:00 -04:00
|
|
|
"If non-nil, format with LSP formatter if it's available.
|
|
|
|
|
2024-07-06 19:54:55 -04:00
|
|
|
LSP formatter is provided by either `lsp-mode' or `eglot'.
|
2022-08-16 08:13:55 +01:00
|
|
|
|
2024-07-06 19:54:55 -04:00
|
|
|
This can be set buffer-locally with `setq-hook!' to disable LSP formatting in
|
|
|
|
select buffers, from a project's .dir-locals.el file, or as a file-local
|
|
|
|
variable."
|
|
|
|
:type 'boolean
|
|
|
|
:safe 'booleanp)
|
2024-02-14 17:44:21 -05:00
|
|
|
|
2024-07-06 19:54:55 -04:00
|
|
|
(defvaralias '+format-with 'apheleia-formatter)
|
2024-07-07 16:31:24 -04:00
|
|
|
(defvaralias '+format-inhibit 'apheleia-inhibit)
|
2024-02-14 17:44:21 -05:00
|
|
|
|
2018-08-22 02:15:44 +02:00
|
|
|
|
|
|
|
;;
|
2019-06-14 11:08:59 +02:00
|
|
|
;;; Bootstrap
|
2018-08-22 02:15:44 +02:00
|
|
|
|
2024-07-06 19:54:55 -04:00
|
|
|
(use-package! apheleia
|
|
|
|
:defer t
|
|
|
|
:init
|
|
|
|
(when (modulep! +onsave)
|
|
|
|
(add-hook 'doom-first-file-hook #'apheleia-global-mode)
|
|
|
|
;; apheleia autoloads `apheleia-inhibit-functions' so it will be immediately
|
|
|
|
;; available to mutate early.
|
|
|
|
(add-hook! 'apheleia-inhibit-functions
|
|
|
|
(defun +format-maybe-inhibit-h ()
|
|
|
|
"Check if formatting should be disabled for current buffer.
|
2022-11-19 16:41:16 +00:00
|
|
|
This is controlled by `+format-on-save-disabled-modes'."
|
2024-07-06 19:54:55 -04:00
|
|
|
(or (eq major-mode 'fundamental-mode)
|
|
|
|
(string-blank-p (buffer-name))
|
|
|
|
(eq +format-on-save-disabled-modes t)
|
|
|
|
(not (null (memq major-mode +format-on-save-disabled-modes)))))))
|
2023-09-13 10:19:39 +01:00
|
|
|
|
2024-07-06 19:54:55 -04:00
|
|
|
;; Use the formatter provided by lsp-mode and eglot, if they are available and
|
|
|
|
;; `+format-with-lsp' is non-nil.
|
|
|
|
(cond ((modulep! :tools lsp +eglot)
|
2024-07-06 21:18:34 -04:00
|
|
|
(add-hook 'eglot-managed-mode-hook #'+format-toggle-eglot-formatter-h))
|
2024-07-06 19:54:55 -04:00
|
|
|
((modulep! :tools lsp)
|
|
|
|
(add-hook 'lsp-configure-hook #'+format-enable-lsp-formatter-h)
|
|
|
|
(add-hook 'lsp-unconfigure-hook #'+format-disable-lsp-formatter-h)))
|
|
|
|
|
2024-07-07 14:21:29 -04:00
|
|
|
:config
|
|
|
|
(add-to-list 'doom-debug-variables '(apheleia-log-only-errors . nil))
|
|
|
|
|
|
|
|
(add-to-list 'apheleia-formatters '(eglot . +format-eglot-buffer))
|
|
|
|
(add-to-list 'apheleia-formatters '(lsp . +format-lsp-buffer))
|
|
|
|
|
2024-07-06 19:54:55 -04:00
|
|
|
(defadvice! +format--inhibit-reformat-on-prefix-arg-a (orig-fn &optional arg)
|
|
|
|
"Make it so \\[save-buffer] with prefix arg inhibits reformatting."
|
|
|
|
:around #'save-buffer
|
|
|
|
(let ((apheleia-mode (and apheleia-mode (memq arg '(nil 1)))))
|
|
|
|
(funcall orig-fn)))
|
|
|
|
|
|
|
|
;; HACK: Apheleia suppresses notifications that the current buffer has
|
|
|
|
;; changed, so plugins that listen for them need to be manually informed:
|
|
|
|
(add-hook!
|
|
|
|
'apheleia-post-format-hook
|
|
|
|
(defun +format--update-web-mode-h ()
|
|
|
|
(when (eq major-mode 'web-mode)
|
|
|
|
(setq web-mode-fontification-off nil)
|
|
|
|
(when (and web-mode-scan-beg web-mode-scan-end global-font-lock-mode)
|
|
|
|
(save-excursion
|
|
|
|
(font-lock-fontify-region web-mode-scan-beg web-mode-scan-end)))))
|
|
|
|
(defun +format--update-vc-gutter-h ()
|
|
|
|
(when (fboundp '+vc-gutter-update-h)
|
|
|
|
(+vc-gutter-update-h)))))
|