Skip to content

Commit 9ba8bb1

Browse files
committed
gopls/internal/regtest: clean up workspace symbol helpers
Consolidate the redundant env.Symbol and env.WorkspaceSymbol wrappers, and eliminate the unnecessary wrapper types. Updates golang/go#39384 Change-Id: Ibe3b7ca89c531a914e5044a7dc45ac30e7210f1b Reviewed-on: https://go-review.googlesource.com/c/tools/+/461897 gopls-CI: kokoro <[email protected]> Reviewed-by: Alan Donovan <[email protected]> Run-TryBot: Robert Findley <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 91b6070 commit 9ba8bb1

File tree

7 files changed

+19
-62
lines changed

7 files changed

+19
-62
lines changed

gopls/internal/lsp/fake/edit.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,6 @@ import (
99
"golang.org/x/tools/internal/diff"
1010
)
1111

12-
// Location is the editor friendly equivalent of protocol.Location
13-
type Location struct {
14-
Path string
15-
Range protocol.Range
16-
}
17-
18-
// SymbolInformation is an editor friendly version of
19-
// protocol.SymbolInformation, with location information transformed to byte
20-
// offsets. Field names correspond to the protocol type.
21-
type SymbolInformation struct {
22-
Name string
23-
Kind protocol.SymbolKind
24-
Location Location
25-
}
26-
2712
// NewEdit creates an edit replacing all content between
2813
// (startLine, startColumn) and (endLine, endColumn) with text.
2914
func NewEdit(startLine, startColumn, endLine, endColumn uint32, text string) protocol.TextEdit {

gopls/internal/lsp/fake/editor.go

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -815,29 +815,11 @@ func (e *Editor) extractFirstPathAndPos(ctx context.Context, locs []protocol.Loc
815815
}
816816

817817
// Symbol performs a workspace symbol search using query
818-
func (e *Editor) Symbol(ctx context.Context, query string) ([]SymbolInformation, error) {
818+
func (e *Editor) Symbol(ctx context.Context, query string) ([]protocol.SymbolInformation, error) {
819819
params := &protocol.WorkspaceSymbolParams{}
820820
params.Query = query
821821

822-
resp, err := e.Server.Symbol(ctx, params)
823-
if err != nil {
824-
return nil, fmt.Errorf("symbol: %w", err)
825-
}
826-
var res []SymbolInformation
827-
for _, si := range resp {
828-
ploc := si.Location
829-
path := e.sandbox.Workdir.URIToPath(ploc.URI)
830-
loc := Location{
831-
Path: path,
832-
Range: ploc.Range,
833-
}
834-
res = append(res, SymbolInformation{
835-
Name: si.Name,
836-
Kind: si.Kind,
837-
Location: loc,
838-
})
839-
}
840-
return res, nil
822+
return e.Server.Symbol(ctx, params)
841823
}
842824

843825
// OrganizeImports requests and performs the source.organizeImports codeAction.

gopls/internal/lsp/regtest/wrappers.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,6 @@ func (e *Env) GoToDefinition(name string, pos protocol.Position) (string, protoc
178178
return n, p
179179
}
180180

181-
// Symbol returns symbols matching query
182-
func (e *Env) Symbol(query string) []fake.SymbolInformation {
183-
e.T.Helper()
184-
r, err := e.Editor.Symbol(e.Ctx, query)
185-
if err != nil {
186-
e.T.Fatal(err)
187-
}
188-
return r
189-
}
190-
191181
// FormatBuffer formats the editor buffer, calling t.Fatal on any error.
192182
func (e *Env) FormatBuffer(name string) {
193183
e.T.Helper()
@@ -394,10 +384,10 @@ func (e *Env) InlayHints(path string) []protocol.InlayHint {
394384
return hints
395385
}
396386

397-
// WorkspaceSymbol calls workspace/symbol
398-
func (e *Env) WorkspaceSymbol(sym string) []protocol.SymbolInformation {
387+
// Symbol calls workspace/symbol
388+
func (e *Env) Symbol(query string) []protocol.SymbolInformation {
399389
e.T.Helper()
400-
ans, err := e.Editor.Symbols(e.Ctx, sym)
390+
ans, err := e.Editor.Symbols(e.Ctx, query)
401391
if err != nil {
402392
e.T.Fatal(err)
403393
}

gopls/internal/regtest/bench/workspace_symbols_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func BenchmarkWorkspaceSymbols(b *testing.B) {
1818
env := benchmarkEnv(b)
1919

2020
// Make an initial symbol query to warm the cache.
21-
symbols := env.WorkspaceSymbol(*symbolQuery)
21+
symbols := env.Symbol(*symbolQuery)
2222

2323
if testing.Verbose() {
2424
fmt.Println("Results:")
@@ -30,6 +30,6 @@ func BenchmarkWorkspaceSymbols(b *testing.B) {
3030
b.ResetTimer()
3131

3232
for i := 0; i < b.N; i++ {
33-
env.WorkspaceSymbol(*symbolQuery)
33+
env.Symbol(*symbolQuery)
3434
}
3535
}

gopls/internal/regtest/misc/workspace_symbol_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ const C2 = "exclude.go"
3434

3535
Run(t, files, func(t *testing.T, env *Env) {
3636
env.OpenFile("a.go")
37-
syms := env.WorkspaceSymbol("C")
37+
syms := env.Symbol("C")
3838
if got, want := len(syms), 1; got != want {
3939
t.Errorf("got %d symbols, want %d", got, want)
4040
}
4141

4242
// Opening up an ignored file will result in an overlay with missing
4343
// metadata, but this shouldn't break workspace symbols requests.
4444
env.OpenFile("exclude.go")
45-
syms = env.WorkspaceSymbol("C")
45+
syms = env.Symbol("C")
4646
if got, want := len(syms), 1; got != want {
4747
t.Errorf("got %d symbols, want %d", got, want)
4848
}
@@ -78,7 +78,7 @@ const (
7878
"Fooey", // shorter than Fooest, Foobar
7979
"Fooest",
8080
}
81-
got := env.WorkspaceSymbol("Foo")
81+
got := env.Symbol("Foo")
8282
compareSymbols(t, got, want)
8383
})
8484
}
@@ -102,11 +102,11 @@ const (
102102
WithOptions(
103103
Settings{"symbolMatcher": symbolMatcher},
104104
).Run(t, files, func(t *testing.T, env *Env) {
105-
compareSymbols(t, env.WorkspaceSymbol("ABC"), []string{"ABC", "AxxBxxCxx"})
106-
compareSymbols(t, env.WorkspaceSymbol("'ABC"), []string{"ABC"})
107-
compareSymbols(t, env.WorkspaceSymbol("^mod.com"), []string{"mod.com/a.ABC", "mod.com/a.AxxBxxCxx"})
108-
compareSymbols(t, env.WorkspaceSymbol("^mod.com Axx"), []string{"mod.com/a.AxxBxxCxx"})
109-
compareSymbols(t, env.WorkspaceSymbol("C$"), []string{"ABC"})
105+
compareSymbols(t, env.Symbol("ABC"), []string{"ABC", "AxxBxxCxx"})
106+
compareSymbols(t, env.Symbol("'ABC"), []string{"ABC"})
107+
compareSymbols(t, env.Symbol("^mod.com"), []string{"mod.com/a.ABC", "mod.com/a.AxxBxxCxx"})
108+
compareSymbols(t, env.Symbol("^mod.com Axx"), []string{"mod.com/a.AxxBxxCxx"})
109+
compareSymbols(t, env.Symbol("C$"), []string{"ABC"})
110110
})
111111
}
112112

gopls/internal/regtest/workspace/directoryfilters_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestDirectoryFilters(t *testing.T) {
2727
"directoryFilters": []string{"-inner"},
2828
},
2929
).Run(t, workspaceModule, func(t *testing.T, env *Env) {
30-
syms := env.WorkspaceSymbol("Hi")
30+
syms := env.Symbol("Hi")
3131
sort.Slice(syms, func(i, j int) bool { return syms[i].ContainerName < syms[j].ContainerName })
3232
for _, s := range syms {
3333
if strings.Contains(s.ContainerName, "inner") {
@@ -149,7 +149,7 @@ func TestDirectoryFilters_Wildcard(t *testing.T) {
149149
"directoryFilters": filters,
150150
},
151151
).Run(t, workspaceModule, func(t *testing.T, env *Env) {
152-
syms := env.WorkspaceSymbol("Bye")
152+
syms := env.Symbol("Bye")
153153
sort.Slice(syms, func(i, j int) bool { return syms[i].ContainerName < syms[j].ContainerName })
154154
for _, s := range syms {
155155
if strings.Contains(s.ContainerName, "bye") {

gopls/internal/regtest/workspace/standalone_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func main() {
5454
).Run(t, files, func(t *testing.T, env *Env) {
5555
// Initially, gopls should not know about the standalone file as it hasn't
5656
// been opened. Therefore, we should only find one symbol 'C'.
57-
syms := env.WorkspaceSymbol("C")
57+
syms := env.Symbol("C")
5858
if got, want := len(syms), 1; got != want {
5959
t.Errorf("got %d symbols, want %d", got, want)
6060
}
@@ -95,7 +95,7 @@ func main() {
9595

9696
// Having opened the standalone file, we should find its symbols in the
9797
// workspace.
98-
syms = env.WorkspaceSymbol("C")
98+
syms = env.Symbol("C")
9999
if got, want := len(syms), 2; got != want {
100100
t.Fatalf("got %d symbols, want %d", got, want)
101101
}

0 commit comments

Comments
 (0)