Skip to content

Commit b56e350

Browse files
committed
Reordering
1 parent c91f455 commit b56e350

File tree

2 files changed

+107
-107
lines changed

2 files changed

+107
-107
lines changed

ordering/main.go

+74-74
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,58 @@ import (
1111
"strings"
1212
)
1313

14+
const reorderSignature = "// -- "
15+
1416
// DefaultOrder is the default order of elements.
1517
//
1618
// Note, Init and Main are not in the list. If they are present, the init and main functions
1719
// will be moved.
1820
var DefaultOrder = []Order{Const, Var, Interface, Type, Func}
1921

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+
}
2166

2267
func formatWithCommand(content []byte, output []byte, opt ReorderConfig) (newcontent []byte, err error) {
2368
// we use the format command given by the user
@@ -47,6 +92,16 @@ func formatWithCommand(content []byte, output []byte, opt ReorderConfig) (newcon
4792
return newcontent, nil
4893
}
4994

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+
50105
// const and vars
51106
func processConst(
52107
info *ParsedInfo,
@@ -206,6 +261,24 @@ func processVars(
206261
return source
207262
}
208263

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+
209282
// ReorderSource reorders the source code in the given filename.
210283
// It will be helped by the formatCommand (gofmt or goimports).
211284
// 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) {
362435
}
363436
return string(newcontent), nil
364437
}
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-
}

ordering/parser.go

+33-33
Original file line numberDiff line numberDiff line change
@@ -196,39 +196,6 @@ func findGlobalVarsAndConsts(d *ast.GenDecl, fset *token.FileSet, sourceLines []
196196
}
197197
}
198198

199-
func parseConstantAndVars(name *ast.Ident, d *ast.GenDecl, fset *token.FileSet, sourceLines []string, varTypes, constTypes map[string]*GoType) {
200-
201-
// log the source code for the variable or constant
202-
varDef := &GoType{
203-
Name: name.Name,
204-
OpeningLine: fset.Position(d.Pos()).Line,
205-
ClosingLine: fset.Position(d.End()).Line,
206-
}
207-
comments := GetTypeComments(d)
208-
if len(comments) > 0 {
209-
varDef.SourceCode = strings.Join(comments, "\n") + "\n"
210-
}
211-
varDef.SourceCode += strings.Join(sourceLines[varDef.OpeningLine-1:varDef.ClosingLine], "\n")
212-
varDef.OpeningLine -= len(comments)
213-
214-
// this time, if const or vars are defined in a parenthesis, the source code is the same for all
215-
// found var or const. So, what we do is to check if the source code is already in the map, and if
216-
// so, we skip it.
217-
// we will use the source code signature as the key for the map
218-
signature := fmt.Sprintf("%d-%d", varDef.OpeningLine, varDef.ClosingLine)
219-
if _, ok := varTypes[signature]; ok {
220-
return
221-
}
222-
223-
switch d.Tok {
224-
case token.CONST:
225-
constTypes[signature] = varDef
226-
case token.VAR:
227-
varTypes[signature] = varDef
228-
}
229-
230-
}
231-
232199
func findInterfaces(d *ast.GenDecl, fset *token.FileSet, sourceLines []string, interfaceNames *StingList, interfaceTypes map[string]*GoType) {
233200
// finc interfaces
234201
if d.Tok != token.TYPE {
@@ -320,6 +287,39 @@ func findTypes(d *ast.GenDecl, fset *token.FileSet, sourceLines []string, typeNa
320287
}
321288
}
322289

290+
func parseConstantAndVars(name *ast.Ident, d *ast.GenDecl, fset *token.FileSet, sourceLines []string, varTypes, constTypes map[string]*GoType) {
291+
292+
// log the source code for the variable or constant
293+
varDef := &GoType{
294+
Name: name.Name,
295+
OpeningLine: fset.Position(d.Pos()).Line,
296+
ClosingLine: fset.Position(d.End()).Line,
297+
}
298+
comments := GetTypeComments(d)
299+
if len(comments) > 0 {
300+
varDef.SourceCode = strings.Join(comments, "\n") + "\n"
301+
}
302+
varDef.SourceCode += strings.Join(sourceLines[varDef.OpeningLine-1:varDef.ClosingLine], "\n")
303+
varDef.OpeningLine -= len(comments)
304+
305+
// this time, if const or vars are defined in a parenthesis, the source code is the same for all
306+
// found var or const. So, what we do is to check if the source code is already in the map, and if
307+
// so, we skip it.
308+
// we will use the source code signature as the key for the map
309+
signature := fmt.Sprintf("%d-%d", varDef.OpeningLine, varDef.ClosingLine)
310+
if _, ok := varTypes[signature]; ok {
311+
return
312+
}
313+
314+
switch d.Tok {
315+
case token.CONST:
316+
constTypes[signature] = varDef
317+
case token.VAR:
318+
varTypes[signature] = varDef
319+
}
320+
321+
}
322+
323323
func inConstructors(constructorMap map[string][]*GoType, funcname string) bool {
324324
for _, constructors := range constructorMap {
325325
for _, constructor := range constructors {

0 commit comments

Comments
 (0)