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 <noreply+kokoro@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
diff --git a/gopls/internal/lsp/fake/edit.go b/gopls/internal/lsp/fake/edit.go
index e7eeca1..0b688e7 100644
--- a/gopls/internal/lsp/fake/edit.go
+++ b/gopls/internal/lsp/fake/edit.go
@@ -9,21 +9,6 @@
"golang.org/x/tools/internal/diff"
)
-// Location is the editor friendly equivalent of protocol.Location
-type Location struct {
- Path string
- Range protocol.Range
-}
-
-// SymbolInformation is an editor friendly version of
-// protocol.SymbolInformation, with location information transformed to byte
-// offsets. Field names correspond to the protocol type.
-type SymbolInformation struct {
- Name string
- Kind protocol.SymbolKind
- Location Location
-}
-
// NewEdit creates an edit replacing all content between
// (startLine, startColumn) and (endLine, endColumn) with text.
func NewEdit(startLine, startColumn, endLine, endColumn uint32, text string) protocol.TextEdit {
diff --git a/gopls/internal/lsp/fake/editor.go b/gopls/internal/lsp/fake/editor.go
index 00bba62..7a61785 100644
--- a/gopls/internal/lsp/fake/editor.go
+++ b/gopls/internal/lsp/fake/editor.go
@@ -815,29 +815,11 @@
}
// Symbol performs a workspace symbol search using query
-func (e *Editor) Symbol(ctx context.Context, query string) ([]SymbolInformation, error) {
+func (e *Editor) Symbol(ctx context.Context, query string) ([]protocol.SymbolInformation, error) {
params := &protocol.WorkspaceSymbolParams{}
params.Query = query
- resp, err := e.Server.Symbol(ctx, params)
- if err != nil {
- return nil, fmt.Errorf("symbol: %w", err)
- }
- var res []SymbolInformation
- for _, si := range resp {
- ploc := si.Location
- path := e.sandbox.Workdir.URIToPath(ploc.URI)
- loc := Location{
- Path: path,
- Range: ploc.Range,
- }
- res = append(res, SymbolInformation{
- Name: si.Name,
- Kind: si.Kind,
- Location: loc,
- })
- }
- return res, nil
+ return e.Server.Symbol(ctx, params)
}
// OrganizeImports requests and performs the source.organizeImports codeAction.
diff --git a/gopls/internal/lsp/regtest/wrappers.go b/gopls/internal/lsp/regtest/wrappers.go
index 3699597..03f3551 100644
--- a/gopls/internal/lsp/regtest/wrappers.go
+++ b/gopls/internal/lsp/regtest/wrappers.go
@@ -178,16 +178,6 @@
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()
@@ -394,10 +384,10 @@
return hints
}
-// WorkspaceSymbol calls workspace/symbol
-func (e *Env) WorkspaceSymbol(sym string) []protocol.SymbolInformation {
+// Symbol calls workspace/symbol
+func (e *Env) Symbol(query string) []protocol.SymbolInformation {
e.T.Helper()
- ans, err := e.Editor.Symbols(e.Ctx, sym)
+ ans, err := e.Editor.Symbols(e.Ctx, query)
if err != nil {
e.T.Fatal(err)
}
diff --git a/gopls/internal/regtest/bench/workspace_symbols_test.go b/gopls/internal/regtest/bench/workspace_symbols_test.go
index fccc818..a540dfd 100644
--- a/gopls/internal/regtest/bench/workspace_symbols_test.go
+++ b/gopls/internal/regtest/bench/workspace_symbols_test.go
@@ -18,7 +18,7 @@
env := benchmarkEnv(b)
// Make an initial symbol query to warm the cache.
- symbols := env.WorkspaceSymbol(*symbolQuery)
+ symbols := env.Symbol(*symbolQuery)
if testing.Verbose() {
fmt.Println("Results:")
@@ -30,6 +30,6 @@
b.ResetTimer()
for i := 0; i < b.N; i++ {
- env.WorkspaceSymbol(*symbolQuery)
+ env.Symbol(*symbolQuery)
}
}
diff --git a/gopls/internal/regtest/misc/workspace_symbol_test.go b/gopls/internal/regtest/misc/workspace_symbol_test.go
index eb150f6..33b8147 100644
--- a/gopls/internal/regtest/misc/workspace_symbol_test.go
+++ b/gopls/internal/regtest/misc/workspace_symbol_test.go
@@ -34,7 +34,7 @@
Run(t, files, func(t *testing.T, env *Env) {
env.OpenFile("a.go")
- syms := env.WorkspaceSymbol("C")
+ syms := env.Symbol("C")
if got, want := len(syms), 1; got != want {
t.Errorf("got %d symbols, want %d", got, want)
}
@@ -42,7 +42,7 @@
// Opening up an ignored file will result in an overlay with missing
// metadata, but this shouldn't break workspace symbols requests.
env.OpenFile("exclude.go")
- syms = env.WorkspaceSymbol("C")
+ syms = env.Symbol("C")
if got, want := len(syms), 1; got != want {
t.Errorf("got %d symbols, want %d", got, want)
}
@@ -78,7 +78,7 @@
"Fooey", // shorter than Fooest, Foobar
"Fooest",
}
- got := env.WorkspaceSymbol("Foo")
+ got := env.Symbol("Foo")
compareSymbols(t, got, want)
})
}
@@ -102,11 +102,11 @@
WithOptions(
Settings{"symbolMatcher": symbolMatcher},
).Run(t, files, func(t *testing.T, env *Env) {
- compareSymbols(t, env.WorkspaceSymbol("ABC"), []string{"ABC", "AxxBxxCxx"})
- compareSymbols(t, env.WorkspaceSymbol("'ABC"), []string{"ABC"})
- compareSymbols(t, env.WorkspaceSymbol("^mod.com"), []string{"mod.com/a.ABC", "mod.com/a.AxxBxxCxx"})
- compareSymbols(t, env.WorkspaceSymbol("^mod.com Axx"), []string{"mod.com/a.AxxBxxCxx"})
- compareSymbols(t, env.WorkspaceSymbol("C$"), []string{"ABC"})
+ compareSymbols(t, env.Symbol("ABC"), []string{"ABC", "AxxBxxCxx"})
+ compareSymbols(t, env.Symbol("'ABC"), []string{"ABC"})
+ compareSymbols(t, env.Symbol("^mod.com"), []string{"mod.com/a.ABC", "mod.com/a.AxxBxxCxx"})
+ compareSymbols(t, env.Symbol("^mod.com Axx"), []string{"mod.com/a.AxxBxxCxx"})
+ compareSymbols(t, env.Symbol("C$"), []string{"ABC"})
})
}
diff --git a/gopls/internal/regtest/workspace/directoryfilters_test.go b/gopls/internal/regtest/workspace/directoryfilters_test.go
index 5db8d89..6e2a155 100644
--- a/gopls/internal/regtest/workspace/directoryfilters_test.go
+++ b/gopls/internal/regtest/workspace/directoryfilters_test.go
@@ -27,7 +27,7 @@
"directoryFilters": []string{"-inner"},
},
).Run(t, workspaceModule, func(t *testing.T, env *Env) {
- syms := env.WorkspaceSymbol("Hi")
+ syms := env.Symbol("Hi")
sort.Slice(syms, func(i, j int) bool { return syms[i].ContainerName < syms[j].ContainerName })
for _, s := range syms {
if strings.Contains(s.ContainerName, "inner") {
@@ -149,7 +149,7 @@
"directoryFilters": filters,
},
).Run(t, workspaceModule, func(t *testing.T, env *Env) {
- syms := env.WorkspaceSymbol("Bye")
+ syms := env.Symbol("Bye")
sort.Slice(syms, func(i, j int) bool { return syms[i].ContainerName < syms[j].ContainerName })
for _, s := range syms {
if strings.Contains(s.ContainerName, "bye") {
diff --git a/gopls/internal/regtest/workspace/standalone_test.go b/gopls/internal/regtest/workspace/standalone_test.go
index d7f0346..c9618eb 100644
--- a/gopls/internal/regtest/workspace/standalone_test.go
+++ b/gopls/internal/regtest/workspace/standalone_test.go
@@ -54,7 +54,7 @@
).Run(t, files, func(t *testing.T, env *Env) {
// Initially, gopls should not know about the standalone file as it hasn't
// been opened. Therefore, we should only find one symbol 'C'.
- syms := env.WorkspaceSymbol("C")
+ syms := env.Symbol("C")
if got, want := len(syms), 1; got != want {
t.Errorf("got %d symbols, want %d", got, want)
}
@@ -95,7 +95,7 @@
// Having opened the standalone file, we should find its symbols in the
// workspace.
- syms = env.WorkspaceSymbol("C")
+ syms = env.Symbol("C")
if got, want := len(syms), 2; got != want {
t.Fatalf("got %d symbols, want %d", got, want)
}