doomemacs/modules/editor/format/config.el
Henrik Lissner b405225b90
refactor!(vc-gutter): drop git-gutter for diff-hl
BREAKING CHANGE: This removes git-gutter as an implementation for the
`:ui vc-gutter` module, leaving only the diff-hl implementation. There
are no longer any +git-gutter or +diff-hl flags for this module. Users
don't have to do anything to keep the vc gutter, unless they prefer
git-gutter for any reason (in which case they'll need to install and set
it up themselves).

This has been planned for some time, because of a roadmap goal for Doom
to lean into native/built-in functionality where it's equal or better
than the third party alternatives. diff-hl relies on the built-in vc.el
library instead of talking to git directly (thus expanding support to
whatever VCS's vc.el supports, and not git alone), which also means it
can take advantage of its caching and other user configuration for
vc.el. Overall, it is faster and lighter.

What I've also been waiting for was a stage-hunk command, similar to
git-gutter:stage-hunk, which arrived in dgutov/diff-hl@a0560551cd and
dgutov/diff-hl@133538973b, and have evolved since.

Ref: dgutov/diff-hl@a0560551cd
Ref: dgutov/diff-hl@133538973b
Ref: https://github.com/orgs/doomemacs/projects/5/views/1?pane=issue&itemId=58747789
2024-06-22 18:14:04 -04:00

87 lines
3.1 KiB
EmacsLisp

;;; editor/format/config.el -*- lexical-binding: t; -*-
(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 not reformat the buffer upon saving.
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.
Irrelevant if you do not have the +onsave flag enabled for this module.")
(defvar +format-preserve-indentation t
"If non-nil, the leading indentation is preserved when formatting the whole
buffer. This is particularly useful for partials.
Indentation is always preserved when formatting regions.")
(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.
This has no effect on the +onsave flag, apheleia will always be used there.")
(defvaralias '+format-with 'apheleia-formatter
"Set this to explicitly use a certain formatter for the current buffer.")
(defvar +format-functions
'(+format-in-org-src-blocks-fn
+format-with-lsp-fn
+format-with-eglot-fn)
"A list of functions to run when formatting a buffer or region.
Each function is given three arguments: the starting point, end point, and a
symbol indicating the type of operation being requested (as a symbol: either
`region' or `buffer').
The first function to return non-nil will abort all functions after it,
including Apheleia itself.")
;;
;;; Bootstrap
(when (modulep! +onsave)
(add-hook 'doom-first-file-hook #'apheleia-global-mode))
(defun +format-maybe-inhibit-h ()
"Check if formatting should be disabled for current buffer.
This is controlled by `+format-on-save-disabled-modes'."
(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)))))
(after! apheleia
(add-to-list 'doom-debug-variables '(apheleia-log-only-errors . nil))
(when (modulep! +onsave)
(add-to-list 'apheleia-inhibit-functions #'+format-maybe-inhibit-h)))
;;
;;; Hacks
(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)))
(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-vc-gutter-h ()
(when (fboundp '+vc-gutter-update-h)
(+vc-gutter-update-h))))