-
Notifications
You must be signed in to change notification settings - Fork 43
/
helper.go
384 lines (324 loc) · 8.06 KB
/
helper.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package yakdoc
import (
"bytes"
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"path/filepath"
"reflect"
"runtime"
"sort"
"strings"
"github.com/olekukonko/tablewriter"
"github.com/yaklang/yaklang/common/utils"
)
var (
defaultPackageName = "github.com/yaklang/yaklang"
defaultHooks = make([]func(h *DocumentHelper), 0)
projectPath string = ""
)
func GetProjectPath() string {
if projectPath == "" {
_, filename, _, ok := runtime.Caller(0)
if ok {
projectPath, _ = filepath.Abs(filepath.Join(filename, "../../../../"))
}
}
return projectPath
}
func GetProjectAstPackages() (map[string]*ast.Package, *token.FileSet, error) {
rootDir := GetProjectPath()
fset := token.NewFileSet()
packages := make(map[string]*ast.Package)
err := filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
pkgs, err := parser.ParseDir(fset, path, nil, parser.ParseComments|parser.AllErrors)
if err != nil {
return err
}
for _, pkg := range pkgs {
path, _ = filepath.Rel(rootDir, path)
path = fmt.Sprintf("%s/%s", defaultPackageName, path)
path = strings.ReplaceAll(path, string(filepath.Separator), "/")
packages[path] = pkg
}
}
return nil
})
return packages, fset, err
}
type DocumentHelper struct {
Libs map[string]*ScriptLib
Functions map[string]*FuncDecl
Instances map[string]*LibInstance
StructMethods map[string]*ScriptLib // 结构体方法,名字 -> 所有结构体与结构体指针方法
DeprecatedFunctions []*DeprecateFunction
hooks []func(h *DocumentHelper)
}
func RegisterHook(hook func(h *DocumentHelper)) {
defaultHooks = append(defaultHooks, hook)
}
func (h *DocumentHelper) Callback() {
for _, hook := range defaultHooks {
hook(h)
}
for _, hook := range h.hooks {
hook(h)
}
}
func (h *DocumentHelper) InjectInterfaceDocumentManually(interfacePath, sourceCodePath string) error {
lib, ok := h.StructMethods[interfacePath]
if !ok {
return utils.Errorf("interface not found in document helper: %v", interfacePath)
}
_ = lib
if !filepath.IsAbs(sourceCodePath) {
sourceCodePath, _ = filepath.Abs(filepath.Join(GetProjectPath(), sourceCodePath))
}
bundle, err := GetCacheAstBundle(sourceCodePath)
if err != nil {
return err
}
splited := strings.Split(interfacePath, ".")
interfaceName := splited[len(splited)-1]
for _, decl := range bundle.parsedFile.Decls {
genDecl, ok := decl.(*ast.GenDecl)
if !ok || genDecl.Tok != token.TYPE {
continue
}
for _, spec := range genDecl.Specs {
typeSpec, ok := spec.(*ast.TypeSpec)
if !ok || typeSpec.Name.Name != interfaceName {
continue
}
iface, ok := typeSpec.Type.(*ast.InterfaceType)
if !ok {
continue
}
for _, field := range iface.Methods.List {
if field.Doc == nil {
continue
}
methodName := field.Names[0].Name
f, ok := lib.Functions[methodName]
if !ok {
continue
}
f.Document = strings.TrimSpace(field.Doc.Text())
}
}
}
return nil
}
func (h *DocumentHelper) GetAllLibs() []string {
var k []string
if h == nil {
return k
}
for name := range h.Libs {
k = append(k, name)
}
sort.Strings(k)
return k
}
func (h *DocumentHelper) HelpInfo() string {
if h == nil {
return ""
}
buffer := bytes.NewBuffer(nil)
// 内置用户函数
buffer.WriteString("### 函数定义\n")
var items []string
for _, i := range h.Functions {
items = append(items, i.String())
}
sort.Strings(items)
for _, i := range items {
buffer.WriteString(fmt.Sprintf(" %v\n", i))
}
buffer.WriteString(fmt.Sprintf("\n%v\n", strings.Repeat("-", 48)))
t := tablewriter.NewWriter(buffer)
t.SetHeader([]string{"内置值", "值的类型", "值"})
for name, item := range h.Instances {
if item.ValueStr == "" {
t.Append([]string{name, item.Type, "-"})
} else {
t.Append([]string{name, item.Type, item.ValueStr})
}
}
t.Render()
buffer.WriteString(fmt.Sprintf("\n%v\n", strings.Repeat("-", 48)))
t = tablewriter.NewWriter(buffer)
t.SetHeader([]string{"可用依赖库", "依赖库可用元素(值/函数)"})
for libName, libs := range h.Libs {
t.Append([]string{libName, fmt.Sprint(len(libs.ElementDocs))})
}
t.Render()
return buffer.String()
}
func (h *DocumentHelper) ShowHelpInfo() {
if h == nil {
return
}
fmt.Println(h.HelpInfo())
}
func (h *DocumentHelper) LibHelpInfo(name string) string {
if h == nil {
return ""
}
lib, ok := h.Libs[name]
if !ok {
return ""
}
return lib.String()
}
func (h *DocumentHelper) ShowLibHelpInfo(name string) {
if h == nil {
return
}
fmt.Println(h.LibHelpInfo(name))
}
func (h *DocumentHelper) LibFuncDefinitionStr(libName, funcName string) string {
return h.libFuncToStr(libName, funcName, "def")
}
func (h *DocumentHelper) LibFuncHelpInfo(libName, funcName string) string {
return h.libFuncToStr(libName, funcName, "help")
}
func (h *DocumentHelper) LibFuncAutoCompletion(libName, funcName string) string {
return h.libFuncToStr(libName, funcName, "completion")
}
func (h *DocumentHelper) libFuncToStr(libName, funcName string, strType string) string {
if h == nil {
return ""
}
var (
lib *ScriptLib
f *FuncDecl
ok bool
)
if libName == "__GLOBAL__" || libName == "__global__" {
f, ok = h.Functions[funcName]
if !ok {
return ""
}
} else {
lib, ok = h.Libs[libName]
if !ok {
return ""
}
f, ok = lib.Functions[funcName]
if !ok {
return ""
}
}
switch strType {
case "help":
return fmt.Sprintf("函 数 名: %s.%s\n函数声明: %s\n函数文档: %s", libName, funcName, f.Decl, f.Document)
case "completion":
return f.VSCodeSnippets
default:
if f.Document == "" {
return f.Decl
}
return fmt.Sprintf("%v doc:%v", f.Decl, f.Document)
}
}
func (h *DocumentHelper) ShowLibFuncHelpInfo(libName, funcName string) {
if h == nil {
return
}
fmt.Println(h.LibFuncHelpInfo(libName, funcName))
}
type LibInstance struct {
LibName string
InstanceName string
Type string
ValueStr string
}
func (i *LibInstance) String() string {
if i == nil {
return ""
}
return fmt.Sprintf("%v.%v: %v = %v", i.LibName, i.InstanceName, i.Type, i.ValueStr)
}
type ScriptLib struct {
Name string
Instances map[string]*LibInstance
Functions map[string]*FuncDecl
ElementDocs []string
}
func (l *ScriptLib) String() string {
buff := bytes.NewBuffer(nil)
lenOfElements := len(l.ElementDocs)
buff.WriteString(fmt.Sprintf("### Palm ExtLib: [%v] - %v elements\n\n", l.Name, lenOfElements))
for _, i := range l.ElementDocs {
buff.WriteString(fmt.Sprintf(" %v\n", i))
}
return buff.String()
}
type Field struct {
Name string
Type string
RefType reflect.Type
}
type FuncDecl struct {
LibName string
MethodName string
Document string
Decl string
Params []*Field
Results []*Field
VSCodeSnippets string
}
func (f *FuncDecl) String() string {
decl, doc := f.Decl, f.Document
if doc != "" {
doc = fmt.Sprintf(`: "%s"`, doc)
}
decl = fmt.Sprintf("%s.%s", f.LibName, decl)
return fmt.Sprintf("%s%s", decl, doc)
}
type DeprecateFunction struct {
Name string
Self *FuncDecl
Msg string
}
// func FuncToFuncDecl(libName, methodName string, refType reflect.Type, f interface{}) *FuncDecl {
// }
func CustomHandleTypeName(typName string) string {
// 这里需要手动处理context
if strings.Contains(typName, "context") {
return "context.Context"
}
return typName
}
func AnyTypeToLibInstance(libName, name string, typ reflect.Type, value interface{}) *LibInstance {
var (
typKind = typ.Kind()
pkgPath, typName string
)
if typKind == reflect.Struct || typKind == reflect.Interface {
pkgPath = typ.PkgPath()
typName = typ.Name()
} else if typKind == reflect.Ptr {
pkgPath = typ.Elem().PkgPath()
typName = typ.Elem().Name()
}
if typName == "" {
typName = typ.String()
}
if pkgPath != "" {
typName = fmt.Sprintf("%s.%s", pkgPath, typName)
}
return &LibInstance{
LibName: libName,
InstanceName: name,
Type: CustomHandleTypeName(typName),
ValueStr: utils.AsDebugString(value),
}
}