1
0
mirror of https://github.com/golang/go synced 2024-11-18 16:14:46 -07:00
go/internal/lsp/cmd/test/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

76 lines
2.1 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 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))...)
}