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)))) (define-fringe-bitmap 'git-gutter-fr:deleted [224] nil nil '(center repeated))))
(use-package apheleia :demand :if (package-installed-p 'apheleia) (use-package apheleia :demand :if (package-installed-p 'apheleia)
:hook (python-base-mode . (apheleia--determine-python-formatter))
:config :config
(setf (alist-get 'djlint apheleia-formatters) '("djlint" "--reformat" "-")) (setf (alist-get 'djlint apheleia-formatters) '("djlint" "--reformat" "-"))
(setf (alist-get "\\.dj\\.html$" apheleia-mode-alist) '(djlint)) (setf (alist-get "\\.dj\\.html$" apheleia-mode-alist) '(djlint))
@ -1090,6 +1091,7 @@
"Disable apheleia when a formatter's configuration does not exist" "Disable apheleia when a formatter's configuration does not exist"
(not (let ((formatters (apheleia--get-formatters))) (not (let ((formatters (apheleia--get-formatters)))
(cond (cond
((member 'ruff formatters) (apheleia--pyproject-contains-section "tool.ruff"))
((member 'black formatters) (apheleia--pyproject-contains-section "tool.black")) ((member 'black formatters) (apheleia--pyproject-contains-section "tool.black"))
((member 'djlint formatters) (apheleia--pyproject-contains-section "tool.djlint")) ((member 'djlint formatters) (apheleia--pyproject-contains-section "tool.djlint"))
((seq-some (lambda (f) (string-prefix-p "prettier" (symbol-name f))) formatters) ((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) (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)) (apheleia-global-mode t))
(use-package smali-mode (use-package smali-mode