-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
haste.go
96 lines (88 loc) · 1.49 KB
/
haste.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
90
91
92
93
94
95
96
package haste
import (
"fmt"
"go/ast"
"golang.org/x/tools/go/packages"
)
type Haste struct {
pkgs []*packages.Package
funcs FuncDecls
}
func (h *Haste) Funcs() FuncDecls {
if len(h.funcs) > 0 {
return h.funcs
}
for _, p := range h.pkgs {
for _, f := range p.Syntax {
for _, d := range f.Decls {
fn, ok := d.(*ast.FuncDecl)
if !ok {
continue
}
h.funcs = append(h.funcs, NewFuncDecl(fn))
}
}
}
return h.funcs
}
func New(s ...string) (*Haste, error) {
cfg := &packages.Config{Mode: packages.NeedFiles | packages.NeedSyntax}
pkgs, err := packages.Load(cfg, s...)
if err != nil {
return nil, err
}
h := &Haste{
pkgs: pkgs,
}
return h, nil
}
func exprString(e ast.Expr) string {
switch t := e.(type) {
case *ast.SelectorExpr:
s := &SelectorExpr{
SelectorExpr: t,
}
return s.String()
case *ast.Ident:
return t.Name
case *ast.StarExpr:
s := &StarExpr{
StarExpr: t,
}
return s.String()
case *ast.Ellipsis:
e := &Ellipsis{
Ellipsis: t,
}
return e.String()
case *ast.InterfaceType:
i := &InterfaceType{
InterfaceType: t,
}
return i.String()
case *ast.ArrayType:
a := &ArrayType{
ArrayType: t,
}
return a.String()
case *ast.MapType:
m := &MapType{
MapType: t,
}
return m.String()
case *ast.ChanType:
c := &ChanType{
ChanType: t,
}
return c.String()
case *ast.FuncType:
f := &FuncType{
FuncType: t,
}
return f.String()
default:
x := fmt.Sprintf("TYPE: %T\n", e)
panic(x)
}
return ""
}