mirror of
https://github.com/golang/go
synced 2024-11-18 18:24:48 -07:00
ecd3fc4348
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>
192 lines
4.6 KiB
Go
192 lines
4.6 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/ast"
|
|
"go/token"
|
|
"go/types"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
)
|
|
|
|
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,
|
|
}
|
|
|
|
// CodeLens computes code lens for Go source code.
|
|
func CodeLens(ctx context.Context, snapshot Snapshot, fh FileHandle) ([]protocol.CodeLens, error) {
|
|
pgh := snapshot.View().Session().Cache().ParseGoHandle(ctx, fh, ParseFull)
|
|
f, _, m, _, err := pgh.Parse(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var result []protocol.CodeLens
|
|
for lens, lf := range lensFuncs {
|
|
if !snapshot.View().Options().EnabledCodeLens[lens] {
|
|
continue
|
|
}
|
|
added, err := lf(ctx, snapshot, fh, f, m)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result = append(result, added...)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
var testMatcher = regexp.MustCompile("^Test[^a-z]")
|
|
var benchMatcher = regexp.MustCompile("^Benchmark[^a-z]")
|
|
|
|
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
|
|
}
|
|
|
|
if !strings.HasSuffix(fh.URI().Filename(), "_test.go") {
|
|
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
|
|
}
|
|
|
|
uri := fh.URI()
|
|
codeLens = append(codeLens, protocol.CodeLens{
|
|
Range: rng,
|
|
Command: protocol.Command{
|
|
Title: "run test",
|
|
Command: "test",
|
|
Arguments: []interface{}{fn.Name.Name, uri},
|
|
},
|
|
})
|
|
}
|
|
}
|
|
|
|
return codeLens, nil
|
|
}
|
|
|
|
func isTestFunc(fn *ast.FuncDecl, pkg Package) bool {
|
|
typesInfo := pkg.GetTypesInfo()
|
|
if typesInfo == nil {
|
|
return false
|
|
}
|
|
|
|
sig, ok := typesInfo.ObjectOf(fn.Name).Type().(*types.Signature)
|
|
if !ok {
|
|
return false
|
|
}
|
|
|
|
// test funcs should have a single parameter, so we can exit early if that's not the case.
|
|
if sig.Params().Len() != 1 {
|
|
return false
|
|
}
|
|
|
|
firstParam, ok := sig.Params().At(0).Type().(*types.Pointer)
|
|
if !ok {
|
|
return false
|
|
}
|
|
|
|
firstParamElem, ok := firstParam.Elem().(*types.Named)
|
|
if !ok {
|
|
return false
|
|
}
|
|
|
|
firstParamObj := firstParamElem.Obj()
|
|
if firstParamObj.Pkg().Path() != "testing" {
|
|
return false
|
|
}
|
|
|
|
firstParamName := firstParamObj.Id()
|
|
return (firstParamName == "T" && testMatcher.MatchString(fn.Name.Name)) ||
|
|
(firstParamName == "B" && benchMatcher.MatchString(fn.Name.Name))
|
|
}
|
|
|
|
func goGenerateCodeLens(ctx context.Context, snapshot Snapshot, fh FileHandle, f *ast.File, m *protocol.ColumnMapper) ([]protocol.CodeLens, error) {
|
|
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.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
|
|
}
|
|
|
|
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,
|
|
Arguments: []interface{}{fh.URI()},
|
|
},
|
|
},
|
|
}, nil
|
|
}
|