Skip to content

Commit

Permalink
Support multiple prefixes to group them together #107 (#142)
Browse files Browse the repository at this point in the history
This changes allows to use this to group multiple prefixes


Signed-off-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>
  • Loading branch information
ccoVeille authored Mar 2, 2023
1 parent 5a4fe84 commit 65b147e
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 16 deletions.
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ GCI, a tool that controls golang package import order and makes it always determ
The desired output format is highly configurable and allows for more custom formatting than `goimport` does.

GCI considers a import block based on AST as below:

```
Doc
Name Path Comment
```

All comments will keep as they were, except the isolated comment blocks.

The isolated comment blocks like below:

```
import (
"fmt"
Expand Down Expand Up @@ -78,9 +81,9 @@ Flags:
-h, --help help for write
-s, --section strings Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). The section order is standard > default > custom > blank > dot. The default value is [standard,default].
standard - standard section that Golang provides officially, like "fmt"
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix.
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix. Multiple custom prefixes may be provided, they will be rendered as distinct sections separated by newline. You can regroup multiple prefixes by separating them with comma: Prefix(github.com/daixiang0,gitlab.com/daixiang0,daixiang0)
default - default section, contains all rest imports
blank - blank section, contains all blank imports. This section is not presed unless explicitly enabled. (default [standard,default])
blank - blank section, contains all blank imports.
--skip-generated Skip generated files
--custom-order Enable custom order of sections. If specified, make the section order the same as your configuration order. The default order is standard > default > custom > blank > dot.
```
Expand All @@ -99,11 +102,11 @@ Flags:
-d, --debug Enables debug output from the formatter
-h, --help help for write
-s, --section strings Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). The section order is standard > default > custom > blank > dot. The default value is [standard,default].
standard - standard section thatolang provides officially, like "fmt"
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix.
standard - standard section that Golang provides officially, like "fmt"
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix. Multiple custom prefixes may be provided, they will be rendered as distinct sections separated by newline. You can regroup multiple prefixes by separating them with comma: Prefix(github.com/daixiang0,gitlab.com/daixiang0,daixiang0)
default - default section, contains all rest imports
blank - blank section, contains all blank imports. This section is not presed unless explicitly enabled.
dot - dot section, contains all dot imports. This section is not presed unless explicitly enabled. (default [standard,default])
blank - blank section, contains all blank imports.
dot - dot section, contains all dot imports.
--skip-generated Skip generated files
--custom-order Enable custom order of sections. If specified, make the section order the same as your configuration order. The default order is standard > default > custom > blank > dot.
```
Expand All @@ -119,11 +122,11 @@ Flags:
-d, --debug Enables debug output from the formatter
-h, --help help for write
-s, --section strings Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). The section order is standard > default > custom > blank > dot. The default value is [standard,default].
standard - standard section thatolang provides officially, like "fmt"
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix.
standard - standard section that Golang provides officially, like "fmt"
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix. Multiple custom prefixes may be provided, they will be rendered as distinct sections separated by newline. You can regroup multiple prefixes by separating them with comma: Prefix(github.com/daixiang0,gitlab.com/daixiang0,daixiang0)
default - default section, contains all rest imports
blank - blank section, contains all blank imports. This section is not presed unless explicitly enabled.
dot - dot section, contains all dot imports. This section is not presed unless explicitly enabled. (default [standard,default])
blank - blank section, contains all blank imports.
dot - dot section, contains all dot imports.
--skip-generated Skip generated files
--custom-order Enable custom order of sections. If specified, make the section order the same as your configuration order. The default order is standard > default > custom > blank > dot.
```
Expand Down
8 changes: 4 additions & 4 deletions cmd/gci/gcicommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ func (e *Executor) newGciCommand(use, short, long string, aliases []string, stdI

sectionHelp := `Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). The section order is standard > default > custom > blank > dot. The default value is [standard,default].
standard - standard section that Golang provides officially, like "fmt"
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix.
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix. Multiple custom prefixes may be provided, they will be rendered as distinct sections separated by newline. You can regroup multiple prefixes by separating them with comma: Prefix(github.com/daixiang0,gitlab.com/daixiang0,daixiang0)
default - default section, contains all rest imports
blank - blank section, contains all blank imports. This section is not presed unless explicitly enabled.
dot - dot section, contains all dot imports. This section is not presed unless explicitly enabled.`
blank - blank section, contains all blank imports.
dot - dot section, contains all dot imports.`

skipGenerated = cmd.Flags().Bool("skip-generated", false, "Skip generated files")

customOrder = cmd.Flags().Bool("custom-order", false, "Enable custom order of sections")
sectionStrings = cmd.Flags().StringSliceP("section", "s", section.DefaultSections().String(), sectionHelp)
sectionStrings = cmd.Flags().StringArrayP("section", "s", section.DefaultSections().String(), sectionHelp)

// deprecated
noInlineComments = cmd.Flags().Bool("NoInlineComments", false, "Drops inline comments while formatting")
Expand Down
4 changes: 4 additions & 0 deletions pkg/gci/internal/testdata/grouped-multiple-custom.cfg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sections:
- Standard
- Default
- Prefix(github.com/daixiang0,gitlab.com/daixiang0,daixiang0)
10 changes: 10 additions & 0 deletions pkg/gci/internal/testdata/grouped-multiple-custom.in.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import (
"daixiang0/lib1"
"fmt"
"github.com/daixiang0/gci"
"gitlab.com/daixiang0/gci"
g "github.com/golang"
"github.com/daixiang0/gci/subtest"
)
12 changes: 12 additions & 0 deletions pkg/gci/internal/testdata/grouped-multiple-custom.out.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"fmt"

g "github.com/golang"

"daixiang0/lib1"
"github.com/daixiang0/gci"
"github.com/daixiang0/gci/subtest"
"gitlab.com/daixiang0/gci"
)
5 changes: 5 additions & 0 deletions pkg/section/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ func TestParse(t *testing.T) {
expectedSection: nil,
expectedError: errors.New("invalid params: prefix("),
},
{
input: []string{"prefix(domainA,domainB)"},
expectedSection: SectionList{Custom{"domainA,domainB"}},
expectedError: nil,
},
}
for _, test := range testCases {
parsedSection, err := Parse(test.input)
Expand Down
11 changes: 9 additions & 2 deletions pkg/section/prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@ type Custom struct {
Prefix string
}

// CustomSeparator allows you to group multiple custom prefix together in the same section
// gci diff -s standard -s default -s prefix(github.com/company,gitlab.com/company,companysuffix)
const CustomSeparator = ","

const CustomType = "custom"

func (c Custom) MatchSpecificity(spec *parse.GciImports) specificity.MatchSpecificity {
if strings.HasPrefix(spec.Path, c.Prefix) {
return specificity.Match{Length: len(c.Prefix)}
for _, prefix := range strings.Split(c.Prefix, CustomSeparator) {
if strings.HasPrefix(spec.Path, prefix) {
return specificity.Match{Length: len(prefix)}
}
}

return specificity.MisMatch{}
}

Expand Down

0 comments on commit 65b147e

Please sign in to comment.