1
0
mirror of https://github.com/golang/go synced 2024-11-18 11:04:42 -07:00
go/internal/lsp/code_lens.go
Rebecca Stambler 5d67d6c6f5 internal/lsp: clean up some of the mod code lens code
Refactor the checks for code lenses being enabled out of the source
package so that the mod code lenses can also make use of them.

Also, a few small changes to the titles of the `go mod tidy` and `go mod
vendor` code lenses.

Change-Id: I4e79ab08a4e7aea4d4d6de6fd652d0b77d30c811
Reviewed-on: https://go-review.googlesource.com/c/tools/+/252397
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
2020-09-02 16:06:48 +00:00

49 lines
1.3 KiB
Go

// 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 lsp
import (
"context"
"fmt"
"golang.org/x/tools/internal/event"
"golang.org/x/tools/internal/lsp/mod"
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/lsp/source"
)
func (s *Server) codeLens(ctx context.Context, params *protocol.CodeLensParams) ([]protocol.CodeLens, error) {
snapshot, fh, ok, release, err := s.beginFileRequest(ctx, params.TextDocument.URI, source.UnknownKind)
defer release()
if !ok {
return nil, err
}
var lensFuncs map[string]source.LensFunc
switch fh.Kind() {
case source.Mod:
lensFuncs = mod.LensFuncs()
case source.Go:
lensFuncs = source.LensFuncs()
default:
// Unsupported file kind for a code lens.
return nil, nil
}
var result []protocol.CodeLens
for lens, lf := range lensFuncs {
if !snapshot.View().Options().EnabledCodeLens[lens] {
continue
}
added, err := lf(ctx, snapshot, fh)
// Code lens is called on every keystroke, so we should just operate in
// a best-effort mode, ignoring errors.
if err != nil {
event.Error(ctx, fmt.Sprintf("code lens %s failed", lens), err)
continue
}
result = append(result, added...)
}
return result, nil
}