Skip to content

Commit a0b03a1

Browse files
authored
revert section order (#72)
* revert section order Signed-off-by: Loong Dai <[email protected]> * delete /standard.go Signed-off-by: Loong Dai <[email protected]>
1 parent 856d805 commit a0b03a1

17 files changed

+58
-196
lines changed

README.md

+23-25
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ GCI splits all import blocks into different sections, now support three section
1616
- custom: Custom section, use full and the longest match(match full string first, if multiple matches, use the longest one)
1717
- default: All rest import blocks
1818

19-
The priority is standard>custom>default, all sections sort alphabetically inside.
19+
The priority is standard > default > custom, all sections sort alphabetically inside.
2020

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

@@ -49,13 +49,12 @@ Aliases:
4949

5050
Flags:
5151
-d, --debug Enables debug output from the formatter
52-
-h, --help help for print
53-
-s, --section strings Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). A section can contain a Prefix and a Suffix section which is delimited by ":". These sections can be used for formatting and will only be rendered if the main section contains an entry. The Section order is the same as below, default value is [Standard,Default].
54-
Std | Standard - Captures all standard packages if they do not match another section
55-
Prefix(github.com/daixiang0) | pkgPrefix(github.com/daixiang0) - Groups all imports with the specified Prefix. Imports will be matched to the longest Prefix.
56-
Def | Default - Contains all imports that could not be matched to another section type
57-
[DEPRECATED] Comment(your text here) | CommentLine(your text here) - Prints the specified indented comment
58-
[DEPRECATED] NL | NewLine - Prints an empty line
52+
-h, --help help for write
53+
-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. The default value is [standard,default].
54+
standard - standard section that Golang provides officially, like "fmt"
55+
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix.
56+
default - default section, contains all rest imports (default [standard,default])
57+
--skip-generated Skip generated files
5958
```
6059

6160
```shell
@@ -71,12 +70,11 @@ Aliases:
7170
Flags:
7271
-d, --debug Enables debug output from the formatter
7372
-h, --help help for write
74-
-s, --section strings Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). A section can contain a Prefix and a Suffix section which is delimited by ":". These sections can be used for formatting and will only be rendered if the main section contains an entry. The Section order is the same as below, default value is [Standard,Default].
75-
Std | Standard - Captures all standard packages if they do not match another section
76-
Prefix(github.com/daixiang0) | pkgPrefix(github.com/daixiang0) - Groups all imports with the specified Prefix. Imports will be matched to the longest Prefix.
77-
Def | Default - Contains all imports that could not be matched to another section type
78-
[DEPRECATED] Comment(your text here) | CommentLine(your text here) - Prints the specified indented comment
79-
[DEPRECATED] NL | NewLine - Prints an empty line
73+
-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. The default value is [standard,default].
74+
standard - standard section thatolang provides officially, like "fmt"
75+
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix.
76+
default - default section, contains all rest imports (default [standard,default])
77+
--skip-generated Skip generated files
8078
```
8179

8280
```shell
@@ -88,13 +86,13 @@ Usage:
8886

8987
Flags:
9088
-d, --debug Enables debug output from the formatter
91-
-h, --help help for diff
92-
-s, --section strings Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). A section can contain a Prefix and a Suffix section which is delimited by ":". These sections can be used for formatting and will only be rendered if the main section contains an entry. The Section order is the same as below, default value is [Standard,Default].
93-
Std | Standard - Captures all standard packages if they do not match another section
94-
Prefix(github.com/daixiang0) | pkgPrefix(github.com/daixiang0) - Groups all imports with the specified Prefix. Imports will be matched to the full and longest Prefix. All groups are in alphabetical order.
95-
Def | Default - Contains all imports that could not be matched to another section type
96-
[DEPRECATED] Comment(your text here) | CommentLine(your text here) - Prints the specified indented comment
97-
[DEPRECATED] NL | NewLine - Prints an empty line
89+
-h, --help help for write
90+
-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. The default value is [standard,default].
91+
standard - standard section thatolang provides officially, like "fmt"
92+
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix.
93+
default - default section, contains all rest imports (default [standard,default])
94+
--skip-generated Skip generated files
95+
9896
```
9997

10098
### Old style
@@ -140,9 +138,9 @@ package main
140138
import (
141139
"fmt"
142140

143-
"github.com/daixiang0/gci"
144-
145141
"golang.org/x/tools"
142+
143+
"github.com/daixiang0/gci"
146144
)
147145
```
148146

@@ -164,9 +162,9 @@ package main
164162
import (
165163
"fmt"
166164

167-
"github.com/daixiang0/gci"
168-
169165
go "github.com/golang"
166+
167+
"github.com/daixiang0/gci"
170168
)
171169
```
172170

cmd/gci/gcicommand.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,14 @@ func (e *Executor) newGciCommand(use, short, long string, aliases []string, stdI
4646

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

49-
sectionHelp := `Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). A section can contain a Prefix and a Suffix section which is delimited by ":". These sections can be used for formatting and will only be rendered if the main section contains an entry. The Section order is the same as below, default value is [Standard,Default].
50-
Std | Standard - Captures all standard packages if they do not match another section
51-
Prefix(github.com/daixiang0) | pkgPrefix(github.com/daixiang0) - Groups all imports with the specified Prefix. Imports will be matched to the longest Prefix.
52-
Def | Default - Contains all imports that could not be matched to another section type
53-
[DEPRECATED] Comment(your text here) | CommentLine(your text here) - Prints the specified indented comment
54-
[DEPRECATED] NL | NewLine - Prints an empty line`
49+
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. The default value is [standard,default].
50+
standard - standard section that Golang provides officially, like "fmt"
51+
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix.
52+
default - default section, contains all rest imports`
5553

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

