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-05-14 12:02:48 -06:00
|
|
|
type lensFunc func(context.Context, Snapshot, FileHandle, *ast.File, *protocol.ColumnMapper) ([]protocol.CodeLens, error)
|
|
|
|
|
|
|
|
var lensFuncs = map[string]lensFunc{
|
|
|
|
CommandGenerate: goGenerateCodeLens,
|
|
|
|
CommandTest: runTestCodeLens,
|
|
|
|
CommandRegenerateCgo: regenerateCgoLens,
|
|
|
|
}
|
|
|
|
|
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) {
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
pgh := snapshot.View().Session().Cache().ParseGoHandle(ctx, fh, ParseFull)
|
|
|
|
f, _, m, _, err := pgh.Parse(ctx)
|
2020-03-10 22:49:10 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-05 12:43:51 -06:00
|
|
|
|
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-05-14 12:02:48 -06:00
|
|
|
added, err := lf(ctx, snapshot, fh, f, m)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func runTestCodeLens(ctx context.Context, snapshot Snapshot, fh FileHandle, f *ast.File, m *protocol.ColumnMapper) ([]protocol.CodeLens, error) {
|
|
|
|
codeLens := make([]protocol.CodeLens, 0)
|
|
|
|
|
|
|
|
pkg, _, err := getParsedFile(ctx, snapshot, fh, WidestPackageHandle)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
if !strings.HasSuffix(fh.URI().Filename(), "_test.go") {
|
2020-05-05 12:43:51 -06:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range f.Decls {
|
|
|
|
fn, ok := d.(*ast.FuncDecl)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if isTestFunc(fn, pkg) {
|
|
|
|
fset := snapshot.View().Session().Cache().FileSet()
|
|
|
|
rng, err := newMappedRange(fset, m, d.Pos(), d.Pos()).Range()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
uri := fh.URI()
|
2020-05-05 12:43:51 -06:00
|
|
|
codeLens = append(codeLens, protocol.CodeLens{
|
|
|
|
Range: rng,
|
|
|
|
Command: protocol.Command{
|
|
|
|
Title: "run test",
|
|
|
|
Command: "test",
|
|
|
|
Arguments: []interface{}{fn.Name.Name, uri},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return codeLens, nil
|
|
|
|
}
|
|
|
|
|
2020-06-24 15:51:35 -06:00
|
|
|
var testRe = regexp.MustCompile("^Test[^a-z]")
|
|
|
|
var benchmarkRe = regexp.MustCompile("^Benchmark[^a-z]")
|
|
|
|
|
2020-05-05 12:43:51 -06:00
|
|
|
func isTestFunc(fn *ast.FuncDecl, pkg Package) bool {
|
2020-06-24 15:51:35 -06:00
|
|
|
// Make sure that the function name matches either a test or benchmark function.
|
|
|
|
if !(testRe.MatchString(fn.Name.Name) || benchmarkRe.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-06-24 15:51:35 -06:00
|
|
|
// Check the type of the only parameter to confirm that it is *testing.T
|
|
|
|
// or *testing.B.
|
|
|
|
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-06-24 15:51:35 -06:00
|
|
|
paramName := namedObj.Id()
|
|
|
|
return paramName == "T" || paramName == "B"
|
2020-05-05 12:43:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func goGenerateCodeLens(ctx context.Context, snapshot Snapshot, fh FileHandle, f *ast.File, m *protocol.ColumnMapper) ([]protocol.CodeLens, error) {
|
2020-03-10 22:49:10 -06:00
|
|
|
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
|
|
|
|
}
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
dir := filepath.Dir(fh.URI().Filename())
|
2020-03-10 22:49:10 -06:00
|
|
|
return []protocol.CodeLens{
|
|
|
|
{
|
|
|
|
Range: rng,
|
|
|
|
Command: protocol.Command{
|
|
|
|
Title: "run go generate",
|
2020-05-06 20:54:50 -06:00
|
|
|
Command: CommandGenerate,
|
2020-03-10 22:49:10 -06:00
|
|
|
Arguments: []interface{}{dir, false},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Range: rng,
|
|
|
|
Command: protocol.Command{
|
|
|
|
Title: "run go generate ./...",
|
2020-05-06 20:54:50 -06:00
|
|
|
Command: CommandGenerate,
|
2020-03-10 22:49:10 -06:00
|
|
|
Arguments: []interface{}{dir, true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-05-14 12:02:48 -06:00
|
|
|
|
|
|
|
func regenerateCgoLens(ctx context.Context, snapshot Snapshot, fh FileHandle, f *ast.File, m *protocol.ColumnMapper) ([]protocol.CodeLens, error) {
|
|
|
|
var c *ast.ImportSpec
|
|
|
|
for _, imp := range f.Imports {
|
|
|
|
if imp.Path.Value == `"C"` {
|
|
|
|
c = imp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if c == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
fset := snapshot.View().Session().Cache().FileSet()
|
|
|
|
rng, err := newMappedRange(fset, m, c.Pos(), c.EndPos).Range()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return []protocol.CodeLens{
|
|
|
|
{
|
|
|
|
Range: rng,
|
|
|
|
Command: protocol.Command{
|
|
|
|
Title: "regenerate cgo definitions",
|
|
|
|
Command: CommandRegenerateCgo,
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
Arguments: []interface{}{fh.URI()},
|
2020-05-14 12:02:48 -06:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|