From 8b7e0bcdf9fef7d5b690d7c3539df594c29d8ac0 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Thu, 18 Jan 2024 13:01:37 -0500 Subject: [PATCH] Use ruff/ruff-isort formatter in apheleia when detected --- .emacs.d/init.el | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/.emacs.d/init.el b/.emacs.d/init.el index c940f39..67e6dfe 100644 --- a/.emacs.d/init.el +++ b/.emacs.d/init.el @@ -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