go/internal/lsp/cache/file.go
Rebecca Stambler 69e0dcfa11 internal/lsp: cache package metadata to minimize calls to packages.Load
Instead of calling packages.Load on every character change, we reparse
the import declarations of the file and determine if they have
changed. We also introduce a metadata cache that caches the import
graph. This is used in type-checking and only updated on calls to
packages.Load.

Change-Id: I7cb384aba77ef3c1565d3b0db58e6c754d5fed15
Reviewed-on: https://go-review.googlesource.com/c/tools/+/165137
Reviewed-by: Ian Cottrell <iancottrell@google.com>
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-03-06 16:29:03 +00:00

91 lines
1.7 KiB
Go

// 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 (
"go/ast"
"go/token"
"io/ioutil"
"golang.org/x/tools/go/packages"
"golang.org/x/tools/internal/lsp/source"
)
// File holds all the information we know about a file.
type File struct {
URI source.URI
view *View
active bool
content []byte
ast *ast.File
token *token.File
pkg *packages.Package
meta *metadata
imports []*ast.ImportSpec
}
// GetContent returns the contents of the file, reading it from file system if needed.
func (f *File) GetContent() []byte {
f.view.mu.Lock()
defer f.view.mu.Unlock()
f.read()
return f.content
}
func (f *File) GetFileSet() *token.FileSet {
return f.view.Config.Fset
}
func (f *File) GetToken() *token.File {
f.view.mu.Lock()
defer f.view.mu.Unlock()
if f.token == nil {
if err := f.view.parse(f.URI); err != nil {
return nil
}
}
return f.token
}
func (f *File) GetAST() *ast.File {
f.view.mu.Lock()
defer f.view.mu.Unlock()
if f.ast == nil {
if err := f.view.parse(f.URI); err != nil {
return nil
}
}
return f.ast
}
func (f *File) GetPackage() *packages.Package {
f.view.mu.Lock()
defer f.view.mu.Unlock()
if f.pkg == nil {
if err := f.view.parse(f.URI); err != nil {
return nil
}
}
return f.pkg
}
// read is the internal part of GetContent. It assumes that the caller is
// holding the mutex of the file's view.
func (f *File) read() {
if f.content != nil {
return
}
// We don't know the content yet, so read it.
filename, err := f.URI.Filename()
if err != nil {
return
}
content, err := ioutil.ReadFile(filename)
if err != nil {
return
}
f.content = content
}