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
|
package gopls
import (
"encoding/json"
"fmt"
)
func (p *GoplsProxy) OpenVirtualFile(uri, content string, version int) error {
p.virtualFilesMu.Lock()
p.virtualFiles[uri] = &VirtualFile{
URI: uri,
Content: content,
Version: version,
}
p.virtualFilesMu.Unlock()
params := map[string]any{
"textDocument": map[string]any{
"uri": uri,
"languageId": "go",
"version": version,
"text": content,
},
}
return p.notify("textDocument/didOpen", params)
}
func (p *GoplsProxy) UpdateVirtualFile(uri, content string, version int) error {
p.virtualFilesMu.Lock()
if vf, ok := p.virtualFiles[uri]; ok {
vf.Content = content
vf.Version = version
}
p.virtualFilesMu.Unlock()
params := map[string]any{
"textDocument": map[string]any{
"uri": uri,
"version": version,
},
"contentChanges": []map[string]any{
{"text": content},
},
}
return p.notify("textDocument/didChange", params)
}
func (p *GoplsProxy) CloseVirtualFile(uri string) error {
p.virtualFilesMu.Lock()
delete(p.virtualFiles, uri)
p.virtualFilesMu.Unlock()
params := map[string]any{
"textDocument": map[string]any{
"uri": uri,
},
}
return p.notify("textDocument/didClose", params)
}
func (p *GoplsProxy) Completion(uri string, pos Position) ([]CompletionItem, error) {
params := CompletionParams{
TextDocumentPositionParams: TextDocumentPositionParams{
TextDocument: TextDocumentIdentifier{URI: uri},
Position: pos,
},
}
result, err := p.call("textDocument/completion", params)
if err != nil {
return nil, err
}
var list CompletionList
if err := json.Unmarshal(result, &list); err != nil {
var items []CompletionItem
if err := json.Unmarshal(result, &items); err != nil {
return nil, fmt.Errorf("parsing completion result: %w", err)
}
return items, nil
}
return list.Items, nil
}
func (p *GoplsProxy) Hover(uri string, pos Position) (*Hover, error) {
params := TextDocumentPositionParams{
TextDocument: TextDocumentIdentifier{URI: uri},
Position: pos,
}
result, err := p.call("textDocument/hover", params)
if err != nil {
return nil, err
}
if result == nil || string(result) == "null" {
return nil, nil
}
var hover Hover
if err := json.Unmarshal(result, &hover); err != nil {
return nil, fmt.Errorf("parsing hover result: %w", err)
}
return &hover, nil
}
func (p *GoplsProxy) Definition(uri string, pos Position) ([]Location, error) {
params := TextDocumentPositionParams{
TextDocument: TextDocumentIdentifier{URI: uri},
Position: pos,
}
result, err := p.call("textDocument/definition", params)
if err != nil {
return nil, err
}
if result == nil || string(result) == "null" {
return nil, nil
}
var locs []Location
if err := json.Unmarshal(result, &locs); err != nil {
var loc Location
if err := json.Unmarshal(result, &loc); err != nil {
return nil, fmt.Errorf("parsing definition result: %w", err)
}
return []Location{loc}, nil
}
return locs, nil
}
func (p *GoplsProxy) call(method string, params any) (json.RawMessage, error) {
id := p.nextID.Add(1)
respChan := make(chan *Response, 1)
p.pendingMu.Lock()
p.pending[id] = respChan
p.pendingMu.Unlock()
defer func() {
p.pendingMu.Lock()
delete(p.pending, id)
p.pendingMu.Unlock()
}()
req := Request{
JSONRPC: "2.0",
ID: id,
Method: method,
Params: params,
}
if err := p.send(req); err != nil {
return nil, err
}
select {
case <-p.ctx.Done():
return nil, p.ctx.Err()
case resp := <-respChan:
if resp.Error != nil {
return nil, fmt.Errorf("gopls error %d: %s", resp.Error.Code, resp.Error.Message)
}
return resp.Result, nil
}
}
func (p *GoplsProxy) notify(method string, params any) error {
req := Request{
JSONRPC: "2.0",
Method: method,
Params: params,
}
return p.send(req)
}
|