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"
|
|
|
|
"io/ioutil"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
// File holds all the information we know about a file.
|
|
|
|
type File struct {
|
2019-03-27 07:25:30 -06:00
|
|
|
uris []span.URI
|
|
|
|
filename string
|
|
|
|
basename string
|
|
|
|
|
2018-12-05 15:00:36 -07:00
|
|
|
view *View
|
|
|
|
active bool
|
|
|
|
content []byte
|
|
|
|
ast *ast.File
|
|
|
|
token *token.File
|
2019-03-06 14:33:47 -07:00
|
|
|
pkg *Package
|
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-02-19 19:11:15 -07:00
|
|
|
func (f *File) URI() span.URI {
|
2019-03-27 07:25:30 -06:00
|
|
|
return f.uris[0]
|
2019-02-19 19:11:15 -07:00
|
|
|
}
|
|
|
|
|
2019-04-17 16:21:47 -06:00
|
|
|
// View returns the view associated with the file.
|
|
|
|
func (f *File) View() source.View {
|
|
|
|
return f.view
|
|
|
|
}
|
|
|
|
|
2019-02-13 14:08:29 -07:00
|
|
|
// GetContent returns the contents of the file, reading it from file system if needed.
|
2019-03-05 15:30:44 -07:00
|
|
|
func (f *File) GetContent(ctx context.Context) []byte {
|
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 ctx.Err() == nil {
|
|
|
|
f.read(ctx)
|
|
|
|
}
|
|
|
|
|
2019-02-13 14:08:29 -07:00
|
|
|
return f.content
|
2018-12-05 15:00:36 -07:00
|
|
|
}
|
|
|
|
|
2019-03-05 15:30:44 -07:00
|
|
|
func (f *File) GetFileSet(ctx context.Context) *token.FileSet {
|
2019-02-13 11:23:28 -07:00
|
|
|
return f.view.Config.Fset
|
2018-12-05 15:00:36 -07:00
|
|
|
}
|
|
|
|
|
2019-03-05 15:30:44 -07:00
|
|
|
func (f *File) 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-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-03-05 15:30:44 -07:00
|
|
|
func (f *File) 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-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-03-06 14:33:47 -07:00
|
|
|
func (f *File) 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-03-11 15:14:55 -06:00
|
|
|
// Create diagnostics for errors if we are able to.
|
|
|
|
if len(errs) > 0 {
|
|
|
|
return &Package{errors: errs}
|
|
|
|
}
|
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-03-04 16:01:51 -07:00
|
|
|
// read is the internal part of GetContent. It assumes that the caller is
|
|
|
|
// holding the mutex of the file's view.
|
2019-03-05 15:30:44 -07:00
|
|
|
func (f *File) read(ctx context.Context) {
|
2018-12-05 15:00:36 -07:00
|
|
|
if f.content != 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()
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
2018-12-05 15:00:36 -07:00
|
|
|
}
|
2019-04-16 14:19:03 -06:00
|
|
|
// We might have the content saved in an overlay.
|
|
|
|
if content, ok := f.view.Config.Overlay[f.filename]; ok {
|
|
|
|
f.content = content
|
|
|
|
return
|
|
|
|
}
|
2019-03-04 16:01:51 -07:00
|
|
|
// We don't know the content yet, so read it.
|
2019-03-27 07:25:30 -06:00
|
|
|
content, err := ioutil.ReadFile(f.filename)
|
2018-12-05 15:00:36 -07:00
|
|
|
if err != nil {
|
2019-04-16 14:19:03 -06:00
|
|
|
f.view.Logger().Errorf(ctx, "unable to read file %s: %v", f.filename, err)
|
2019-02-13 14:08:29 -07:00
|
|
|
return
|
2018-12-05 15:00:36 -07:00
|
|
|
}
|
|
|
|
f.content = content
|
|
|
|
}
|
2019-03-05 15:30:44 -07:00
|
|
|
|
|
|
|
// isPopulated returns true if all of the computed fields of the file are set.
|
|
|
|
func (f *File) isPopulated() bool {
|
|
|
|
return f.ast != nil && f.token != nil && f.pkg != nil && f.meta != nil && f.imports != nil
|
|
|
|
}
|
2019-05-01 20:46:07 -06:00
|
|
|
|
|
|
|
func (f *File) GetActiveReverseDeps(ctx context.Context) []source.File {
|
|
|
|
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
|
|
|
|
results := make(map[*File]struct{})
|
|
|
|
f.view.reverseDeps(ctx, seen, results, pkg.PkgPath())
|
|
|
|
|
|
|
|
files := make([]source.File, 0, len(results))
|
|
|
|
for rd := range results {
|
|
|
|
if rd == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Don't return any of the active file's in this package.
|
|
|
|
if rd.pkg != nil && rd.pkg == pkg {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
files = append(files, rd)
|
|
|
|
}
|
|
|
|
return files
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *View) reverseDeps(ctx context.Context, seen map[string]struct{}, results map[*File]struct{}, pkgPath string) {
|
|
|
|
if _, ok := seen[pkgPath]; ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
seen[pkgPath] = struct{}{}
|
|
|
|
m, ok := v.mcache.packages[pkgPath]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, filename := range m.files {
|
|
|
|
if f, err := v.getFile(span.FileURI(filename)); err == nil && f.active {
|
|
|
|
results[f] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for parentPkgPath := range m.parents {
|
|
|
|
v.reverseDeps(ctx, seen, results, parentPkgPath)
|
|
|
|
}
|
|
|
|
}
|