2019-06-21 15:00:02 -06:00
|
|
|
// Copyright 2019 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.
|
|
|
|
|
2019-05-23 13:03:11 -06:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"go/ast"
|
2019-06-26 12:34:36 -06:00
|
|
|
"sync"
|
2019-05-23 13:03:11 -06:00
|
|
|
|
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
2019-07-14 21:08:10 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/telemetry"
|
2019-09-23 18:06:15 -06:00
|
|
|
"golang.org/x/tools/internal/span"
|
2019-08-06 13:13:11 -06:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-05-23 13:03:11 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// goFile holds all of the information we know about a Go file.
|
|
|
|
type goFile struct {
|
|
|
|
fileBase
|
|
|
|
|
2019-06-26 12:34:36 -06:00
|
|
|
// mu protects all mutable state of the Go file,
|
|
|
|
// which can be modified during type-checking.
|
|
|
|
mu sync.Mutex
|
2019-05-23 11:51:56 -06:00
|
|
|
|
2019-05-23 13:03:11 -06:00
|
|
|
imports []*ast.ImportSpec
|
|
|
|
}
|
|
|
|
|
2019-09-19 14:53:53 -06:00
|
|
|
type packageKey struct {
|
|
|
|
id packageID
|
|
|
|
mode source.ParseMode
|
|
|
|
}
|
|
|
|
|
2019-09-23 18:19:50 -06:00
|
|
|
func (f *goFile) CheckPackageHandles(ctx context.Context) (results []source.CheckPackageHandle, err error) {
|
2019-07-14 21:08:10 -06:00
|
|
|
ctx = telemetry.File.With(ctx, f.URI())
|
2019-09-05 15:34:00 -06:00
|
|
|
fh := f.Handle(ctx)
|
2019-05-23 13:03:11 -06:00
|
|
|
|
2019-09-23 18:19:50 -06:00
|
|
|
var dirty bool
|
|
|
|
meta, pkgs := f.view.getSnapshot(f.URI())
|
|
|
|
if len(meta) == 0 {
|
|
|
|
dirty = true
|
|
|
|
}
|
|
|
|
for _, m := range meta {
|
|
|
|
if len(m.missingDeps) != 0 {
|
|
|
|
dirty = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, cph := range pkgs {
|
|
|
|
// If we're explicitly checking if a file needs to be type-checked,
|
|
|
|
// we need it to be fully parsed.
|
|
|
|
if cph.mode() != source.ParseFull {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Check if there is a fully-parsed package to which this file belongs.
|
|
|
|
for _, file := range cph.Files() {
|
|
|
|
if file.File().Identity() == fh.Identity() {
|
|
|
|
results = append(results, cph)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if dirty || len(results) == 0 {
|
|
|
|
cphs, err := f.view.loadParseTypecheck(ctx, f, fh)
|
2019-09-23 18:06:15 -06:00
|
|
|
if err != nil {
|
2019-07-09 15:52:23 -06:00
|
|
|
return nil, err
|
2019-05-23 13:03:11 -06:00
|
|
|
}
|
2019-09-23 18:19:50 -06:00
|
|
|
if cphs == nil {
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
results = cphs
|
2019-05-23 13:03:11 -06:00
|
|
|
}
|
2019-09-23 18:19:50 -06:00
|
|
|
if len(results) == 0 {
|
2019-09-23 18:06:15 -06:00
|
|
|
return nil, errors.Errorf("no CheckPackageHandles for %s", f.URI())
|
|
|
|
}
|
2019-09-23 18:19:50 -06:00
|
|
|
return results, nil
|
2019-09-23 18:06:15 -06:00
|
|
|
}
|
2019-06-28 17:59:25 -06:00
|
|
|
|
2019-09-23 18:06:15 -06:00
|
|
|
func (v *view) GetActiveReverseDeps(ctx context.Context, uri span.URI) (results []source.CheckPackageHandle) {
|
|
|
|
var (
|
|
|
|
rdeps = v.reverseDependencies(ctx, uri)
|
|
|
|
files = v.openFiles(ctx, rdeps)
|
|
|
|
seen = make(map[span.URI]struct{})
|
|
|
|
)
|
|
|
|
for _, f := range files {
|
|
|
|
if _, ok := seen[f.URI()]; ok {
|
2019-09-19 14:53:53 -06:00
|
|
|
continue
|
|
|
|
}
|
2019-09-23 18:06:15 -06:00
|
|
|
gof, ok := f.(source.GoFile)
|
|
|
|
if !ok {
|
2019-09-19 14:53:53 -06:00
|
|
|
continue
|
|
|
|
}
|
2019-09-23 18:06:15 -06:00
|
|
|
cphs, err := gof.CheckPackageHandles(ctx)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
2019-09-09 17:26:26 -06:00
|
|
|
}
|
2019-09-23 18:06:15 -06:00
|
|
|
cph := source.WidestCheckPackageHandle(cphs)
|
|
|
|
for _, ph := range cph.Files() {
|
|
|
|
seen[ph.File().Identity().URI] = struct{}{}
|
2019-09-06 21:58:07 -06:00
|
|
|
}
|
2019-09-23 18:06:15 -06:00
|
|
|
results = append(results, cph)
|
2019-09-06 21:58:07 -06:00
|
|
|
}
|
2019-09-23 18:06:15 -06:00
|
|
|
return results
|
2019-08-06 16:51:17 -06:00
|
|
|
}
|