forked from golang/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tools/gopls: add cmd support for workspace_symbol
This change adds command line support for workspace/symbol. Symbols are formatted as '{span} {name} {type}'. $ gopls workspace_symbol -matcher fuzzy 'wsymbols' $ $ workspacesymbol/a/a.go:5:7-31 WorkspaceSymbolConstantA Constant $ workspacesymbol/b/b.go:5:6-28 WorkspaceSymbolStructB Struct Optional arguments are: -matcher, which specifies the type of matcher: fuzzy, caseSensitive, or caseInsensitive. The default is caseInsensitive. Updates golang/go#32875 Change-Id: Ieef443b13710f9c973210e58f66ab7679f258b30
- Loading branch information
Showing
11 changed files
with
192 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright 2020 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package cmdtest | ||
|
||
import ( | ||
"path/filepath" | ||
"sort" | ||
"strings" | ||
"testing" | ||
|
||
"golang.org/x/tools/internal/lsp/protocol" | ||
) | ||
|
||
func (r *runner) WorkspaceSymbols(t *testing.T, query string, expectedSymbols []protocol.SymbolInformation, dirs map[string]struct{}) { | ||
r.runWorkspaceSymbols(t, "default", query, dirs) | ||
} | ||
|
||
func (r *runner) FuzzyWorkspaceSymbols(t *testing.T, query string, expectedSymbols []protocol.SymbolInformation, dirs map[string]struct{}) { | ||
r.runWorkspaceSymbols(t, "fuzzy", query, dirs) | ||
} | ||
|
||
func (r *runner) CaseSensitiveWorkspaceSymbols(t *testing.T, query string, expectedSymbols []protocol.SymbolInformation, dirs map[string]struct{}) { | ||
r.runWorkspaceSymbols(t, "caseSensitive", query, dirs) | ||
} | ||
|
||
func (r *runner) runWorkspaceSymbols(t *testing.T, matcher, query string, dirs map[string]struct{}) { | ||
t.Helper() | ||
|
||
out, _ := r.runGoplsCmd(t, "workspace_symbol", "-matcher", matcher, query) | ||
var filtered []string | ||
for _, line := range strings.Split(out, "\n") { | ||
for dir := range dirs { | ||
if strings.HasPrefix(line, dir) { | ||
filtered = append(filtered, line) | ||
break | ||
} | ||
} | ||
} | ||
sort.Strings(filtered) | ||
got := r.Normalize(strings.Join(filtered, "\n")) | ||
|
||
expect := string(r.data.Golden("workspace_symbol", workspaceSymbolsGolden(matcher, query), func() ([]byte, error) { | ||
return []byte(got), nil | ||
})) | ||
|
||
if expect != got { | ||
t.Errorf("workspace_symbol failed for %s expected:\n%s\ngot:\n%s", query, expect, got) | ||
} | ||
} | ||
|
||
var workspaceSymbolsDir = map[string]string{ | ||
"default": "", | ||
"fuzzy": "fuzzy", | ||
"caseSensitive": "casesensitive", | ||
} | ||
|
||
func workspaceSymbolsGolden(matcher, query string) string { | ||
dir := []string{"workspacesymbol", workspaceSymbolsDir[matcher]} | ||
if query == "" { | ||
return filepath.Join(append(dir, "EmptyQuery")...) | ||
} | ||
|
||
var name []rune | ||
for _, r := range query { | ||
if 'A' <= r && r <= 'Z' { | ||
// Escape uppercase to '!' + lowercase for case insensitive file systems. | ||
name = append(name, '!', r+'a'-'A') | ||
} else { | ||
name = append(name, r) | ||
} | ||
} | ||
return filepath.Join(append(dir, string(name))...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright 2020 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package cmd | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
|
||
"golang.org/x/tools/internal/lsp/protocol" | ||
"golang.org/x/tools/internal/lsp/source" | ||
"golang.org/x/tools/internal/tool" | ||
) | ||
|
||
// workspaceSymbol implements the workspace_symbol verb for gopls. | ||
type workspaceSymbol struct { | ||
Matcher string `flag:"matcher" help:"specifies the type of matcher: fuzzy, caseSensitive, or caseInsensitive.\nThe default is caseInsensitive."` | ||
|
||
app *Application | ||
} | ||
|
||
func (r *workspaceSymbol) Name() string { return "workspace_symbol" } | ||
func (r *workspaceSymbol) Usage() string { return "<query>" } | ||
func (r *workspaceSymbol) ShortHelp() string { return "search symbols in workspace" } | ||
func (r *workspaceSymbol) DetailedHelp(f *flag.FlagSet) { | ||
fmt.Fprint(f.Output(), ` | ||
Example: | ||
$ gopls workspace_symbol -matcher fuzzy 'wsymbols' | ||
gopls workspace_symbol flags are: | ||
`) | ||
f.PrintDefaults() | ||
} | ||
|
||
func (r *workspaceSymbol) Run(ctx context.Context, args ...string) error { | ||
if len(args) != 1 { | ||
return tool.CommandLineErrorf("workspace_symbol expects 1 argument") | ||
} | ||
|
||
opts := r.app.options | ||
r.app.options = func(o *source.Options) { | ||
if opts != nil { | ||
opts(o) | ||
} | ||
switch r.Matcher { | ||
case "fuzzy": | ||
o.Matcher = source.Fuzzy | ||
case "caseSensitive": | ||
o.Matcher = source.CaseSensitive | ||
default: | ||
o.Matcher = source.CaseInsensitive | ||
} | ||
} | ||
|
||
conn, err := r.app.connect(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
defer conn.terminate(ctx) | ||
|
||
p := protocol.WorkspaceSymbolParams{ | ||
Query: args[0], | ||
} | ||
|
||
symbols, err := conn.Symbol(ctx, &p) | ||
if err != nil { | ||
return err | ||
} | ||
for _, s := range symbols { | ||
f := conn.AddFile(ctx, fileURI(s.Location.URI)) | ||
span, err := f.mapper.Span(s.Location) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("%s %s %s\n", span, s.Name, s.Kind) | ||
} | ||
|
||
return nil | ||
} |
2 changes: 2 additions & 0 deletions
2
internal/lsp/testdata/lsp/primarymod/workspacesymbol/EmptyQuery.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- workspace_symbol -- | ||
|
2 changes: 2 additions & 0 deletions
2
internal/lsp/testdata/lsp/primarymod/workspacesymbol/casesensitive/!dunk.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- workspace_symbol -- | ||
symbols/main.go:58:6-10 Dunk Function |
2 changes: 2 additions & 0 deletions
2
internal/lsp/testdata/lsp/primarymod/workspacesymbol/casesensitive/dunk.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- workspace_symbol -- | ||
symbols/main.go:60:6-10 dunk Function |
5 changes: 5 additions & 0 deletions
5
internal/lsp/testdata/lsp/primarymod/workspacesymbol/fuzzy/symbola.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
-- workspace_symbol -- | ||
workspacesymbol/a/a.go:3:5-29 WorkspaceSymbolVariableA Variable | ||
workspacesymbol/a/a.go:5:7-31 WorkspaceSymbolConstantA Constant | ||
workspacesymbol/a/a.go:8:2-27 workspacesymbolinvariable Constant | ||
workspacesymbol/b/b.go:3:5-29 WorkspaceSymbolVariableB Variable |
5 changes: 5 additions & 0 deletions
5
internal/lsp/testdata/lsp/primarymod/workspacesymbol/fuzzy/symbolb.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
-- workspace_symbol -- | ||
workspacesymbol/a/a.go:3:5-29 WorkspaceSymbolVariableA Variable | ||
workspacesymbol/a/a.go:8:2-27 workspacesymbolinvariable Constant | ||
workspacesymbol/b/b.go:3:5-29 WorkspaceSymbolVariableB Variable | ||
workspacesymbol/b/b.go:5:6-28 WorkspaceSymbolStructB Struct |
6 changes: 6 additions & 0 deletions
6
internal/lsp/testdata/lsp/primarymod/workspacesymbol/fuzzy/wsym.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
-- workspace_symbol -- | ||
workspacesymbol/a/a.go:3:5-29 WorkspaceSymbolVariableA Variable | ||
workspacesymbol/a/a.go:5:7-31 WorkspaceSymbolConstantA Constant | ||
workspacesymbol/a/a.go:8:2-27 workspacesymbolinvariable Constant | ||
workspacesymbol/b/b.go:3:5-29 WorkspaceSymbolVariableB Variable | ||
workspacesymbol/b/b.go:5:6-28 WorkspaceSymbolStructB Struct |
3 changes: 3 additions & 0 deletions
3
internal/lsp/testdata/lsp/primarymod/workspacesymbol/workspacesymbolvar.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
-- workspace_symbol -- | ||
workspacesymbol/a/a.go:3:5-29 WorkspaceSymbolVariableA Variable | ||
workspacesymbol/b/b.go:3:5-29 WorkspaceSymbolVariableB Variable |