-
Notifications
You must be signed in to change notification settings - Fork 19
/
flycheck-crystal.el
96 lines (78 loc) · 3.24 KB
/
flycheck-crystal.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
;;; flycheck-crystal.el --- Add support for Crystal to Flycheck
;; Copyright (C) 2015-2018 crystal-lang-tools
;; Authors: crystal-lang-tools
;; URL: https://github.com/crystal-lang-tools/emacs-crystal-mode
;; Keywords: tools crystal
;; Version: 0.1
;; Package-Requires: ((flycheck "30"))
;; This file is not part of GNU Emacs.
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This package provides error-checking support for the Crystal language to the
;; Flycheck package. To use it, have Flycheck installed, then add the following
;; to your init file:
;;
;; (require 'flycheck-crystal)
;; (add-hook 'crystal-mode-hook 'flycheck-mode)
;;; Code:
(require 'flycheck)
(require 'json)
(defgroup flycheck-crystal nil
"Crystal mode's flycheck checker."
:group 'crystal)
(defcustom flycheck-crystal-show-instantiating nil
"Whether \"instantiated by\" messages should be shown.
These messages typically show the (static) backtrace of an error and are of
little interest."
:type 'boolean
:group 'flycheck-crystal)
(defun flycheck-crystal--find-default-directory (_checker)
"Come up with a suitable default directory to run CHECKER in.
This will either be the directory that contains `shard.yml' or,
if no such file is found in the directory hierarchy, the directory
of the current file."
(or
(and
buffer-file-name
(locate-dominating-file buffer-file-name "shard.yml"))
default-directory))
(flycheck-define-checker crystal-build
"A Crystal syntax checker using crystal build"
:command ("crystal"
"build"
"--no-codegen"
"--no-color"
"-f" "json"
"--stdin-filename"
(eval (buffer-file-name)))
:working-directory flycheck-crystal--find-default-directory
:error-parser flycheck-crystal--error-parser
:modes crystal-mode
:standard-input t
)
(defun flycheck-crystal--error-parser (output checker buffer)
(unless (zerop (length output))
(mapcan
(lambda (err)
(when (or flycheck-crystal-show-instantiating
(not (string-prefix-p "instantiating" (cdr-safe (assoc 'message err)))))
(list (flycheck-error-new-at (cdr-safe (assoc 'line err))
(cdr-safe (assoc 'column err))
'error
(cdr-safe (assoc 'message err))
:checker checker
:buffer buffer
:filename (cdr-safe (assoc 'file err))))))
(json-read-from-string output))))
(add-to-list 'flycheck-checkers 'crystal-build)
(provide 'flycheck-crystal)
;;; flycheck-crystal.el ends here