Skip to content

Commit 9aa23ab

Browse files
committed
internal/lsp/regtest: consolidate Env wrappers
Helper functions on the regtest.Env wrapping workspace or editor functionality are moved to a new wrappers.go file. Also, rename 'WriteBuffer' to 'SaveBuffer', for less confusion with 'WriteFile'. Change-Id: Ide9a5cf919dcee6e4a4fbfdb167eddf751a26eeb Reviewed-on: https://go-review.googlesource.com/c/tools/+/221538 Reviewed-by: Rohan Challa <[email protected]> Run-TryBot: Robert Findley <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent b1e4e04 commit 9aa23ab

File tree

3 files changed

+81
-65
lines changed

3 files changed

+81
-65
lines changed

internal/lsp/fake/editor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,9 @@ func (e *Editor) CloseBuffer(ctx context.Context, path string) error {
232232
return nil
233233
}
234234

235-
// WriteBuffer writes the content of the buffer specified by the given path to
235+
// SaveBuffer writes the content of the buffer specified by the given path to
236236
// the filesystem.
237-
func (e *Editor) WriteBuffer(ctx context.Context, path string) error {
237+
func (e *Editor) SaveBuffer(ctx context.Context, path string) error {
238238
e.mu.Lock()
239239
buf, ok := e.buffers[path]
240240
if !ok {

internal/lsp/regtest/env.go

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -273,58 +273,6 @@ func NewEnv(ctx context.Context, t *testing.T, ws *fake.Workspace, ts servertest
273273
return env
274274
}
275275

276-
// RemoveFileFromWorkspace deletes a file on disk but does nothing in the
277-
// editor. It calls t.Fatal on any error.
278-
func (e *Env) RemoveFileFromWorkspace(name string) {
279-
e.t.Helper()
280-
if err := e.W.RemoveFile(e.ctx, name); err != nil {
281-
e.t.Fatal(err)
282-
}
283-
}
284-
285-
// OpenFile opens a file in the editor, calling t.Fatal on any error.
286-
func (e *Env) OpenFile(name string) {
287-
e.t.Helper()
288-
if err := e.E.OpenFile(e.ctx, name); err != nil {
289-
e.t.Fatal(err)
290-
}
291-
}
292-
293-
// CreateBuffer creates a buffer in the editor, calling t.Fatal on any error.
294-
func (e *Env) CreateBuffer(name string, content string) {
295-
e.t.Helper()
296-
if err := e.E.CreateBuffer(e.ctx, name, content); err != nil {
297-
e.t.Fatal(err)
298-
}
299-
}
300-
301-
// CloseBuffer closes an editor buffer, calling t.Fatal on any error.
302-
func (e *Env) CloseBuffer(name string) {
303-
e.t.Helper()
304-
if err := e.E.CloseBuffer(e.ctx, name); err != nil {
305-
e.t.Fatal(err)
306-
}
307-
}
308-
309-
// EditBuffer applies edits to an editor buffer, calling t.Fatal on any error.
310-
func (e *Env) EditBuffer(name string, edits ...fake.Edit) {
311-
e.t.Helper()
312-
if err := e.E.EditBuffer(e.ctx, name, edits); err != nil {
313-
e.t.Fatal(err)
314-
}
315-
}
316-
317-
// GoToDefinition goes to definition in the editor, calling t.Fatal on any
318-
// error.
319-
func (e *Env) GoToDefinition(name string, pos fake.Pos) (string, fake.Pos) {
320-
e.t.Helper()
321-
n, p, err := e.E.GoToDefinition(e.ctx, name, pos)
322-
if err != nil {
323-
e.t.Fatal(err)
324-
}
325-
return n, p
326-
}
327-
328276
func (e *Env) onDiagnostics(_ context.Context, d *protocol.PublishDiagnosticsParams) error {
329277
e.mu.Lock()
330278
defer e.mu.Unlock()
@@ -341,17 +289,6 @@ func (e *Env) onDiagnostics(_ context.Context, d *protocol.PublishDiagnosticsPar
341289
return nil
342290
}
343291

344-
// CloseEditor shuts down the editor, calling t.Fatal on any error.
345-
func (e *Env) CloseEditor() {
346-
e.t.Helper()
347-
if err := e.E.Shutdown(e.ctx); err != nil {
348-
e.t.Fatal(err)
349-
}
350-
if err := e.E.Exit(e.ctx); err != nil {
351-
e.t.Fatal(err)
352-
}
353-
}
354-
355292
func meetsCondition(m map[string]*protocol.PublishDiagnosticsParams, expectations []DiagnosticExpectation) bool {
356293
for _, e := range expectations {
357294
if !e.IsMet(m) {

internal/lsp/regtest/wrappers.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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.
4+
5+
package regtest
6+
7+
import "golang.org/x/tools/internal/lsp/fake"
8+
9+
// RemoveFileFromWorkspace deletes a file on disk but does nothing in the
10+
// editor. It calls t.Fatal on any error.
11+
func (e *Env) RemoveFileFromWorkspace(name string) {
12+
e.t.Helper()
13+
if err := e.W.RemoveFile(e.ctx, name); err != nil {
14+
e.t.Fatal(err)
15+
}
16+
}
17+
18+
// OpenFile opens a file in the editor, calling t.Fatal on any error.
19+
func (e *Env) OpenFile(name string) {
20+
e.t.Helper()
21+
if err := e.E.OpenFile(e.ctx, name); err != nil {
22+
e.t.Fatal(err)
23+
}
24+
}
25+
26+
// CreateBuffer creates a buffer in the editor, calling t.Fatal on any error.
27+
func (e *Env) CreateBuffer(name string, content string) {
28+
e.t.Helper()
29+
if err := e.E.CreateBuffer(e.ctx, name, content); err != nil {
30+
e.t.Fatal(err)
31+
}
32+
}
33+
34+
// CloseBuffer closes an editor buffer without saving, calling t.Fatal on any
35+
// error.
36+
func (e *Env) CloseBuffer(name string) {
37+
e.t.Helper()
38+
if err := e.E.CloseBuffer(e.ctx, name); err != nil {
39+
e.t.Fatal(err)
40+
}
41+
}
42+
43+
// EditBuffer applies edits to an editor buffer, calling t.Fatal on any error.
44+
func (e *Env) EditBuffer(name string, edits ...fake.Edit) {
45+
e.t.Helper()
46+
if err := e.E.EditBuffer(e.ctx, name, edits); err != nil {
47+
e.t.Fatal(err)
48+
}
49+
}
50+
51+
// SaveBuffer saves an editor buffer, calling t.Fatal on any error.
52+
func (e *Env) SaveBuffer(name string) {
53+
e.t.Helper()
54+
if err := e.E.SaveBuffer(e.ctx, name); err != nil {
55+
e.t.Fatal(err)
56+
}
57+
}
58+
59+
// GoToDefinition goes to definition in the editor, calling t.Fatal on any
60+
// error.
61+
func (e *Env) GoToDefinition(name string, pos fake.Pos) (string, fake.Pos) {
62+
e.t.Helper()
63+
n, p, err := e.E.GoToDefinition(e.ctx, name, pos)
64+
if err != nil {
65+
e.t.Fatal(err)
66+
}
67+
return n, p
68+
}
69+
70+
// CloseEditor shuts down the editor, calling t.Fatal on any error.
71+
func (e *Env) CloseEditor() {
72+
e.t.Helper()
73+
if err := e.E.Shutdown(e.ctx); err != nil {
74+
e.t.Fatal(err)
75+
}
76+
if err := e.E.Exit(e.ctx); err != nil {
77+
e.t.Fatal(err)
78+
}
79+
}

0 commit comments

Comments
 (0)