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.
11 "golang.org/x/tools/internal/lsp/fake"
12 "golang.org/x/tools/internal/lsp/protocol"
13 "golang.org/x/tools/internal/lsp/source"
14 errors "golang.org/x/xerrors"
17 func (e *Env) ChangeFilesOnDisk(events []fake.FileEvent) {
19 if err := e.Sandbox.Workdir.ChangeFilesOnDisk(e.Ctx, events); err != nil {
24 // RemoveWorkspaceFile deletes a file on disk but does nothing in the
25 // editor. It calls t.Fatal on any error.
26 func (e *Env) RemoveWorkspaceFile(name string) {
28 if err := e.Sandbox.Workdir.RemoveFile(e.Ctx, name); err != nil {
33 // ReadWorkspaceFile reads a file from the workspace, calling t.Fatal on any
35 func (e *Env) ReadWorkspaceFile(name string) string {
37 content, err := e.Sandbox.Workdir.ReadFile(name)
44 // WriteWorkspaceFile writes a file to disk but does nothing in the editor.
45 // It calls t.Fatal on any error.
46 func (e *Env) WriteWorkspaceFile(name, content string) {
48 if err := e.Sandbox.Workdir.WriteFile(e.Ctx, name, content); err != nil {
53 // WriteWorkspaceFiles deletes a file on disk but does nothing in the
54 // editor. It calls t.Fatal on any error.
55 func (e *Env) WriteWorkspaceFiles(files map[string]string) {
57 if err := e.Sandbox.Workdir.WriteFiles(e.Ctx, files); err != nil {
62 // OpenFile opens a file in the editor, calling t.Fatal on any error.
63 func (e *Env) OpenFile(name string) {
65 if err := e.Editor.OpenFile(e.Ctx, name); err != nil {
70 // CreateBuffer creates a buffer in the editor, calling t.Fatal on any error.
71 func (e *Env) CreateBuffer(name string, content string) {
73 if err := e.Editor.CreateBuffer(e.Ctx, name, content); err != nil {
78 // CloseBuffer closes an editor buffer without saving, calling t.Fatal on any
80 func (e *Env) CloseBuffer(name string) {
82 if err := e.Editor.CloseBuffer(e.Ctx, name); err != nil {
87 // EditBuffer applies edits to an editor buffer, calling t.Fatal on any error.
88 func (e *Env) EditBuffer(name string, edits ...fake.Edit) {
90 if err := e.Editor.EditBuffer(e.Ctx, name, edits); err != nil {
95 // RegexpRange returns the range of the first match for re in the buffer
96 // specified by name, calling t.Fatal on any error. It first searches for the
97 // position in open buffers, then in workspace files.
98 func (e *Env) RegexpRange(name, re string) (fake.Pos, fake.Pos) {
100 start, end, err := e.Editor.RegexpRange(name, re)
101 if err == fake.ErrUnknownBuffer {
102 start, end, err = e.Sandbox.Workdir.RegexpRange(name, re)
105 e.T.Fatalf("RegexpRange: %v, %v", name, err)
110 // RegexpSearch returns the starting position of the first match for re in the
111 // buffer specified by name, calling t.Fatal on any error. It first searches
112 // for the position in open buffers, then in workspace files.
113 func (e *Env) RegexpSearch(name, re string) fake.Pos {
115 pos, err := e.Editor.RegexpSearch(name, re)
116 if err == fake.ErrUnknownBuffer {
117 pos, err = e.Sandbox.Workdir.RegexpSearch(name, re)
120 e.T.Fatalf("RegexpSearch: %v, %v", name, err)
125 // RegexpReplace replaces the first group in the first match of regexpStr with
126 // the replace text, calling t.Fatal on any error.
127 func (e *Env) RegexpReplace(name, regexpStr, replace string) {
129 if err := e.Editor.RegexpReplace(e.Ctx, name, regexpStr, replace); err != nil {
130 e.T.Fatalf("RegexpReplace: %v", err)
134 // SaveBuffer saves an editor buffer, calling t.Fatal on any error.
135 func (e *Env) SaveBuffer(name string) {
137 if err := e.Editor.SaveBuffer(e.Ctx, name); err != nil {
142 // GoToDefinition goes to definition in the editor, calling t.Fatal on any
144 func (e *Env) GoToDefinition(name string, pos fake.Pos) (string, fake.Pos) {
146 n, p, err := e.Editor.GoToDefinition(e.Ctx, name, pos)
153 // Symbol returns symbols matching query
154 func (e *Env) Symbol(query string) []fake.SymbolInformation {
156 r, err := e.Editor.Symbol(e.Ctx, query)
163 // FormatBuffer formats the editor buffer, calling t.Fatal on any error.
164 func (e *Env) FormatBuffer(name string) {
166 if err := e.Editor.FormatBuffer(e.Ctx, name); err != nil {
171 // OrganizeImports processes the source.organizeImports codeAction, calling
172 // t.Fatal on any error.
173 func (e *Env) OrganizeImports(name string) {
175 if err := e.Editor.OrganizeImports(e.Ctx, name); err != nil {
180 // ApplyQuickFixes processes the quickfix codeAction, calling t.Fatal on any error.
181 func (e *Env) ApplyQuickFixes(path string, diagnostics []protocol.Diagnostic) {
183 if err := e.Editor.ApplyQuickFixes(e.Ctx, path, nil, diagnostics); err != nil {
188 // Hover in the editor, calling t.Fatal on any error.
189 func (e *Env) Hover(name string, pos fake.Pos) (*protocol.MarkupContent, fake.Pos) {
191 c, p, err := e.Editor.Hover(e.Ctx, name, pos)
198 func (e *Env) DocumentLink(name string) []protocol.DocumentLink {
200 links, err := e.Editor.DocumentLink(e.Ctx, name)
207 func checkIsFatal(t *testing.T, err error) {
209 if err != nil && !errors.Is(err, io.EOF) && !errors.Is(err, io.ErrClosedPipe) {
214 // CloseEditor shuts down the editor, calling t.Fatal on any error.
215 func (e *Env) CloseEditor() {
217 checkIsFatal(e.T, e.Editor.Close(e.Ctx))
220 // RunGenerate runs go:generate on the given dir, calling t.Fatal on any error.
221 // It waits for the generate command to complete and checks for file changes
223 func (e *Env) RunGenerate(dir string) {
225 if err := e.Editor.RunGenerate(e.Ctx, dir); err != nil {
228 e.Await(NoOutstandingWork())
229 // Ideally the fake.Workspace would handle all synthetic file watching, but
230 // we help it out here as we need to wait for the generate command to
231 // complete before checking the filesystem.
232 e.CheckForFileChanges()
235 // RunGoCommand runs the given command in the sandbox's default working
237 func (e *Env) RunGoCommand(verb string, args ...string) {
238 if err := e.Sandbox.RunGoCommand(e.Ctx, "", verb, args); err != nil {
243 // CheckForFileChanges triggers a manual poll of the workspace for any file
244 // changes since creation, or since last polling. It is a workaround for the
245 // lack of true file watching support in the fake workspace.
246 func (e *Env) CheckForFileChanges() {
248 if err := e.Sandbox.Workdir.CheckForFileChanges(e.Ctx); err != nil {
253 // CodeLens calls textDocument/codeLens for the given path, calling t.Fatal on
255 func (e *Env) CodeLens(path string) []protocol.CodeLens {
257 lens, err := e.Editor.CodeLens(e.Ctx, path)
264 // ExecuteCodeLensCommand executes the command for the code lens matching the
265 // given command name.
266 func (e *Env) ExecuteCodeLensCommand(path string, cmd *source.Command) {
267 lenses := e.CodeLens(path)
268 var lens protocol.CodeLens
270 for _, l := range lenses {
271 if l.Command.Command == cmd.ID() {
277 e.T.Fatalf("found no command with the ID %s", cmd.ID())
279 if _, err := e.Editor.ExecuteCommand(e.Ctx, &protocol.ExecuteCommandParams{
280 Command: lens.Command.Command,
281 Arguments: lens.Command.Arguments,
287 // References calls textDocument/references for the given path at the given
289 func (e *Env) References(path string, pos fake.Pos) []protocol.Location {
291 locations, err := e.Editor.References(e.Ctx, path, pos)
298 // Completion executes a completion request on the server.
299 func (e *Env) Completion(path string, pos fake.Pos) *protocol.CompletionList {
301 completions, err := e.Editor.Completion(e.Ctx, path, pos)
308 // CodeAction calls testDocument/codeAction for the given path, and calls
309 // t.Fatal if there are errors.
310 func (e *Env) CodeAction(path string) []protocol.CodeAction {
312 actions, err := e.Editor.CodeAction(e.Ctx, path, nil)
319 func (e *Env) changeConfiguration(t *testing.T, config *fake.EditorConfig) {
320 e.Editor.Config = *config
321 if err := e.Editor.Server.DidChangeConfiguration(e.Ctx, &protocol.DidChangeConfigurationParams{
322 // gopls currently ignores the Settings field
328 // ChangeEnv modifies the editor environment and reconfigures the LSP client.
329 // TODO: extend this to "ChangeConfiguration", once we refactor the way editor
330 // configuration is defined.
331 func (e *Env) ChangeEnv(overlay map[string]string) {
333 // TODO: to be correct, this should probably be synchronized, but right now
334 // configuration is only ever modified synchronously in a regtest, so this
335 // correctness can wait for the previously mentioned refactoring.
336 if e.Editor.Config.Env == nil {
337 e.Editor.Config.Env = make(map[string]string)
339 for k, v := range overlay {
340 e.Editor.Config.Env[k] = v
342 var params protocol.DidChangeConfigurationParams
343 if err := e.Editor.Server.DidChangeConfiguration(e.Ctx, ¶ms); err != nil {