github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/internal/lsp/cmd/workspace_symbol.go (about) 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. 4 5 package cmd 6 7 import ( 8 "context" 9 "flag" 10 "fmt" 11 12 "github.com/powerman/golang-tools/internal/lsp/protocol" 13 "github.com/powerman/golang-tools/internal/lsp/source" 14 "github.com/powerman/golang-tools/internal/tool" 15 ) 16 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."` 20 21 app *Application 22 } 23 24 func (r *workspaceSymbol) Name() string { return "workspace_symbol" } 25 func (r *workspaceSymbol) Parent() string { return r.app.Name() } 26 func (r *workspaceSymbol) Usage() string { return "[workspace_symbol-flags] <query>" } 27 func (r *workspaceSymbol) ShortHelp() string { return "search symbols in workspace" } 28 func (r *workspaceSymbol) DetailedHelp(f *flag.FlagSet) { 29 fmt.Fprint(f.Output(), ` 30 Example: 31 32 $ gopls workspace_symbol -matcher fuzzy 'wsymbols' 33 34 workspace_symbol-flags: 35 `) 36 printFlagDefaults(f) 37 } 38 39 func (r *workspaceSymbol) Run(ctx context.Context, args ...string) error { 40 if len(args) != 1 { 41 return tool.CommandLineErrorf("workspace_symbol expects 1 argument") 42 } 43 44 opts := r.app.options 45 r.app.options = func(o *source.Options) { 46 if opts != nil { 47 opts(o) 48 } 49 switch r.Matcher { 50 case "fuzzy": 51 o.SymbolMatcher = source.SymbolFuzzy 52 case "caseSensitive": 53 o.SymbolMatcher = source.SymbolCaseSensitive 54 case "fastfuzzy": 55 o.SymbolMatcher = source.SymbolFastFuzzy 56 default: 57 o.SymbolMatcher = source.SymbolCaseInsensitive 58 } 59 } 60 61 conn, err := r.app.connect(ctx) 62 if err != nil { 63 return err 64 } 65 defer conn.terminate(ctx) 66 67 p := protocol.WorkspaceSymbolParams{ 68 Query: args[0], 69 } 70 71 symbols, err := conn.Symbol(ctx, &p) 72 if err != nil { 73 return err 74 } 75 for _, s := range symbols { 76 f := conn.AddFile(ctx, fileURI(s.Location.URI)) 77 span, err := f.mapper.Span(s.Location) 78 if err != nil { 79 return err 80 } 81 fmt.Printf("%s %s %s\n", span, s.Name, s.Kind) 82 } 83 84 return nil 85 }