2018-12-05 15:00:36 -07:00
|
|
|
// Copyright 2018 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 (
|
2019-03-05 15:30:44 -07:00
|
|
|
"context"
|
2018-12-05 15:00:36 -07:00
|
|
|
"go/ast"
|
|
|
|
"go/token"
|
2019-03-27 07:25:30 -06:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2018-12-05 15:00:36 -07:00
|
|
|
|
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
2019-02-19 19:11:15 -07:00
|
|
|
"golang.org/x/tools/internal/span"
|
2018-12-05 15:00:36 -07:00
|
|
|
)
|
|
|
|
|
2019-05-03 22:04:18 -06:00
|
|
|
// viewFile extends source.File with helper methods for the view package.
|
|
|
|
type viewFile interface {
|
|
|
|
source.File
|
|
|
|
filename() string
|
|
|
|
addURI(uri span.URI) int
|
|
|
|
}
|
|
|
|
|
|
|
|
// fileBase holds the common functionality for all files.
|
|
|
|
// It is intended to be embedded in the file implementations
|
|
|
|
type fileBase struct {
|
|
|
|
uris []span.URI
|
|
|
|
fname string
|
2019-03-27 07:25:30 -06:00
|
|
|
|
2019-05-17 10:15:22 -06:00
|
|
|
view *view
|
|
|
|
fc *source.FileContent
|
|
|
|
token *token.File
|
2019-05-03 22:04:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// goFile holds all the information we know about a go file.
|
|
|
|
type goFile struct {
|
|
|
|
fileBase
|
|
|
|
|
|
|
|
ast *ast.File
|
2019-05-14 21:04:23 -06:00
|
|
|
pkg *pkg
|
2019-03-04 16:01:51 -07:00
|
|
|
meta *metadata
|
|
|
|
imports []*ast.ImportSpec
|
2018-12-05 15:00:36 -07:00
|
|
|
}
|
|
|
|
|
2019-03-27 07:25:30 -06:00
|
|
|
func basename(filename string) string {
|
|
|
|
return strings.ToLower(filepath.Base(filename))
|
|
|
|
}
|
|
|
|
|
2019-05-03 22:04:18 -06:00
|
|
|
func (f *fileBase) URI() span.URI {
|
2019-03-27 07:25:30 -06:00
|
|
|
return f.uris[0]
|
2019-02-19 19:11:15 -07:00
|
|
|
}
|
|
|
|
|
2019-05-03 22:04:18 -06:00
|
|
|
func (f *fileBase) filename() string {
|
|
|
|
return f.fname
|
|
|
|
}
|
|
|
|
|
2019-04-17 16:21:47 -06:00
|
|
|
// View returns the view associated with the file.
|
2019-05-03 22:04:18 -06:00
|
|
|
func (f *fileBase) View() source.View {
|
2019-04-17 16:21:47 -06:00
|
|
|
return f.view
|
|
|
|
}
|
|
|
|
|
2019-05-17 10:15:22 -06:00
|
|
|
// Content returns the contents of the file, reading it from file system if needed.
|
|
|
|
func (f *fileBase) Content(ctx context.Context) *source.FileContent {
|
2018-12-05 15:00:36 -07:00
|
|
|
f.view.mu.Lock()
|
|
|
|
defer f.view.mu.Unlock()
|
2019-05-17 10:15:22 -06:00
|
|
|
f.read(ctx)
|
|
|
|
return f.fc
|
2018-12-05 15:00:36 -07:00
|
|
|
}
|
|
|
|
|
2019-05-17 08:51:19 -06:00
|
|
|
func (f *fileBase) FileSet() *token.FileSet {
|
|
|
|
return f.view.Session().Cache().FileSet()
|
2018-12-05 15:00:36 -07:00
|
|
|
}
|
|
|
|
|
2019-05-03 22:04:18 -06:00
|
|
|
func (f *goFile) GetToken(ctx context.Context) *token.File {
|
2018-12-05 15:00:36 -07:00
|
|
|
f.view.mu.Lock()
|
|
|
|
defer f.view.mu.Unlock()
|
2019-03-05 15:30:44 -07:00
|
|
|
if f.token == nil || len(f.view.contentChanges) > 0 {
|
2019-03-27 07:25:30 -06:00
|
|
|
if _, err := f.view.parse(ctx, f); err != nil {
|
2019-05-20 16:44:24 -06:00
|
|
|
f.View().Session().Logger().Errorf(ctx, "unable to check package for %s: %v", f.URI(), err)
|
2019-02-13 11:23:28 -07:00
|
|
|
return nil
|
2018-12-05 15:00:36 -07:00
|
|
|
}
|
|
|
|
}
|
2019-02-13 11:23:28 -07:00
|
|
|
return f.token
|
2018-12-05 15:00:36 -07:00
|
|
|
}
|
|
|
|
|
2019-05-03 22:04:18 -06:00
|
|
|
func (f *goFile) GetAST(ctx context.Context) *ast.File {
|
2018-12-05 15:00:36 -07:00
|
|
|
f.view.mu.Lock()
|
|
|
|
defer f.view.mu.Unlock()
|
2019-03-05 15:30:44 -07:00
|
|
|
|
|
|
|
if f.ast == nil || len(f.view.contentChanges) > 0 {
|
2019-03-27 07:25:30 -06:00
|
|
|
if _, err := f.view.parse(ctx, f); err != nil {
|
2019-05-20 16:44:24 -06:00
|
|
|
f.View().Session().Logger().Errorf(ctx, "unable to check package for %s: %v", f.URI(), err)
|
2019-02-13 11:23:28 -07:00
|
|
|
return nil
|
2019-02-06 16:47:00 -07:00
|
|
|
}
|
2018-12-05 15:00:36 -07:00
|
|
|
}
|
2019-02-13 11:23:28 -07:00
|
|
|
return f.ast
|
2018-12-05 15:00:36 -07:00
|
|
|
}
|
|
|
|
|
2019-05-03 22:04:18 -06:00
|
|
|
func (f *goFile) GetPackage(ctx context.Context) source.Package {
|
2018-12-05 15:00:36 -07:00
|
|
|
f.view.mu.Lock()
|
|
|
|
defer f.view.mu.Unlock()
|
2019-05-01 20:46:07 -06:00
|
|
|
|
2019-03-05 15:30:44 -07:00
|
|
|
if f.pkg == nil || len(f.view.contentChanges) > 0 {
|
2019-04-24 17:31:07 -06:00
|
|
|
if errs, err := f.view.parse(ctx, f); err != nil {
|
2019-05-20 16:44:24 -06:00
|
|
|
f.View().Session().Logger().Errorf(ctx, "unable to check package for %s: %v", f.URI(), err)
|
|
|
|
|
2019-03-11 15:14:55 -06:00
|
|
|
// Create diagnostics for errors if we are able to.
|
|
|
|
if len(errs) > 0 {
|
2019-05-14 21:04:23 -06:00
|
|
|
return &pkg{errors: errs}
|
2019-03-11 15:14:55 -06:00
|
|
|
}
|
2019-02-13 11:23:28 -07:00
|
|
|
return nil
|
2019-02-06 16:47:00 -07:00
|
|
|
}
|
2018-12-05 15:00:36 -07:00
|
|
|
}
|
2019-02-13 11:23:28 -07:00
|
|
|
return f.pkg
|
2018-12-05 15:00:36 -07:00
|
|
|
}
|
|
|
|
|
2019-05-17 10:15:22 -06:00
|
|
|
// read is the internal part of Content. It assumes that the caller is
|
2019-03-04 16:01:51 -07:00
|
|
|
// holding the mutex of the file's view.
|
2019-05-03 22:04:18 -06:00
|
|
|
func (f *fileBase) read(ctx context.Context) {
|
2019-05-17 10:15:22 -06:00
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
f.fc = &source.FileContent{Error: err}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if f.fc != nil {
|
2019-03-05 15:30:44 -07:00
|
|
|
if len(f.view.contentChanges) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
f.view.mcache.mu.Lock()
|
|
|
|
err := f.view.applyContentChanges(ctx)
|
|
|
|
f.view.mcache.mu.Unlock()
|
|
|
|
|
2019-05-17 10:15:22 -06:00
|
|
|
if err != nil {
|
|
|
|
f.fc = &source.FileContent{Error: err}
|
2019-03-05 15:30:44 -07:00
|
|
|
return
|
|
|
|
}
|
2018-12-05 15:00:36 -07:00
|
|
|
}
|
2019-03-04 16:01:51 -07:00
|
|
|
// We don't know the content yet, so read it.
|
2019-05-17 10:15:22 -06:00
|
|
|
f.fc = f.view.Session().ReadFile(f.URI())
|
2018-12-05 15:00:36 -07:00
|
|
|
}
|
2019-03-05 15:30:44 -07:00
|
|
|
|
|
|
|
// isPopulated returns true if all of the computed fields of the file are set.
|
2019-05-03 22:04:18 -06:00
|
|
|
func (f *goFile) isPopulated() bool {
|
2019-03-05 15:30:44 -07:00
|
|
|
return f.ast != nil && f.token != nil && f.pkg != nil && f.meta != nil && f.imports != nil
|
|
|
|
}
|
2019-05-01 20:46:07 -06:00
|
|
|
|
2019-05-03 22:04:18 -06:00
|
|
|
func (f *goFile) GetActiveReverseDeps(ctx context.Context) []source.GoFile {
|
2019-05-01 20:46:07 -06:00
|
|
|
pkg := f.GetPackage(ctx)
|
|
|
|
if pkg == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
f.view.mu.Lock()
|
|
|
|
defer f.view.mu.Unlock()
|
|
|
|
|
|
|
|
f.view.mcache.mu.Lock()
|
|
|
|
defer f.view.mcache.mu.Unlock()
|
|
|
|
|
|
|
|
seen := make(map[string]struct{}) // visited packages
|
2019-05-03 22:04:18 -06:00
|
|
|
results := make(map[*goFile]struct{})
|
2019-05-01 20:46:07 -06:00
|
|
|
f.view.reverseDeps(ctx, seen, results, pkg.PkgPath())
|
|
|
|
|
2019-05-15 15:58:16 -06:00
|
|
|
var files []source.GoFile
|
2019-05-01 20:46:07 -06:00
|
|
|
for rd := range results {
|
|
|
|
if rd == nil {
|
|
|
|
continue
|
|
|
|
}
|
2019-05-15 15:58:16 -06:00
|
|
|
// Don't return any of the active files in this package.
|
2019-05-01 20:46:07 -06:00
|
|
|
if rd.pkg != nil && rd.pkg == pkg {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
files = append(files, rd)
|
|
|
|
}
|
|
|
|
return files
|
|
|
|
}
|
|
|
|
|
2019-05-03 22:04:18 -06:00
|
|
|
func (v *view) reverseDeps(ctx context.Context, seen map[string]struct{}, results map[*goFile]struct{}, pkgPath string) {
|
2019-05-01 20:46:07 -06:00
|
|
|
if _, ok := seen[pkgPath]; ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
seen[pkgPath] = struct{}{}
|
|
|
|
m, ok := v.mcache.packages[pkgPath]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, filename := range m.files {
|
2019-05-17 10:15:22 -06:00
|
|
|
uri := span.FileURI(filename)
|
|
|
|
if f, err := v.getFile(uri); err == nil && v.session.IsOpen(uri) {
|
2019-05-03 22:04:18 -06:00
|
|
|
results[f.(*goFile)] = struct{}{}
|
2019-05-01 20:46:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for parentPkgPath := range m.parents {
|
|
|
|
v.reverseDeps(ctx, seen, results, parentPkgPath)
|
|
|
|
}
|
|
|
|
}
|