Replace prettier-js and blacken with apheleia

This commit is contained in:
Adam Goldsmith 2023-05-04 00:24:50 -04:00
parent 387df0553e
commit 9e09f7e05f

View File

@ -999,15 +999,6 @@
(which-key-posframe-mode) (which-key-posframe-mode)
(set-face-background 'which-key-posframe "#333333"))) (set-face-background 'which-key-posframe "#333333")))
(use-package prettier-js
:hook ((js2-mode typescript-mode web-mode json-mode) . prettier-js-mode-maybe)
:commands prettier-js
:init
(defun prettier-js-mode-maybe ()
(interactive)
(when (= 0 (call-process "prettier" nil nil nil "--find-config-path" (buffer-file-name)))
(prettier-js-mode t))))
(use-package show-marks (use-package show-marks
:config :config
:bind ("C-S-<left>" . backward-mark) :bind ("C-S-<left>" . backward-mark)
@ -1037,12 +1028,6 @@
:bind (:map typescript-mode-map :bind (:map typescript-mode-map
("<M-return>" . c-indent-new-comment-line))) ("<M-return>" . c-indent-new-comment-line)))
(use-package blacken
:hook (python-mode . blacken-mode)
:delight
:config
(setq blacken-only-if-project-is-blackened t))
(use-package rustic (use-package rustic
:config :config
(setq rustic-format-on-save t (setq rustic-format-on-save t
@ -1067,6 +1052,30 @@
(define-fringe-bitmap 'git-gutter-fr:modified [224] nil nil '(center repeated)) (define-fringe-bitmap 'git-gutter-fr:modified [224] nil nil '(center repeated))
(define-fringe-bitmap 'git-gutter-fr:deleted [224] nil nil '(center repeated)))) (define-fringe-bitmap 'git-gutter-fr:deleted [224] nil nil '(center repeated))))
(use-package apheleia :demand :if (package-installed-p 'apheleia)
:config
(setf (alist-get 'djlint apheleia-formatters) '("djlint" "--reformat" "-"))
(setf (alist-get "\\.dj\\.html$" apheleia-mode-alist) '(djlint))
(defun apheleia--pyproject-contains-section (section)
(when-let (parent (locate-dominating-file default-directory "pyproject.toml"))
(with-temp-buffer
(insert-file-contents (concat parent "pyproject.toml"))
(re-search-forward (concat "^\\[" section "\\]$") nil t 1))))
(defun apheleia--check-formatter-configured ()
"Disable apheleia when a formatter's configuration does not exist"
(not (let ((formatters (apheleia--get-formatters)))
(cond
((member 'black formatters) (apheleia--pyproject-contains-section "tool.black"))
((member 'djlint formatters) (apheleia--pyproject-contains-section "tool.djlint"))
((member 'prettier formatters)
(= 0 (call-process "prettier" nil nil nil "--find-config-path" (buffer-file-name))))))))
(add-to-list 'apheleia-inhibit-functions 'apheleia--check-formatter-configured)
(apheleia-global-mode t))
;;; Local Variables ;;; Local Variables
(add-to-list 'safe-local-eval-forms '(outline-hide-body)) (add-to-list 'safe-local-eval-forms '(outline-hide-body))
;; Local Variables: ;; Local Variables: