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-11-02 14:15:31 -06:00
|
|
|
package source
|
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-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-11-05 15:54:12 -07:00
|
|
|
files map[URI]*File
|
2018-09-27 16:15:45 -06:00
|
|
|
}
|
|
|
|
|
2018-11-02 14:15:31 -06:00
|
|
|
func NewView() *View {
|
|
|
|
return &View{
|
|
|
|
Config: &packages.Config{
|
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-11-05 15:54:12 -07:00
|
|
|
files: make(map[URI]*File),
|
2018-09-27 16:15:45 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-02 16:10:49 -06:00
|
|
|
// GetFile returns a File for the given uri.
|
|
|
|
// It will always succeed, adding the file to the managed set if needed.
|
2018-11-05 15:54:12 -07:00
|
|
|
func (v *View) GetFile(uri URI) *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.
|
|
|
|
func (v *View) getFile(uri 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-11-05 15:54:12 -07:00
|
|
|
func (v *View) parse(uri URI) error {
|
|
|
|
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())
|
|
|
|
fURI := ToURI(fToken.Name())
|
|
|
|
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
|
|
|
}
|