| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package golang |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"go/ast" |
| 10 |
"go/token" |
| 11 |
"go/types" |
| 12 |
"regexp" |
| 13 |
"strings" |
| 14 |
|
| 15 |
"golang.org/x/tools/gopls/internal/cache" |
| 16 |
"golang.org/x/tools/gopls/internal/cache/parsego" |
| 17 |
"golang.org/x/tools/gopls/internal/file" |
| 18 |
"golang.org/x/tools/gopls/internal/protocol" |
| 19 |
"golang.org/x/tools/gopls/internal/protocol/command" |
| 20 |
"golang.org/x/tools/gopls/internal/settings" |
| 21 |
) |
| 22 |
|
| 23 |
|
| 24 |
func CodeLensSources() map[settings.CodeLensSource]cache.CodeLensSourceFunc { |
| 25 |
return map[settings.CodeLensSource]cache.CodeLensSourceFunc{ |
| 26 |
settings.CodeLensGenerate: goGenerateCodeLens, |
| 27 |
settings.CodeLensTest: runTestCodeLens, |
| 28 |
settings.CodeLensRegenerateCgo: regenerateCgoLens, |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
var ( |
| 33 |
testRe = regexp.MustCompile(`^Test([^a-z]|$)`) |
| 34 |
benchmarkRe = regexp.MustCompile(`^Benchmark([^a-z]|$)`) |
| 35 |
) |
| 36 |
|
| 37 |
func runTestCodeLens(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle) ([]protocol.CodeLens, error) { |
| 38 |
var codeLens []protocol.CodeLens |
| 39 |
|
| 40 |
pkg, pgf, err := NarrowestPackageForFile(ctx, snapshot, fh.URI()) |
| 41 |
if err != nil { |
| 42 |
return nil, err |
| 43 |
} |
| 44 |
testFuncs, benchFuncs, err := testsAndBenchmarks(pkg.TypesInfo(), pgf) |
| 45 |
if err != nil { |
| 46 |
return nil, err |
| 47 |
} |
| 48 |
puri := fh.URI() |
| 49 |
for _, fn := range testFuncs { |
| 50 |
cmd := command.NewRunTestsCommand("run test", command.RunTestsArgs{ |
| 51 |
URI: puri, |
| 52 |
Tests: []string{fn.name}, |
| 53 |
}) |
| 54 |
rng := protocol.Range{Start: fn.rng.Start, End: fn.rng.Start} |
| 55 |
codeLens = append(codeLens, protocol.CodeLens{Range: rng, Command: cmd}) |
| 56 |
} |
| 57 |
|
| 58 |
for _, fn := range benchFuncs { |
| 59 |
cmd := command.NewRunTestsCommand("run benchmark", command.RunTestsArgs{ |
| 60 |
URI: puri, |
| 61 |
Benchmarks: []string{fn.name}, |
| 62 |
}) |
| 63 |
rng := protocol.Range{Start: fn.rng.Start, End: fn.rng.Start} |
| 64 |
codeLens = append(codeLens, protocol.CodeLens{Range: rng, Command: cmd}) |
| 65 |
} |
| 66 |
|
| 67 |
if len(benchFuncs) > 0 { |
| 68 |
pgf, err := snapshot.ParseGo(ctx, fh, parsego.Full) |
| 69 |
if err != nil { |
| 70 |
return nil, err |
| 71 |
} |
| 72 |
|
| 73 |
rng, err := pgf.PosRange(pgf.File.Package, pgf.File.Package) |
| 74 |
if err != nil { |
| 75 |
return nil, err |
| 76 |
} |
| 77 |
var benches []string |
| 78 |
for _, fn := range benchFuncs { |
| 79 |
benches = append(benches, fn.name) |
| 80 |
} |
| 81 |
cmd := command.NewRunTestsCommand("run file benchmarks", command.RunTestsArgs{ |
| 82 |
URI: puri, |
| 83 |
Benchmarks: benches, |
| 84 |
}) |
| 85 |
codeLens = append(codeLens, protocol.CodeLens{Range: rng, Command: cmd}) |
| 86 |
} |
| 87 |
return codeLens, nil |
| 88 |
} |
| 89 |
|
| 90 |
type testFunc struct { |
| 91 |
name string |
| 92 |
rng protocol.Range |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
func testsAndBenchmarks(info *types.Info, pgf *parsego.File) (tests, benchmarks []testFunc, _ error) { |
| 98 |
if !strings.HasSuffix(pgf.URI.Path(), "_test.go") { |
| 99 |
return nil, nil, nil |
| 100 |
} |
| 101 |
|
| 102 |
for _, d := range pgf.File.Decls { |
| 103 |
fn, ok := d.(*ast.FuncDecl) |
| 104 |
if !ok { |
| 105 |
continue |
| 106 |
} |
| 107 |
|
| 108 |
rng, err := pgf.NodeRange(fn) |
| 109 |
if err != nil { |
| 110 |
return nil, nil, err |
| 111 |
} |
| 112 |
|
| 113 |
if matchTestFunc(fn, info, testRe, "T") { |
| 114 |
tests = append(tests, testFunc{fn.Name.Name, rng}) |
| 115 |
} else if matchTestFunc(fn, info, benchmarkRe, "B") { |
| 116 |
benchmarks = append(benchmarks, testFunc{fn.Name.Name, rng}) |
| 117 |
} |
| 118 |
} |
| 119 |
return |
| 120 |
} |
| 121 |
|
| 122 |
func matchTestFunc(fn *ast.FuncDecl, info *types.Info, nameRe *regexp.Regexp, paramID string) bool { |
| 123 |
|
| 124 |
if !nameRe.MatchString(fn.Name.Name) { |
| 125 |
return false |
| 126 |
} |
| 127 |
obj, ok := info.ObjectOf(fn.Name).(*types.Func) |
| 128 |
if !ok { |
| 129 |
return false |
| 130 |
} |
| 131 |
sig := obj.Signature() |
| 132 |
|
| 133 |
if sig.Params().Len() != 1 { |
| 134 |
return false |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
paramTyp, ok := sig.Params().At(0).Type().(*types.Pointer) |
| 143 |
if !ok { |
| 144 |
return false |
| 145 |
} |
| 146 |
named, ok := paramTyp.Elem().(*types.Named) |
| 147 |
if !ok { |
| 148 |
return false |
| 149 |
} |
| 150 |
namedObj := named.Obj() |
| 151 |
if namedObj.Pkg().Path() != "testing" { |
| 152 |
return false |
| 153 |
} |
| 154 |
return namedObj.Id() == paramID |
| 155 |
} |
| 156 |
|
| 157 |
func goGenerateCodeLens(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle) ([]protocol.CodeLens, error) { |
| 158 |
pgf, err := snapshot.ParseGo(ctx, fh, parsego.Full) |
| 159 |
if err != nil { |
| 160 |
return nil, err |
| 161 |
} |
| 162 |
const ggDirective = "//go:generate" |
| 163 |
for _, c := range pgf.File.Comments { |
| 164 |
for _, l := range c.List { |
| 165 |
if !strings.HasPrefix(l.Text, ggDirective) { |
| 166 |
continue |
| 167 |
} |
| 168 |
rng, err := pgf.PosRange(l.Pos(), l.Pos()+token.Pos(len(ggDirective))) |
| 169 |
if err != nil { |
| 170 |
return nil, err |
| 171 |
} |
| 172 |
dir := fh.URI().Dir() |
| 173 |
nonRecursiveCmd := command.NewGenerateCommand("run go generate", command.GenerateArgs{Dir: dir, Recursive: false}) |
| 174 |
recursiveCmd := command.NewGenerateCommand("run go generate ./...", command.GenerateArgs{Dir: dir, Recursive: true}) |
| 175 |
return []protocol.CodeLens{ |
| 176 |
{Range: rng, Command: recursiveCmd}, |
| 177 |
{Range: rng, Command: nonRecursiveCmd}, |
| 178 |
}, nil |
| 179 |
|
| 180 |
} |
| 181 |
} |
| 182 |
return nil, nil |
| 183 |
} |
| 184 |
|
| 185 |
func regenerateCgoLens(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle) ([]protocol.CodeLens, error) { |
| 186 |
pgf, err := snapshot.ParseGo(ctx, fh, parsego.Full) |
| 187 |
if err != nil { |
| 188 |
return nil, err |
| 189 |
} |
| 190 |
var c *ast.ImportSpec |
| 191 |
for _, imp := range pgf.File.Imports { |
| 192 |
if imp.Path.Value == `"C"` { |
| 193 |
c = imp |
| 194 |
} |
| 195 |
} |
| 196 |
if c == nil { |
| 197 |
return nil, nil |
| 198 |
} |
| 199 |
rng, err := pgf.NodeRange(c) |
| 200 |
if err != nil { |
| 201 |
return nil, err |
| 202 |
} |
| 203 |
puri := fh.URI() |
| 204 |
cmd := command.NewRegenerateCgoCommand("regenerate cgo definitions", command.URIArg{URI: puri}) |
| 205 |
return []protocol.CodeLens{{Range: rng, Command: cmd}}, nil |
| 206 |
} |