diff --git a/.emacs.d/init.el b/.emacs.d/init.el index 1e32d1e..f5355c2 100644 --- a/.emacs.d/init.el +++ b/.emacs.d/init.el @@ -999,15 +999,6 @@ (which-key-posframe-mode) (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 :config :bind ("C-S-" . backward-mark) @@ -1037,12 +1028,6 @@ :bind (:map typescript-mode-map ("" . 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 :config (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: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 (add-to-list 'safe-local-eval-forms '(outline-hide-body)) ;; Local Variables: