1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
// 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/ast"
"go/token"
"go/types"
"path/filepath"
"regexp"
"strings"
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/span"
)
type LensFunc func(context.Context, Snapshot, FileHandle) ([]protocol.CodeLens, error)
// LensFuncs returns the supported lensFuncs for Go files.
func LensFuncs() map[string]LensFunc {
return map[string]LensFunc{
CommandGenerate.Name: goGenerateCodeLens,
CommandTest.Name: runTestCodeLens,
CommandRegenerateCgo.Name: regenerateCgoLens,
CommandToggleDetails.Name: toggleDetailsCodeLens,
}
}
var (
testRe = regexp.MustCompile("^Test[^a-z]")
benchmarkRe = regexp.MustCompile("^Benchmark[^a-z]")
)
func runTestCodeLens(ctx context.Context, snapshot Snapshot, fh FileHandle) ([]protocol.CodeLens, error) {
codeLens := make([]protocol.CodeLens, 0)
fns, err := TestsAndBenchmarks(ctx, snapshot, fh)
if err != nil {
return nil, err
}
for _, fn := range fns.Tests {
jsonArgs, err := MarshalArgs(fh.URI(), []string{fn.Name}, nil)
if err != nil {
return nil, err
}
codeLens = append(codeLens, protocol.CodeLens{
Range: protocol.Range{Start: fn.Rng.Start, End: fn.Rng.Start},
Command: protocol.Command{
Title: "run test",
Command: CommandTest.ID(),
Arguments: jsonArgs,
},
})
}
for _, fn := range fns.Benchmarks {
jsonArgs, err := MarshalArgs(fh.URI(), nil, []string{fn.Name})
if err != nil {
return nil, err
}
codeLens = append(codeLens, protocol.CodeLens{
Range: protocol.Range{Start: fn.Rng.Start, End: fn.Rng.Start},
Command: protocol.Command{
Title: "run benchmark",
Command: CommandTest.ID(),
Arguments: jsonArgs,
},
})
}
if len(fns.Benchmarks) > 0 {
_, pgf, err := GetParsedFile(ctx, snapshot, fh, WidestPackage)
if err != nil {
return nil, err
}
// 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{}, fns.Benchmarks)
if err != nil {
return nil, err
}
codeLens = append(codeLens, protocol.CodeLens{
Range: rng,
Command: protocol.Command{
Title: "run file benchmarks",
Command: CommandTest.ID(),
Arguments: args,
},
})
}
return codeLens, nil
}
type testFn struct {
Name string
Rng protocol.Range
}
type testFns struct {
Tests []testFn
Benchmarks []testFn
}
func TestsAndBenchmarks(ctx context.Context, snapshot Snapshot, fh FileHandle) (testFns, error) {
var out testFns
if !strings.HasSuffix(fh.URI().Filename(), "_test.go") {
return out, nil
}
pkg, pgf, err := GetParsedFile(ctx, snapshot, fh, WidestPackage)
if err != nil {
return out, err
}
for _, d := range pgf.File.Decls {
fn, ok := d.(*ast.FuncDecl)
if !ok {
continue
}
rng, err := NewMappedRange(snapshot.FileSet(), pgf.Mapper, d.Pos(), fn.End()).Range()
if err != nil {
return out, err
}
if matchTestFunc(fn, pkg, testRe, "T") {
out.Tests = append(out.Tests, testFn{fn.Name.Name, rng})
}
if matchTestFunc(fn, pkg, benchmarkRe, "B") {
out.Benchmarks = append(out.Benchmarks, testFn{fn.Name.Name, rng})
}
}
return out, nil
}
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) {
return false
}
info := pkg.GetTypesInfo()
if info == nil {
return false
}
obj := info.ObjectOf(fn.Name)
if obj == nil {
return false
}
sig, ok := obj.Type().(*types.Signature)
if !ok {
return false
}
// Test functions should have only one parameter.
if sig.Params().Len() != 1 {
return false
}
// Check the type of the only parameter
paramTyp, ok := sig.Params().At(0).Type().(*types.Pointer)
if !ok {
return false
}
named, ok := paramTyp.Elem().(*types.Named)
if !ok {
return false
}
namedObj := named.Obj()
if namedObj.Pkg().Path() != "testing" {
return false
}
return namedObj.Id() == paramID
}
func goGenerateCodeLens(ctx context.Context, snapshot Snapshot, fh FileHandle) ([]protocol.CodeLens, error) {
pgf, err := snapshot.ParseGo(ctx, fh, ParseFull)
if err != nil {
return nil, err
}
const ggDirective = "//go:generate"
for _, c := range pgf.File.Comments {
for _, l := range c.List {
if !strings.HasPrefix(l.Text, ggDirective) {
continue
}
rng, err := NewMappedRange(snapshot.FileSet(), pgf.Mapper, l.Pos(), l.Pos()+token.Pos(len(ggDirective))).Range()
if err != nil {
return nil, err
}
dir := span.URIFromPath(filepath.Dir(fh.URI().Filename()))
nonRecursiveArgs, err := MarshalArgs(dir, false)
if err != nil {
return nil, err
}
recursiveArgs, err := MarshalArgs(dir, true)
if err != nil {
return nil, err
}
return []protocol.CodeLens{
{
Range: rng,
Command: protocol.Command{
Title: "run go generate",
Command: CommandGenerate.ID(),
Arguments: nonRecursiveArgs,
},
},
{
Range: rng,
Command: protocol.Command{
Title: "run go generate ./...",
Command: CommandGenerate.ID(),
Arguments: recursiveArgs,
},
},
}, nil
}
}
return nil, nil
}
func regenerateCgoLens(ctx context.Context, snapshot Snapshot, fh FileHandle) ([]protocol.CodeLens, error) {
pgf, err := snapshot.ParseGo(ctx, fh, ParseFull)
if err != nil {
return nil, err
}
var c *ast.ImportSpec
for _, imp := range pgf.File.Imports {
if imp.Path.Value == `"C"` {
c = imp
}
}
if c == nil {
return nil, nil
}
rng, err := NewMappedRange(snapshot.FileSet(), pgf.Mapper, c.Pos(), c.EndPos).Range()
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: "regenerate cgo definitions",
Command: CommandRegenerateCgo.ID(),
Arguments: jsonArgs,
},
},
}, nil
}
func toggleDetailsCodeLens(ctx context.Context, snapshot Snapshot, fh FileHandle) ([]protocol.CodeLens, error) {
_, pgf, err := GetParsedFile(ctx, snapshot, fh, WidestPackage)
if err != nil {
return nil, err
}
rng, err := NewMappedRange(snapshot.FileSet(), pgf.Mapper, pgf.File.Package, pgf.File.Package).Range()
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.ID(),
Arguments: jsonArgs,
},
}}, nil
}
|