@@ -11,13 +11,58 @@ import (
11
11
"strings"
12
12
)
13
13
14
+ const reorderSignature = "// -- "
15
+
14
16
// DefaultOrder is the default order of elements.
15
17
//
16
18
// Note, Init and Main are not in the list. If they are present, the init and main functions
17
19
// will be moved.
18
20
var DefaultOrder = []Order {Const , Var , Interface , Type , Func }
19
21
20
- const reorderSignature = "// -- "
22
+ // findMissingOrderElement finds the missing order element.
23
+ // If the default order is not complete, it will add the missing elements.
24
+ func findMissingOrderElement (opt * ReorderConfig ) {
25
+
26
+ if len (opt .DefOrder ) != len (DefaultOrder ) {
27
+ // wich one is missing?
28
+ for _ , order := range DefaultOrder {
29
+ found := false
30
+ for _ , defOrder := range opt .DefOrder {
31
+ if order == defOrder {
32
+ found = true
33
+ break
34
+ }
35
+ }
36
+ if ! found {
37
+ // add it to the end
38
+ opt .DefOrder = append (opt .DefOrder , order )
39
+ }
40
+ }
41
+ }
42
+ }
43
+
44
+ func formatSource (content , output []byte , opt ReorderConfig ) ([]byte , error ) {
45
+
46
+ // write in a temporary file and use "gofmt" to format it
47
+ //newcontent := []byte(output)
48
+ var newcontent []byte
49
+ var err error
50
+ switch opt .FormatCommand {
51
+ case "gofmt" :
52
+ // format the temporary file
53
+ newcontent , err = format .Source ([]byte (output ))
54
+ if err != nil {
55
+ return content , errors .New ("Failed to format source: " + err .Error ())
56
+ }
57
+ default :
58
+ newcontent , err = formatWithCommand (content , output , opt )
59
+ if err != nil {
60
+ return content , errors .New ("Failed to format source: " + err .Error ())
61
+ }
62
+ }
63
+
64
+ return newcontent , nil
65
+ }
21
66
22
67
func formatWithCommand (content []byte , output []byte , opt ReorderConfig ) (newcontent []byte , err error ) {
23
68
// we use the format command given by the user
@@ -47,6 +92,16 @@ func formatWithCommand(content []byte, output []byte, opt ReorderConfig) (newcon
47
92
return newcontent , nil
48
93
}
49
94
95
+ func getKeys (m map [string ]* GoType ) []string {
96
+ keys := make ([]string , len (m ))
97
+ i := 0
98
+ for k := range m {
99
+ keys [i ] = k
100
+ i ++
101
+ }
102
+ return keys
103
+ }
104
+
50
105
// const and vars
51
106
func processConst (
52
107
info * ParsedInfo ,
@@ -206,6 +261,24 @@ func processVars(
206
261
return source
207
262
}
208
263
264
+ func removeSignedLine (originalContent []string , sign string ) []string {
265
+ // remove the lines that were marked as "// -- line to remove"
266
+ temp := []string {}
267
+ for _ , line := range originalContent {
268
+ if line != reorderSignature + sign {
269
+ temp = append (temp , line )
270
+ }
271
+ }
272
+
273
+ return temp
274
+ }
275
+
276
+ func sortGoTypes (v []* GoType ) {
277
+ sort .Slice (v , func (i , j int ) bool {
278
+ return v [i ].Name < v [j ].Name
279
+ })
280
+ }
281
+
209
282
// ReorderSource reorders the source code in the given filename.
210
283
// It will be helped by the formatCommand (gofmt or goimports).
211
284
// If gofmt is used, the source code will be formatted with the go/fmt package in memory.
@@ -362,76 +435,3 @@ func ReorderSource(opt ReorderConfig) (string, error) {
362
435
}
363
436
return string (newcontent ), nil
364
437
}
365
-
366
- // findMissingOrderElement finds the missing order element.
367
- // If the default order is not complete, it will add the missing elements.
368
- func findMissingOrderElement (opt * ReorderConfig ) {
369
-
370
- if len (opt .DefOrder ) != len (DefaultOrder ) {
371
- // wich one is missing?
372
- for _ , order := range DefaultOrder {
373
- found := false
374
- for _ , defOrder := range opt .DefOrder {
375
- if order == defOrder {
376
- found = true
377
- break
378
- }
379
- }
380
- if ! found {
381
- // add it to the end
382
- opt .DefOrder = append (opt .DefOrder , order )
383
- }
384
- }
385
- }
386
- }
387
-
388
- func sortGoTypes (v []* GoType ) {
389
- sort .Slice (v , func (i , j int ) bool {
390
- return v [i ].Name < v [j ].Name
391
- })
392
- }
393
-
394
- func getKeys (m map [string ]* GoType ) []string {
395
- keys := make ([]string , len (m ))
396
- i := 0
397
- for k := range m {
398
- keys [i ] = k
399
- i ++
400
- }
401
- return keys
402
- }
403
-
404
- func formatSource (content , output []byte , opt ReorderConfig ) ([]byte , error ) {
405
-
406
- // write in a temporary file and use "gofmt" to format it
407
- //newcontent := []byte(output)
408
- var newcontent []byte
409
- var err error
410
- switch opt .FormatCommand {
411
- case "gofmt" :
412
- // format the temporary file
413
- newcontent , err = format .Source ([]byte (output ))
414
- if err != nil {
415
- return content , errors .New ("Failed to format source: " + err .Error ())
416
- }
417
- default :
418
- newcontent , err = formatWithCommand (content , output , opt )
419
- if err != nil {
420
- return content , errors .New ("Failed to format source: " + err .Error ())
421
- }
422
- }
423
-
424
- return newcontent , nil
425
- }
426
-
427
- func removeSignedLine (originalContent []string , sign string ) []string {
428
- // remove the lines that were marked as "// -- line to remove"
429
- temp := []string {}
430
- for _ , line := range originalContent {
431
- if line != reorderSignature + sign {
432
- temp = append (temp , line )
433
- }
434
- }
435
-
436
- return temp
437
- }
0 commit comments