Skip to content

Commit aa94e73

Browse files
committed
internal/lsp/source: add a new symbolStyle configuration option
Add a symbolStyle configuration option, and use it to parameterize the following behavior when computing workspace symbols: + package (default): include package name in the workspace symbol. + full: fully qualify the symbol by import path + dynamic: use as the symbol the shortest suffix of the full path that contains the match. To implement this, expose package name in the source.Package interface. To be consistent with other handling in the cache package, define a new cache.packageName named string type, to avoid confusion with packageID or packagePath (if confusing those two identifiers was a problem, surely it is a potential problem for package name as well). Change-Id: Ic8ed6ba5473b0523b97e677878e5e6bddfff10a7 Reviewed-on: https://go-review.googlesource.com/c/tools/+/236842 Run-TryBot: Robert Findley <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Paul Jolly <[email protected]>
1 parent 84cfede commit aa94e73

File tree

18 files changed

+220
-98
lines changed

18 files changed

+220
-98
lines changed

internal/lsp/cache/check.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ func typeCheck(ctx context.Context, fset *token.FileSet, m *metadata, mode sourc
276276

277277
pkg := &pkg{
278278
id: m.id,
279+
name: m.name,
279280
pkgPath: m.pkgPath,
280281
mode: mode,
281282
goFiles: goFiles,
@@ -352,7 +353,7 @@ func typeCheck(ctx context.Context, fset *token.FileSet, m *metadata, mode sourc
352353
} else if len(files) == 0 { // not the unsafe package, no parsed files
353354
return nil, errors.Errorf("no parsed files for package %s, expected: %s, errors: %v, list errors: %v", pkg.pkgPath, pkg.compiledGoFiles, actualErrors, rawErrors)
354355
} else {
355-
pkg.types = types.NewPackage(string(m.pkgPath), m.name)
356+
pkg.types = types.NewPackage(string(m.pkgPath), string(m.name))
356357
}
357358

358359
cfg := &types.Config{

internal/lsp/cache/load.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
type metadata struct {
2424
id packageID
2525
pkgPath packagePath
26-
name string
26+
name packageName
2727
goFiles []span.URI
2828
compiledGoFiles []span.URI
2929
forTest packagePath
@@ -171,7 +171,7 @@ func (s *snapshot) setMetadata(ctx context.Context, pkgPath packagePath, pkg *pa
171171
m := &metadata{
172172
id: id,
173173
pkgPath: pkgPath,
174-
name: pkg.Name,
174+
name: packageName(pkg.Name),
175175
forTest: packagePath(packagesinternal.GetForTest(pkg)),
176176
typesSizes: pkg.TypesSizes,
177177
errors: pkg.Errors,

internal/lsp/cache/pkg.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
type pkg struct {
1919
// ID and package path have their own types to avoid being used interchangeably.
2020
id packageID
21+
name packageName
2122
pkgPath packagePath
2223
mode source.ParseMode
2324
forTest packagePath
@@ -32,11 +33,15 @@ type pkg struct {
3233
typesSizes types.Sizes
3334
}
3435

35-
// Declare explicit types for package paths and IDs to ensure that we never use
36-
// an ID where a path belongs, and vice versa. If we confused the two, it would
37-
// result in confusing errors because package IDs often look like package paths.
38-
type packageID string
39-
type packagePath string
36+
// Declare explicit types for package paths, names, and IDs to ensure that we
37+
// never use an ID where a path belongs, and vice versa. If we confused these,
38+
// it would result in confusing errors because package IDs often look like
39+
// package paths.
40+
type (
41+
packageID string
42+
packagePath string
43+
packageName string
44+
)
4045

4146
// Declare explicit types for files and directories to distinguish between the two.
4247
type fileURI span.URI
@@ -47,6 +52,10 @@ func (p *pkg) ID() string {
4752
return string(p.id)
4853
}
4954

55+
func (p *pkg) Name() string {
56+
return string(p.name)
57+
}
58+
5059
func (p *pkg) PkgPath() string {
5160
return string(p.pkgPath)
5261
}

internal/lsp/regtest/symbol_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ type myInterface interface { // interface
4444
DoSomeCoolStuff() string // interface method
4545
}
4646
-- p/p.go --
47-
package main
47+
package p
4848
4949
const Message = "Hello World." // constant
5050
`
5151

5252
var caseSensitiveSymbolChecks = map[string]*expSymbolInformation{
5353
"main": {
54-
Name: pString("mod.com.main"),
54+
Name: pString("main.main"),
5555
Kind: pKind(protocol.Function),
5656
Location: &expLocation{
5757
Path: pString("main.go"),
@@ -64,7 +64,7 @@ var caseSensitiveSymbolChecks = map[string]*expSymbolInformation{
6464
},
6565
},
6666
"Message": {
67-
Name: pString("mod.com/p.Message"),
67+
Name: pString("p.Message"),
6868
Kind: pKind(protocol.Constant),
6969
Location: &expLocation{
7070
Path: pString("p/p.go"),
@@ -77,7 +77,7 @@ var caseSensitiveSymbolChecks = map[string]*expSymbolInformation{
7777
},
7878
},
7979
"myvar": {
80-
Name: pString("mod.com.myvar"),
80+
Name: pString("main.myvar"),
8181
Kind: pKind(protocol.Variable),
8282
Location: &expLocation{
8383
Path: pString("main.go"),
@@ -90,7 +90,7 @@ var caseSensitiveSymbolChecks = map[string]*expSymbolInformation{
9090
},
9191
},
9292
"myType": {
93-
Name: pString("mod.com.myType"),
93+
Name: pString("main.myType"),
9494
Kind: pKind(protocol.String),
9595
Location: &expLocation{
9696
Path: pString("main.go"),
@@ -103,7 +103,7 @@ var caseSensitiveSymbolChecks = map[string]*expSymbolInformation{
103103
},
104104
},
105105
"Blahblah": {
106-
Name: pString("mod.com.myType.Blahblah"),
106+
Name: pString("main.myType.Blahblah"),
107107
Kind: pKind(protocol.Method),
108108
Location: &expLocation{
109109
Path: pString("main.go"),
@@ -116,11 +116,11 @@ var caseSensitiveSymbolChecks = map[string]*expSymbolInformation{
116116
},
117117
},
118118
"NewEncoder": {
119-
Name: pString("encoding/json.NewEncoder"),
119+
Name: pString("json.NewEncoder"),
120120
Kind: pKind(protocol.Function),
121121
},
122122
"myStruct": {
123-
Name: pString("mod.com.myStruct"),
123+
Name: pString("main.myStruct"),
124124
Kind: pKind(protocol.Struct),
125125
Location: &expLocation{
126126
Path: pString("main.go"),
@@ -134,7 +134,7 @@ var caseSensitiveSymbolChecks = map[string]*expSymbolInformation{
134134
},
135135
// TODO: not sure we should be returning struct fields
136136
"myStructField": {
137-
Name: pString("mod.com.myStruct.myStructField"),
137+
Name: pString("main.myStruct.myStructField"),
138138
Kind: pKind(protocol.Field),
139139
Location: &expLocation{
140140
Path: pString("main.go"),
@@ -147,7 +147,7 @@ var caseSensitiveSymbolChecks = map[string]*expSymbolInformation{
147147
},
148148
},
149149
"myInterface": {
150-
Name: pString("mod.com.myInterface"),
150+
Name: pString("main.myInterface"),
151151
Kind: pKind(protocol.Interface),
152152
Location: &expLocation{
153153
Path: pString("main.go"),
@@ -161,7 +161,7 @@ var caseSensitiveSymbolChecks = map[string]*expSymbolInformation{
161161
},
162162
// TODO: not sure we should be returning interface methods
163163
"DoSomeCoolStuff": {
164-
Name: pString("mod.com.myInterface.DoSomeCoolStuff"),
164+
Name: pString("main.myInterface.DoSomeCoolStuff"),
165165
Kind: pKind(protocol.Method),
166166
Location: &expLocation{
167167
Path: pString("main.go"),
@@ -180,7 +180,7 @@ var caseInsensitiveSymbolChecks = map[string]*expSymbolInformation{
180180
}
181181

182182
var fuzzySymbolChecks = map[string]*expSymbolInformation{
183-
"mod.Mn": caseSensitiveSymbolChecks["main"],
183+
"Mn": caseSensitiveSymbolChecks["main"],
184184
}
185185

186186
// TestSymbolPos tests that, at a basic level, we get the correct position
@@ -202,7 +202,7 @@ func checkChecks(t *testing.T, matcher string, checks map[string]*expSymbolInfor
202202
t.Run(query, func(t *testing.T) {
203203
res := env.Symbol(query)
204204
if !exp.matchAgainst(res) {
205-
t.Fatalf("failed to find a match against query %q for %v", query, exp)
205+
t.Fatalf("failed to find a match against query %q for %v,\ngot: %v", query, exp, res)
206206
}
207207
})
208208
}

internal/lsp/source/options.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ type UserOptions struct {
215215
// SymbolMatcher specifies the type of matcher to use for symbol requests.
216216
SymbolMatcher SymbolMatcher
217217

218+
// SymbolStyle specifies what style of symbols to return in symbol requests
219+
// (package qualified, fully qualified, etc).
220+
SymbolStyle SymbolStyle
221+
218222
// DeepCompletion allows completion to perform nested searches through
219223
// possible candidates.
220224
DeepCompletion bool
@@ -303,6 +307,14 @@ const (
303307
SymbolCaseSensitive
304308
)
305309

310+
type SymbolStyle int
311+
312+
const (
313+
PackageQualifiedSymbols = SymbolStyle(iota)
314+
FullyQualifiedSymbols
315+
DynamicSymbols
316+
)
317+
306318
type HoverKind int
307319

308320
const (
@@ -449,6 +461,20 @@ func (o *Options) set(name string, value interface{}) OptionResult {
449461
o.SymbolMatcher = SymbolCaseInsensitive
450462
}
451463

464+
case "symbolStyle":
465+
style, ok := result.asString()
466+
if !ok {
467+
break
468+
}
469+
switch style {
470+
case "full":
471+
o.SymbolStyle = FullyQualifiedSymbols
472+
case "dynamic":
473+
o.SymbolStyle = DynamicSymbols
474+
default:
475+
o.SymbolStyle = PackageQualifiedSymbols
476+
}
477+
452478
case "hoverKind":
453479
hoverKind, ok := result.asString()
454480
if !ok {

internal/lsp/source/source_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ func (r *runner) CaseSensitiveWorkspaceSymbols(t *testing.T, query string, expec
817817

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

internal/lsp/source/view.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ func (a Analyzer) Enabled(snapshot Snapshot) bool {
440440
// only the relevant fields of a *go/packages.Package.
441441
type Package interface {
442442
ID() string
443+
Name() string
443444
PkgPath() string
444445
CompiledGoFiles() []ParseGoHandle
445446
File(uri span.URI) (ParseGoHandle, error)

0 commit comments

Comments
 (0)