-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathformat.go
78 lines (70 loc) · 1.62 KB
/
format.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
package wrapmsg
import (
"context"
"fmt"
"go/ast"
"github.com/Warashi/ssautil"
"golang.org/x/tools/go/ssa"
)
func formatCall(ctx context.Context, call *ssa.Call) ([]string, bool) {
c, ok := getCallExpr(ctx, call)
if !ok {
return nil, false
}
return formatCallExpr(c), true
}
func getCallExpr(ctx context.Context, call ssautil.Poser) (*ast.CallExpr, bool) {
posMap := ssautil.PosMap(ctx)
for i := call.Pos(); i > 0; i-- {
stack := posMap[i]
if len(stack) == 0 {
break
}
for j := range stack {
node := stack[len(stack)-1-j]
ident, ok := node.(*ast.CallExpr)
if ok {
return ident, true
}
}
}
return nil, false
}
func formatCallExpr(call *ast.CallExpr) []string {
switch f := call.Fun.(type) {
case *ast.SelectorExpr:
return formatSelectorExpr(f)
case *ast.Ident:
return []string{f.Name}
}
return nil
}
func formatSelectorExpr(sel *ast.SelectorExpr) []string {
var ret []string
switch x := sel.X.(type) {
case *ast.CallExpr:
ret = append(formatCallExpr(x), ret...)
case *ast.Ident:
ret = append(ret, x.Name)
case *ast.SelectorExpr:
ret = append(formatSelectorExpr(x), ret...)
case *ast.IndexExpr:
ret = append(formatIndexExpr(x), ret...)
}
ret = append(ret, sel.Sel.Name)
return ret
}
func formatIndexExpr(expr *ast.IndexExpr) []string {
var ret []string
switch x := expr.X.(type) {
case *ast.SelectorExpr:
ret = append(formatSelectorExpr(x), ret...)
}
switch x := expr.Index.(type) {
case *ast.Ident:
ret[len(ret)-1] = fmt.Sprintf("%s[%s]", ret[len(ret)-1], x.Name)
case *ast.BasicLit:
ret[len(ret)-1] = fmt.Sprintf("%s[%s]", ret[len(ret)-1], x.Value)
}
return ret
}