Skip to content

Commit

Permalink
deps: upgrade x/tools and gopls to 1c30660f (#911)
Browse files Browse the repository at this point in the history
* gopls/integration/govim: enable running at main 1c30660f
* internal/lsp: fix a few staticcheck suggestions, some cleanup 6b78e25f
* internal/lsp: don't show diagnostic in go.mod for direct dependencies 8afe7e08
* internal/lsp: properly check for nil on i.enclosing 449c5851
* internal/lsp/source: move initial extract function logic to lsp/command 1c1ec420
* internal/lsp/source: add extra nil check in searchForEnclosing df70183b
* internal/lsp: fix logged message for gc_details e8769ccb
* internal/lsp: remove uses of crypto/sha1 in gopls 0dd562f3
* internal/lsp: show compiler optimization decisions 95780ea8
* internal/lsp: de-duplicate logic for canExtractVariable edd3c8e9
* internal/lsp: check URIs of all workspace folders 342ee105
* internal/lsp/source: add a unit test for searchForEnclosing e7a7e3a8
* internal/lsp: remove PackageHandle b6476686
* internal/lsp: replace ParseGoHandle with concrete data a9439ae9
* internal/lsp/cache: fix parseKey 4d1d9acc
* internal/lsp: pass snapshot/view to memoize.Functions 72051f79
* internal/lsp/cache: remove files from the memoize.Store 60da08ac
* internal/lsp: minimize PackageHandle interface 051e64e6
* internal/lsp: handle bad formatting with CRLF line endings 2ad651e9
* internal/lsp: support return statements in extract function 3c048e20
* internal/lsp: update code for LSP protocol cd83430b
* internal/lsp: allow narrower scope for convenience CodeActions 55644ead
* internal/lsp: correctly marshal arguments for upgrade code lens 7b4c4ad3
* internal/lsp: correctly invalidate metadata for batched changes 59c6fc0b
* internal/lsp: support refactor.extract through commands 92670837
* internal/lsp: fix hover link for embedded fields and methods eaaaedc6
* internal/lsp: fix GlobPattern for watching files 102e7d35
* internal/lsp: add parse errors as diagnostics even when parsing fails b5fc9d35
* internal/lsp: fix hover for implicit type switch variable declarations 7017fd6b
* internal/lsp: add errors for missing modules that don't map to an import cbb3c69a
* internal/lsp: move undeclaredname suggested fix out of analysis 37a045f3
* internal/lsp/regtest: fix WithoutWorkspaceFolders option 0e1f6f5c
* internal/lsp/regtest: add a simple stress test 93b64502
* internal/lsp/regtest: add run options to support stress testing 84d0e3d1
* internal/lsp/fake: move to a struct for configuring the sandbox a5e28d8d
* internal/lsp: lift env out of the sandbox into the editor 1ff154e6
* internal/lsp: show errors in indirect dependencies as diagnostics d2c1b4b2
* internal/lsp: treat the module root as the workspace root, if available 0cdb17d1
* internal/lsp/cache: copy *packages.Config environment completely b3d141dd
* internal/lsp: don't publish duplicate diagnostics in one report 80cb7970
* go/packages: add failing test for golang/go#37098 188b3828
* go/packages: find filenames in error strings if not in position a7c6fd06
* internal/lsp: separate test and benchmark codelens bd1e9de8
* go/analysis/passes/unmarshal: Add check for asn1.Unmarshal 70419130
* internal/lsp: support `go mod tidy` on save without diagnostics 6123e778
* internal/lsp: try to parse diagnostics out of `go list` errors b42efcd1
* internal/lsp: handle unknown revision in go.mod file 0a5cd101
* internal/lsp: handle on-disk file deletions for opened files acdb8c15
* internal/lsp: change the way that we pass arguments to command 5ea36318
* internal/lsp: return the actual range from convenience code actions b8e13e1a
  • Loading branch information
myitcv authored Jul 29, 2020
1 parent c6e168a commit b911480
Show file tree
Hide file tree
Showing 73 changed files with 3,398 additions and 2,061 deletions.
32 changes: 16 additions & 16 deletions cmd/govim/gopls_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,10 @@ func (l loggingGoplsServer) Declaration(ctxt context.Context, params *protocol.D
return res, err
}

func (l loggingGoplsServer) LogTraceNotification(ctxt context.Context, params *protocol.LogTraceParams) error {
l.Logf("gopls.LogTraceNotification() call; params:\n%v", pretty.Sprint(params))
err := l.u.LogTraceNotification(ctxt, params)
l.Logf("gopls.LogTraceNotification() return; err: %v", err)
func (l loggingGoplsServer) LogTrace(ctxt context.Context, params *protocol.LogTraceParams) error {
l.Logf("gopls.LogTrace() call; params:\n%v", pretty.Sprint(params))
err := l.u.LogTrace(ctxt, params)
l.Logf("gopls.LogTrace() return; err: %v", err)
return err
}

Expand All @@ -301,10 +301,10 @@ func (l loggingGoplsServer) PrepareRename(ctxt context.Context, params *protocol
return res, err
}

func (l loggingGoplsServer) SetTraceNotification(ctxt context.Context, params *protocol.SetTraceParams) error {
l.Logf("gopls.SetTraceNotification() call; params:\n%v", pretty.Sprint(params))
err := l.u.SetTraceNotification(ctxt, params)
l.Logf("gopls.SetTraceNotification() return; err: %v", err)
func (l loggingGoplsServer) SetTrace(ctxt context.Context, params *protocol.SetTraceParams) error {
l.Logf("gopls.SetTrace() call; params:\n%v", pretty.Sprint(params))
err := l.u.SetTrace(ctxt, params)
l.Logf("gopls.SetTrace() return; err: %v", err)
return err
}

Expand Down Expand Up @@ -343,17 +343,17 @@ func (l loggingGoplsServer) PrepareCallHierarchy(ctxt context.Context, params *p
return res, err
}

func (l loggingGoplsServer) SemanticTokens(ctxt context.Context, params *protocol.SemanticTokensParams) (*protocol.SemanticTokens, error) {
l.Logf("gopls.SemanticTokens() call; params:\n%v", pretty.Sprint(params))
res, err := l.u.SemanticTokens(ctxt, params)
l.Logf("gopls.SemanticTokens() return; err: %v; res\n%v", err, pretty.Sprint(res))
func (l loggingGoplsServer) SemanticTokensFull(ctxt context.Context, params *protocol.SemanticTokensParams) (*protocol.SemanticTokens, error) {
l.Logf("gopls.SemanticTokensFull() call; params:\n%v", pretty.Sprint(params))
res, err := l.u.SemanticTokensFull(ctxt, params)
l.Logf("gopls.SemanticTokensFull() return; err: %v; res\n%v", err, pretty.Sprint(res))
return res, err
}

func (l loggingGoplsServer) SemanticTokensEdits(ctxt context.Context, params *protocol.SemanticTokensEditsParams) (interface{}, error) {
l.Logf("gopls.SemanticTokensEdits() call; params:\n%v", pretty.Sprint(params))
res, err := l.u.SemanticTokensEdits(ctxt, params)
l.Logf("gopls.SemanticTokensEdits() return; err: %v; res\n%v", err, pretty.Sprint(res))
func (l loggingGoplsServer) SemanticTokensFullDelta(ctxt context.Context, params *protocol.SemanticTokensDeltaParams) (interface{}, error) {
l.Logf("gopls.SemanticTokensFullDelta() call; params:\n%v", pretty.Sprint(params))
res, err := l.u.SemanticTokensFullDelta(ctxt, params)
l.Logf("gopls.SemanticTokensFullDelta() return; err: %v; res\n%v", err, pretty.Sprint(res))
return res, err
}

Expand Down
19 changes: 19 additions & 0 deletions cmd/govim/internal/golang_org_x_tools/analysisinternal/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,22 @@ func baseIfStmt(path []ast.Node, index int) ast.Stmt {
}
return stmt.(ast.Stmt)
}

// WalkASTWithParent walks the AST rooted at n. The semantics are
// similar to ast.Inspect except it does not call f(nil).
func WalkASTWithParent(n ast.Node, f func(n ast.Node, parent ast.Node) bool) {
var ancestors []ast.Node
ast.Inspect(n, func(n ast.Node) (recurse bool) {
if n == nil {
ancestors = ancestors[:len(ancestors)-1]
return false
}

var parent ast.Node
if len(ancestors) > 0 {
parent = ancestors[len(ancestors)-1]
}
ancestors = append(ancestors, n)
return f(n, parent)
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/go/ast/inspector"
"github.com/govim/govim/cmd/govim/internal/golang_org_x_tools/analysisinternal"
"github.com/govim/govim/cmd/govim/internal/golang_org_x_tools/span"
)

const Doc = `note incomplete struct initializations
Expand Down Expand Up @@ -114,14 +115,16 @@ func run(pass *analysis.Pass) (interface{}, error) {
}
pass.Report(analysis.Diagnostic{
Message: fmt.Sprintf("Fill %s with default values", name),
Pos: expr.Lbrace,
End: expr.Rbrace,
Pos: expr.Pos(),
End: expr.End(),
})
})
return nil, nil
}

func SuggestedFix(fset *token.FileSet, pos token.Pos, content []byte, file *ast.File, pkg *types.Package, info *types.Info) (*analysis.SuggestedFix, error) {
func SuggestedFix(fset *token.FileSet, rng span.Range, content []byte, file *ast.File, pkg *types.Package, info *types.Info) (*analysis.SuggestedFix, error) {
pos := rng.Start // don't use the end

// TODO(rstambler): Using ast.Inspect would probably be more efficient than
// calling PathEnclosingInterval. Switch this approach.
path, _ := astutil.PathEnclosingInterval(file, pos, pos)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import (
"bytes"
"fmt"
"go/ast"
"go/printer"
"go/token"
"go/types"
"strings"

"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/ast/astutil"
"github.com/govim/govim/cmd/govim/internal/golang_org_x_tools/analysisinternal"
"github.com/govim/govim/cmd/govim/internal/golang_org_x_tools/span"
)

const Doc = `suggested fixes for "undeclared name: <>"
Expand Down Expand Up @@ -65,51 +67,62 @@ func run(pass *analysis.Pass) (interface{}, error) {
if _, ok := path[1].(*ast.SelectorExpr); ok {
continue
}
// TODO(golang.org/issue/34644): in a follow up handle call expressions
// with suggested fix to create function
// TODO(golang.org/issue/34644): Handle call expressions with suggested
// fixes to create a function.
if _, ok := path[1].(*ast.CallExpr); ok {
continue
}

// Get the place to insert the new statement.
insertBeforeStmt := analysisinternal.StmtToInsertVarBefore(path)
if insertBeforeStmt == nil {
continue
}

var buf bytes.Buffer
if err := printer.Fprint(&buf, pass.Fset, file); err != nil {
tok := pass.Fset.File(file.Pos())
if tok == nil {
continue
}
old := buf.Bytes()
insertBefore := pass.Fset.Position(insertBeforeStmt.Pos()).Offset

// Get the indent to add on the line after the new statement.
// Since this will have a parse error, we can not use format.Source().
contentBeforeStmt, indent := old[:insertBefore], "\n"
if nl := bytes.LastIndex(contentBeforeStmt, []byte("\n")); nl != -1 {
indent = string(contentBeforeStmt[nl:])
}
// Create the new local variable statement.
newStmt := fmt.Sprintf("%s := %s", ident.Name, indent)

offset := pass.Fset.Position(err.Pos).Offset
end := tok.Pos(offset + len(name))
pass.Report(analysis.Diagnostic{
Pos: err.Pos,
End: analysisinternal.TypeErrorEndPos(pass.Fset, old, err.Pos),
End: end,
Message: err.Msg,
SuggestedFixes: []analysis.SuggestedFix{{
Message: fmt.Sprintf("Create variable \"%s\"", ident.Name),
TextEdits: []analysis.TextEdit{{
Pos: insertBeforeStmt.Pos(),
End: insertBeforeStmt.Pos(),
NewText: []byte(newStmt),
}},
}},
})
}
return nil, nil
}

func SuggestedFix(fset *token.FileSet, rng span.Range, content []byte, file *ast.File, _ *types.Package, _ *types.Info) (*analysis.SuggestedFix, error) {
pos := rng.Start // don't use the end
path, _ := astutil.PathEnclosingInterval(file, pos, pos)
if len(path) < 2 {
return nil, fmt.Errorf("")
}
ident, ok := path[0].(*ast.Ident)
if !ok {
return nil, fmt.Errorf("")
}
// Get the place to insert the new statement.
insertBeforeStmt := analysisinternal.StmtToInsertVarBefore(path)
if insertBeforeStmt == nil {
return nil, fmt.Errorf("")
}

insertBefore := fset.Position(insertBeforeStmt.Pos()).Offset

// Get the indent to add on the line after the new statement.
// Since this will have a parse error, we can not use format.Source().
contentBeforeStmt, indent := content[:insertBefore], "\n"
if nl := bytes.LastIndex(contentBeforeStmt, []byte("\n")); nl != -1 {
indent = string(contentBeforeStmt[nl:])
}
// Create the new local variable statement.
newStmt := fmt.Sprintf("%s := %s", ident.Name, indent)
return &analysis.SuggestedFix{
Message: fmt.Sprintf("Create variable \"%s\"", ident.Name),
TextEdits: []analysis.TextEdit{{
Pos: insertBeforeStmt.Pos(),
End: insertBeforeStmt.Pos(),
NewText: []byte(newStmt),
}},
}, nil
}

func FixesError(msg string) bool {
return strings.HasPrefix(msg, undeclaredNamePrefix)
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,20 @@ func run(pass *analysis.Pass) (interface{}, error) {
if nObj := pass.TypesInfo.ObjectOf(n); nObj != param.typObj {
return false
}
if _, ok := unused[param]; ok {
delete(unused, param)
}
delete(unused, param)
return false
})

// Create the reports for the unused parameters.
for u, _ := range unused {
for u := range unused {
start, end := u.field.Pos(), u.field.End()
if len(u.field.Names) > 1 {
start, end = u.ident.Pos(), u.ident.End()
}
// TODO(golang/go#36602): add suggested fixes to automatically remove the unused parameter,
// to start, just remove it from the function declaration,
// later, remove from every use of this function
// TODO(golang/go#36602): Add suggested fixes to automatically
// remove the unused parameter. To start, just remove it from the
// function declaration. Later, remove it from every use of this
// function.
pass.Report(analysis.Diagnostic{
Pos: start,
End: end,
Expand Down
40 changes: 22 additions & 18 deletions cmd/govim/internal/golang_org_x_tools/lsp/cache/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package cache
import (
"context"
"fmt"
"go/token"
"go/ast"
"go/types"
"reflect"
"sort"
Expand Down Expand Up @@ -41,7 +41,7 @@ func (s *snapshot) Analyze(ctx context.Context, id string, analyzers ...*analysi

var results []*source.Error
for _, ah := range roots {
diagnostics, _, err := ah.analyze(ctx)
diagnostics, _, err := ah.analyze(ctx, s)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -84,16 +84,16 @@ type packageFactKey struct {
func (s *snapshot) actionHandle(ctx context.Context, id packageID, a *analysis.Analyzer) (*actionHandle, error) {
ph := s.getPackage(id, source.ParseFull)
if ph == nil {
return nil, errors.Errorf("no PackageHandle for %s", id)
return nil, errors.Errorf("no package for %s", id)
}
act := s.getActionHandle(id, ph.mode, a)
if act != nil {
return act, nil
}
if len(ph.key) == 0 {
return nil, errors.Errorf("no key for PackageHandle %s", id)
return nil, errors.Errorf("no key for package %s", id)
}
pkg, err := ph.check(ctx)
pkg, err := ph.check(ctx, s)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -133,26 +133,25 @@ func (s *snapshot) actionHandle(ctx context.Context, id packageID, a *analysis.A
}
}

fset := s.view.session.cache.fset

h := s.view.session.cache.store.Bind(buildActionKey(a, ph), func(ctx context.Context) interface{} {
h := s.view.session.cache.store.Bind(buildActionKey(a, ph), func(ctx context.Context, arg memoize.Arg) interface{} {
snapshot := arg.(*snapshot)
// Analyze dependencies first.
results, err := execAll(ctx, deps)
results, err := execAll(ctx, snapshot, deps)
if err != nil {
return &actionData{
err: err,
}
}
return runAnalysis(ctx, fset, a, pkg, results)
return runAnalysis(ctx, snapshot, a, pkg, results)
})
act.handle = h

act = s.addActionHandle(act)
return act, nil
}

func (act *actionHandle) analyze(ctx context.Context) ([]*source.Error, interface{}, error) {
v, err := act.handle.Get(ctx)
func (act *actionHandle) analyze(ctx context.Context, snapshot *snapshot) ([]*source.Error, interface{}, error) {
v, err := act.handle.Get(ctx, snapshot)
if v == nil {
return nil, nil, err
}
Expand All @@ -174,15 +173,15 @@ func (act *actionHandle) String() string {
return fmt.Sprintf("%s@%s", act.analyzer, act.pkg.PkgPath())
}

func execAll(ctx context.Context, actions []*actionHandle) (map[*actionHandle]*actionData, error) {
func execAll(ctx context.Context, snapshot *snapshot, actions []*actionHandle) (map[*actionHandle]*actionData, error) {
var mu sync.Mutex
results := make(map[*actionHandle]*actionData)

g, ctx := errgroup.WithContext(ctx)
for _, act := range actions {
act := act
g.Go(func() error {
v, err := act.handle.Get(ctx)
v, err := act.handle.Get(ctx, snapshot)
if err != nil {
return err
}
Expand All @@ -201,7 +200,7 @@ func execAll(ctx context.Context, actions []*actionHandle) (map[*actionHandle]*a
return results, g.Wait()
}

func runAnalysis(ctx context.Context, fset *token.FileSet, analyzer *analysis.Analyzer, pkg *pkg, deps map[*actionHandle]*actionData) (data *actionData) {
func runAnalysis(ctx context.Context, snapshot *snapshot, analyzer *analysis.Analyzer, pkg *pkg, deps map[*actionHandle]*actionData) (data *actionData) {
data = &actionData{
objectFacts: make(map[objectFactKey]analysis.Fact),
packageFacts: make(map[packageFactKey]analysis.Fact),
Expand Down Expand Up @@ -246,13 +245,18 @@ func runAnalysis(ctx context.Context, fset *token.FileSet, analyzer *analysis.An
}
}

var syntax []*ast.File
for _, cgf := range pkg.compiledGoFiles {
syntax = append(syntax, cgf.File)
}

var diagnostics []*analysis.Diagnostic

// Run the analysis.
pass := &analysis.Pass{
Analyzer: analyzer,
Fset: fset,
Files: pkg.GetSyntax(),
Fset: snapshot.view.session.cache.fset,
Files: syntax,
Pkg: pkg.GetTypes(),
TypesInfo: pkg.GetTypesInfo(),
TypesSizes: pkg.GetTypesSizes(),
Expand Down Expand Up @@ -343,7 +347,7 @@ func runAnalysis(ctx context.Context, fset *token.FileSet, analyzer *analysis.An
}

for _, diag := range diagnostics {
srcErr, err := sourceError(ctx, fset, pkg, diag)
srcErr, err := sourceError(ctx, snapshot, pkg, diag)
if err != nil {
event.Error(ctx, "unable to compute analysis error position", err, tag.Category.Of(diag.Category), tag.Package.Of(pkg.ID()))
continue
Expand Down
Loading

0 comments on commit b911480

Please sign in to comment.