1
0
mirror of https://github.com/golang/go synced 2024-11-19 08:44:39 -07:00
go/internal/lsp/cmd/workspace_symbol.go
Daisuke Suzuki d58d27dcbb 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
Reviewed-on: https://go-review.googlesource.com/c/tools/+/224677
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-03-26 19:59:04 +00:00

83 lines
1.9 KiB
Go

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