Skip to content

Commit

Permalink
tools/gopls: add cmd support for workspace_symbol
Browse files Browse the repository at this point in the history
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
daisuzu committed Mar 21, 2020
1 parent 268ba72 commit cc229fc
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 12 deletions.
10 changes: 10 additions & 0 deletions internal/lsp/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ func (app *Application) featureCommands() []tool.Application {
&signature{app: app},
&suggestedfix{app: app},
&symbols{app: app},
&workspaceSymbol{app: app},
}
}

Expand Down Expand Up @@ -239,6 +240,12 @@ func (app *Application) connectRemote(ctx context.Context, remote string) (*conn
return connection, connection.initialize(ctx, app.options)
}

var matcherString = map[source.Matcher]string{
source.Fuzzy: "fuzzy",
source.CaseSensitive: "caseSensitive",
source.CaseInsensitive: "default",
}

func (c *connection) initialize(ctx context.Context, options func(*source.Options)) error {
params := &protocol.ParamInitialize{}
params.RootURI = protocol.URIFromPath(c.Client.app.wd)
Expand All @@ -253,6 +260,9 @@ func (c *connection) initialize(ctx context.Context, options func(*source.Option
ContentFormat: []protocol.MarkupKind{opts.PreferredContentFormat},
}
params.Capabilities.TextDocument.DocumentSymbol.HierarchicalDocumentSymbolSupport = opts.HierarchicalDocumentSymbolSupport
params.InitializationOptions = map[string]interface{}{
"matcher": matcherString[opts.Matcher],
}

if _, err := c.Server.Initialize(ctx, params); err != nil {
return err
Expand Down
12 changes: 0 additions & 12 deletions internal/lsp/cmd/test/cmdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,6 @@ func (r *runner) RankCompletion(t *testing.T, src span.Span, test tests.Completi
//TODO: add command line completions tests when it works
}

func (r *runner) WorkspaceSymbols(*testing.T, string, []protocol.SymbolInformation, map[string]struct{}) {
//TODO: add command line workspace symbol tests when it works
}

func (r *runner) FuzzyWorkspaceSymbols(*testing.T, string, []protocol.SymbolInformation, map[string]struct{}) {
//TODO: add command line workspace symbol tests when it works
}

func (r *runner) CaseSensitiveWorkspaceSymbols(*testing.T, string, []protocol.SymbolInformation, map[string]struct{}) {
//TODO: add command line workspace symbol tests when it works
}

func (r *runner) runGoplsCmd(t testing.TB, args ...string) (string, string) {
rStdout, wStdout, err := os.Pipe()
if err != nil {
Expand Down
75 changes: 75 additions & 0 deletions internal/lsp/cmd/test/workspace_symbol.go
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))...)
}
82 changes: 82 additions & 0 deletions internal/lsp/cmd/workspace_symbol.go
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
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- workspace_symbol --

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- workspace_symbol --
symbols/main.go:58:6-10 Dunk Function
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- workspace_symbol --
symbols/main.go:60:6-10 dunk Function
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
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
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
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

0 comments on commit cc229fc

Please sign in to comment.