mirror of
https://github.com/golang/go
synced 2024-11-18 21:54:49 -07:00
cb8d9cd245
Some code lenses may be undesirable for certain users or editors -- for example a code lens that runs tests, when VSCode already supports this functionality outside of the LSP. To handle such situations, support configuring code lenses via a new 'codelens' gopls option. Add support for code lens in regtests, and use this to test the new configuration. To achieve this, thread through a new 'EditorConfig' type that configures the fake editor's LSP session. It made sense to move the test Env overlay onto this config object as well. While looking at them, document some types in source.Options. Change-Id: I961077422a273829c5cbd83c3b87fae29f77eeda Reviewed-on: https://go-review.googlesource.com/c/tools/+/232680 Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
60 lines
1.5 KiB
Go
60 lines
1.5 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 source
|
|
|
|
import (
|
|
"context"
|
|
"go/token"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
)
|
|
|
|
// CodeLens computes code lens for Go source code.
|
|
func CodeLens(ctx context.Context, snapshot Snapshot, fh FileHandle) ([]protocol.CodeLens, error) {
|
|
if !snapshot.View().Options().EnabledCodeLens[CommandGenerate] {
|
|
return nil, nil
|
|
}
|
|
f, _, m, _, err := snapshot.View().Session().Cache().ParseGoHandle(fh, ParseFull).Parse(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
const ggDirective = "//go:generate"
|
|
for _, c := range f.Comments {
|
|
for _, l := range c.List {
|
|
if !strings.HasPrefix(l.Text, ggDirective) {
|
|
continue
|
|
}
|
|
fset := snapshot.View().Session().Cache().FileSet()
|
|
rng, err := newMappedRange(fset, m, l.Pos(), l.Pos()+token.Pos(len(ggDirective))).Range()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
dir := filepath.Dir(fh.Identity().URI.Filename())
|
|
return []protocol.CodeLens{
|
|
{
|
|
Range: rng,
|
|
Command: protocol.Command{
|
|
Title: "run go generate",
|
|
Command: CommandGenerate,
|
|
Arguments: []interface{}{dir, false},
|
|
},
|
|
},
|
|
{
|
|
Range: rng,
|
|
Command: protocol.Command{
|
|
Title: "run go generate ./...",
|
|
Command: CommandGenerate,
|
|
Arguments: []interface{}{dir, true},
|
|
},
|
|
},
|
|
}, nil
|
|
|
|
}
|
|
}
|
|
return nil, nil
|
|
}
|