2020-03-10 22:49:10 -06:00
|
|
|
// 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"
|
2020-05-05 12:43:51 -06:00
|
|
|
"go/ast"
|
2020-03-10 22:49:10 -06:00
|
|
|
"go/token"
|
2020-05-05 12:43:51 -06:00
|
|
|
"go/types"
|
2020-03-10 22:49:10 -06:00
|
|
|
"path/filepath"
|
2020-05-05 12:43:51 -06:00
|
|
|
"regexp"
|
2020-03-10 22:49:10 -06:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
2020-07-14 18:40:38 -06:00
|
|
|
"golang.org/x/tools/internal/span"
|
2020-03-10 22:49:10 -06:00
|
|
|
)
|
|
|
|
|
2020-07-15 21:42:01 -06:00
|
|
|
type lensFunc func(context.Context, Snapshot, FileHandle) ([]protocol.CodeLens, error)
|
2020-05-14 12:02:48 -06:00
|
|
|
|
|
|
|
var lensFuncs = map[string]lensFunc{
|
2020-07-23 21:24:36 -06:00
|
|
|
CommandGenerate.Name: goGenerateCodeLens,
|
|
|
|
CommandTest.Name: runTestCodeLens,
|
|
|
|
CommandRegenerateCgo.Name: regenerateCgoLens,
|
2020-07-16 08:45:30 -06:00
|
|
|
CommandToggleDetails.Name: toggleDetailsCodeLens,
|
2020-05-14 12:02:48 -06:00
|
|
|
}
|
|
|
|
|
2020-05-06 20:54:50 -06:00
|
|
|
// CodeLens computes code lens for Go source code.
|
2020-03-10 22:49:10 -06:00
|
|
|
func CodeLens(ctx context.Context, snapshot Snapshot, fh FileHandle) ([]protocol.CodeLens, error) {
|
2020-05-14 12:02:48 -06:00
|
|
|
var result []protocol.CodeLens
|
|
|
|
for lens, lf := range lensFuncs {
|
|
|
|
if !snapshot.View().Options().EnabledCodeLens[lens] {
|
|
|
|
continue
|
2020-05-05 12:43:51 -06:00
|
|
|
}
|
2020-07-15 21:42:01 -06:00
|
|
|
added, err := lf(ctx, snapshot, fh)
|
2020-05-05 12:43:51 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-14 12:02:48 -06:00
|
|
|
result = append(result, added...)
|
2020-05-05 12:43:51 -06:00
|
|
|
}
|
2020-05-14 12:02:48 -06:00
|
|
|
return result, nil
|
2020-05-05 12:43:51 -06:00
|
|
|
}
|
|
|
|
|
2020-07-23 21:24:36 -06:00
|
|
|
var (
|
|
|
|
testRe = regexp.MustCompile("^Test[^a-z]")
|
|
|
|
benchmarkRe = regexp.MustCompile("^Benchmark[^a-z]")
|
|
|
|
)
|
2020-07-16 14:14:04 -06:00
|
|
|
|
2020-07-15 21:42:01 -06:00
|
|
|
func runTestCodeLens(ctx context.Context, snapshot Snapshot, fh FileHandle) ([]protocol.CodeLens, error) {
|
2020-05-05 12:43:51 -06:00
|
|
|
codeLens := make([]protocol.CodeLens, 0)
|
|
|
|
|
2020-07-15 21:42:01 -06:00
|
|
|
if !strings.HasSuffix(fh.URI().Filename(), "_test.go") {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-07-22 09:32:32 -06:00
|
|
|
pkg, pgf, err := getParsedFile(ctx, snapshot, fh, WidestPackage)
|
2020-05-05 12:43:51 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-30 16:48:51 -06:00
|
|
|
|
|
|
|
var benchFns []string
|
2020-07-21 13:15:06 -06:00
|
|
|
for _, d := range pgf.File.Decls {
|
2020-05-05 12:43:51 -06:00
|
|
|
fn, ok := d.(*ast.FuncDecl)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2020-07-30 16:48:51 -06:00
|
|
|
if benchmarkRe.MatchString(fn.Name.Name) {
|
|
|
|
benchFns = append(benchFns, fn.Name.Name)
|
|
|
|
}
|
2020-07-28 15:00:10 -06:00
|
|
|
rng, err := newMappedRange(snapshot.FileSet(), pgf.Mapper, d.Pos(), d.Pos()).Range()
|
2020-07-15 21:42:01 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-05-05 12:43:51 -06:00
|
|
|
}
|
2020-07-16 14:14:04 -06:00
|
|
|
|
|
|
|
if matchTestFunc(fn, pkg, testRe, "T") {
|
2020-07-30 16:48:51 -06:00
|
|
|
jsonArgs, err := MarshalArgs(fh.URI(), []string{fn.Name.Name}, nil)
|
2020-07-16 14:14:04 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
codeLens = append(codeLens, protocol.CodeLens{
|
|
|
|
Range: rng,
|
|
|
|
Command: protocol.Command{
|
|
|
|
Title: "run test",
|
2020-07-23 21:24:36 -06:00
|
|
|
Command: CommandTest.Name,
|
2020-07-16 14:14:04 -06:00
|
|
|
Arguments: jsonArgs,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if matchTestFunc(fn, pkg, benchmarkRe, "B") {
|
2020-07-30 16:48:51 -06:00
|
|
|
jsonArgs, err := MarshalArgs(fh.URI(), nil, []string{fn.Name.Name})
|
2020-07-16 14:14:04 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
codeLens = append(codeLens, protocol.CodeLens{
|
|
|
|
Range: rng,
|
|
|
|
Command: protocol.Command{
|
|
|
|
Title: "run benchmark",
|
2020-07-23 21:24:36 -06:00
|
|
|
Command: CommandTest.Name,
|
2020-07-16 14:14:04 -06:00
|
|
|
Arguments: jsonArgs,
|
|
|
|
},
|
|
|
|
})
|
2020-07-14 18:40:38 -06:00
|
|
|
}
|
2020-05-05 12:43:51 -06:00
|
|
|
}
|
2020-07-30 16:48:51 -06:00
|
|
|
// add a code lens to the top of the file which runs all benchmarks in the file
|
|
|
|
rng, err := newMappedRange(snapshot.FileSet(), pgf.Mapper, pgf.File.Package, pgf.File.Package).Range()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
args, err := MarshalArgs(fh.URI(), []string{}, benchFns)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
codeLens = append(codeLens, protocol.CodeLens{
|
|
|
|
Range: rng,
|
|
|
|
Command: protocol.Command{
|
|
|
|
Title: "run file benchmarks",
|
|
|
|
Command: CommandTest.Name,
|
|
|
|
Arguments: args,
|
|
|
|
},
|
|
|
|
})
|
2020-05-05 12:43:51 -06:00
|
|
|
return codeLens, nil
|
|
|
|
}
|
|
|
|
|
2020-07-16 14:14:04 -06:00
|
|
|
func matchTestFunc(fn *ast.FuncDecl, pkg Package, nameRe *regexp.Regexp, paramID string) bool {
|
|
|
|
// Make sure that the function name matches a test function.
|
|
|
|
if !nameRe.MatchString(fn.Name.Name) {
|
2020-05-05 12:43:51 -06:00
|
|
|
return false
|
|
|
|
}
|
2020-06-24 15:51:35 -06:00
|
|
|
info := pkg.GetTypesInfo()
|
|
|
|
if info == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
obj := info.ObjectOf(fn.Name)
|
|
|
|
if obj == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
sig, ok := obj.Type().(*types.Signature)
|
2020-05-05 12:43:51 -06:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
2020-06-24 15:51:35 -06:00
|
|
|
// Test functions should have only one parameter.
|
2020-05-05 12:43:51 -06:00
|
|
|
if sig.Params().Len() != 1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-07-16 14:14:04 -06:00
|
|
|
// Check the type of the only parameter
|
2020-06-24 15:51:35 -06:00
|
|
|
paramTyp, ok := sig.Params().At(0).Type().(*types.Pointer)
|
2020-05-05 12:43:51 -06:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
2020-06-24 15:51:35 -06:00
|
|
|
named, ok := paramTyp.Elem().(*types.Named)
|
2020-05-05 12:43:51 -06:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
2020-06-24 15:51:35 -06:00
|
|
|
namedObj := named.Obj()
|
|
|
|
if namedObj.Pkg().Path() != "testing" {
|
2020-05-05 12:43:51 -06:00
|
|
|
return false
|
|
|
|
}
|
2020-07-16 14:14:04 -06:00
|
|
|
return namedObj.Id() == paramID
|
2020-05-05 12:43:51 -06:00
|
|
|
}
|
|
|
|
|
2020-07-15 21:42:01 -06:00
|
|
|
func goGenerateCodeLens(ctx context.Context, snapshot Snapshot, fh FileHandle) ([]protocol.CodeLens, error) {
|
2020-07-21 13:15:06 -06:00
|
|
|
pgf, err := snapshot.ParseGo(ctx, fh, ParseFull)
|
2020-07-15 21:42:01 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-10 22:49:10 -06:00
|
|
|
const ggDirective = "//go:generate"
|
2020-07-21 13:15:06 -06:00
|
|
|
for _, c := range pgf.File.Comments {
|
2020-03-10 22:49:10 -06:00
|
|
|
for _, l := range c.List {
|
|
|
|
if !strings.HasPrefix(l.Text, ggDirective) {
|
|
|
|
continue
|
|
|
|
}
|
2020-07-28 15:00:10 -06:00
|
|
|
rng, err := newMappedRange(snapshot.FileSet(), pgf.Mapper, l.Pos(), l.Pos()+token.Pos(len(ggDirective))).Range()
|
2020-03-10 22:49:10 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-14 18:40:38 -06:00
|
|
|
dir := span.URIFromPath(filepath.Dir(fh.URI().Filename()))
|
2020-07-23 21:24:36 -06:00
|
|
|
nonRecursiveArgs, err := MarshalArgs(dir, false)
|
2020-07-14 18:40:38 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-23 21:24:36 -06:00
|
|
|
recursiveArgs, err := MarshalArgs(dir, true)
|
2020-07-14 18:40:38 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-10 22:49:10 -06:00
|
|
|
return []protocol.CodeLens{
|
|
|
|
{
|
|
|
|
Range: rng,
|
|
|
|
Command: protocol.Command{
|
|
|
|
Title: "run go generate",
|
2020-07-23 21:24:36 -06:00
|
|
|
Command: CommandGenerate.Name,
|
2020-07-14 18:40:38 -06:00
|
|
|
Arguments: nonRecursiveArgs,
|
2020-03-10 22:49:10 -06:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Range: rng,
|
|
|
|
Command: protocol.Command{
|
|
|
|
Title: "run go generate ./...",
|
2020-07-23 21:24:36 -06:00
|
|
|
Command: CommandGenerate.Name,
|
2020-07-14 18:40:38 -06:00
|
|
|
Arguments: recursiveArgs,
|
2020-03-10 22:49:10 -06:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-05-14 12:02:48 -06:00
|
|
|
|
2020-07-15 21:42:01 -06:00
|
|
|
func regenerateCgoLens(ctx context.Context, snapshot Snapshot, fh FileHandle) ([]protocol.CodeLens, error) {
|
2020-07-21 13:15:06 -06:00
|
|
|
pgf, err := snapshot.ParseGo(ctx, fh, ParseFull)
|
2020-07-15 21:42:01 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-14 12:02:48 -06:00
|
|
|
var c *ast.ImportSpec
|
2020-07-21 13:15:06 -06:00
|
|
|
for _, imp := range pgf.File.Imports {
|
2020-05-14 12:02:48 -06:00
|
|
|
if imp.Path.Value == `"C"` {
|
|
|
|
c = imp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if c == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-07-28 15:00:10 -06:00
|
|
|
rng, err := newMappedRange(snapshot.FileSet(), pgf.Mapper, c.Pos(), c.EndPos).Range()
|
2020-05-14 12:02:48 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-23 21:24:36 -06:00
|
|
|
jsonArgs, err := MarshalArgs(fh.URI())
|
2020-07-14 18:40:38 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-14 12:02:48 -06:00
|
|
|
return []protocol.CodeLens{
|
|
|
|
{
|
|
|
|
Range: rng,
|
|
|
|
Command: protocol.Command{
|
|
|
|
Title: "regenerate cgo definitions",
|
2020-07-23 21:24:36 -06:00
|
|
|
Command: CommandRegenerateCgo.Name,
|
2020-07-14 18:40:38 -06:00
|
|
|
Arguments: jsonArgs,
|
2020-05-14 12:02:48 -06:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
2020-07-16 08:45:30 -06:00
|
|
|
|
|
|
|
func toggleDetailsCodeLens(ctx context.Context, snapshot Snapshot, fh FileHandle) ([]protocol.CodeLens, error) {
|
|
|
|
_, pgf, err := getParsedFile(ctx, snapshot, fh, WidestPackage)
|
2020-07-28 18:05:06 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-28 15:00:10 -06:00
|
|
|
rng, err := newMappedRange(snapshot.FileSet(), pgf.Mapper, pgf.File.Package, pgf.File.Package).Range()
|
2020-07-16 08:45:30 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
jsonArgs, err := MarshalArgs(fh.URI())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return []protocol.CodeLens{
|
|
|
|
{
|
|
|
|
Range: rng,
|
|
|
|
Command: protocol.Command{
|
|
|
|
Title: "Toggle gc annotation details",
|
|
|
|
Command: CommandToggleDetails.Name,
|
|
|
|
Arguments: jsonArgs,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|