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.
12 "golang.org/x/tools/internal/lsp/protocol"
13 "golang.org/x/tools/internal/lsp/source"
14 "golang.org/x/tools/internal/tool"
17 // workspaceSymbol implements the workspace_symbol verb for gopls.
18 type workspaceSymbol struct {
19 Matcher string `flag:"matcher" help:"specifies the type of matcher: fuzzy, caseSensitive, or caseInsensitive.\nThe default is caseInsensitive."`
24 func (r *workspaceSymbol) Name() string { return "workspace_symbol" }
25 func (r *workspaceSymbol) Usage() string { return "<query>" }
26 func (r *workspaceSymbol) ShortHelp() string { return "search symbols in workspace" }
27 func (r *workspaceSymbol) DetailedHelp(f *flag.FlagSet) {
28 fmt.Fprint(f.Output(), `
31 $ gopls workspace_symbol -matcher fuzzy 'wsymbols'
33 gopls workspace_symbol flags are:
38 func (r *workspaceSymbol) Run(ctx context.Context, args ...string) error {
40 return tool.CommandLineErrorf("workspace_symbol expects 1 argument")
44 r.app.options = func(o *source.Options) {
50 o.SymbolMatcher = source.SymbolFuzzy
52 o.SymbolMatcher = source.SymbolCaseSensitive
54 o.SymbolMatcher = source.SymbolCaseInsensitive
58 conn, err := r.app.connect(ctx)
62 defer conn.terminate(ctx)
64 p := protocol.WorkspaceSymbolParams{
68 symbols, err := conn.Symbol(ctx, &p)
72 for _, s := range symbols {
73 f := conn.AddFile(ctx, fileURI(s.Location.URI))
74 span, err := f.mapper.Span(s.Location)
78 fmt.Printf("%s %s %s\n", span, s.Name, s.Kind)