From 5e7f3e50951d759be8001c546d259b0d82c39fec Mon Sep 17 00:00:00 2001 From: DanWlker <60170554+DanWlker@users.noreply.github.com> Date: Mon, 26 Aug 2024 09:24:20 +0800 Subject: [PATCH] Add flag to allow multiple prefix() sections to not be automatically sorted (#210) * Add no lex order for custom sections Signed-off-by: DanWlker * Add tests Signed-off-by: DanWlker * Invert check to decrease steps Signed-off-by: DanWlker --------- Signed-off-by: DanWlker --- cmd/gci/gcicommand.go | 4 +++- pkg/analyzer/analyzer.go | 4 ++++ pkg/config/config.go | 8 +++++--- pkg/config/config_test.go | 13 +++++++++++++ 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/cmd/gci/gcicommand.go b/cmd/gci/gcicommand.go index 7279ccf..1d80a85 100644 --- a/cmd/gci/gcicommand.go +++ b/cmd/gci/gcicommand.go @@ -12,7 +12,7 @@ import ( type processingFunc = func(args []string, gciCfg config.Config) error func (e *Executor) newGciCommand(use, short, long string, aliases []string, stdInSupport bool, processingFunc processingFunc) *cobra.Command { - var noInlineComments, noPrefixComments, skipGenerated, skipVendor, customOrder, debug *bool + var noInlineComments, noPrefixComments, skipGenerated, skipVendor, customOrder, noLexOrder, debug *bool var sectionStrings, sectionSeparatorStrings *[]string cmd := cobra.Command{ Use: use, @@ -28,6 +28,7 @@ func (e *Executor) newGciCommand(use, short, long string, aliases []string, stdI SkipGenerated: *skipGenerated, SkipVendor: *skipVendor, CustomOrder: *customOrder, + NoLexOrder: *noLexOrder, } gciCfg, err := config.YamlConfig{Cfg: fmtCfg, SectionStrings: *sectionStrings, SectionSeparatorStrings: *sectionSeparatorStrings}.Parse() if err != nil { @@ -61,6 +62,7 @@ localmodule: localmodule section, contains all imports from local packages` skipVendor = cmd.Flags().Bool("skip-vendor", false, "Skip files inside vendor directory") customOrder = cmd.Flags().Bool("custom-order", false, "Enable custom order of sections") + noLexOrder = cmd.Flags().Bool("no-lex-order", false, "Drops lexical ordering for custom sections") sectionStrings = cmd.Flags().StringArrayP("section", "s", section.DefaultSections().String(), sectionHelp) // deprecated diff --git a/pkg/analyzer/analyzer.go b/pkg/analyzer/analyzer.go index 85a89e8..5685ac2 100644 --- a/pkg/analyzer/analyzer.go +++ b/pkg/analyzer/analyzer.go @@ -142,6 +142,10 @@ func generateCmdLine(cfg config.Config) string { result += " --custom-order " } + if cfg.BoolConfig.NoLexOrder { + result += " --no-lex-order" + } + for _, s := range cfg.Sections.String() { result += fmt.Sprintf(" --Section \"%s\" ", s) } diff --git a/pkg/config/config.go b/pkg/config/config.go index cc43f2f..814201a 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -26,6 +26,7 @@ type BoolConfig struct { SkipGenerated bool `yaml:"skipGenerated"` SkipVendor bool `yaml:"skipVendor"` CustomOrder bool `yaml:"customOrder"` + NoLexOrder bool `yaml:"noLexOrder"` } type Config struct { @@ -63,10 +64,11 @@ func (g YamlConfig) Parse() (*Config, error) { sort.Slice(sections, func(i, j int) bool { sectionI, sectionJ := sections[i].Type(), sections[j].Type() - if strings.Compare(sectionI, sectionJ) == 0 { - return strings.Compare(sections[i].String(), sections[j].String()) < 0 + if g.Cfg.NoLexOrder || strings.Compare(sectionI, sectionJ) != 0 { + return defaultOrder[sectionI] < defaultOrder[sectionJ] } - return defaultOrder[sectionI] < defaultOrder[sectionJ] + + return strings.Compare(sections[i].String(), sections[j].String()) < 0 }) } diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 7a29949..9a41d5d 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -29,3 +29,16 @@ func TestParseCustomOrder(t *testing.T) { assert.NoError(t, err) assert.Equal(t, section.SectionList{section.Default{}, section.Custom{Prefix: "github/daixiang0/gci"}, section.Custom{Prefix: "github/daixiang0/gai"}}, gciCfg.Sections) } + +func TestParseNoLexOrder(t *testing.T) { + cfg := YamlConfig{ + SectionStrings: []string{"prefix(github/daixiang0/gci)", "prefix(github/daixiang0/gai)", "default"}, + Cfg: BoolConfig{ + NoLexOrder: true, + }, + } + + gciCfg, err := cfg.Parse() + assert.NoError(t, err) + assert.Equal(t, section.SectionList{section.Default{}, section.Custom{Prefix: "github/daixiang0/gci"}, section.Custom{Prefix: "github/daixiang0/gai"}}, gciCfg.Sections) +}