/
sort.go
209 lines (179 loc) · 4.04 KB
/
sort.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
package main
import (
"bytes"
"fmt"
"go/ast"
"go/token"
"log"
"reflect"
"sort"
"github.com/dave/dst"
"github.com/dave/dst/decorator"
"github.com/pkg/errors"
)
func Sort(source []byte) (string, error) {
root, err := decorator.Parse(source)
if err != nil {
return "", errors.Wrap(err, "could not parse")
}
sort.SliceStable(root.Decls, sortDecl(root.Decls))
alignWhiteSpace(root.Decls)
var buf bytes.Buffer
err = decorator.Fprint(&buf, root)
if err != nil {
return "", errors.Wrap(err, "could not format output")
}
return buf.String(), nil
}
func funcName(f *dst.FuncDecl) string {
if f.Recv == nil {
return f.Name.Name
}
recvType, ok := f.Recv.List[0].Type.(*dst.StarExpr)
if !ok {
log.Print(f.Recv.List[0].Type)
return f.Name.Name
}
ident, ok := recvType.X.(*dst.Ident)
if !ok {
log.Print(recvType.X)
return f.Name.Name
}
return ident.Name + f.Name.Name
}
func genName(g *dst.GenDecl) string {
switch g.Tok {
case token.CONST, token.VAR:
spec, ok := g.Specs[0].(*dst.ValueSpec)
if !ok {
log.Print("genName", g.Specs[0])
return ""
}
return spec.Names[0].Name
case token.STRUCT, token.TYPE:
spec, ok := g.Specs[0].(*dst.TypeSpec)
if !ok {
log.Print("genName", g.Specs[0])
return ""
}
return spec.Name.Name
default:
log.Print("genName ", g.Tok, " not supported")
return ""
}
}
func genScore(g *dst.GenDecl) int {
switch g.Tok {
case token.CONST:
return 2
case token.IMPORT:
return 1
case token.VAR:
return 3
case token.STRUCT, token.FUNC, token.TYPE:
return 5
default:
return 4
}
}
func lessFuncFunc(this, other *dst.FuncDecl) bool {
return lessName(funcName(this), funcName(other))
}
func lessFuncGen(this *dst.FuncDecl, other *dst.GenDecl) bool {
// only structs/types are mixed with functions
if other.Tok != token.STRUCT && other.Tok != token.TYPE {
return false
}
// free functions are always later/bigger
if this.Recv == nil || len(this.Recv.List) == 0 {
return false
}
return lessName(funcName(this), genName(other))
}
func lessGen(this, other *dst.GenDecl) bool {
if genScore(this) != genScore(other) {
return genScore(this) < genScore(other)
}
return lessName(genName(this), genName(other))
}
func lessName(this, other string) bool {
// public (exported) nodes first
if ast.IsExported(this) != ast.IsExported(other) {
return ast.IsExported(this)
}
return this < other
}
/*
imports
types and methods
functions
*/
func sortDecl(a []dst.Decl) func(i, j int) bool {
DeclPriority := map[reflect.Type]int{
reflect.TypeOf(&dst.FuncDecl{}): 1,
reflect.TypeOf(&dst.GenDecl{}): 1,
}
return func(i, j int) bool {
this := a[i]
other := a[j]
thisPrio, ok := DeclPriority[reflect.TypeOf(this)]
if !ok {
fmt.Printf("%T %t not implemented\n", this, this)
return true
}
otherPrio, ok := DeclPriority[reflect.TypeOf(other)]
if !ok {
fmt.Printf("%T %t not implemented\n", other, other)
return true
}
if thisPrio != otherPrio {
return thisPrio < otherPrio
}
switch thisV := this.(type) {
case *dst.FuncDecl:
switch otherV := other.(type) {
case *dst.FuncDecl:
return lessFuncFunc(thisV, otherV)
case *dst.GenDecl:
return lessFuncGen(thisV, otherV)
default:
fmt.Printf("%T %t other not implemented\n", other, other)
}
case *dst.GenDecl:
switch otherV := other.(type) {
case *dst.FuncDecl:
return !lessFuncGen(otherV, thisV)
case *dst.GenDecl:
return lessGen(thisV, otherV)
default:
fmt.Printf("%T %t other not implemented\n", other, other)
}
default:
fmt.Printf("%T %t this not implemented\n", other, other)
}
return true
}
}
// when sorting var and const declarations we may create too much new whitespace
func alignWhiteSpace(data []dst.Decl) {
if len(data) <= 1 {
return
}
last := 0
i := 1
for {
if i == len(data) {
break
}
lastType, lastOk := data[last].(*dst.GenDecl)
iType, iOk := data[i].(*dst.GenDecl)
if lastOk && iOk {
if lastType.Tok == iType.Tok {
lastType.Decs.After = dst.NewLine
iType.Decs.Before = dst.NewLine
}
}
last++
i++
}
}