golang.org/x/tools/gopls@v0.15.3/internal/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  	"strings"
    12  
    13  	"golang.org/x/tools/gopls/internal/protocol"
    14  	"golang.org/x/tools/gopls/internal/settings"
    15  	"golang.org/x/tools/internal/tool"
    16  )
    17  
    18  // workspaceSymbol implements the workspace_symbol verb for gopls.
    19  type workspaceSymbol struct {
    20  	Matcher string `flag:"matcher" help:"specifies the type of matcher: fuzzy, fastfuzzy, casesensitive, or caseinsensitive.\nThe default is caseinsensitive."`
    21  
    22  	app *Application
    23  }
    24  
    25  func (r *workspaceSymbol) Name() string      { return "workspace_symbol" }
    26  func (r *workspaceSymbol) Parent() string    { return r.app.Name() }
    27  func (r *workspaceSymbol) Usage() string     { return "[workspace_symbol-flags] <query>" }
    28  func (r *workspaceSymbol) ShortHelp() string { return "search symbols in workspace" }
    29  func (r *workspaceSymbol) DetailedHelp(f *flag.FlagSet) {
    30  	fmt.Fprint(f.Output(), `
    31  Example:
    32  
    33  	$ gopls workspace_symbol -matcher fuzzy 'wsymbols'
    34  
    35  workspace_symbol-flags:
    36  `)
    37  	printFlagDefaults(f)
    38  }
    39  
    40  func (r *workspaceSymbol) Run(ctx context.Context, args ...string) error {
    41  	if len(args) != 1 {
    42  		return tool.CommandLineErrorf("workspace_symbol expects 1 argument")
    43  	}
    44  
    45  	opts := r.app.options
    46  	r.app.options = func(o *settings.Options) {
    47  		if opts != nil {
    48  			opts(o)
    49  		}
    50  		switch strings.ToLower(r.Matcher) {
    51  		case "fuzzy":
    52  			o.SymbolMatcher = settings.SymbolFuzzy
    53  		case "casesensitive":
    54  			o.SymbolMatcher = settings.SymbolCaseSensitive
    55  		case "fastfuzzy":
    56  			o.SymbolMatcher = settings.SymbolFastFuzzy
    57  		default:
    58  			o.SymbolMatcher = settings.SymbolCaseInsensitive
    59  		}
    60  	}
    61  
    62  	conn, err := r.app.connect(ctx, nil)
    63  	if err != nil {
    64  		return err
    65  	}
    66  	defer conn.terminate(ctx)
    67  
    68  	p := protocol.WorkspaceSymbolParams{
    69  		Query: args[0],
    70  	}
    71  
    72  	symbols, err := conn.Symbol(ctx, &p)
    73  	if err != nil {
    74  		return err
    75  	}
    76  	for _, s := range symbols {
    77  		f, err := conn.openFile(ctx, s.Location.URI)
    78  		if err != nil {
    79  			return err
    80  		}
    81  		span, err := f.locationSpan(s.Location)
    82  		if err != nil {
    83  			return err
    84  		}
    85  		fmt.Printf("%s %s %s\n", span, s.Name, s.Kind)
    86  	}
    87  
    88  	return nil
    89  }