2018-08-22 02:15:44 +02:00
|
|
|
;;; editor/format/config.el -*- lexical-binding: t; -*-
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
;;
|
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
|
|
|
|
(let ((apheleia-mode (and apheleia-mode (member arg '(nil 1)))))
|
|
|
|
(funcall orig-fn)))
|
|
|
|
|
|
|
|
(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 (bound-and-true-p git-gutter-mode)
|
|
|
|
(git-gutter))))
|
2018-09-08 23:39:52 -04:00
|
|
|
|
2020-11-01 08:49:14 -08:00
|
|
|
|
2022-08-16 08:13:55 +01:00
|
|
|
;;
|
|
|
|
;;; Additional formatters
|
2021-06-04 15:56:07 -04:00
|
|
|
|
2022-08-14 16:26:33 +01:00
|
|
|
(after! apheleia
|
2022-08-16 08:13:55 +01:00
|
|
|
;; TODO html-tidy
|
2022-08-14 16:26:33 +01:00
|
|
|
(cl-defun apheleia--indent-lisp-buffer
|
|
|
|
(&key buffer scratch callback &allow-other-keys)
|
|
|
|
"Format a Lisp BUFFER. Use SCRATCH as a temporary buffer and CALLBACK to
|
|
|
|
apply the transformation. For more implementation detail, see
|
|
|
|
`apheleia--run-formatter-function'."
|
|
|
|
(with-current-buffer scratch
|
|
|
|
(setq-local indent-line-function
|
|
|
|
(buffer-local-value 'indent-line-function buffer))
|
|
|
|
(setq-local lisp-indent-function
|
|
|
|
(buffer-local-value 'lisp-indent-function buffer))
|
|
|
|
(funcall (with-current-buffer buffer major-mode))
|
|
|
|
(goto-char (point-min))
|
|
|
|
(let ((inhibit-message t)
|
|
|
|
(message-log-max nil))
|
|
|
|
(indent-region (point-min) (point-max)))
|
|
|
|
(funcall callback))))
|