-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathflycheck-ycmd.el
124 lines (108 loc) · 4.38 KB
/
flycheck-ycmd.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
;;; flycheck-ycmd.el --- flycheck integration for ycmd
;; Copyright (c) 2014-2017 Austin Bingham
;;
;; Author: Austin Bingham <[email protected]>
;; Version: 0.1
;; URL: https://github.com/abingham/emacs-ycmd
;; Package-Requires: ((emacs "24") (dash "2.13.0") (flycheck "0.22") (ycmd "1.2") (let-alist "1.0.5"))
;;
;; This file is not part of GNU Emacs.
;;
;;; Commentary:
;;
;; Description:
;;
;; This provides flycheck integration for ycmd. It allows flycheck to
;; use ycmd's parse results for it display. It essentially works by
;; caching the ycmd parse results and then using them when the checker
;; is invoked.
;;
;; Basic usage:
;;
;; (require 'flycheck-ycmd)
;; (flycheck-ycmd-setup)
;;
;;; License:
;;
;; Permission is hereby granted, free of charge, to any person
;; obtaining a copy of this software and associated documentation
;; files (the "Software"), to deal in the Software without
;; restriction, including without limitation the rights to use, copy,
;; modify, merge, publish, distribute, sublicense, and/or sell copies
;; of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;;
;; The above copyright notice and this permission notice shall be
;; included in all copies or substantial portions of the Software.
;;
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
;; BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
;; ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
;; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
;; SOFTWARE.
;;
;;; Code:
(eval-when-compile
(require 'let-alist))
(require 'dash)
(require 'flycheck)
(require 'ycmd)
;; See http://www.lunaryorn.com/2014/12/03/generic-syntax-checkers-in-flycheck.html for more info
;; This maps ycmd result 'kinds' to flycheck 'levels'.
(defconst flycheck-ycmd--level-map
'(("ERROR" . error)
("WARNING" . warning)))
(defvar-local flycheck-ycmd--cache nil
"Cache for parse results.")
(defun flycheck-ycmd--result-to-error (result checker)
"Convert ycmd parse RESULT for CHECKER into a flycheck error object."
(let-alist result
(when (string-equal (convert-standard-filename .location.filepath) (convert-standard-filename (buffer-file-name)))
(flycheck-error-new
:line .location.line_num
:column .location.column_num
:buffer (current-buffer)
:filename .location.filepath
:message (concat .text (when (eq .fixit_available t) " (FixIt available)"))
:checker checker
:level (or (cdr (assoc-string .kind flycheck-ycmd--level-map)) 'error)))))
(defun flycheck-ycmd--start (checker callback)
"Start ycmd flycheck CHECKER using CALLBACK to communicate with flycheck."
(let ((errors (delq
nil
(mapcar (lambda (result)
(flycheck-ycmd--result-to-error result checker))
flycheck-ycmd--cache))))
(funcall callback 'finished errors))
;; OR call (callback 'errored some-message)
)
(defun flycheck-ycmd--cache-parse-results (results)
"Cache ycmd output RESULTS for error display.
We cache the results and use them as the basis for the error
display."
(setq flycheck-ycmd--cache results)
(flycheck-buffer-automatically))
(defun flycheck-ycmd--in-supported-mode ()
"Determines if buffer is in `ycmd-mode` and another mode supported by ycmd."
(and ycmd-mode (ycmd-file-types-with-diagnostics major-mode)))
;;;###autoload
(defun flycheck-ycmd-setup ()
"Convenience function to setup the ycmd flycheck checker.
This adds a hook to watch for ycmd parse results, and it adds the
ycmd checker to the list of flycheck checkers."
(add-hook 'ycmd-file-parse-result-hook 'flycheck-ycmd--cache-parse-results)
(add-to-list 'flycheck-checkers 'ycmd)
(add-hook 'ycmd-after-teardown-hook #'flycheck-ycmd--teardown))
(flycheck-define-generic-checker 'ycmd
"A flycheck checker using parse results from ycmd."
:start #'flycheck-ycmd--start
:predicate #'flycheck-ycmd--in-supported-mode
:modes (ycmd-major-modes-with-diagnostics))
(defun flycheck-ycmd--teardown ()
"Reset `flycheck-ycmd--cache'."
(setq flycheck-ycmd--cache nil))
(provide 'flycheck-ycmd)
;;; flycheck-ycmd.el ends here