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>
diff --git a/internal/lsp/cmd/cmd.go b/internal/lsp/cmd/cmd.go
index 0788754..299bc59 100644
--- a/internal/lsp/cmd/cmd.go
+++ b/internal/lsp/cmd/cmd.go
@@ -184,6 +184,7 @@
 		&signature{app: app},
 		&suggestedfix{app: app},
 		&symbols{app: app},
+		&workspaceSymbol{app: app},
 	}
 }
 
@@ -239,6 +240,12 @@
 	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)
@@ -253,6 +260,9 @@
 		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
diff --git a/internal/lsp/cmd/test/cmdtest.go b/internal/lsp/cmd/test/cmdtest.go
index 12c2fd5..a1bff9b 100644
--- a/internal/lsp/cmd/test/cmdtest.go
+++ b/internal/lsp/cmd/test/cmdtest.go
@@ -103,18 +103,6 @@
 	//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 {
diff --git a/internal/lsp/cmd/test/workspace_symbol.go b/internal/lsp/cmd/test/workspace_symbol.go
new file mode 100644
index 0000000..6b19e34
--- /dev/null
+++ b/internal/lsp/cmd/test/workspace_symbol.go
@@ -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"
+	"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 path.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 path.Join(append(dir, string(name))...)
+}
diff --git a/internal/lsp/cmd/workspace_symbol.go b/internal/lsp/cmd/workspace_symbol.go
new file mode 100644
index 0000000..f0b2302
--- /dev/null
+++ b/internal/lsp/cmd/workspace_symbol.go
@@ -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
+}
diff --git a/internal/lsp/testdata/lsp/primarymod/workspacesymbol/EmptyQuery.golden b/internal/lsp/testdata/lsp/primarymod/workspacesymbol/EmptyQuery.golden
new file mode 100644
index 0000000..6f5d587
--- /dev/null
+++ b/internal/lsp/testdata/lsp/primarymod/workspacesymbol/EmptyQuery.golden
@@ -0,0 +1,2 @@
+-- workspace_symbol --
+
diff --git "a/internal/lsp/testdata/lsp/primarymod/workspacesymbol/casesensitive/\041dunk.golden" "b/internal/lsp/testdata/lsp/primarymod/workspacesymbol/casesensitive/\041dunk.golden"
new file mode 100644
index 0000000..52f25a7
--- /dev/null
+++ "b/internal/lsp/testdata/lsp/primarymod/workspacesymbol/casesensitive/\041dunk.golden"
@@ -0,0 +1,2 @@
+-- workspace_symbol --
+symbols/main.go:58:6-10 Dunk Function
diff --git a/internal/lsp/testdata/lsp/primarymod/workspacesymbol/casesensitive/dunk.golden b/internal/lsp/testdata/lsp/primarymod/workspacesymbol/casesensitive/dunk.golden
new file mode 100644
index 0000000..9de7528
--- /dev/null
+++ b/internal/lsp/testdata/lsp/primarymod/workspacesymbol/casesensitive/dunk.golden
@@ -0,0 +1,2 @@
+-- workspace_symbol --
+symbols/main.go:60:6-10 dunk Function
diff --git a/internal/lsp/testdata/lsp/primarymod/workspacesymbol/fuzzy/symbola.golden b/internal/lsp/testdata/lsp/primarymod/workspacesymbol/fuzzy/symbola.golden
new file mode 100644
index 0000000..ccfa859
--- /dev/null
+++ b/internal/lsp/testdata/lsp/primarymod/workspacesymbol/fuzzy/symbola.golden
@@ -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
diff --git a/internal/lsp/testdata/lsp/primarymod/workspacesymbol/fuzzy/symbolb.golden b/internal/lsp/testdata/lsp/primarymod/workspacesymbol/fuzzy/symbolb.golden
new file mode 100644
index 0000000..aa2601b
--- /dev/null
+++ b/internal/lsp/testdata/lsp/primarymod/workspacesymbol/fuzzy/symbolb.golden
@@ -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
diff --git a/internal/lsp/testdata/lsp/primarymod/workspacesymbol/fuzzy/wsym.golden b/internal/lsp/testdata/lsp/primarymod/workspacesymbol/fuzzy/wsym.golden
new file mode 100644
index 0000000..b0b69c5
--- /dev/null
+++ b/internal/lsp/testdata/lsp/primarymod/workspacesymbol/fuzzy/wsym.golden
@@ -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
diff --git a/internal/lsp/testdata/lsp/primarymod/workspacesymbol/workspacesymbolvar.golden b/internal/lsp/testdata/lsp/primarymod/workspacesymbol/workspacesymbolvar.golden
new file mode 100644
index 0000000..8650fad
--- /dev/null
+++ b/internal/lsp/testdata/lsp/primarymod/workspacesymbol/workspacesymbolvar.golden
@@ -0,0 +1,3 @@
+-- workspace_symbol --
+workspacesymbol/a/a.go:3:5-29 WorkspaceSymbolVariableA Variable
+workspacesymbol/b/b.go:3:5-29 WorkspaceSymbolVariableB Variable