-
Notifications
You must be signed in to change notification settings - Fork 20
/
handler.go
239 lines (198 loc) · 5.86 KB
/
handler.go
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package main
import (
"context"
"encoding/json"
"fmt"
"os/exec"
"path/filepath"
"strings"
"github.com/sourcegraph/jsonrpc2"
)
func NewHandler(logger logger, noLinterName bool) jsonrpc2.Handler {
handler := &langHandler{
logger: logger,
request: make(chan DocumentURI),
noLinterName: noLinterName,
}
go handler.linter()
return jsonrpc2.HandlerWithError(handler.handle)
}
type langHandler struct {
logger logger
conn *jsonrpc2.Conn
request chan DocumentURI
command []string
noLinterName bool
rootURI string
rootDir string
}
func (h *langHandler) errToDiagnostics(err error) []Diagnostic {
var message string
switch e := err.(type) {
case *exec.ExitError:
message = string(e.Stderr)
default:
h.logger.DebugJSON("golangci-lint-langserver: errToDiagnostics message", message)
message = e.Error()
}
return []Diagnostic{
{Severity: DSError, Message: message},
}
}
func (h *langHandler) lint(uri DocumentURI) ([]Diagnostic, error) {
diagnostics := make([]Diagnostic, 0)
path := uriToPath(string(uri))
dir, file := filepath.Split(path)
args := make([]string, 0, len(h.command))
args = append(args, h.command[1:]...)
args = append(args, dir)
//nolint:gosec
cmd := exec.Command(h.command[0], args...)
if strings.HasPrefix(path, h.rootDir) {
cmd.Dir = h.rootDir
file = path[len(h.rootDir)+1:]
} else {
cmd.Dir = dir
}
h.logger.DebugJSON("golangci-lint-langserver: golingci-lint cmd", cmd)
b, err := cmd.Output()
if err == nil {
return diagnostics, nil
} else if len(b) == 0 {
// golangci-lint would output critical error to stderr rather than stdout
// https://github.com/nametake/golangci-lint-langserver/issues/24
return h.errToDiagnostics(err), nil
}
var result GolangCILintResult
if err := json.Unmarshal(b, &result); err != nil {
return h.errToDiagnostics(err), nil
}
h.logger.DebugJSON("golangci-lint-langserver: result:", result)
for _, issue := range result.Issues {
issue := issue
if file != issue.Pos.Filename {
continue
}
d := Diagnostic{
Range: Range{
Start: Position{
Line: max(issue.Pos.Line-1, 0),
Character: max(issue.Pos.Column-1, 0),
},
End: Position{
Line: max(issue.Pos.Line-1, 0),
Character: max(issue.Pos.Column-1, 0),
},
},
Severity: issue.DiagSeverity(),
Source: &issue.FromLinter,
Message: h.diagnosticMessage(&issue),
}
diagnostics = append(diagnostics, d)
}
return diagnostics, nil
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func (h *langHandler) diagnosticMessage(issue *Issue) string {
if h.noLinterName {
return issue.Text
}
return fmt.Sprintf("%s: %s", issue.FromLinter, issue.Text)
}
func (h *langHandler) linter() {
for {
uri, ok := <-h.request
if !ok {
break
}
diagnostics, err := h.lint(uri)
if err != nil {
h.logger.Printf("%s", err)
continue
}
if err := h.conn.Notify(
context.Background(),
"textDocument/publishDiagnostics",
&PublishDiagnosticsParams{
URI: uri,
Diagnostics: diagnostics,
}); err != nil {
h.logger.Printf("%s", err)
}
}
}
func (h *langHandler) handle(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (result interface{}, err error) {
h.logger.DebugJSON("golangci-lint-langserver: request:", req)
switch req.Method {
case "initialize":
return h.handleInitialize(ctx, conn, req)
case "initialized":
return
case "shutdown":
return h.handleShutdown(ctx, conn, req)
case "textDocument/didOpen":
return h.handleTextDocumentDidOpen(ctx, conn, req)
case "textDocument/didClose":
return h.handleTextDocumentDidClose(ctx, conn, req)
case "textDocument/didChange":
return h.handleTextDocumentDidChange(ctx, conn, req)
case "textDocument/didSave":
return h.handleTextDocumentDidSave(ctx, conn, req)
case "workspace/didChangeConfiguration":
return h.handlerWorkspaceDidChangeConfiguration(ctx, conn, req)
}
return nil, &jsonrpc2.Error{Code: jsonrpc2.CodeMethodNotFound, Message: fmt.Sprintf("method not supported: %s", req.Method)}
}
func (h *langHandler) handleInitialize(_ context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (result interface{}, err error) {
var params InitializeParams
if err := json.Unmarshal(*req.Params, ¶ms); err != nil {
return nil, err
}
h.rootURI = params.RootURI
h.rootDir = uriToPath(params.RootURI)
h.conn = conn
h.command = params.InitializationOptions.Command
return InitializeResult{
Capabilities: ServerCapabilities{
TextDocumentSync: TextDocumentSyncOptions{
Change: TDSKNone,
OpenClose: true,
Save: true,
},
},
}, nil
}
func (h *langHandler) handleShutdown(_ context.Context, _ *jsonrpc2.Conn, _ *jsonrpc2.Request) (result interface{}, err error) {
close(h.request)
return nil, nil
}
func (h *langHandler) handleTextDocumentDidOpen(_ context.Context, _ *jsonrpc2.Conn, req *jsonrpc2.Request) (result interface{}, err error) {
var params DidOpenTextDocumentParams
if err := json.Unmarshal(*req.Params, ¶ms); err != nil {
return nil, err
}
h.request <- params.TextDocument.URI
return nil, nil
}
func (h *langHandler) handleTextDocumentDidClose(_ context.Context, _ *jsonrpc2.Conn, _ *jsonrpc2.Request) (result interface{}, err error) {
return nil, nil
}
func (h *langHandler) handleTextDocumentDidChange(_ context.Context, _ *jsonrpc2.Conn, _ *jsonrpc2.Request) (result interface{}, err error) {
return nil, nil
}
func (h *langHandler) handleTextDocumentDidSave(_ context.Context, _ *jsonrpc2.Conn, req *jsonrpc2.Request) (result interface{}, err error) {
var params DidSaveTextDocumentParams
if err := json.Unmarshal(*req.Params, ¶ms); err != nil {
return nil, err
}
h.request <- params.TextDocument.URI
return nil, nil
}
func (h *langHandler) handlerWorkspaceDidChangeConfiguration(_ context.Context, _ *jsonrpc2.Conn, req *jsonrpc2.Request) (result interface{}, err error) {
return nil, nil
}