mirror of
https://github.com/golang/go
synced 2024-11-18 22:04:43 -07:00
eb7cb10de1
This change moves from caching package information the file object to caching in a map that gets invalidated when content changes. This simplifies cache invalidation and reduces the number of fields guarded by the (*goFile).mu lock. Change-Id: I33fef6e0b18badb97e49052d9d6e3c15047c4b63 Reviewed-on: https://go-review.googlesource.com/c/tools/+/196984 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Cottrell <iancottrell@google.com>
105 lines
2.6 KiB
Go
105 lines
2.6 KiB
Go
// 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.
|
|
|
|
package cache
|
|
|
|
import (
|
|
"context"
|
|
"go/ast"
|
|
"sync"
|
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
"golang.org/x/tools/internal/lsp/telemetry"
|
|
"golang.org/x/tools/internal/span"
|
|
errors "golang.org/x/xerrors"
|
|
)
|
|
|
|
// goFile holds all of the information we know about a Go file.
|
|
type goFile struct {
|
|
fileBase
|
|
|
|
// mu protects all mutable state of the Go file,
|
|
// which can be modified during type-checking.
|
|
mu sync.Mutex
|
|
|
|
// missingImports is the set of unresolved imports for this package.
|
|
// It contains any packages with `go list` errors.
|
|
missingImports map[packagePath]struct{}
|
|
|
|
imports []*ast.ImportSpec
|
|
}
|
|
|
|
type packageKey struct {
|
|
id packageID
|
|
mode source.ParseMode
|
|
}
|
|
|
|
func (f *goFile) CheckPackageHandles(ctx context.Context) (cphs []source.CheckPackageHandle, err error) {
|
|
ctx = telemetry.File.With(ctx, f.URI())
|
|
fh := f.Handle(ctx)
|
|
|
|
cphs = f.isDirty(ctx, fh)
|
|
if len(cphs) == 0 {
|
|
cphs, err = f.view.loadParseTypecheck(ctx, f, fh)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if len(cphs) == 0 {
|
|
return nil, errors.Errorf("no CheckPackageHandles for %s", f.URI())
|
|
}
|
|
return cphs, nil
|
|
}
|
|
|
|
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 {
|
|
continue
|
|
}
|
|
gof, ok := f.(source.GoFile)
|
|
if !ok {
|
|
continue
|
|
}
|
|
cphs, err := gof.CheckPackageHandles(ctx)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
cph := source.WidestCheckPackageHandle(cphs)
|
|
for _, ph := range cph.Files() {
|
|
seen[ph.File().Identity().URI] = struct{}{}
|
|
}
|
|
results = append(results, cph)
|
|
}
|
|
return results
|
|
}
|
|
|
|
// isDirty is true if the file needs to be type-checked.
|
|
// It assumes that the file's view's mutex is held by the caller.
|
|
func (f *goFile) isDirty(ctx context.Context, fh source.FileHandle) []source.CheckPackageHandle {
|
|
meta, cphs := f.view.getSnapshot(f.URI())
|
|
if len(meta) == 0 {
|
|
return nil
|
|
}
|
|
var results []source.CheckPackageHandle
|
|
for key, cph := range cphs {
|
|
// If we're explicitly checking if a file needs to be type-checked,
|
|
// we need it to be fully parsed.
|
|
if key.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)
|
|
}
|
|
}
|
|
}
|
|
return results
|
|
}
|