From 34946093c84d7eb8b986d851b139590b74d42de9 Mon Sep 17 00:00:00 2001 From: 10sr <8.slashes@gmail.com> Date: Wed, 30 Jan 2019 13:53:31 +0900 Subject: [PATCH] Fix and enable python-black flycheck checker --- emacs.el | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/emacs.el b/emacs.el index 7ef9a4d..f392eb9 100644 --- a/emacs.el +++ b/emacs.el @@ -2030,12 +2030,25 @@ initializing." (require 'flycheck) -'(flycheck-define-checker python-black +(flycheck-define-checker python-black + "A Python style checker." + :command ("python3" + "-m" "black" + (config-file "--config" flycheck-black) + "--check" source) + :error-parser my-flycheck-parse-any-error + :enabled (lambda () + (or (not (flycheck-python-needs-module-p 'python-black)) + (flycheck-python-find-module 'python-black "black"))) + :verify (lambda (_) (flycheck-python-verify-module 'python-black "black")) + :modes python-mode) + +'(flycheck-define-checker python-black-diff "A Python style checker." :command ("python3" "-m" "black" (config-file "--config" flycheck-black) - "--check" source) + "--diff" source) :error-parser my-flycheck-parse-unified-diff :enabled (lambda () (or (not (flycheck-python-needs-module-p 'python-black)) @@ -2050,8 +2063,19 @@ initializing." 'python-black) (defun my-flycheck-parse-any-error (output checker buffer) - "Flycheck parser to check if OUTPUT is not empty." - ()) + "Flycheck parser to check if reformat is required." + (with-temp-buffer + (insert output) + (goto-char (point-min)) + (when (re-search-forward "^would reformat .*$" nil t) + (list (flycheck-error-new-at + (point-min) + nil + 'error + ;;(format "Black: %s" (match-string 0)) + "Black: would reformat buffer" + :buffer buffer + :checker checker))))) (defun my-flycheck-parse-unified-diff (output checker buffer) "Flycheck parser to parse diff output."