-
-
Notifications
You must be signed in to change notification settings - Fork 906
/
Copy pathlsp-golangci-lint.el
168 lines (140 loc) · 6.36 KB
/
lsp-golangci-lint.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
;;; lsp-golangci-lint.el --- golangci-lint-langserver Client settings -*- lexical-binding: t; -*-
;; Copyright (C) 2023 Jim Myhrberg
;; Author: Jim Myhrberg
;; Keywords: lsp, go, golang, golangci-lint
;; This file is not part of GNU Emacs
;;; License:
;;
;; This program 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 of the License, or
;; (at your option) any later version.
;;
;; This program 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; lsp-golangci-lint client
;;; Code:
(require 'lsp-mode)
(require 'lsp-go)
(require 'cl-lib)
(defgroup lsp-golangci-lint nil
"Configuration options for lsp-golangci-lint."
:group 'lsp-mode
:link '(url-link "https://github.com/nametake/golangci-lint-langserver")
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-golangci-lint-server-path "golangci-lint-langserver"
"Command to run golangci-lint-langserver."
:type 'string
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-golangci-lint-server-debug nil
"Whether to run golangci-lint-langserver in debug mode or not."
:type 'boolean
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-golangci-lint-server-args nil
"Arguments to pass to golangci-lint-langserver."
:type '(repeat string)
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-golangci-lint-path "golangci-lint"
"Command to run golangci-lint."
:type 'string
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-golangci-lint-allow-parallel-runners t
"If not nil, pass --allow-parallel-runners flag to golangci-lint run."
:type 'boolean
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-golangci-lint-build-tags nil
"If non-empty list, pass as --build-tags flag value to golangci-lint run."
:type '(repeat string)
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-golangci-lint-fast nil
"If not nil, pass --fast flag to golangci-lint run."
:type 'boolean
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-golangci-lint-enable-all nil
"If not nil, pass --enable-all flag to golangci-lint run."
:type 'boolean
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-golangci-lint-enable nil
"If non-empty list, pass as --enable flag value to golangci-lint run."
:type '(repeat string)
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-golangci-lint-disable-all nil
"If not nil, pass --disable-all to golangci-lint run."
:type 'boolean
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-golangci-lint-disable nil
"If non-empty list, pass as --disable flag value to golangci-lint run."
:type '(repeat string)
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-golangci-lint-config nil
"If set, pass value as --config flag to golangci-lint run."
:type 'string
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-golangci-lint-no-config nil
"If not nil, pass --no-config flag to golangci-lint run."
:type 'boolean
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-golangci-lint-run-args nil
"Arguments to pass to golangci-lint run command."
:type '(repeat string)
:package-version '(lsp-mode . "9.0.0"))
(defun lsp-golangci-lint-server--stdio-command ()
"Return the command and args to start golangci-lint-langserver."
(let ((args (list lsp-golangci-lint-server-path)))
(when (and (listp lsp-golangci-lint-server-args)
(> (length lsp-golangci-lint-server-args) 0))
(setq args (append args lsp-golangci-lint-server-args)))
(when lsp-golangci-lint-server-debug
(setq args (append args '("-debug"))))
args))
(defun lsp-golangci-lint--run-args ()
"Return the arguments to pass to golangci-lint run command."
(let* ((tags (string-join lsp-golangci-lint-build-tags " "))
(enable (string-join lsp-golangci-lint-enable ","))
(disable (string-join lsp-golangci-lint-disable ","))
(args (cl-loop for (condition flag value) in
`((,lsp-golangci-lint-fast "--fast" nil)
(,(not (string-empty-p tags)) "--build-tags" ,tags)
(,lsp-golangci-lint-enable-all "--enable-all" nil)
(,lsp-golangci-lint-disable-all "--disable-all" nil)
(,(not (string-empty-p enable)) "--enable" ,enable)
(,(not (string-empty-p disable)) "--disable" ,disable)
(,lsp-golangci-lint-allow-parallel-runners
"--allow-parallel-runners" nil)
(,(and (stringp lsp-golangci-lint-config)
(not (string-empty-p lsp-golangci-lint-config)))
"--config" lsp-golangci-lint-config))
when condition
append (if value (list flag value) (list flag)))))
(when (and (listp lsp-golangci-lint-run-args)
(> (length lsp-golangci-lint-run-args) 0))
(setq args (append args lsp-golangci-lint-run-args)))
args))
(defun lsp-golangci-lint--get-initialization-options ()
"Return initialization options for golangci-lint-langserver."
(let ((opts (make-hash-table :test 'equal))
(command (vconcat `(,lsp-golangci-lint-path)
["run" "--out-format=json" "--issues-exit-code=1"]
(lsp-golangci-lint--run-args))))
(puthash "command" command opts)
opts))
(lsp-register-client
(make-lsp-client :new-connection (lsp-stdio-connection
#'lsp-golangci-lint-server--stdio-command)
:activation-fn (lsp-activate-on "go")
:language-id "go"
:priority 0
:server-id 'golangci-lint
:add-on? t
:library-folders-fn #'lsp-go--library-default-directories
:initialization-options #'lsp-golangci-lint--get-initialization-options))
(lsp-consistency-check lsp-golangci-lint)
(provide 'lsp-golangci-lint)
;;; lsp-golangci-lint.el ends here