Use ruff/ruff-isort formatter in apheleia when detected

This commit is contained in:
Adam Goldsmith 2024-01-18 13:01:37 -05:00
parent 140a490ad9
commit 8b7e0bcdf9

View File

@ -1076,6 +1076,7 @@
(define-fringe-bitmap 'git-gutter-fr:deleted [224] nil nil '(center repeated))))
(use-package apheleia :demand :if (package-installed-p 'apheleia)
:hook (python-base-mode . (apheleia--determine-python-formatter))
:config
(setf (alist-get 'djlint apheleia-formatters) '("djlint" "--reformat" "-"))
(setf (alist-get "\\.dj\\.html$" apheleia-mode-alist) '(djlint))
@ -1090,6 +1091,7 @@
"Disable apheleia when a formatter's configuration does not exist"
(not (let ((formatters (apheleia--get-formatters)))
(cond
((member 'ruff formatters) (apheleia--pyproject-contains-section "tool.ruff"))
((member 'black formatters) (apheleia--pyproject-contains-section "tool.black"))
((member 'djlint formatters) (apheleia--pyproject-contains-section "tool.djlint"))
((seq-some (lambda (f) (string-prefix-p "prettier" (symbol-name f))) formatters)
@ -1102,6 +1104,14 @@
(add-to-list 'apheleia-inhibit-functions 'apheleia--check-formatter-configured)
(defun apheleia--determine-python-formatter ()
"Determine which formatter to use for a python buffer based on existence of pyproject.toml sections"
(cond ((apheleia--pyproject-contains-section "tool.ruff") (setq-local apheleia-formatter '(ruff-isort ruff)))
((apheleia--pyproject-contains-section "tool.black")
(if (apheleia--pyproject-contains-section "tool.isort")
(setq-local apheleia-formatter '(black isort))
(setq-local apheleia-formatter '(black))))))
(apheleia-global-mode t))
(use-package smali-mode