Skip to content

Commit ca5866b

Browse files
committed
internal/lsp: add config option for SymbolMatch
In preparation for later changes to the workspace Symbol method, we add a separate configuration option keyed by "symbolMatcher" that specifies the type of matcher to use for workspace symbol requests. We also define a new type SymbolMatcher, the type of this new option. We require SymbolMatcher to be a separate type from Matcher because a later CL adds a type of symbol matcher that does not make sense in the context of other uses of Matcher, e.g. completion. Change-Id: Icde7d270b9efb64444f35675a8d0b48ad3b8b3dd Reviewed-on: https://go-review.googlesource.com/c/tools/+/228122 Reviewed-by: Robert Findley <[email protected]>
1 parent 8b0f8a7 commit ca5866b

File tree

9 files changed

+79
-25
lines changed

9 files changed

+79
-25
lines changed

gopls/doc/settings.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,23 @@ Default: `true`.
139139
### **staticcheck** *boolean*
140140

141141
If true, it enables the use of the staticcheck.io analyzers.
142+
143+
### **matcher** *string*
144+
145+
Defines the algorithm that is used when calculating completion candidates. Must be one of:
146+
147+
* `"fuzzy"`
148+
* `"caseSensitive"`
149+
* `"caseInsensitive"`
150+
151+
Default: `"caseInsensitive"`.
152+
153+
### **symbolMatcher** *string*
154+
155+
Defines the algorithm that is used when calculating workspace symbol results. Must be one of:
156+
157+
* `"fuzzy"`
158+
* `"caseSensitive"`
159+
* `"caseInsensitive"`
160+
161+
Default: `"caseInsensitive"`.

internal/lsp/cmd/cmd.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,10 @@ func (app *Application) connectRemote(ctx context.Context, remote string) (*conn
241241
return connection, connection.initialize(ctx, app.options)
242242
}
243243

