Everything seems to be working OK now.

This commit is contained in:
Austin Bingham
2017-05-14 15:51:56 +02:00
parent 8551cf9426
commit ccb6d68d14

View File

@@ -4,7 +4,7 @@
;; Author: Austin Bingham <austin.bingham@gmail.com> ;; Author: Austin Bingham <austin.bingham@gmail.com>
;; Version: 0.1 ;; Version: 0.1
;; URL: https://github.com/abingham/flycheck-vale ;; URL: https://github.com/abingham/flycheck-vale
;; Package-Requires: ((emacs "24") (flycheck "0.22") (cl-lib "0.5")) ;; Package-Requires: ((emacs "24") (flycheck "0.22"))
;; ;;
;; This file is not part of GNU Emacs. ;; This file is not part of GNU Emacs.
;; ;;
@@ -56,24 +56,39 @@
:type '(string) :type '(string)
:group 'flycheck-vale) :group 'flycheck-vale)
(defcustom flycheck-vale-modes '(text-mode markdown-mode rst-mode)
"List of major modes in which to apply this checker."
:type '(repeat function))
(defconst flycheck-vale--level-map (defconst flycheck-vale--level-map
'(("error" . error) '(("error" . error)
("warning" . warning))) ("warning" . warning)))
(defun flycheck-vale--issue-to-error (result) (defun flycheck-vale--issue-to-error (result)
"Parse a single vale issue into a flycheck error struct.
We only fill in what we can get from the vale issue directly. The
rest (e.g. filename) gets filled in elsewhere."
(let-alist result (let-alist result
(flycheck-error-new (flycheck-error-new
:line .Line :line .Line
:column (elt .Span 0) :column (elt .Span 0)
;; :buffer (current-buffer)
;; :filename "TODO"
:message .Message :message .Message
;; :checker checker
:level (assoc-default .Severity flycheck-vale--level-map 'string-equal 'error)))) :level (assoc-default .Severity flycheck-vale--level-map 'string-equal 'error))))
(defun flycheck-vale--output-to-errors (output) (defun flycheck-vale--output-to-errors (output)
"Parse the full JSON output of vale into a sequence of flycheck
error structs."
(let* ((full-results (json-read-from-string output)) (let* ((full-results (json-read-from-string output))
;; Get the list of issues for each file.
(result-vecs (mapcar 'cdr full-results)) (result-vecs (mapcar 'cdr full-results))
;; Chain all of the issues together. The point here, really, is that we
;; don't expect results from more than one file, but we should be
;; prepared for the theoretical possibility that the issues are somehow
;; split across multiple files. This is basically a punt in lieu of
;; more information.
(issues (apply 'concatenate 'list (mapcar 'cdr full-results)))) (issues (apply 'concatenate 'list (mapcar 'cdr full-results))))
(mapcar 'flycheck-vale--issue-to-error issues))) (mapcar 'flycheck-vale--issue-to-error issues)))
@@ -82,6 +97,7 @@
(let ((orig-buf (current-buffer)) (let ((orig-buf (current-buffer))
(outbuf (get-buffer-create "*flycheck-vale-output*"))) (outbuf (get-buffer-create "*flycheck-vale-output*")))
;; Clear the output buffer ;; Clear the output buffer
(with-current-buffer outbuf (with-current-buffer outbuf
(read-only-mode 0) (read-only-mode 0)
@@ -98,31 +114,29 @@
"--output" "--output"
"JSON") "JSON")
;; Parse the content of the output buffer into flycheck error structures,
;; passing them to the provided callback.
(with-current-buffer outbuf (with-current-buffer outbuf
(let ((errors (flycheck-vale--output-to-errors (buffer-string)))) (let ((errors (flycheck-vale--output-to-errors (buffer-string))))
;; Fill in the rest of the error struct data.
(loop for err in errors do (loop for err in errors do
(setf (flycheck-error-buffer err) orig-buf) (setf
(setf (flycheck-error-filename err) (buffer-file-name orig-buf)) (flycheck-error-buffer err) orig-buf
(setf (flycheck-error-checker err) checker)) (flycheck-error-filename err) (buffer-file-name orig-buf)
(flycheck-error-checker err) checker))
(funcall callback 'finished errors))))) (funcall callback 'finished errors)))))
(defun flycheck-vale--in-supported-mode ()
"Determines if buffer is in a mode where vale linting is appropriate."
text-mode)
;;;###autoload ;;;###autoload
(defun flycheck-vale-setup () (defun flycheck-vale-setup ()
"Convenience function to setup the vale flycheck checker. "Convenience function to setup the vale flycheck checker.
This adds a hook to watch for ycmd parse results, and it adds the This adds the vale checker to the list of flycheck checkers."
ycmd checker to the list of flycheck checkers."
(add-to-list 'flycheck-checkers 'vale)) (add-to-list 'flycheck-checkers 'vale))
(flycheck-define-generic-checker 'vale (flycheck-define-generic-checker 'vale
"A flycheck checker using vale natural language linting." "A flycheck checker using vale natural language linting."
:start #'flycheck-vale--start :start #'flycheck-vale--start
;; :predicate #'flycheck-vale--in-supported-mode :modes flycheck-vale-modes)
:modes '(text-mode))
(provide 'flycheck-vale) (provide 'flycheck-vale)