2018-11-05 12:48:08 -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.
|
|
|
|
|
2018-12-05 15:00:36 -07:00
|
|
|
package cache
|
2018-09-27 16:15:45 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-10-19 14:03:29 -06:00
|
|
|
"go/token"
|
2018-09-27 16:15:45 -06:00
|
|
|
"sync"
|
|
|
|
|
2018-10-19 14:03:29 -06:00
|
|
|
"golang.org/x/tools/go/packages"
|
2018-12-05 15:00:36 -07:00
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
2018-09-27 16:15:45 -06:00
|
|
|
)
|
|
|
|
|
2018-11-02 14:15:31 -06:00
|
|
|
type View struct {
|
2018-11-02 16:10:49 -06:00
|
|
|
mu sync.Mutex // protects all mutable state of the view
|
2018-11-02 14:15:31 -06:00
|
|
|
|
2018-11-02 16:10:49 -06:00
|
|
|
Config *packages.Config
|
2018-10-19 14:03:29 -06:00
|
|
|
|
2018-12-05 15:00:36 -07:00
|
|
|
files map[source.URI]*File
|
2018-09-27 16:15:45 -06:00
|
|
|
}
|
|
|
|
|
2018-12-12 11:57:52 -07:00
|
|
|
func NewView(rootPath string) *View {
|
2018-11-02 14:15:31 -06:00
|
|
|
return &View{
|
|
|
|
Config: &packages.Config{
|
2018-12-12 11:57:52 -07:00
|
|
|
Dir: rootPath,
|
2018-11-05 19:23:02 -07:00
|
|
|
Mode: packages.LoadSyntax,
|
|
|
|
Fset: token.NewFileSet(),
|
|
|
|
Tests: true,
|
|
|
|
Overlay: make(map[string][]byte),
|
2018-10-29 16:12:41 -06:00
|
|
|
},
|
2018-12-05 15:00:36 -07:00
|
|
|
files: make(map[source.URI]*File),
|
2018-09-27 16:15:45 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-18 13:46:14 -07:00
|
|
|
// GetFile returns a File for the given URI. It will always succeed because it
|
|
|
|
// adds the file to the managed set if needed.
|
|
|
|
func (v *View) GetFile(uri source.URI) source.File {
|
2018-11-02 16:10:49 -06:00
|
|
|
v.mu.Lock()
|
2018-11-05 15:54:12 -07:00
|
|
|
f := v.getFile(uri)
|
|
|
|
v.mu.Unlock()
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// getFile is the unlocked internal implementation of GetFile.
|
2018-12-05 15:00:36 -07:00
|
|
|
func (v *View) getFile(uri source.URI) *File {
|
2018-11-02 16:10:49 -06:00
|
|
|
f, found := v.files[uri]
|
|
|
|
if !found {
|
2018-11-05 19:23:02 -07:00
|
|
|
f = &File{
|
|
|
|
URI: uri,
|
|
|
|
view: v,
|
|
|
|
}
|
2018-11-02 16:10:49 -06:00
|
|
|
v.files[f.URI] = f
|
2018-10-19 14:03:29 -06:00
|
|
|
}
|
2018-11-02 16:10:49 -06:00
|
|
|
return f
|
2018-09-27 16:15:45 -06:00
|
|
|
}
|
2018-10-19 14:03:29 -06:00
|
|
|
|
2018-12-05 15:00:36 -07:00
|
|
|
func (v *View) parse(uri source.URI) error {
|
2018-11-05 15:54:12 -07:00
|
|
|
path, err := uri.Filename()
|
2018-11-02 14:15:31 -06:00
|
|
|
if err != nil {
|
2018-11-05 15:54:12 -07:00
|
|
|
return err
|
2018-11-02 14:15:31 -06:00
|
|
|
}
|
|
|
|
pkgs, err := packages.Load(v.Config, fmt.Sprintf("file=%s", path))
|
2018-10-19 14:03:29 -06:00
|
|
|
if len(pkgs) == 0 {
|
2018-11-05 19:23:02 -07:00
|
|
|
if err == nil {
|
|
|
|
err = fmt.Errorf("no packages found for %s", path)
|
|
|
|
}
|
2018-11-05 15:54:12 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, pkg := range pkgs {
|
|
|
|
// add everything we find to the files cache
|
|
|
|
for _, fAST := range pkg.Syntax {
|
|
|
|
// if a file was in multiple packages, which token/ast/pkg do we store
|
|
|
|
fToken := v.Config.Fset.File(fAST.Pos())
|
2018-12-05 15:00:36 -07:00
|
|
|
fURI := source.ToURI(fToken.Name())
|
2018-11-05 15:54:12 -07:00
|
|
|
f := v.getFile(fURI)
|
|
|
|
f.token = fToken
|
|
|
|
f.ast = fAST
|
|
|
|
f.pkg = pkg
|
2018-11-05 19:23:02 -07:00
|
|
|
}
|
|
|
|
}
|
2018-11-05 15:54:12 -07:00
|
|
|
return nil
|
2018-11-05 19:23:02 -07:00
|
|
|
}
|