1 // Copyright 2020 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
18 "golang.org/x/tools/internal/event"
19 "golang.org/x/tools/internal/lsp/fuzzy"
20 "golang.org/x/tools/internal/lsp/protocol"
21 "golang.org/x/tools/internal/span"
24 // maxSymbols defines the maximum number of symbol results that should ever be
25 // sent in response to a client.
26 const maxSymbols = 100
28 // WorkspaceSymbols matches symbols across all views using the given query,
29 // according to the match semantics parameterized by matcherType and style.
31 // The workspace symbol method is defined in the spec as follows:
33 // The workspace symbol request is sent from the client to the server to
34 // list project-wide symbols matching the query string.
36 // It is unclear what "project-wide" means here, but given the parameters of
37 // workspace/symbol do not include any workspace identifier, then it has to be
38 // assumed that "project-wide" means "across all workspaces". Hence why
39 // WorkspaceSymbols receives the views []View.
41 // However, it then becomes unclear what it would mean to call WorkspaceSymbols
42 // with a different configured SymbolMatcher per View. Therefore we assume that
43 // Session level configuration will define the SymbolMatcher to be used for the
44 // WorkspaceSymbols method.
45 func WorkspaceSymbols(ctx context.Context, matcherType SymbolMatcher, style SymbolStyle, views []View, query string) ([]protocol.SymbolInformation, error) {
46 ctx, done := event.Start(ctx, "source.WorkspaceSymbols")
51 sc := newSymbolCollector(matcherType, style, query)
52 return sc.walk(ctx, views)
55 // A matcherFunc determines the matching score of a symbol.
57 // See the comment for symbolCollector for more information.
58 type matcherFunc func(name string) float64
60 // A symbolizer returns the best symbol match for name with pkg, according to
63 // See the comment for symbolCollector for more information.
64 type symbolizer func(name string, pkg Package, m matcherFunc) (string, float64)
66 func fullyQualifiedSymbolMatch(name string, pkg Package, matcher matcherFunc) (string, float64) {
67 _, score := dynamicSymbolMatch(name, pkg, matcher)
69 return pkg.PkgPath() + "." + name, score
74 func dynamicSymbolMatch(name string, pkg Package, matcher matcherFunc) (string, float64) {
75 // Prefer any package-qualified match.
76 pkgQualified := pkg.Name() + "." + name
77 if match, score := bestMatch(pkgQualified, matcher); match != "" {
80 fullyQualified := pkg.PkgPath() + "." + name
81 if match, score := bestMatch(fullyQualified, matcher); match != "" {
87 func packageSymbolMatch(name string, pkg Package, matcher matcherFunc) (string, float64) {
88 qualified := pkg.Name() + "." + name
89 if matcher(qualified) > 0 {
95 // bestMatch returns the highest scoring symbol suffix of fullPath, starting
96 // from the right and splitting on selectors and path components.
98 // e.g. given a symbol path of the form 'host.com/dir/pkg.type.field', we
99 // check the match quality of the following:
103 // - dir/pkg.type.field
104 // - host.com/dir/pkg.type.field
106 // and return the best match, along with its score.
108 // This is used to implement the 'dynamic' symbol style.
109 func bestMatch(fullPath string, matcher matcherFunc) (string, float64) {
110 pathParts := strings.Split(fullPath, "/")
111 dottedParts := strings.Split(pathParts[len(pathParts)-1], ".")
116 for i := 0; i < len(dottedParts); i++ {
117 path := strings.Join(dottedParts[len(dottedParts)-1-i:], ".")
118 if match := matcher(path); match > score {
123 for i := 0; i < len(pathParts); i++ {
124 path := strings.Join(pathParts[len(pathParts)-1-i:], "/")
125 if match := matcher(path); match > score {
133 // symbolCollector holds context as we walk Packages, gathering symbols that
134 // match a given query.
136 // How we match symbols is parameterized by two interfaces:
137 // * A matcherFunc determines how well a string symbol matches a query. It
138 // returns a non-negative score indicating the quality of the match. A score
139 // of zero indicates no match.
140 // * A symbolizer determines how we extract the symbol for an object. This
141 // enables the 'symbolStyle' configuration option.
142 type symbolCollector struct {
143 // These types parameterize the symbol-matching pass.
145 symbolizer symbolizer
147 // current holds metadata for the package we are currently walking.
149 curFile *ParsedGoFile
151 res [maxSymbols]symbolInformation
154 func newSymbolCollector(matcher SymbolMatcher, style SymbolStyle, query string) *symbolCollector {
158 m = parseQuery(query)
159 case SymbolCaseSensitive:
160 m = func(s string) float64 {
161 if strings.Contains(s, query) {
166 case SymbolCaseInsensitive:
167 q := strings.ToLower(query)
168 m = func(s string) float64 {
169 if strings.Contains(strings.ToLower(s), q) {
175 panic(fmt.Errorf("unknown symbol matcher: %v", matcher))
180 s = dynamicSymbolMatch
181 case FullyQualifiedSymbols:
182 s = fullyQualifiedSymbolMatch
183 case PackageQualifiedSymbols:
184 s = packageSymbolMatch
186 panic(fmt.Errorf("unknown symbol style: %v", style))
188 return &symbolCollector{
194 // parseQuery parses a field-separated symbol query, extracting the special
195 // characters listed below, and returns a matcherFunc corresponding to the AND
196 // of all field queries.
198 // Special characters:
199 // ^ match exact prefix
200 // $ match exact suffix
203 // In all three of these special queries, matches are 'smart-cased', meaning
204 // they are case sensitive if the symbol query contains any upper-case
205 // characters, and case insensitive otherwise.
206 func parseQuery(q string) matcherFunc {
207 fields := strings.Fields(q)
208 if len(fields) == 0 {
209 return func(string) float64 { return 0 }
211 var funcs []matcherFunc
212 for _, field := range fields {
215 case strings.HasPrefix(field, "^"):
217 f = smartCase(prefix, func(s string) float64 {
218 if strings.HasPrefix(s, prefix) {
223 case strings.HasPrefix(field, "'"):
225 f = smartCase(exact, func(s string) float64 {
226 if strings.Contains(s, exact) {
231 case strings.HasSuffix(field, "$"):
232 suffix := field[0 : len(field)-1]
233 f = smartCase(suffix, func(s string) float64 {
234 if strings.HasSuffix(s, suffix) {
240 fm := fuzzy.NewMatcher(field)
241 f = func(s string) float64 {
242 return float64(fm.Score(s))
245 funcs = append(funcs, f)
247 return comboMatcher(funcs).match
250 // smartCase returns a matcherFunc that is case-sensitive if q contains any
251 // upper-case characters, and case-insensitive otherwise.
252 func smartCase(q string, m matcherFunc) matcherFunc {
253 insensitive := strings.ToLower(q) == q
254 return func(s string) float64 {
256 s = strings.ToLower(s)
262 type comboMatcher []matcherFunc
264 func (c comboMatcher) match(s string) float64 {
266 for _, f := range c {
272 // walk walks views, gathers symbols, and returns the results.
273 func (sc *symbolCollector) walk(ctx context.Context, views []View) (_ []protocol.SymbolInformation, err error) {
274 toWalk, err := sc.collectPackages(ctx, views)
278 // Make sure we only walk files once (we might see them more than once due to
279 // build constraints).
280 seen := make(map[span.URI]bool)
281 for _, pv := range toWalk {
283 for _, pgf := range pv.pkg.CompiledGoFiles() {
289 sc.walkFilesDecls(pgf.File.Decls)
292 return sc.results(), nil
295 func (sc *symbolCollector) results() []protocol.SymbolInformation {
296 var res []protocol.SymbolInformation
297 for _, si := range sc.res {
301 res = append(res, si.asProtocolSymbolInformation())
306 // collectPackages gathers all known packages and sorts for stability.
307 func (sc *symbolCollector) collectPackages(ctx context.Context, views []View) ([]*pkgView, error) {
308 var toWalk []*pkgView
309 for _, v := range views {
310 snapshot, release := v.Snapshot(ctx)
312 knownPkgs, err := snapshot.KnownPackages(ctx)
316 workspacePackages, err := snapshot.WorkspacePackages(ctx)
320 isWorkspacePkg := make(map[Package]bool)
321 for _, wp := range workspacePackages {
322 isWorkspacePkg[wp] = true
324 for _, pkg := range knownPkgs {
325 toWalk = append(toWalk, &pkgView{
327 isWorkspace: isWorkspacePkg[pkg],
331 // Now sort for stability of results. We order by
332 // (pkgView.isWorkspace, pkgView.p.ID())
333 sort.Slice(toWalk, func(i, j int) bool {
337 case lhs.isWorkspace == rhs.isWorkspace:
338 return lhs.pkg.ID() < rhs.pkg.ID()
339 case lhs.isWorkspace:
348 func (sc *symbolCollector) walkFilesDecls(decls []ast.Decl) {
349 for _, decl := range decls {
350 switch decl := decl.(type) {
352 kind := protocol.Function
354 if decl.Recv != nil {
355 kind = protocol.Method
356 switch typ := decl.Recv.List[0].Type.(type) {
358 recv = typ.X.(*ast.Ident)
364 sc.match(decl.Name.Name, kind, decl.Name, recv)
366 sc.match(decl.Name.Name, kind, decl.Name)
369 for _, spec := range decl.Specs {
370 switch spec := spec.(type) {
372 sc.match(spec.Name.Name, typeToKind(sc.current.pkg.GetTypesInfo().TypeOf(spec.Type)), spec.Name)
373 sc.walkType(spec.Type, spec.Name)
375 for _, name := range spec.Names {
376 kind := protocol.Variable
377 if decl.Tok == token.CONST {
378 kind = protocol.Constant
380 sc.match(name.Name, kind, name)
388 // walkType processes symbols related to a type expression. path is path of
389 // nested type identifiers to the type expression.
390 func (sc *symbolCollector) walkType(typ ast.Expr, path ...*ast.Ident) {
391 switch st := typ.(type) {
392 case *ast.StructType:
393 for _, field := range st.Fields.List {
394 sc.walkField(field, protocol.Field, protocol.Field, path...)
396 case *ast.InterfaceType:
397 for _, field := range st.Methods.List {
398 sc.walkField(field, protocol.Interface, protocol.Method, path...)
403 // walkField processes symbols related to the struct field or interface method.
405 // unnamedKind and namedKind are the symbol kinds if the field is resp. unnamed
406 // or named. path is the path of nested identifiers containing the field.
407 func (sc *symbolCollector) walkField(field *ast.Field, unnamedKind, namedKind protocol.SymbolKind, path ...*ast.Ident) {
408 if len(field.Names) == 0 {
409 sc.match(types.ExprString(field.Type), unnamedKind, field, path...)
411 for _, name := range field.Names {
412 sc.match(name.Name, namedKind, name, path...)
413 sc.walkType(field.Type, append(path, name)...)
417 func typeToKind(typ types.Type) protocol.SymbolKind {
418 switch typ := typ.Underlying().(type) {
419 case *types.Interface:
420 return protocol.Interface
422 return protocol.Struct
423 case *types.Signature:
424 if typ.Recv() != nil {
425 return protocol.Method
427 return protocol.Function
429 return typeToKind(typ.Underlying())
433 case i&types.IsNumeric != 0:
434 return protocol.Number
435 case i&types.IsBoolean != 0:
436 return protocol.Boolean
437 case i&types.IsString != 0:
438 return protocol.String
441 return protocol.Variable
444 // match finds matches and gathers the symbol identified by name, kind and node
445 // via the symbolCollector's matcher after first de-duping against previously
448 // path specifies the identifier path to a nested field or interface method.
449 func (sc *symbolCollector) match(name string, kind protocol.SymbolKind, node ast.Node, path ...*ast.Ident) {
450 if !node.Pos().IsValid() || !node.End().IsValid() {
454 isExported := isExported(name)
456 var nameBuilder strings.Builder
457 for _, ident := range path {
458 nameBuilder.WriteString(ident.Name)
459 nameBuilder.WriteString(".")
460 if !ident.IsExported() {
464 nameBuilder.WriteString(name)
465 name = nameBuilder.String()
468 // Factors to apply to the match score for the purpose of downranking
471 // These numbers were crudely calibrated based on trial-and-error using a
472 // small number of sample queries. Adjust as necessary.
474 // All factors are multiplicative, meaning if more than one applies they are
475 // multiplied together.
477 // nonWorkspaceFactor is applied to symbols outside of any active
478 // workspace. Developers are less likely to want to jump to code that they
479 // are not actively working on.
480 nonWorkspaceFactor = 0.5
481 // nonWorkspaceUnexportedFactor is applied to unexported symbols outside of
482 // any active workspace. Since one wouldn't usually jump to unexported
483 // symbols to understand a package API, they are particularly irrelevant.
484 nonWorkspaceUnexportedFactor = 0.5
485 // fieldFactor is applied to fields and interface methods. One would
486 // typically jump to the type definition first, so ranking fields highly
490 symbol, score := sc.symbolizer(name, sc.current.pkg, sc.matcher)
492 // Downrank symbols outside of the workspace.
493 if !sc.current.isWorkspace {
494 score *= nonWorkspaceFactor
496 score *= nonWorkspaceUnexportedFactor
505 // Avoid the work below if we know this score will not be sorted into the
507 if score <= sc.res[len(sc.res)-1].score {
511 rng, err := fileRange(sc.curFile, node.Pos(), node.End())
515 si := symbolInformation{
519 container: sc.current.pkg.PkgPath(),
521 location: protocol.Location{
522 URI: protocol.URIFromSpanURI(sc.curFile.URI),
526 insertAt := sort.Search(len(sc.res), func(i int) bool {
527 return sc.res[i].score < score
529 if insertAt < len(sc.res)-1 {
530 copy(sc.res[insertAt+1:], sc.res[insertAt:len(sc.res)-1])
532 sc.res[insertAt] = si
535 func fileRange(pgf *ParsedGoFile, start, end token.Pos) (protocol.Range, error) {
536 s, err := span.FileSpan(pgf.Tok, pgf.Mapper.Converter, start, end)
538 return protocol.Range{}, nil
540 return pgf.Mapper.Range(s)
543 // isExported reports if a token is exported. Copied from
544 // token.IsExported (go1.13+).
546 // TODO: replace usage with token.IsExported once go1.12 is no longer
548 func isExported(name string) bool {
549 ch, _ := utf8.DecodeRuneInString(name)
550 return unicode.IsUpper(ch)
553 // pkgView holds information related to a package that we are going to walk.
554 type pkgView struct {
559 // symbolInformation is a cut-down version of protocol.SymbolInformation that
560 // allows struct values of this type to be used as map keys.
561 type symbolInformation struct {
566 kind protocol.SymbolKind
567 location protocol.Location
570 // asProtocolSymbolInformation converts s to a protocol.SymbolInformation value.
572 // TODO: work out how to handle tags if/when they are needed.
573 func (s symbolInformation) asProtocolSymbolInformation() protocol.SymbolInformation {
574 return protocol.SymbolInformation{
577 Location: s.location,
578 ContainerName: s.container,