1 // Copyright 2020 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 "golang.org/x/tools/internal/lsp/command"
13 "golang.org/x/tools/internal/lsp/fake"
14 "golang.org/x/tools/internal/lsp/protocol"
15 errors "golang.org/x/xerrors"
18 func (e *Env) ChangeFilesOnDisk(events []fake.FileEvent) {
20 if err := e.Sandbox.Workdir.ChangeFilesOnDisk(e.Ctx, events); err != nil {
25 // RemoveWorkspaceFile deletes a file on disk but does nothing in the
26 // editor. It calls t.Fatal on any error.
27 func (e *Env) RemoveWorkspaceFile(name string) {
29 if err := e.Sandbox.Workdir.RemoveFile(e.Ctx, name); err != nil {
34 // ReadWorkspaceFile reads a file from the workspace, calling t.Fatal on any
36 func (e *Env) ReadWorkspaceFile(name string) string {
38 content, err := e.Sandbox.Workdir.ReadFile(name)
45 // WriteWorkspaceFile writes a file to disk but does nothing in the editor.
46 // It calls t.Fatal on any error.
47 func (e *Env) WriteWorkspaceFile(name, content string) {
49 if err := e.Sandbox.Workdir.WriteFile(e.Ctx, name, content); err != nil {
54 // WriteWorkspaceFiles deletes a file on disk but does nothing in the
55 // editor. It calls t.Fatal on any error.
56 func (e *Env) WriteWorkspaceFiles(files map[string]string) {
58 if err := e.Sandbox.Workdir.WriteFiles(e.Ctx, files); err != nil {
63 // OpenFile opens a file in the editor, calling t.Fatal on any error.
64 func (e *Env) OpenFile(name string) {
66 if err := e.Editor.OpenFile(e.Ctx, name); err != nil {
71 // CreateBuffer creates a buffer in the editor, calling t.Fatal on any error.
72 func (e *Env) CreateBuffer(name string, content string) {
74 if err := e.Editor.CreateBuffer(e.Ctx, name, content); err != nil {
79 // CloseBuffer closes an editor buffer without saving, calling t.Fatal on any
81 func (e *Env) CloseBuffer(name string) {
83 if err := e.Editor.CloseBuffer(e.Ctx, name); err != nil {
88 // EditBuffer applies edits to an editor buffer, calling t.Fatal on any error.
89 func (e *Env) EditBuffer(name string, edits ...fake.Edit) {
91 if err := e.Editor.EditBuffer(e.Ctx, name, edits); err != nil {
96 func (e *Env) SetBufferContent(name string, content string) {
98 if err := e.Editor.SetBufferContent(e.Ctx, name, content); err != nil {
103 // RegexpRange returns the range of the first match for re in the buffer
104 // specified by name, calling t.Fatal on any error. It first searches for the
105 // position in open buffers, then in workspace files.
106 func (e *Env) RegexpRange(name, re string) (fake.Pos, fake.Pos) {
108 start, end, err := e.Editor.RegexpRange(name, re)
109 if err == fake.ErrUnknownBuffer {
110 start, end, err = e.Sandbox.Workdir.RegexpRange(name, re)
113 e.T.Fatalf("RegexpRange: %v, %v", name, err)
118 // RegexpSearch returns the starting position of the first match for re in the
119 // buffer specified by name, calling t.Fatal on any error. It first searches
120 // for the position in open buffers, then in workspace files.
121 func (e *Env) RegexpSearch(name, re string) fake.Pos {
123 pos, err := e.Editor.RegexpSearch(name, re)
124 if err == fake.ErrUnknownBuffer {
125 pos, err = e.Sandbox.Workdir.RegexpSearch(name, re)
128 e.T.Fatalf("RegexpSearch: %v, %v", name, err)
133 // RegexpReplace replaces the first group in the first match of regexpStr with
134 // the replace text, calling t.Fatal on any error.
135 func (e *Env) RegexpReplace(name, regexpStr, replace string) {
137 if err := e.Editor.RegexpReplace(e.Ctx, name, regexpStr, replace); err != nil {
138 e.T.Fatalf("RegexpReplace: %v", err)
142 // SaveBuffer saves an editor buffer, calling t.Fatal on any error.
143 func (e *Env) SaveBuffer(name string) {
145 if err := e.Editor.SaveBuffer(e.Ctx, name); err != nil {
150 func (e *Env) SaveBufferWithoutActions(name string) {
152 if err := e.Editor.SaveBufferWithoutActions(e.Ctx, name); err != nil {
157 // GoToDefinition goes to definition in the editor, calling t.Fatal on any
159 func (e *Env) GoToDefinition(name string, pos fake.Pos) (string, fake.Pos) {
161 n, p, err := e.Editor.GoToDefinition(e.Ctx, name, pos)
168 // Symbol returns symbols matching query
169 func (e *Env) Symbol(query string) []fake.SymbolInformation {
171 r, err := e.Editor.Symbol(e.Ctx, query)
178 // FormatBuffer formats the editor buffer, calling t.Fatal on any error.
179 func (e *Env) FormatBuffer(name string) {
181 if err := e.Editor.FormatBuffer(e.Ctx, name); err != nil {
186 // OrganizeImports processes the source.organizeImports codeAction, calling
187 // t.Fatal on any error.
188 func (e *Env) OrganizeImports(name string) {
190 if err := e.Editor.OrganizeImports(e.Ctx, name); err != nil {
195 // ApplyQuickFixes processes the quickfix codeAction, calling t.Fatal on any error.
196 func (e *Env) ApplyQuickFixes(path string, diagnostics []protocol.Diagnostic) {
198 if err := e.Editor.ApplyQuickFixes(e.Ctx, path, nil, diagnostics); err != nil {
203 // GetQuickFixes returns the available quick fix code actions.
204 func (e *Env) GetQuickFixes(path string, diagnostics []protocol.Diagnostic) []protocol.CodeAction {
206 actions, err := e.Editor.GetQuickFixes(e.Ctx, path, nil, diagnostics)
213 // Hover in the editor, calling t.Fatal on any error.
214 func (e *Env) Hover(name string, pos fake.Pos) (*protocol.MarkupContent, fake.Pos) {
216 c, p, err := e.Editor.Hover(e.Ctx, name, pos)
223 func (e *Env) DocumentLink(name string) []protocol.DocumentLink {
225 links, err := e.Editor.DocumentLink(e.Ctx, name)
232 func checkIsFatal(t *testing.T, err error) {
234 if err != nil && !errors.Is(err, io.EOF) && !errors.Is(err, io.ErrClosedPipe) {
239 // CloseEditor shuts down the editor, calling t.Fatal on any error.
240 func (e *Env) CloseEditor() {
242 checkIsFatal(e.T, e.Editor.Close(e.Ctx))
245 // RunGenerate runs go:generate on the given dir, calling t.Fatal on any error.
246 // It waits for the generate command to complete and checks for file changes
248 func (e *Env) RunGenerate(dir string) {
250 if err := e.Editor.RunGenerate(e.Ctx, dir); err != nil {
253 e.Await(NoOutstandingWork())
254 // Ideally the fake.Workspace would handle all synthetic file watching, but
255 // we help it out here as we need to wait for the generate command to
256 // complete before checking the filesystem.
257 e.CheckForFileChanges()
260 // RunGoCommand runs the given command in the sandbox's default working
262 func (e *Env) RunGoCommand(verb string, args ...string) {
264 if err := e.Sandbox.RunGoCommand(e.Ctx, "", verb, args); err != nil {
269 // RunGoCommandInDir is like RunGoCommand, but executes in the given
270 // relative directory of the sandbox.
271 func (e *Env) RunGoCommandInDir(dir, verb string, args ...string) {
273 if err := e.Sandbox.RunGoCommand(e.Ctx, dir, verb, args); err != nil {
278 // DumpGoSum prints the correct go.sum contents for dir in txtar format,
279 // for use in creating regtests.
280 func (e *Env) DumpGoSum(dir string) {
283 if err := e.Sandbox.RunGoCommand(e.Ctx, dir, "list", []string{"-mod=mod", "..."}); err != nil {
286 sumFile := path.Join(dir, "/go.sum")
287 e.T.Log("\n\n-- " + sumFile + " --\n" + e.ReadWorkspaceFile(sumFile))
288 e.T.Fatal("see contents above")
291 // CheckForFileChanges triggers a manual poll of the workspace for any file
292 // changes since creation, or since last polling. It is a workaround for the
293 // lack of true file watching support in the fake workspace.
294 func (e *Env) CheckForFileChanges() {
296 if err := e.Sandbox.Workdir.CheckForFileChanges(e.Ctx); err != nil {
301 // CodeLens calls textDocument/codeLens for the given path, calling t.Fatal on
303 func (e *Env) CodeLens(path string) []protocol.CodeLens {
305 lens, err := e.Editor.CodeLens(e.Ctx, path)
312 // ExecuteCodeLensCommand executes the command for the code lens matching the
313 // given command name.
314 func (e *Env) ExecuteCodeLensCommand(path string, cmd command.Command) {
315 lenses := e.CodeLens(path)
316 var lens protocol.CodeLens
318 for _, l := range lenses {
319 if l.Command.Command == cmd.ID() {
325 e.T.Fatalf("found no command with the ID %s", cmd.ID())
327 if _, err := e.Editor.ExecuteCommand(e.Ctx, &protocol.ExecuteCommandParams{
328 Command: lens.Command.Command,
329 Arguments: lens.Command.Arguments,
335 // References calls textDocument/references for the given path at the given
337 func (e *Env) References(path string, pos fake.Pos) []protocol.Location {
339 locations, err := e.Editor.References(e.Ctx, path, pos)
346 // Completion executes a completion request on the server.
347 func (e *Env) Completion(path string, pos fake.Pos) *protocol.CompletionList {
349 completions, err := e.Editor.Completion(e.Ctx, path, pos)
356 // AcceptCompletion accepts a completion for the given item at the given
358 func (e *Env) AcceptCompletion(path string, pos fake.Pos, item protocol.CompletionItem) {
360 if err := e.Editor.AcceptCompletion(e.Ctx, path, pos, item); err != nil {
365 // CodeAction calls testDocument/codeAction for the given path, and calls
366 // t.Fatal if there are errors.
367 func (e *Env) CodeAction(path string) []protocol.CodeAction {
369 actions, err := e.Editor.CodeAction(e.Ctx, path, nil)
376 func (e *Env) ChangeConfiguration(t *testing.T, config *fake.EditorConfig) {
377 e.Editor.Config = *config
378 if err := e.Editor.Server.DidChangeConfiguration(e.Ctx, &protocol.DidChangeConfigurationParams{
379 // gopls currently ignores the Settings field
385 // ChangeEnv modifies the editor environment and reconfigures the LSP client.
386 // TODO: extend this to "ChangeConfiguration", once we refactor the way editor
387 // configuration is defined.
388 func (e *Env) ChangeEnv(overlay map[string]string) {
390 // TODO: to be correct, this should probably be synchronized, but right now
391 // configuration is only ever modified synchronously in a regtest, so this
392 // correctness can wait for the previously mentioned refactoring.
393 if e.Editor.Config.Env == nil {
394 e.Editor.Config.Env = make(map[string]string)
396 for k, v := range overlay {
397 e.Editor.Config.Env[k] = v
399 var params protocol.DidChangeConfigurationParams
400 if err := e.Editor.Server.DidChangeConfiguration(e.Ctx, ¶ms); err != nil {