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-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-03-05 15:30:44 -07:00
|
|
|
if f.pkg == nil || len(f.view.contentChanges) > 0 {
|
2019-03-27 07:25:30 -06:00
|
|
|
errs, err := f.view.parse(ctx, f)
|
2019-03-11 15:14:55 -06:00
|
|
|
if err != nil {
|
|
|
|
// 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-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-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
|
|
|
|
}
|