Skip to content

Commit 5e7f3e5

Browse files
authored
Add flag to allow multiple prefix() sections to not be automatically sorted (#210)
* Add no lex order for custom sections Signed-off-by: DanWlker <[email protected]> * Add tests Signed-off-by: DanWlker <[email protected]> * Invert check to decrease steps Signed-off-by: DanWlker <[email protected]> --------- Signed-off-by: DanWlker <[email protected]>
1 parent 5969d4e commit 5e7f3e5

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

cmd/gci/gcicommand.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
type processingFunc = func(args []string, gciCfg config.Config) error
1313

1414
func (e *Executor) newGciCommand(use, short, long string, aliases []string, stdInSupport bool, processingFunc processingFunc) *cobra.Command {
15-
var noInlineComments, noPrefixComments, skipGenerated, skipVendor, customOrder, debug *bool
15+
var noInlineComments, noPrefixComments, skipGenerated, skipVendor, customOrder, noLexOrder, debug *bool
1616
var sectionStrings, sectionSeparatorStrings *[]string
1717
cmd := cobra.Command{
1818
Use: use,
@@ -28,6 +28,7 @@ func (e *Executor) newGciCommand(use, short, long string, aliases []string, stdI
2828
SkipGenerated: *skipGenerated,
2929
SkipVendor: *skipVendor,
3030
CustomOrder: *customOrder,
31+
NoLexOrder: *noLexOrder,
3132
}
3233
gciCfg, err := config.YamlConfig{Cfg: fmtCfg, SectionStrings: *sectionStrings, SectionSeparatorStrings: *sectionSeparatorStrings}.Parse()
3334
if err != nil {
@@ -61,6 +62,7 @@ localmodule: localmodule section, contains all imports from local packages`
6162
skipVendor = cmd.Flags().Bool("skip-vendor", false, "Skip files inside vendor directory")
6263

6364
customOrder = cmd.Flags().Bool("custom-order", false, "Enable custom order of sections")
65+
noLexOrder = cmd.Flags().Bool("no-lex-order", false, "Drops lexical ordering for custom sections")
6466
sectionStrings = cmd.Flags().StringArrayP("section", "s", section.DefaultSections().String(), sectionHelp)
6567

6668
// deprecated

pkg/analyzer/analyzer.go

+4
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ func generateCmdLine(cfg config.Config) string {
142142
result += " --custom-order "
143143
}
144144

145+
if cfg.BoolConfig.NoLexOrder {
146+
result += " --no-lex-order"
147+
}
148+
145149
for _, s := range cfg.Sections.String() {
146150
result += fmt.Sprintf(" --Section \"%s\" ", s)
147151
}

pkg/config/config.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type BoolConfig struct {
2626
SkipGenerated bool `yaml:"skipGenerated"`
2727
SkipVendor bool `yaml:"skipVendor"`
2828
CustomOrder bool `yaml:"customOrder"`
29+
NoLexOrder bool `yaml:"noLexOrder"`
2930
}
3031

3132
type Config struct {
@@ -63,10 +64,11 @@ func (g YamlConfig) Parse() (*Config, error) {
6364
sort.Slice(sections, func(i, j int) bool {
6465
sectionI, sectionJ := sections[i].Type(), sections[j].Type()
6566

66-
if strings.Compare(sectionI, sectionJ) == 0 {
67-
return strings.Compare(sections[i].String(), sections[j].String()) < 0
67+
if g.Cfg.NoLexOrder || strings.Compare(sectionI, sectionJ) != 0 {
68+
return defaultOrder[sectionI] < defaultOrder[sectionJ]
6869
}
69-
return defaultOrder[sectionI] < defaultOrder[sectionJ]
70+
71+
return strings.Compare(sections[i].String(), sections[j].String()) < 0
7072
})
7173
}
7274

pkg/config/config_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,16 @@ func TestParseCustomOrder(t *testing.T) {
2929
assert.NoError(t, err)
3030
assert.Equal(t, section.SectionList{section.Default{}, section.Custom{Prefix: "github/daixiang0/gci"}, section.Custom{Prefix: "github/daixiang0/gai"}}, gciCfg.Sections)
3131
}
32+
33+
func TestParseNoLexOrder(t *testing.T) {
34+
cfg := YamlConfig{
35+
SectionStrings: []string{"prefix(github/daixiang0/gci)", "prefix(github/daixiang0/gai)", "default"},
36+
Cfg: BoolConfig{
37+
NoLexOrder: true,
38+
},
39+
}
40+
41+
gciCfg, err := cfg.Parse()
42+
assert.NoError(t, err)
43+
assert.Equal(t, section.SectionList{section.Default{}, section.Custom{Prefix: "github/daixiang0/gci"}, section.Custom{Prefix: "github/daixiang0/gai"}}, gciCfg.Sections)
44+
}

0 commit comments

Comments
 (0)