2018-08-22 02:15:44 +02:00
|
|
|
;;; editor/format/config.el -*- lexical-binding: t; -*-
|
|
|
|
|
2022-11-19 16:41:16 +00:00
|
|
|
(defvar +format-on-save-disabled-modes
|
|
|
|
'(sql-mode ; sqlformat is currently broken
|
|
|
|
tex-mode ; latexindent is broken
|
|
|
|
latex-mode
|
|
|
|
org-msg-edit-mode) ; doesn't need a formatter
|
|
|
|
"A list of major modes in which to reformat the buffer upon saving.
|
|
|
|
If this list begins with `not', then it negates the list.
|
|
|
|
If it is `t', it is enabled in all modes.
|
|
|
|
If nil, it is disabled in all modes, the same as if the +onsave flag wasn't
|
|
|
|
used at all.
|
|
|
|
Irrelevant if you do not have the +onsave flag enabled for this module.")
|
|
|
|
|
2018-09-18 13:14:39 -04:00
|
|
|
(defvar +format-preserve-indentation t
|
|
|
|
"If non-nil, the leading indentation is preserved when formatting the whole
|
|
|
|
buffer. This is particularly useful for partials.
|
|
|
|
|
2019-06-14 11:08:59 +02:00
|
|
|
Indentation is always preserved when formatting regions.")
|
2018-09-18 13:14:39 -04:00
|
|
|
|
2020-05-21 01:20:00 -04:00
|
|
|
(defvar +format-with-lsp t
|
|
|
|
"If non-nil, format with LSP formatter if it's available.
|
|
|
|
|
|
|
|
This can be set buffer-locally with `setq-hook!' to disable LSP formatting in
|
|
|
|
select buffers.")
|
|
|
|
|
2022-08-16 08:13:55 +01:00
|
|
|
(defvaralias '+format-with 'apheleia-formatter
|
|
|
|
"Set this to explicitly use a certain formatter for the current buffer.")
|
|
|
|
|
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
|
|
|
|
2022-08-12 20:29:19 +02:00
|
|
|
(when (modulep! +onsave)
|
2022-08-16 08:13:55 +01:00
|
|
|
(add-hook 'doom-first-file-hook #'apheleia-global-mode))
|
2018-09-08 23:36:24 -04:00
|
|
|
|
2022-12-03 09:37:45 +00:00
|
|
|
(defun +format-inhibit-maybe-h ()
|
2022-11-19 16:41:16 +00:00
|
|
|
"Enable formatting on save in certain major modes.
|
|
|
|
This is controlled by `+format-on-save-disabled-modes'."
|
2022-12-03 09:37:45 +00:00
|
|
|
(or (eq major-mode 'fundamental-mode)
|
2023-09-19 23:37:51 +02:00
|
|
|
(string-blank-p (buffer-name))
|
2022-12-03 09:37:45 +00:00
|
|
|
(not (null (memq major-mode +format-on-save-disabled-modes)))))
|
2022-11-19 16:41:16 +00:00
|
|
|
|
2023-09-13 10:19:39 +01:00
|
|
|
|
2023-09-20 15:40:13 +02:00
|
|
|
(after! apheleia-core
|
|
|
|
(add-to-list 'doom-debug-variables '(apheleia-log-only-errors . nil))
|
|
|
|
|
|
|
|
(when (modulep! +onsave)
|
2023-01-08 15:20:12 +00:00
|
|
|
(add-to-list 'apheleia-inhibit-functions #'+format-inhibit-maybe-h)))
|
2018-09-08 23:36:24 -04:00
|
|
|
|
2023-09-19 23:37:51 +02:00
|
|
|
|
2018-09-08 23:36:24 -04:00
|
|
|
;;
|
2019-06-14 11:08:59 +02:00
|
|
|
;;; Hacks
|
2018-09-08 23:36:24 -04:00
|
|
|
|
2022-08-16 08:13:55 +01: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
|
2023-09-19 23:37:51 +02:00
|
|
|
(let ((apheleia-mode (and apheleia-mode (memq arg '(nil 1)))))
|
2022-08-16 08:13:55 +01:00
|
|
|
(funcall orig-fn)))
|
|
|
|
|
2023-09-13 10:19:39 +01:00
|
|
|
(add-hook!
|
|
|
|
'apheleia-post-format-hook
|
|
|
|
;; HACK `web-mode' doesn't update syntax highlighting after arbitrary buffer
|
|
|
|
;; modifications, so we must trigger refontification manually.
|
|
|
|
(defun +format--fix-web-mode-fontification-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--refresh-git-gutter-h ()
|
|
|
|
(when (fboundp '+vc-gutter-update-h)
|
2023-09-15 21:08:37 -05:00
|
|
|
(+vc-gutter-update-h))))
|