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/command"
14 "golang.org/x/tools/internal/lsp/mod"
15 "golang.org/x/tools/internal/lsp/protocol"
16 "golang.org/x/tools/internal/lsp/source"
19 func (s *Server) codeLens(ctx context.Context, params *protocol.CodeLensParams) ([]protocol.CodeLens, error) {
20 snapshot, fh, ok, release, err := s.beginFileRequest(ctx, params.TextDocument.URI, source.UnknownKind)
25 var lenses map[command.Command]source.LensFunc
28 lenses = mod.LensFuncs()
30 lenses = source.LensFuncs()
32 // Unsupported file kind for a code lens.
35 var result []protocol.CodeLens
36 for cmd, lf := range lenses {
37 if !snapshot.View().Options().Codelenses[string(cmd)] {
40 added, err := lf(ctx, snapshot, fh)
41 // Code lens is called on every keystroke, so we should just operate in
42 // a best-effort mode, ignoring errors.
44 event.Error(ctx, fmt.Sprintf("code lens %s failed", cmd), err)
47 result = append(result, added...)
49 sort.Slice(result, func(i, j int) bool {
50 a, b := result[i], result[j]
51 if protocol.CompareRange(a.Range, b.Range) == 0 {
52 return a.Command.Command < b.Command.Command
54 return protocol.CompareRange(a.Range, b.Range) < 0