-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathworkspace_symbol.go
333 lines (309 loc) · 9.11 KB
/
workspace_symbol.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// 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 source
import (
"context"
"go/ast"
"go/token"
"go/types"
"strings"
"golang.org/x/tools/internal/event"
"golang.org/x/tools/internal/lsp/fuzzy"
"golang.org/x/tools/internal/lsp/protocol"
)
const maxSymbols = 100
// WorkspaceSymbols matches symbols across views using the given query,
// according to the SymbolMatcher matcher.
//
// The workspace symbol method is defined in the spec as follows:
//
// > The workspace symbol request is sent from the client to the server to
// > list project-wide symbols matching the query string.
//
// It is unclear what "project-wide" means here, but given the parameters of
// workspace/symbol do not include any workspace identifier, then it has to be
// assumed that "project-wide" means "across all workspaces". Hence why
// WorkspaceSymbols receives the views []View.
//
// However, it then becomes unclear what it would mean to call WorkspaceSymbols
// with a different configured SymbolMatcher per View. Therefore we assume that
// Session level configuration will define the SymbolMatcher to be used for the
// WorkspaceSymbols method.
func WorkspaceSymbols(ctx context.Context, matcherType SymbolMatcher, style SymbolStyle, views []View, query string) ([]protocol.SymbolInformation, error) {
ctx, done := event.Start(ctx, "source.WorkspaceSymbols")
defer done()
if query == "" {
return nil, nil
}
queryMatcher := makeQueryMatcher(matcherType, query)
seen := make(map[string]struct{})
var symbols []protocol.SymbolInformation
outer:
for _, view := range views {
snapshot, release := view.Snapshot(ctx)
defer release() // TODO: refactor so this runs promptly instead of at the end of the function
knownPkgs, err := snapshot.KnownPackages(ctx)
if err != nil {
return nil, err
}
// TODO: apply some kind of ordering to the search, and sort the results.
for _, pkg := range knownPkgs {
symbolMatcher := makePackageSymbolMatcher(style, pkg, queryMatcher)
if err != nil {
return nil, err
}
if _, ok := seen[pkg.PkgPath()]; ok {
continue
}
seen[pkg.PkgPath()] = struct{}{}
for _, pgf := range pkg.CompiledGoFiles() {
for _, si := range findSymbol(pgf.File.Decls, pkg.GetTypesInfo(), symbolMatcher) {
mrng, err := posToMappedRange(snapshot, pkg, si.node.Pos(), si.node.End())
if err != nil {
event.Error(ctx, "Error getting mapped range for node", err)
continue
}
rng, err := mrng.Range()
if err != nil {
event.Error(ctx, "Error getting range from mapped range", err)
continue
}
symbols = append(symbols, protocol.SymbolInformation{
Name: si.name,
Kind: si.kind,
Location: protocol.Location{
URI: protocol.URIFromSpanURI(mrng.URI()),
Range: rng,
},
ContainerName: pkg.PkgPath(),
})
if len(symbols) > maxSymbols {
break outer
}
}
}
}
}
return symbols, nil
}
type symbolInformation struct {
name string
kind protocol.SymbolKind
node ast.Node
}
type matcherFunc func(string) bool
func makeQueryMatcher(m SymbolMatcher, query string) matcherFunc {
switch m {
case SymbolFuzzy:
fm := fuzzy.NewMatcher(query)
return func(s string) bool {
return fm.Score(s) > 0
}
case SymbolCaseSensitive:
return func(s string) bool {
return strings.Contains(s, query)
}
default:
q := strings.ToLower(query)
return func(s string) bool {
return strings.Contains(strings.ToLower(s), q)
}
}
}
// packageSymbolMatcher matches (possibly partially) qualified symbols within a
// package scope.
//
// The given symbolizer controls how symbol names are extracted from the
// package scope.
type packageSymbolMatcher struct {
queryMatcher matcherFunc
pkg Package
symbolize symbolizer
}
// symbolMatch returns the package symbol for name that matches the underlying
// query, or the empty string if no match is found.
func (s packageSymbolMatcher) symbolMatch(name string) string {
return s.symbolize(name, s.pkg, s.queryMatcher)
}
func makePackageSymbolMatcher(style SymbolStyle, pkg Package, matcher matcherFunc) func(string) string {
var s symbolizer
switch style {
case DynamicSymbols:
s = dynamicSymbolMatch
case FullyQualifiedSymbols:
s = fullyQualifiedSymbolMatch
default:
s = packageSymbolMatch
}
return packageSymbolMatcher{queryMatcher: matcher, pkg: pkg, symbolize: s}.symbolMatch
}
// A symbolizer returns a qualified symbol match for the unqualified name
// within pkg, if one exists, or the empty string if no match is found.
type symbolizer func(name string, pkg Package, m matcherFunc) string
func fullyQualifiedSymbolMatch(name string, pkg Package, matcher matcherFunc) string {
// TODO: this should probably include pkg.Name() as well.
fullyQualified := pkg.PkgPath() + "." + name
if matcher(fullyQualified) {
return fullyQualified
}
return ""
}
func dynamicSymbolMatch(name string, pkg Package, matcher matcherFunc) string {
pkgQualified := pkg.Name() + "." + name
if match := shortestMatch(pkgQualified, matcher); match != "" {
return match
}
fullyQualified := pkg.PkgPath() + "." + name
if match := shortestMatch(fullyQualified, matcher); match != "" {
return match
}
return ""
}
func packageSymbolMatch(name string, pkg Package, matcher matcherFunc) string {
qualified := pkg.Name() + "." + name
if matcher(qualified) {
return qualified
}
return ""
}
func shortestMatch(fullPath string, matcher func(string) bool) string {
pathParts := strings.Split(fullPath, "/")
dottedParts := strings.Split(pathParts[len(pathParts)-1], ".")
// First match the smallest package identifier.
if m := matchRight(dottedParts, ".", matcher); m != "" {
return m
}
// Then match the shortest subpath.
return matchRight(pathParts, "/", matcher)
}
func matchRight(parts []string, sep string, matcher func(string) bool) string {
for i := 0; i < len(parts); i++ {
path := strings.Join(parts[len(parts)-1-i:], sep)
if matcher(path) {
return path
}
}
return ""
}
func findSymbol(decls []ast.Decl, info *types.Info, symbolMatch func(string) string) []symbolInformation {
var result []symbolInformation
for _, decl := range decls {
switch decl := decl.(type) {
case *ast.FuncDecl:
fn := decl.Name.Name
kind := protocol.Function
if decl.Recv != nil {
kind = protocol.Method
switch typ := decl.Recv.List[0].Type.(type) {
case *ast.StarExpr:
fn = typ.X.(*ast.Ident).Name + "." + fn
case *ast.Ident:
fn = typ.Name + "." + fn
}
}
if m := symbolMatch(fn); m != "" {
result = append(result, symbolInformation{
name: m,
kind: kind,
node: decl.Name,
})
}
case *ast.GenDecl:
for _, spec := range decl.Specs {
switch spec := spec.(type) {
case *ast.TypeSpec:
target := spec.Name.Name
if m := symbolMatch(target); m != "" {
result = append(result, symbolInformation{
name: m,
kind: typeToKind(info.TypeOf(spec.Type)),
node: spec.Name,
})
}
switch st := spec.Type.(type) {
case *ast.StructType:
for _, field := range st.Fields.List {
result = append(result, findFieldSymbol(field, protocol.Field, symbolMatch, target)...)
}
case *ast.InterfaceType:
for _, field := range st.Methods.List {
kind := protocol.Method
if len(field.Names) == 0 {
kind = protocol.Interface
}
result = append(result, findFieldSymbol(field, kind, symbolMatch, target)...)
}
}
case *ast.ValueSpec:
for _, name := range spec.Names {
if m := symbolMatch(name.Name); m != "" {
kind := protocol.Variable
if decl.Tok == token.CONST {
kind = protocol.Constant
}
result = append(result, symbolInformation{
name: m,
kind: kind,
node: name,
})
}
}
}
}
}
}
return result
}
func typeToKind(typ types.Type) protocol.SymbolKind {
switch typ := typ.Underlying().(type) {
case *types.Interface:
return protocol.Interface
case *types.Struct:
return protocol.Struct
case *types.Signature:
if typ.Recv() != nil {
return protocol.Method
}
return protocol.Function
case *types.Named:
return typeToKind(typ.Underlying())
case *types.Basic:
i := typ.Info()
switch {
case i&types.IsNumeric != 0:
return protocol.Number
case i&types.IsBoolean != 0:
return protocol.Boolean
case i&types.IsString != 0:
return protocol.String
}
}
return protocol.Variable
}
func findFieldSymbol(field *ast.Field, kind protocol.SymbolKind, symbolMatch func(string) string, prefix string) []symbolInformation {
var result []symbolInformation
if len(field.Names) == 0 {
name := types.ExprString(field.Type)
target := prefix + "." + name
if m := symbolMatch(target); m != "" {
result = append(result, symbolInformation{
name: m,
kind: kind,
node: field,
})
}
return result
}
for _, name := range field.Names {
target := prefix + "." + name.Name
if m := symbolMatch(target); m != "" {
result = append(result, symbolInformation{
name: m,
kind: kind,
node: name,
})
}
}
return result
}