Skip to content

Commit

Permalink
Add Alias Section (#178)
Browse files Browse the repository at this point in the history
Signed-off-by: ryokusnadi <ryokusnadi@gmail.com>
  • Loading branch information
RyoKusnadi committed Oct 18, 2023
1 parent 75159c7 commit 19d84f7
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 8 deletions.
49 changes: 43 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ GCI splits all import blocks into different sections, now support five section t
- default: All rest import blocks
- blank: Put blank imports together in a separate group
- dot: Put dot imports together in a separate group
- alias: Put alias imports together in a separate group

The priority is standard > default > custom > blank > dot, all sections sort alphabetically inside.
By default, blank and dot sections are not used and the corresponding lines end up in the other groups.
The priority is standard > default > custom > blank > dot > alias, all sections sort alphabetically inside.
By default, blank , dot and alias sections are not used and the corresponding lines end up in the other groups.

All import blocks use one TAB(`\t`) as Indent.

Expand Down Expand Up @@ -88,12 +89,13 @@ Flags:
--custom-order Enable custom order of sections
-d, --debug Enables debug output from the formatter
-h, --help help for print
-s, --section stringArray 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].
-s, --section stringArray 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 > alias. The default value is [standard,default].
standard - standard section that Go 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.
dot - dot section, contains all dot imports. (default [standard,default])
alias - alias section, contains all alias imports.
--skip-generated Skip generated files
--skip-vendor Skip files inside vendor directory
```
Expand All @@ -112,12 +114,13 @@ Flags:
--custom-order Enable custom order of sections
-d, --debug Enables debug output from the formatter
-h, --help help for write
-s, --section stringArray 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].
-s, --section stringArray 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 > alias. The default value is [standard,default].
standard - standard section that Go 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.
dot - dot section, contains all dot imports. (default [standard,default])
alias - alias section, contains all alias imports.
--skip-generated Skip generated files
--skip-vendor Skip files inside vendor directory
```
Expand All @@ -133,11 +136,12 @@ Flags:
--custom-order Enable custom order of sections
-d, --debug Enables debug output from the formatter
-h, --help help for list
-s, --section stringArray 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].
-s, --section stringArray 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 > alias. The default value is [standard,default].
standard - standard section that Go 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.
alias - alias section, contains all alias imports.
dot - dot section, contains all dot imports. (default [standard,default])
--skip-generated Skip generated files
--skip-vendor Skip files inside vendor directory
Expand All @@ -154,11 +158,12 @@ Flags:
--custom-order Enable custom order of sections
-d, --debug Enables debug output from the formatter
-h, --help help for diff
-s, --section stringArray 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].
-s, --section stringArray 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 > alias. The default value is [standard,default].
standard - standard section that Go 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.
alias - alias section, contains all alias imports.
dot - dot section, contains all dot imports. (default [standard,default])
--skip-generated Skip generated files
--skip-vendor Skip files inside vendor directory
Expand Down Expand Up @@ -284,6 +289,38 @@ import (
)
```
### with alias grouping enabled
```go
package main

import (
testing "github.com/daixiang0/test"
"fmt"

g "github.com/golang"

"github.com/daixiang0/gci"
"github.com/daixiang0/gci/subtest"
)
```
to
```go
package main

import (
"fmt"

"github.com/daixiang0/gci"
"github.com/daixiang0/gci/subtest"

testing "github.com/daixiang0/test"
g "github.com/golang"
)
```
## TODO
- Ensure only one blank between `Name` and `Path` in an import block
Expand Down
5 changes: 3 additions & 2 deletions cmd/gci/gcicommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ func (e *Executor) newGciCommand(use, short, long string, aliases []string, stdI

debug = cmd.Flags().BoolP("debug", "d", false, "Enables debug output from the formatter")

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].
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 > alias. The default value is [standard,default].
standard - standard section that Go 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.
dot - dot section, contains all dot imports.`
dot - dot section, contains all dot imports.
alias - alias section, contains all alias imports.`

skipGenerated = cmd.Flags().Bool("skip-generated", false, "Skip generated files")
skipVendor = cmd.Flags().Bool("skip-vendor", false, "Skip files inside vendor directory")
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var defaultOrder = map[string]int{
section.CustomType: 2,
section.BlankType: 3,
section.DotType: 4,
section.AliasType: 5,
}

type BoolConfig struct {
Expand Down
33 changes: 33 additions & 0 deletions pkg/gci/testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,39 @@ import (
"github.com/daixiang0/gci"
"github.com/daixiang0/gci/subtest"
)
`,
},
{
"alias",

`sections:
- Standard
- Default
- Alias
`,
`package main
import (
testing "github.com/daixiang0/test"
"fmt"
g "github.com/golang"
"github.com/daixiang0/gci"
"github.com/daixiang0/gci/subtest"
)
`,
`package main
import (
"fmt"
"github.com/daixiang0/gci"
"github.com/daixiang0/gci/subtest"
testing "github.com/daixiang0/test"
g "github.com/golang"
)
`,
},
}
25 changes: 25 additions & 0 deletions pkg/section/alias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package section

import (
"github.com/daixiang0/gci/pkg/parse"
"github.com/daixiang0/gci/pkg/specificity"
)

type Alias struct{}

const AliasType = "alias"

func (b Alias) MatchSpecificity(spec *parse.GciImports) specificity.MatchSpecificity {
if spec.Name != "." && spec.Name != "_" && spec.Name != "" {
return specificity.NameMatch{}
}
return specificity.MisMatch{}
}

func (b Alias) String() string {
return AliasType
}

func (b Alias) Type() string {
return AliasType
}
2 changes: 2 additions & 0 deletions pkg/section/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func Parse(data []string) (SectionList, error) {
list = append(list, Dot{})
} else if s == "blank" {
list = append(list, Blank{})
} else if s == "alias" {
list = append(list, Alias{})
} else {
errString += fmt.Sprintf(" %s", s)
}
Expand Down

0 comments on commit 19d84f7

Please sign in to comment.