forked from aloder/tojen
/
formatting.go
67 lines (61 loc) · 1.77 KB
/
formatting.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
package gen
import (
"go/format"
"regexp"
)
// EXPERIMENTAL
func formatNulls(file []byte) []byte {
reg := regexp.MustCompile("(jen[.]Null[(][)][.])")
return reg.ReplaceAll(file, []byte("jen."))
}
func formatStructs(file []byte) []byte {
reg2 := regexp.MustCompile(`Struct([(]).+[)]`)
ret := reg2.ReplaceAllFunc(file, func(b []byte) []byte {
println(string("find"))
reg := regexp.MustCompile(`(Struct[(])`)
b2 := reg.ReplaceAll(b, []byte("$0\n"))
reg2 := regexp.MustCompile(`([)])([)])$`)
b3 := reg2.ReplaceAll(b2, []byte("$1,\n$2"))
reg3 := regexp.MustCompile(",")
commas := reg3.ReplaceAll(b3, []byte("$0\n"))
return commas
})
return ret
}
func formatBlock(file []byte) []byte {
reg2 := regexp.MustCompile(`Block([(]).+[)]`)
ret := reg2.ReplaceAllFunc(file, func(b []byte) []byte {
println(string("find"))
reg := regexp.MustCompile(`(Block[(])`)
b2 := reg.ReplaceAll(b, []byte("$0\n"))
reg2 := regexp.MustCompile(`([)])([)])$`)
b3 := reg2.ReplaceAll(b2, []byte("$1,\n$2"))
reg3 := regexp.MustCompile(",")
commas := reg3.ReplaceAll(b3, []byte("$0\n"))
return commas
})
return ret
}
func formatParams(file []byte) []byte {
reg2 := regexp.MustCompile(`(:?Params)[(].+"[)]{2}.`)
ret := reg2.ReplaceAllFunc(file, func(b []byte) []byte {
println(string("find params"))
println(string(b))
println(string("End params"))
reg := regexp.MustCompile(`(Params[(])`)
b2 := reg.ReplaceAll(b, []byte("$0\n"))
reg2 := regexp.MustCompile(`([)])([)])$`)
b3 := reg2.ReplaceAll(b2, []byte("$1,\n$2"))
reg3 := regexp.MustCompile(",")
commas := reg3.ReplaceAll(b3, []byte("$0\n"))
return commas
})
return ret
}
func goFormat(file []byte) ([]byte, error) {
fmtBytes, err := format.Source([]byte(file))
if err != nil {
return file, err
}
return fmtBytes, nil
}