forked from golang/tools
/
wrappers.go
353 lines (317 loc) · 9.97 KB
/
wrappers.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package regtest
import (
"io"
"testing"
"golang.org/x/tools/internal/lsp/fake"
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/lsp/source"
errors "golang.org/x/xerrors"
)
func (e *Env) ChangeFilesOnDisk(events []fake.FileEvent) {
e.T.Helper()
if err := e.Sandbox.Workdir.ChangeFilesOnDisk(e.Ctx, events); err != nil {
e.T.Fatal(err)
}
}
// RemoveWorkspaceFile deletes a file on disk but does nothing in the
// editor. It calls t.Fatal on any error.
func (e *Env) RemoveWorkspaceFile(name string) {
e.T.Helper()
if err := e.Sandbox.Workdir.RemoveFile(e.Ctx, name); err != nil {
e.T.Fatal(err)
}
}
// ReadWorkspaceFile reads a file from the workspace, calling t.Fatal on any
// error.
func (e *Env) ReadWorkspaceFile(name string) string {
e.T.Helper()
content, err := e.Sandbox.Workdir.ReadFile(name)
if err != nil {
e.T.Fatal(err)
}
return content
}
// WriteWorkspaceFile writes a file to disk but does nothing in the editor.
// It calls t.Fatal on any error.
func (e *Env) WriteWorkspaceFile(name, content string) {
e.T.Helper()
if err := e.Sandbox.Workdir.WriteFile(e.Ctx, name, content); err != nil {
e.T.Fatal(err)
}
}
// WriteWorkspaceFiles deletes a file on disk but does nothing in the
// editor. It calls t.Fatal on any error.
func (e *Env) WriteWorkspaceFiles(files map[string]string) {
e.T.Helper()
if err := e.Sandbox.Workdir.WriteFiles(e.Ctx, files); err != nil {
e.T.Fatal(err)
}
}
// OpenFile opens a file in the editor, calling t.Fatal on any error.
func (e *Env) OpenFile(name string) {
e.T.Helper()
if err := e.Editor.OpenFile(e.Ctx, name); err != nil {
e.T.Fatal(err)
}
}
func (e *Env) OpenFileWithContent(name, content string) {
e.T.Helper()
if err := e.Editor.OpenFileWithContent(e.Ctx, name, content); err != nil {
e.T.Fatal(err)
}
}
// CreateBuffer creates a buffer in the editor, calling t.Fatal on any error.
func (e *Env) CreateBuffer(name string, content string) {
e.T.Helper()
if err := e.Editor.CreateBuffer(e.Ctx, name, content); err != nil {
e.T.Fatal(err)
}
}
// CloseBuffer closes an editor buffer without saving, calling t.Fatal on any
// error.
func (e *Env) CloseBuffer(name string) {
e.T.Helper()
if err := e.Editor.CloseBuffer(e.Ctx, name); err != nil {
e.T.Fatal(err)
}
}
// EditBuffer applies edits to an editor buffer, calling t.Fatal on any error.
func (e *Env) EditBuffer(name string, edits ...fake.Edit) {
e.T.Helper()
if err := e.Editor.EditBuffer(e.Ctx, name, edits); err != nil {
e.T.Fatal(err)
}
}
// RegexpRange returns the range of the first match for re in the buffer
// specified by name, calling t.Fatal on any error. It first searches for the
// position in open buffers, then in workspace files.
func (e *Env) RegexpRange(name, re string) (fake.Pos, fake.Pos) {
e.T.Helper()
start, end, err := e.Editor.RegexpRange(name, re)
if err == fake.ErrUnknownBuffer {
start, end, err = e.Sandbox.Workdir.RegexpRange(name, re)
}
if err != nil {
e.T.Fatalf("RegexpRange: %v, %v", name, err)
}
return start, end
}
// RegexpSearch returns the starting position of the first match for re in the
// buffer specified by name, calling t.Fatal on any error. It first searches
// for the position in open buffers, then in workspace files.
func (e *Env) RegexpSearch(name, re string) fake.Pos {
e.T.Helper()
pos, err := e.Editor.RegexpSearch(name, re)
if err == fake.ErrUnknownBuffer {
pos, err = e.Sandbox.Workdir.RegexpSearch(name, re)
}
if err != nil {
e.T.Fatalf("RegexpSearch: %v, %v", name, err)
}
return pos
}
// RegexpReplace replaces the first group in the first match of regexpStr with
// the replace text, calling t.Fatal on any error.
func (e *Env) RegexpReplace(name, regexpStr, replace string) {
e.T.Helper()
if err := e.Editor.RegexpReplace(e.Ctx, name, regexpStr, replace); err != nil {
e.T.Fatalf("RegexpReplace: %v", err)
}
}
// SaveBuffer saves an editor buffer, calling t.Fatal on any error.
func (e *Env) SaveBuffer(name string) {
e.T.Helper()
if err := e.Editor.SaveBuffer(e.Ctx, name); err != nil {
e.T.Fatal(err)
}
}
// GoToDefinition goes to definition in the editor, calling t.Fatal on any
// error.
func (e *Env) GoToDefinition(name string, pos fake.Pos) (string, fake.Pos) {
e.T.Helper()
n, p, err := e.Editor.GoToDefinition(e.Ctx, name, pos)
if err != nil {
e.T.Fatal(err)
}
return n, p
}
// Symbol returns symbols matching query
func (e *Env) Symbol(query string) []fake.SymbolInformation {
e.T.Helper()
r, err := e.Editor.Symbol(e.Ctx, query)
if err != nil {
e.T.Fatal(err)
}
return r
}
// FormatBuffer formats the editor buffer, calling t.Fatal on any error.
func (e *Env) FormatBuffer(name string) {
e.T.Helper()
if err := e.Editor.FormatBuffer(e.Ctx, name); err != nil {
e.T.Fatal(err)
}
}
// OrganizeImports processes the source.organizeImports codeAction, calling
// t.Fatal on any error.
func (e *Env) OrganizeImports(name string) {
e.T.Helper()
if err := e.Editor.OrganizeImports(e.Ctx, name); err != nil {
e.T.Fatal(err)
}
}
// ApplyQuickFixes processes the quickfix codeAction, calling t.Fatal on any error.
func (e *Env) ApplyQuickFixes(path string, diagnostics []protocol.Diagnostic) {
e.T.Helper()
if err := e.Editor.ApplyQuickFixes(e.Ctx, path, nil, diagnostics); err != nil {
e.T.Fatal(err)
}
}
// Hover in the editor, calling t.Fatal on any error.
func (e *Env) Hover(name string, pos fake.Pos) (*protocol.MarkupContent, fake.Pos) {
e.T.Helper()
c, p, err := e.Editor.Hover(e.Ctx, name, pos)
if err != nil {
e.T.Fatal(err)
}
return c, p
}
func (e *Env) DocumentLink(name string) []protocol.DocumentLink {
e.T.Helper()
links, err := e.Editor.DocumentLink(e.Ctx, name)
if err != nil {
e.T.Fatal(err)
}
return links
}
func checkIsFatal(t *testing.T, err error) {
t.Helper()
if err != nil && !errors.Is(err, io.EOF) && !errors.Is(err, io.ErrClosedPipe) {
t.Fatal(err)
}
}
// CloseEditor shuts down the editor, calling t.Fatal on any error.
func (e *Env) CloseEditor() {
e.T.Helper()
checkIsFatal(e.T, e.Editor.Close(e.Ctx))
}
// RunGenerate runs go:generate on the given dir, calling t.Fatal on any error.
// It waits for the generate command to complete and checks for file changes
// before returning.
func (e *Env) RunGenerate(dir string) {
e.T.Helper()
if err := e.Editor.RunGenerate(e.Ctx, dir); err != nil {
e.T.Fatal(err)
}
e.Await(NoOutstandingWork())
// Ideally the fake.Workspace would handle all synthetic file watching, but
// we help it out here as we need to wait for the generate command to
// complete before checking the filesystem.
e.CheckForFileChanges()
}
// RunGoCommand runs the given command in the sandbox's default working
// directory.
func (e *Env) RunGoCommand(verb string, args ...string) {
if err := e.Sandbox.RunGoCommand(e.Ctx, "", verb, args); err != nil {
e.T.Fatal(err)
}
}
// CheckForFileChanges triggers a manual poll of the workspace for any file
// changes since creation, or since last polling. It is a workaround for the
// lack of true file watching support in the fake workspace.
func (e *Env) CheckForFileChanges() {
e.T.Helper()
if err := e.Sandbox.Workdir.CheckForFileChanges(e.Ctx); err != nil {
e.T.Fatal(err)
}
}
// CodeLens calls textDocument/codeLens for the given path, calling t.Fatal on
// any error.
func (e *Env) CodeLens(path string) []protocol.CodeLens {
e.T.Helper()
lens, err := e.Editor.CodeLens(e.Ctx, path)
if err != nil {
e.T.Fatal(err)
}
return lens
}
// ExecuteCodeLensCommand executes the command for the code lens matching the
// given command name.
func (e *Env) ExecuteCodeLensCommand(path string, cmd *source.Command) {
lenses := e.CodeLens(path)
var lens protocol.CodeLens
var found bool
for _, l := range lenses {
if l.Command.Command == cmd.Name {
lens = l
found = true
}
}
if !found {
e.T.Fatalf("found no command with the title %s", cmd.Name)
}
if _, err := e.Editor.Server.ExecuteCommand(e.Ctx, &protocol.ExecuteCommandParams{
Command: lens.Command.Command,
Arguments: lens.Command.Arguments,
}); err != nil {
e.T.Fatal(err)
}
}
// References calls textDocument/references for the given path at the given
// position.
func (e *Env) References(path string, pos fake.Pos) []protocol.Location {
e.T.Helper()
locations, err := e.Editor.References(e.Ctx, path, pos)
if err != nil {
e.T.Fatal(err)
}
return locations
}
// Completion executes a completion request on the server.
func (e *Env) Completion(path string, pos fake.Pos) *protocol.CompletionList {
e.T.Helper()
completions, err := e.Editor.Completion(e.Ctx, path, pos)
if err != nil {
e.T.Fatal(err)
}
return completions
}
// CodeAction calls testDocument/codeAction for the given path, and calls
// t.Fatal if there are errors.
func (e *Env) CodeAction(path string) []protocol.CodeAction {
e.T.Helper()
actions, err := e.Editor.CodeAction(e.Ctx, path, nil)
if err != nil {
e.T.Fatal(err)
}
return actions
}
func (e *Env) changeConfiguration(t *testing.T, config *fake.EditorConfig) {
e.Editor.Config = *config
if err := e.Editor.Server.DidChangeConfiguration(e.Ctx, &protocol.DidChangeConfigurationParams{
// gopls currently ignores the Settings field
}); err != nil {
t.Fatal(err)
}
}
// ChangeEnv modifies the editor environment and reconfigures the LSP client.
// TODO: extend this to "ChangeConfiguration", once we refactor the way editor
// configuration is defined.
func (e *Env) ChangeEnv(overlay map[string]string) {
e.T.Helper()
// TODO: to be correct, this should probably be synchronized, but right now
// configuration is only ever modified synchronously in a regtest, so this
// correctness can wait for the previously mentioned refactoring.
if e.Editor.Config.Env == nil {
e.Editor.Config.Env = make(map[string]string)
}
for k, v := range overlay {
e.Editor.Config.Env[k] = v
}
var params protocol.DidChangeConfigurationParams
if err := e.Editor.Server.DidChangeConfiguration(e.Ctx, ¶ms); err != nil {
e.T.Fatal(err)
}
}