58-
sectionStrings = cmd.Flags().StringSliceP("section", "s", nil, sectionHelp)
56+
sectionStrings = cmd.Flags().StringSliceP("section", "s", section.DefaultSections().String(), sectionHelp)
5957

6058
// deprecated
6159
noInlineComments = cmd.Flags().Bool("NoInlineComments", false, "Drops inline comments while formatting")

pkg/gci/gci.go

+14-12
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err
151151
firstWithIndex := true
152152

153153
var body []byte
154-
// order: standard > custom > rest
154+
// order: standard > default > custom
155155
if len(result["standard"]) > 0 {
156156
for _, d := range result["standard"] {
157157
AddIndent(&body, &firstWithIndex)
@@ -162,6 +162,17 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err
162162
}
163163
}
164164

165+
if len(result["default"]) > 0 {
166+
for _, d := range result["default"] {
167+
AddIndent(&body, &firstWithIndex)
168+
body = append(body, src[d.Start:d.End]...)
169+
}
170+
171+
if len(customKeys) > 0 {
172+
body = append(body, utils.Linebreak)
173+
}
174+
}
175+
165176
if len(customKeys) > 0 {
166177
sort.Sort(sort.Reverse(sort.StringSlice(customKeys)))
167178
for _, k := range customKeys {
@@ -172,17 +183,8 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err
172183
body = append(body, utils.Linebreak)
173184
}
174185

175-
// no default section, remove breakline in the end
176-
if len(result["default"]) == 0 {
177-
body = body[:len(body)-1]
178-
}
179-
}
180-
181-
if len(result["default"]) > 0 {
182-
for _, d := range result["default"] {
183-
AddIndent(&body, &firstWithIndex)
184-
body = append(body, src[d.Start:d.End]...)
185-
}
186+
// remove breakline in the end
187+
body = body[:len(body)-1]
186188
}
187189

188190
var totalLen int

pkg/gci/internal/testdata/already-good.out.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package main
22
import (
33
"fmt"
44

5-
"github.com/daixiang0/gci"
6-
75
g "github.com/golang"
6+
7+
"github.com/daixiang0/gci"
88
)

pkg/gci/internal/testdata/drop-prefix-comments.out.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66
// Std imports
77
"os"
88

9-
"github.com/daixiang0/gci"
10-
119
// Github
1210
"github.com/local/dlib/dexec"
11+
12+
"github.com/daixiang0/gci"
1313
// Github
1414
)

pkg/gci/internal/testdata/nolint.out.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package main
33
import (
44
"fmt"
55

6-
_ "github.com/daixiang0/gci" //nolint:depguard
7-
86
"github.com/forbidden/pkg" //nolint:depguard
7+
8+
_ "github.com/daixiang0/gci" //nolint:depguard
99
)

pkg/gci/internal/testdata/number-in-alias.out.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package main
22
import (
33
"fmt"
44

5-
"github.com/daixiang0/gci"
6-
75
go_V1 "github.com/golang"
6+
7+
"github.com/daixiang0/gci"
88
)

pkg/gci/internal/testdata/simple-case.out.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package main
22
import (
33
"fmt"
44

5-
"github.com/daixiang0/gci"
6-
75
"golang.org/x/tools"
6+
7+
"github.com/daixiang0/gci"
88
)

pkg/gci/internal/testdata/whitespace-test.out.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package main
33
import (
44
"fmt"
55

6-
alias "github.com/daixiang0/gci"
7-
86
"github.com/golang" // golang
7+
8+
alias "github.com/daixiang0/gci"
99
)

pkg/gci/internal/testdata/with-above-comment-and-alias.out.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package main
22
import (
33
"fmt"
44

5-
"github.com/daixiang0/gci"
6-
75
// golang
86
_ "github.com/golang"
7+
8+
"github.com/daixiang0/gci"
99
)

pkg/gci/internal/testdata/with-comment-and-alias.out.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package main
22
import (
33
"fmt"
44

5-
"github.com/daixiang0/gci"
6-
75
_ "github.com/golang" // golang
6+
7+
"github.com/daixiang0/gci"
88
)

pkg/gci/specificity/default.go

-19
This file was deleted.

pkg/gci/specificity/match.go

-24
This file was deleted.

pkg/gci/specificity/mismatch.go

-19
This file was deleted.

pkg/gci/specificity/specificity.go

-26
This file was deleted.

pkg/gci/specificity/specificity_test.go

-29
This file was deleted.

pkg/gci/specificity/standard.go

-19
This file was deleted.

0 commit comments

Comments
 (0)