244-
var matcherString = map[source.Matcher]string{
245-
source.Fuzzy: "fuzzy",
246-
source.CaseSensitive: "caseSensitive",
247-
source.CaseInsensitive: "default",
244+
var matcherString = map[source.SymbolMatcher]string{
245+
source.SymbolFuzzy: "fuzzy",
246+
source.SymbolCaseSensitive: "caseSensitive",
247+
source.SymbolCaseInsensitive: "default",
248248
}
249249

250250
func (c *connection) initialize(ctx context.Context, options func(*source.Options)) error {
@@ -262,7 +262,7 @@ func (c *connection) initialize(ctx context.Context, options func(*source.Option
262262
}
263263
params.Capabilities.TextDocument.DocumentSymbol.HierarchicalDocumentSymbolSupport = opts.HierarchicalDocumentSymbolSupport
264264
params.InitializationOptions = map[string]interface{}{
265-
"matcher": matcherString[opts.Matcher],
265+
"symbolMatcher": matcherString[opts.SymbolMatcher],
266266
}
267267
if _, err := c.Server.Initialize(ctx, params); err != nil {
268268
return err

internal/lsp/cmd/workspace_symbol.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ func (r *workspaceSymbol) Run(ctx context.Context, args ...string) error {
4747
}
4848
switch r.Matcher {
4949
case "fuzzy":
50-
o.Matcher = source.Fuzzy
50+
o.SymbolMatcher = source.SymbolFuzzy
5151
case "caseSensitive":
52-
o.Matcher = source.CaseSensitive
52+
o.SymbolMatcher = source.SymbolCaseSensitive
5353
default:
54-
o.Matcher = source.CaseInsensitive
54+
o.SymbolMatcher = source.SymbolCaseInsensitive
5555
}
5656
}
5757

internal/lsp/fake/editor.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ type EditorConfig struct {
5959
// codeLens command. CodeLens which are not present in this map are left in
6060
// their default state.
6161
CodeLens map[string]bool
62+
63+
// SymbolMatcher is the config associated with the "symbolMatcher" gopls
64+
// config option.
65+
SymbolMatcher *string
6266
}
6367

6468
// NewEditor Creates a new Editor.
@@ -135,6 +139,10 @@ func (e *Editor) configuration() map[string]interface{} {
135139
config["codelens"] = e.Config.CodeLens
136140
}
137141

142+
if e.Config.SymbolMatcher != nil {
143+
config["symbolMatcher"] = *e.Config.SymbolMatcher
144+
}
145+
138146
return config
139147
}
140148

internal/lsp/lsp_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -820,23 +820,23 @@ func (r *runner) Symbols(t *testing.T, uri span.URI, expectedSymbols []protocol.
820820
}
821821

822822
func (r *runner) WorkspaceSymbols(t *testing.T, query string, expectedSymbols []protocol.SymbolInformation, dirs map[string]struct{}) {
823-
r.callWorkspaceSymbols(t, query, source.CaseInsensitive, dirs, expectedSymbols)
823+
r.callWorkspaceSymbols(t, query, source.SymbolCaseInsensitive, dirs, expectedSymbols)
824824
}
825825

826826
func (r *runner) FuzzyWorkspaceSymbols(t *testing.T, query string, expectedSymbols []protocol.SymbolInformation, dirs map[string]struct{}) {
827-
r.callWorkspaceSymbols(t, query, source.Fuzzy, dirs, expectedSymbols)
827+
r.callWorkspaceSymbols(t, query, source.SymbolFuzzy, dirs, expectedSymbols)
828828
}
829829

830830
func (r *runner) CaseSensitiveWorkspaceSymbols(t *testing.T, query string, expectedSymbols []protocol.SymbolInformation, dirs map[string]struct{}) {
831-
r.callWorkspaceSymbols(t, query, source.CaseSensitive, dirs, expectedSymbols)
831+
r.callWorkspaceSymbols(t, query, source.SymbolCaseSensitive, dirs, expectedSymbols)
832832
}
833833

834-
func (r *runner) callWorkspaceSymbols(t *testing.T, query string, matcher source.Matcher, dirs map[string]struct{}, expectedSymbols []protocol.SymbolInformation) {
834+
func (r *runner) callWorkspaceSymbols(t *testing.T, query string, matcher source.SymbolMatcher, dirs map[string]struct{}, expectedSymbols []protocol.SymbolInformation) {
835835
t.Helper()
836836

837837
original := r.server.session.Options()
838838
modified := original
839-
modified.Matcher = matcher
839+
modified.SymbolMatcher = matcher
840840
r.server.session.SetOptions(modified)
841841
defer r.server.session.SetOptions(original)
842842

internal/lsp/source/options.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ func DefaultOptions() Options {
9898
HoverKind: FullDocumentation,
9999
LinkTarget: "pkg.go.dev",
100100
Matcher: Fuzzy,
101+
SymbolMatcher: SymbolFuzzy,
101102
DeepCompletion: true,
102103
UnimportedCompletion: true,
103104
CompletionDocumentation: true,
@@ -193,6 +194,9 @@ type UserOptions struct {
193194
// Matcher specifies the type of matcher to use for completion requests.
194195
Matcher Matcher
195196

197+
// SymbolMatcher specifies the type of matcher to use for symbol requests.
198+
SymbolMatcher SymbolMatcher
199+
196200
// DeepCompletion allows completion to perform nested searches through
197201
// possible candidates.
198202
DeepCompletion bool
@@ -267,6 +271,14 @@ const (
267271
CaseSensitive
268272
)
269273

274+
type SymbolMatcher int
275+
276+
const (
277+
SymbolFuzzy = SymbolMatcher(iota)
278+
SymbolCaseInsensitive
279+
SymbolCaseSensitive
280+
)
281+
270282
type HoverKind int
271283

272284
const (
@@ -399,6 +411,20 @@ func (o *Options) set(name string, value interface{}) OptionResult {
399411
o.Matcher = CaseInsensitive
400412
}
401413

414+
case "symbolMatcher":
415+
matcher, ok := result.asString()
416+
if !ok {
417+
break
418+
}
419+
switch matcher {
420+
case "fuzzy":
421+
o.SymbolMatcher = SymbolFuzzy
422+
case "caseSensitive":
423+
o.SymbolMatcher = SymbolCaseSensitive
424+
default:
425+
o.SymbolMatcher = SymbolCaseInsensitive
426+
}
427+
402428
case "hoverKind":
403429
hoverKind, ok := result.asString()
404430
if !ok {

internal/lsp/source/source_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -805,18 +805,18 @@ func (r *runner) Symbols(t *testing.T, uri span.URI, expectedSymbols []protocol.
805805
}
806806

807807
func (r *runner) WorkspaceSymbols(t *testing.T, query string, expectedSymbols []protocol.SymbolInformation, dirs map[string]struct{}) {
808-
r.callWorkspaceSymbols(t, query, source.CaseInsensitive, dirs, expectedSymbols)
808+
r.callWorkspaceSymbols(t, query, source.SymbolCaseInsensitive, dirs, expectedSymbols)
809809
}
810810

811811
func (r *runner) FuzzyWorkspaceSymbols(t *testing.T, query string, expectedSymbols []protocol.SymbolInformation, dirs map[string]struct{}) {
812-
r.callWorkspaceSymbols(t, query, source.Fuzzy, dirs, expectedSymbols)
812+
r.callWorkspaceSymbols(t, query, source.SymbolFuzzy, dirs, expectedSymbols)
813813
}
814814

815815
func (r *runner) CaseSensitiveWorkspaceSymbols(t *testing.T, query string, expectedSymbols []protocol.SymbolInformation, dirs map[string]struct{}) {
816-
r.callWorkspaceSymbols(t, query, source.CaseSensitive, dirs, expectedSymbols)
816+
r.callWorkspaceSymbols(t, query, source.SymbolCaseSensitive, dirs, expectedSymbols)
817817
}
818818

819-
func (r *runner) callWorkspaceSymbols(t *testing.T, query string, matcher source.Matcher, dirs map[string]struct{}, expectedSymbols []protocol.SymbolInformation) {
819+
func (r *runner) callWorkspaceSymbols(t *testing.T, query string, matcher source.SymbolMatcher, dirs map[string]struct{}, expectedSymbols []protocol.SymbolInformation) {
820820
t.Helper()
821821
got, err := source.WorkspaceSymbols(r.ctx, matcher, []source.View{r.view}, query)
822822
if err != nil {

internal/lsp/source/workspace_symbol.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
const maxSymbols = 100
2020

2121
// WorkspaceSymbols matches symbols across views using the given query,
22-
// according to the matcher Matcher.
22+
// according to the SymbolMatcher matcher.
2323
//
2424
// The workspace symbol method is defined in the spec as follows:
2525
//
@@ -32,10 +32,10 @@ const maxSymbols = 100
3232
// WorkspaceSymbols receives the views []View.
3333
//
3434
// However, it then becomes unclear what it would mean to call WorkspaceSymbols
35-
// with a different configured Matcher per View. Therefore we assume that
36-
// Session level configuration will define the Matcher to be used for the
35+
// with a different configured SymbolMatcher per View. Therefore we assume that
36+
// Session level configuration will define the SymbolMatcher to be used for the
3737
// WorkspaceSymbols method.
38-
func WorkspaceSymbols(ctx context.Context, matcherType Matcher, views []View, query string) ([]protocol.SymbolInformation, error) {
38+
func WorkspaceSymbols(ctx context.Context, matcherType SymbolMatcher, views []View, query string) ([]protocol.SymbolInformation, error) {
3939
ctx, done := event.Start(ctx, "source.WorkspaceSymbols")
4040
defer done()
4141
if query == "" {
@@ -102,14 +102,14 @@ type symbolInformation struct {
102102

103103
type matcherFunc func(string) bool
104104

105-
func makeMatcher(m Matcher, query string) matcherFunc {
105+
func makeMatcher(m SymbolMatcher, query string) matcherFunc {
106106
switch m {
107-
case Fuzzy:
107+
case SymbolFuzzy:
108108
fm := fuzzy.NewMatcher(query)
109109
return func(s string) bool {
110110
return fm.Score(s) > 0
111111
}
112-
case CaseSensitive:
112+
case SymbolCaseSensitive:
113113
return func(s string) bool {
114114
return strings.Contains(s, query)
115115
}

internal/lsp/workspace_symbol.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ func (s *Server) symbol(ctx context.Context, params *protocol.WorkspaceSymbolPar
1717
defer done()
1818

1919
views := s.session.Views()
20-
matcher := s.session.Options().Matcher
20+
matcher := s.session.Options().SymbolMatcher
2121
return source.WorkspaceSymbols(ctx, matcher, views, params.Query)
2222
}

0 commit comments

Comments
 (0)