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.
12 "golang.org/x/tools/internal/event"
13 "golang.org/x/tools/internal/lsp/mod"
14 "golang.org/x/tools/internal/lsp/protocol"
15 "golang.org/x/tools/internal/lsp/source"
18 func (s *Server) codeLens(ctx context.Context, params *protocol.CodeLensParams) ([]protocol.CodeLens, error) {
19 snapshot, fh, ok, release, err := s.beginFileRequest(ctx, params.TextDocument.URI, source.UnknownKind)
24 var lensFuncs map[string]source.LensFunc
27 lensFuncs = mod.LensFuncs()
29 lensFuncs = source.LensFuncs()
31 // Unsupported file kind for a code lens.
34 var result []protocol.CodeLens
35 for lens, lf := range lensFuncs {
36 if !snapshot.View().Options().Codelens[lens] {
39 added, err := lf(ctx, snapshot, fh)
40 // Code lens is called on every keystroke, so we should just operate in
41 // a best-effort mode, ignoring errors.
43 event.Error(ctx, fmt.Sprintf("code lens %s failed", lens), err)
46 result = append(result, added...)
48 sort.Slice(result, func(i, j int) bool {
49 a, b := result[i], result[j]
50 if protocol.CompareRange(a.Range, b.Range) == 0 {
51 return a.Command.Command < b.Command.Command
53 return protocol.CompareRange(a.Range, b.Range) < 0