You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+58-46
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,26 @@ GCI, a tool that controls golang package import order and makes it always determ
4
4
5
5
The desired output format is highly configurable and allows for more custom formatting than `goimport` does.
6
6
7
+
GCI considers a import block based on AST as below:
8
+
```
9
+
Doc
10
+
Name Path Comment
11
+
```
12
+
All comments will keep as they were, except the independent comment blocks(line breaks before and after).
13
+
14
+
GCI splits all import blocks into different sections, now support three section type:
15
+
- standard: Golang official imports, like "fmt"
16
+
- custom: Custom section, use full and the longest match(match full string first, if multiple matches, use the longest one)
17
+
- default: All rest import blocks
18
+
19
+
The priority is standard>custom>default, all sections sort alphabetically inside.
20
+
21
+
All import blocks use one TAB(`\t`) as Indent.
22
+
23
+
**Note**:
24
+
25
+
`nolint` is hard to handle at section level, GCI will consider it as a single comment.
26
+
7
27
## Download
8
28
9
29
```shell
@@ -28,18 +48,14 @@ Aliases:
28
48
print, output
29
49
30
50
Flags:
31
-
--NoInlineComments Drops inline comments while formatting
32
-
--NoPrefixComments Drops comment lines above an import statement while formatting
33
-
-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.
34
-
Comment(your text here) | CommentLine(your text here) - Prints the specified indented comment
35
-
Def | Default - Contains all imports that could not be matched to another section type
36
-
NL | NewLine - Prints an empty line
37
-
Prefix(gitlab.com/myorg) | pkgPrefix(gitlab.com/myorg) - Groups all imports with the specified Prefix. Imports will be matched to the longest Prefix.
38
-
Std | Standard - Captures all standard packages if they do not match another section
39
-
(default [Standard,Default])
40
-
-x, --SectionSeparator strings SectionSeparators are inserted between Sections (default [NewLine])
41
-
--SkipGeneratedFiles Don't process generated files
42
-
-h, --help help for print
51
+
-d, --debug Enables debug output from the formatter
52
+
-h, --help helpfor 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
43
59
```
44
60
45
61
```shell
@@ -53,18 +69,14 @@ Aliases:
53
69
write, overwrite
54
70
55
71
Flags:
56
-
--NoInlineComments Drops inline comments while formatting
57
-
--NoPrefixComments Drops comment lines above an import statement while formatting
58
-
-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.
59
-
Comment(your text here) | CommentLine(your text here) - Prints the specified indented comment
60
-
Def | Default - Contains all imports that could not be matched to another section type
61
-
NL | NewLine - Prints an empty line
62
-
Prefix(gitlab.com/myorg) | pkgPrefix(gitlab.com/myorg) - Groups all imports with the specified Prefix. Imports will be matched to the longest Prefix.
63
-
Std | Standard - Captures all standard packages if they do not match another section
64
-
(default [Standard,Default])
65
-
-x, --SectionSeparator strings SectionSeparators are inserted between Sections (default [NewLine])
66
-
--SkipGeneratedFiles Don't process generated files
67
-
-h, --help helpfor write
72
+
-d, --debug Enables debug output from the formatter
73
+
-h, --help helpfor 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
68
80
```
69
81
70
82
```shell
@@ -75,19 +87,14 @@ Usage:
75
87
gci diff path... [flags]
76
88
77
89
Flags:
78
-
--NoInlineComments Drops inline comments while formatting
79
-
--NoPrefixComments Drops comment lines above an import statement while formatting
80
-
-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.
81
-
Comment(your text here) | CommentLine(your text here) - Prints the specified indented comment
82
-
Def | Default - Contains all imports that could not be matched to another section type
83
-
NL | NewLine - Prints an empty line
84
-
Prefix(gitlab.com/myorg) | pkgPrefix(gitlab.com/myorg) - Groups all imports with the specified Prefix. Imports will be matched to the longest Prefix.
85
-
Std | Standard - Captures all standard packages if they do not match another section
86
-
(default [Standard,Default])
87
-
-x, --SectionSeparator strings SectionSeparators are inserted between Sections (default [NewLine])
88
-
--SkipGeneratedFiles Don't process generated files
89
-
-d, --debug Enables debug output from the formatter
90
-
-h, --help help for diff
90
+
-d, --debug Enables debug output from the formatter
91
+
-h, --help helpfor 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
91
98
```
92
99
93
100
### Old style
@@ -107,21 +114,21 @@ Flags:
107
114
108
115
**Note**::
109
116
110
-
The old style is only for local tests, `golangci-lint` uses new style.
117
+
The old style is only forlocal tests, will be deprecated, please uses new style, `golangci-lint` uses new style as well.
111
118
112
119
## Examples
113
120
114
-
Run `gci write --Section Standard --Section Default --Section "Prefix(github.com/daixiang0/gci)" main.go` and you will handle following cases:
121
+
Run `gci write -s standard -s default -s "prefix(github.com/daixiang0/gci)" main.go` and you will handle following cases:
115
122
116
123
### simple case
117
124
118
125
```go
119
126
package main
120
127
import (
121
128
"golang.org/x/tools"
122
-
129
+
123
130
"fmt"
124
-
131
+
125
132
"github.com/daixiang0/gci"
126
133
)
127
134
```
@@ -131,11 +138,11 @@ to
131
138
```go
132
139
package main
133
140
import (
134
-
"fmt"
141
+
"fmt"
135
142
136
-
"golang.org/x/tools"
143
+
"github.com/daixiang0/gci"
137
144
138
-
"github.com/daixiang0/gci"
145
+
"golang.org/x/tools"
139
146
)
140
147
```
141
148
@@ -157,12 +164,17 @@ package main
157
164
import (
158
165
"fmt"
159
166
160
-
go "github.com/golang"
161
-
162
167
"github.com/daixiang0/gci"
168
+
169
+
go "github.com/golang"
163
170
)
164
171
```
165
172
166
173
## TODO
167
174
175
+
- Ensure only one blank between `Name` and `Path`in an import block
176
+
- Ensure only one blank between `Path` and `Comment`in an import block
177
+
- Format comments
168
178
- Add more testcases
179
+
- Support imports completion (please use `goimports` first then use GCI)
@@ -45,18 +44,27 @@ func (e *Executor) newGciCommand(use, short, long string, aliases []string, stdI
45
44
// register command as subcommand
46
45
e.rootCmd.AddCommand(&cmd)
47
46
48
-
sectionHelp:="Sections define how inputs will be processed. "+
49
-
"Section names are case-insensitive and may contain parameters in (). "+
50
-
fmt.Sprintf("A section can contain a Prefix and a Suffix section which is delimited by %q. ", constants.SectionSeparator) +
51
-
"These sections can be used for formatting and will only be rendered if the main section contains an entry."+
52
-
"\n"+
53
-
sectionsPkg.SectionParserInst.SectionHelpTexts()
54
-
// add flags
55
47
debug=cmd.Flags().BoolP("debug", "d", false, "Enables debug output from the formatter")
48
+
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
sectionSeparatorStrings=cmd.Flags().StringSliceP("SectionSeparator", "x", gci.DefaultSectionSeparators().String(), "SectionSeparators are inserted between Sections")
60
-
skipGeneratedFiles=cmd.Flags().Bool("SkipGeneratedFiles", false, "Don't process generated files")
64
+
cmd.Flags().MarkDeprecated("NoPrefixComments", "Drops inline comments while formatting")
65
+
sectionSeparatorStrings=cmd.Flags().StringSliceP("SectionSeparator", "x", section.DefaultSectionSeparators().String(), "SectionSeparators are inserted between Sections")
66
+
cmd.Flags().MarkDeprecated("SectionSeparator", "Drops inline comments while formatting")
67
+
cmd.Flags().MarkDeprecated("x", "Drops inline comments while formatting")
0 commit comments