forked from nsf/gocode
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpackage_types.go
89 lines (79 loc) · 2.33 KB
/
package_types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"bytes"
"go/importer"
"go/token"
"go/types"
"io"
"log"
"github.com/visualfc/gocode/internal/gcexportdata"
pkgwalk "github.com/visualfc/gotools/types"
)
type types_parser struct {
pfc *package_file_cache
pkg *types.Package
}
// func DefaultPkgConfig() *pkgwalk.PkgConfig {
// conf := &pkgwalk.PkgConfig{IgnoreFuncBodies: true, AllowBinary: true, WithTestFiles: false}
// conf.Info = &types.Info{
// Uses: make(map[*ast.Ident]types.Object),
// Defs: make(map[*ast.Ident]types.Object),
// Selections: make(map[*ast.SelectorExpr]*types.Selection),
// //Types: make(map[ast.Expr]types.TypeAndValue),
// //Scopes : make(map[ast.Node]*types.Scope)
// //Implicits : make(map[ast.Node]types.Object)
// }
// conf.XInfo = &types.Info{
// Uses: make(map[*ast.Ident]types.Object),
// Defs: make(map[*ast.Ident]types.Object),
// Selections: make(map[*ast.SelectorExpr]*types.Selection),
// }
// return conf
// }
func (p *types_parser) initSource(import_path string, path string, dir string, pfc *package_file_cache, c *auto_complete_context) {
//conf := &pkgwalk.PkgConfig{IgnoreFuncBodies: true, AllowBinary: false, WithTestFiles: true}
// conf.Info = &types.Info{}
// conf.XInfo = &types.Info{}
c.mutex.Lock()
defer c.mutex.Unlock()
conf := pkgwalk.DefaultPkgConfig()
pkg, _, err := c.typesWalker.ImportHelper(".", path, import_path, conf, nil)
if err != nil {
log.Println(err)
}
p.pkg = pkg
// im := srcimporter.New(&build.Default, c.fset, c.packages)
// if dir != "" {
// p.pkg, _ = im.ImportFrom(path, dir, 0)
// } else {
// p.pkg, _ = im.Import(path)
// }
p.pfc = pfc
}
func (p *types_parser) initData(path string, data []byte, pfc *package_file_cache, c *auto_complete_context) {
p.pkg, _ = importer.For("gc", func(path string) (io.ReadCloser, error) {
return NewMemReadClose(data), nil
}).Import(path)
p.pfc = pfc
if p.pkg != nil {
c.typesWalker.Imported[p.pkg.Path()] = p.pkg
}
}
type MemReadClose struct {
*bytes.Buffer
}
func (m *MemReadClose) Close() error {
return nil
}
func NewMemReadClose(data []byte) *MemReadClose {
return &MemReadClose{bytes.NewBuffer(data)}
}
func (p *types_parser) exportData() []byte {
if p.pkg == nil {
return nil
}
fset := token.NewFileSet()
var buf bytes.Buffer
gcexportdata.Write(&buf, fset, p.pkg)
return buf.Bytes()
}