Skip to content

Commit

Permalink
gopls/internal/regtest: clean up workspace symbol helpers
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
findleyr committed Jan 13, 2023
1 parent 91b6070 commit 9ba8bb1
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 62 deletions.
15 changes: 0 additions & 15 deletions gopls/internal/lsp/fake/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,6 @@ import (
"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 {
Expand Down
22 changes: 2 additions & 20 deletions gopls/internal/lsp/fake/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -815,29 +815,11 @@ func (e *Editor) extractFirstPathAndPos(ctx context.Context, locs []protocol.Loc
}

// 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.
Expand Down
16 changes: 3 additions & 13 deletions gopls/internal/lsp/regtest/wrappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,6 @@ func (e *Env) GoToDefinition(name string, pos protocol.Position) (string, protoc
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()
Expand Down Expand Up @@ -394,10 +384,10 @@ func (e *Env) InlayHints(path string) []protocol.InlayHint {
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)
}
Expand Down
4 changes: 2 additions & 2 deletions gopls/internal/regtest/bench/workspace_symbols_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func BenchmarkWorkspaceSymbols(b *testing.B) {
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:")
Expand All @@ -30,6 +30,6 @@ func BenchmarkWorkspaceSymbols(b *testing.B) {
b.ResetTimer()

for i := 0; i < b.N; i++ {
env.WorkspaceSymbol(*symbolQuery)
env.Symbol(*symbolQuery)
}
}
16 changes: 8 additions & 8 deletions gopls/internal/regtest/misc/workspace_symbol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ const C2 = "exclude.go"

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)
}

// 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)
}
Expand Down Expand Up @@ -78,7 +78,7 @@ const (
"Fooey", // shorter than Fooest, Foobar
"Fooest",
}
got := env.WorkspaceSymbol("Foo")
got := env.Symbol("Foo")
compareSymbols(t, got, want)
})
}
Expand All @@ -102,11 +102,11 @@ const (
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"})
})
}

Expand Down
4 changes: 2 additions & 2 deletions gopls/internal/regtest/workspace/directoryfilters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestDirectoryFilters(t *testing.T) {
"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") {
Expand Down Expand Up @@ -149,7 +149,7 @@ func TestDirectoryFilters_Wildcard(t *testing.T) {
"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") {
Expand Down
4 changes: 2 additions & 2 deletions gopls/internal/regtest/workspace/standalone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func main() {
).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)
}
Expand Down Expand Up @@ -95,7 +95,7 @@ func main() {

// 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)
}
Expand Down

0 comments on commit 9ba8bb1

Please sign in to comment.