Skip to content

Commit 19d84f7

Browse files
authored
Add Alias Section (#178)
Signed-off-by: ryokusnadi <[email protected]>
1 parent 75159c7 commit 19d84f7

File tree

6 files changed

+107
-8
lines changed

6 files changed

+107
-8
lines changed

README.md

+43-6
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ GCI splits all import blocks into different sections, now support five section t
3434
- default: All rest import blocks
3535
- blank: Put blank imports together in a separate group
3636
- dot: Put dot imports together in a separate group
37+
- alias: Put alias imports together in a separate group
3738

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

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

@@ -88,12 +89,13 @@ Flags:
8889
--custom-order Enable custom order of sections
8990
-d, --debug Enables debug output from the formatter
9091
-h, --help help for print
91-
-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].
92+
-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].
9293
standard - standard section that Go provides officially, like "fmt"
9394
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)
9495
default - default section, contains all rest imports
9596
blank - blank section, contains all blank imports.
9697
dot - dot section, contains all dot imports. (default [standard,default])
98+
alias - alias section, contains all alias imports.
9799
--skip-generated Skip generated files
98100
--skip-vendor Skip files inside vendor directory
99101
```
@@ -112,12 +114,13 @@ Flags:
112114
--custom-order Enable custom order of sections
113115
-d, --debug Enables debug output from the formatter
114116
-h, --help help for write
115-
-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].
117+
-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].
116118
standard - standard section that Go provides officially, like "fmt"
117119
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)
118120
default - default section, contains all rest imports
119121
blank - blank section, contains all blank imports.
120122
dot - dot section, contains all dot imports. (default [standard,default])
123+
alias - alias section, contains all alias imports.
121124
--skip-generated Skip generated files
122125
--skip-vendor Skip files inside vendor directory
123126
```
@@ -133,11 +136,12 @@ Flags:
133136
--custom-order Enable custom order of sections
134137
-d, --debug Enables debug output from the formatter
135138
-h, --help help for list
136-
-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].
139+
-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].
137140
standard - standard section that Go provides officially, like "fmt"
138141
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)
139142
default - default section, contains all rest imports
140143
blank - blank section, contains all blank imports.
144+
alias - alias section, contains all alias imports.
141145
dot - dot section, contains all dot imports. (default [standard,default])
142146
--skip-generated Skip generated files
143147
--skip-vendor Skip files inside vendor directory
@@ -154,11 +158,12 @@ Flags:
154158
--custom-order Enable custom order of sections
155159
-d, --debug Enables debug output from the formatter
156160
-h, --help help for diff
157-
-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].
161+
-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].
158162
standard - standard section that Go provides officially, like "fmt"
159163
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)
160164
default - default section, contains all rest imports
161165
blank - blank section, contains all blank imports.
166+
alias - alias section, contains all alias imports.
162167
dot - dot section, contains all dot imports. (default [standard,default])
163168
--skip-generated Skip generated files
164169
--skip-vendor Skip files inside vendor directory
@@ -284,6 +289,38 @@ import (
284289
)
285290
```
286291
292+
### with alias grouping enabled
293+
294+
```go
295+
package main
296+
297+
import (
298+
testing "github.com/daixiang0/test"
299+
"fmt"
300+
301+
g "github.com/golang"
302+
303+
"github.com/daixiang0/gci"
304+
"github.com/daixiang0/gci/subtest"
305+
)
306+
```
307+
308+
to
309+
310+
```go
311+
package main
312+
313+
import (
314+
"fmt"
315+
316+
"github.com/daixiang0/gci"
317+
"github.com/daixiang0/gci/subtest"
318+
319+
testing "github.com/daixiang0/test"
320+
g "github.com/golang"
321+
)
322+
```
323+
287324
## TODO
288325
289326
- Ensure only one blank between `Name` and `Path` in an import block

cmd/gci/gcicommand.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,13 @@ func (e *Executor) newGciCommand(use, short, long string, aliases []string, stdI
4848

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

51-
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].
51+
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].
5252
standard - standard section that Go provides officially, like "fmt"
5353
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)
5454
default - default section, contains all rest imports
5555
blank - blank section, contains all blank imports.
56-
dot - dot section, contains all dot imports.`
56+
dot - dot section, contains all dot imports.
57+
alias - alias section, contains all alias imports.`
5758

5859
skipGenerated = cmd.Flags().Bool("skip-generated", false, "Skip generated files")
5960
skipVendor = cmd.Flags().Bool("skip-vendor", false, "Skip files inside vendor directory")

pkg/config/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var defaultOrder = map[string]int{
1515
section.CustomType: 2,
1616
section.BlankType: 3,
1717
section.DotType: 4,
18+
section.AliasType: 5,
1819
}
1920

2021
type BoolConfig struct {

pkg/gci/testdata.go

+33
Original file line numberDiff line numberDiff line change
@@ -1240,6 +1240,39 @@ import (
12401240
"github.com/daixiang0/gci"
12411241
"github.com/daixiang0/gci/subtest"
12421242
)
1243+
`,
1244+
},
1245+
{
1246+
"alias",
1247+
1248+
`sections:
1249+
- Standard
1250+
- Default
1251+
- Alias
1252+
`,
1253+
`package main
1254+
1255+
import (
1256+
testing "github.com/daixiang0/test"
1257+
"fmt"
1258+
1259+
g "github.com/golang"
1260+
1261+
"github.com/daixiang0/gci"
1262+
"github.com/daixiang0/gci/subtest"
1263+
)
1264+
`,
1265+
`package main
1266+
1267+
import (
1268+
"fmt"
1269+
1270+
"github.com/daixiang0/gci"
1271+
"github.com/daixiang0/gci/subtest"
1272+
1273+
testing "github.com/daixiang0/test"
1274+
g "github.com/golang"
1275+
)
12431276
`,
12441277
},
12451278
}

pkg/section/alias.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package section
2+
3+
import (
4+
"github.com/daixiang0/gci/pkg/parse"
5+
"github.com/daixiang0/gci/pkg/specificity"
6+
)
7+
8+
type Alias struct{}
9+
10+
const AliasType = "alias"
11+
12+
func (b Alias) MatchSpecificity(spec *parse.GciImports) specificity.MatchSpecificity {
13+
if spec.Name != "." && spec.Name != "_" && spec.Name != "" {
14+
return specificity.NameMatch{}
15+
}
16+
return specificity.MisMatch{}
17+
}
18+
19+
func (b Alias) String() string {
20+
return AliasType
21+
}
22+
23+
func (b Alias) Type() string {
24+
return AliasType
25+
}

pkg/section/parser.go

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ func Parse(data []string) (SectionList, error) {
3333
list = append(list, Dot{})
3434
} else if s == "blank" {
3535
list = append(list, Blank{})
36+
} else if s == "alias" {
37+
list = append(list, Alias{})
3638
} else {
3739
errString += fmt.Sprintf(" %s", s)
3840
}

0 commit comments

Comments
 (0)