代码拉取完成,页面将自动刷新
package astcache
import (
"go/ast"
"go/parser"
"go/token"
"golang.org/x/tools/go/loader"
)
type File struct {
F *ast.File
Fset *token.FileSet
err error
}
type Cache struct {
m map[string]*File
s []*File
}
func (c Cache) GetAllValidFiles() []*File {
return c.s
}
func (c *Cache) prepareValidFiles() {
files := make([]*File, 0, len(c.m))
for _, f := range c.m {
if f.err != nil || f.F == nil {
continue
}
files = append(files, f)
}
c.s = files
}
func LoadFromProgram(prog *loader.Program) *Cache {
c := &Cache{
m: map[string]*File{},
}
for _, pkg := range prog.InitialPackages() {
for _, f := range pkg.Files {
pos := prog.Fset.Position(0)
c.m[pos.Filename] = &File{
F: f,
Fset: prog.Fset,
}
}
}
c.prepareValidFiles()
return c
}
func LoadFromFiles(files []string) *Cache {
c := &Cache{
m: map[string]*File{},
}
fset := token.NewFileSet()
for _, filePath := range files {
f, err := parser.ParseFile(fset, filePath, nil, parser.ParseComments) // comments needed by e.g. golint
c.m[filePath] = &File{
F: f,
Fset: fset,
err: err,
}
}
c.prepareValidFiles()
return c
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。