Skip to content

Commit

Permalink
revert section order (#72)
Browse files Browse the repository at this point in the history
* revert section order

Signed-off-by: Loong Dai <loong.dai@intel.com>

* delete /standard.go

Signed-off-by: Loong Dai <loong.dai@intel.com>
  • Loading branch information
daixiang0 authored Jul 11, 2022
1 parent 856d805 commit a0b03a1
Show file tree
Hide file tree
Showing 17 changed files with 58 additions and 196 deletions.
48 changes: 23 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ GCI splits all import blocks into different sections, now support three section
- custom: Custom section, use full and the longest match(match full string first, if multiple matches, use the longest one)
- default: All rest import blocks

The priority is standard>custom>default, all sections sort alphabetically inside.
The priority is standard > default > custom, all sections sort alphabetically inside.

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

Expand Down Expand Up @@ -49,13 +49,12 @@ Aliases:

Flags:
-d, --debug Enables debug output from the formatter
-h, --help help for print
-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].
Std | Standard - Captures all standard packages if they do not match another section
Prefix(github.com/daixiang0) | pkgPrefix(github.com/daixiang0) - Groups all imports with the specified Prefix. Imports will be matched to the longest Prefix.
Def | Default - Contains all imports that could not be matched to another section type
[DEPRECATED] Comment(your text here) | CommentLine(your text here) - Prints the specified indented comment
[DEPRECATED] NL | NewLine - Prints an empty line
-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. 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.
default - default section, contains all rest imports (default [standard,default])
--skip-generated Skip generated files
```

```shell
Expand All @@ -71,12 +70,11 @@ Aliases:
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 (). 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].
Std | Standard - Captures all standard packages if they do not match another section
Prefix(github.com/daixiang0) | pkgPrefix(github.com/daixiang0) - Groups all imports with the specified Prefix. Imports will be matched to the longest Prefix.
Def | Default - Contains all imports that could not be matched to another section type
[DEPRECATED] Comment(your text here) | CommentLine(your text here) - Prints the specified indented comment
[DEPRECATED] NL | NewLine - Prints an empty line
-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].
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.
default - default section, contains all rest imports (default [standard,default])
--skip-generated Skip generated files
```

```shell
Expand All @@ -88,13 +86,13 @@ Usage:

Flags:
-d, --debug Enables debug output from the formatter
-h, --help help for diff
-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].
Std | Standard - Captures all standard packages if they do not match another section
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.
Def | Default - Contains all imports that could not be matched to another section type
[DEPRECATED] Comment(your text here) | CommentLine(your text here) - Prints the specified indented comment
[DEPRECATED] NL | NewLine - Prints an empty line
-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. 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.
default - default section, contains all rest imports (default [standard,default])
--skip-generated Skip generated files

```

### Old style
Expand Down Expand Up @@ -140,9 +138,9 @@ package main
import (
"fmt"

"github.com/daixiang0/gci"

"golang.org/x/tools"

"github.com/daixiang0/gci"
)
```

Expand All @@ -164,9 +162,9 @@ package main
import (
"fmt"

"github.com/daixiang0/gci"

go "github.com/golang"

"github.com/daixiang0/gci"
)
```

Expand Down
12 changes: 5 additions & 7 deletions cmd/gci/gcicommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,14 @@ 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 (). 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].
Std | Standard - Captures all standard packages if they do not match another section
Prefix(github.com/daixiang0) | pkgPrefix(github.com/daixiang0) - Groups all imports with the specified Prefix. Imports will be matched to the longest Prefix.
Def | Default - Contains all imports that could not be matched to another section type
[DEPRECATED] Comment(your text here) | CommentLine(your text here) - Prints the specified indented comment
[DEPRECATED] NL | NewLine - Prints an empty line`
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].
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.
default - default section, contains all rest imports`

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

sectionStrings = cmd.Flags().StringSliceP("section", "s", nil, sectionHelp)
sectionStrings = cmd.Flags().StringSliceP("section", "s", section.DefaultSections().String(), sectionHelp)

// deprecated
noInlineComments = cmd.Flags().Bool("NoInlineComments", false, "Drops inline comments while formatting")
Expand Down
26 changes: 14 additions & 12 deletions pkg/gci/gci.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err
firstWithIndex := true

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

if len(result["default"]) > 0 {
for _, d := range result["default"] {
AddIndent(&body, &firstWithIndex)
body = append(body, src[d.Start:d.End]...)
}

if len(customKeys) > 0 {
body = append(body, utils.Linebreak)
}
}

if len(customKeys) > 0 {
sort.Sort(sort.Reverse(sort.StringSlice(customKeys)))
for _, k := range customKeys {
Expand All @@ -172,17 +183,8 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err
body = append(body, utils.Linebreak)
}

// no default section, remove breakline in the end
if len(result["default"]) == 0 {
body = body[:len(body)-1]
}
}

if len(result["default"]) > 0 {
for _, d := range result["default"] {
AddIndent(&body, &firstWithIndex)
body = append(body, src[d.Start:d.End]...)
}
// remove breakline in the end
body = body[:len(body)-1]
}

var totalLen int
Expand Down
4 changes: 2 additions & 2 deletions pkg/gci/internal/testdata/already-good.out.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main
import (
"fmt"

"github.com/daixiang0/gci"

g "github.com/golang"

"github.com/daixiang0/gci"
)
4 changes: 2 additions & 2 deletions pkg/gci/internal/testdata/drop-prefix-comments.out.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
// Std imports
"os"

"github.com/daixiang0/gci"

// Github
"github.com/local/dlib/dexec"

"github.com/daixiang0/gci"
// Github
)
4 changes: 2 additions & 2 deletions pkg/gci/internal/testdata/nolint.out.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"fmt"

_ "github.com/daixiang0/gci" //nolint:depguard

"github.com/forbidden/pkg" //nolint:depguard

_ "github.com/daixiang0/gci" //nolint:depguard
)
4 changes: 2 additions & 2 deletions pkg/gci/internal/testdata/number-in-alias.out.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main
import (
"fmt"

"github.com/daixiang0/gci"

go_V1 "github.com/golang"

"github.com/daixiang0/gci"
)
4 changes: 2 additions & 2 deletions pkg/gci/internal/testdata/simple-case.out.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main
import (
"fmt"

"github.com/daixiang0/gci"

"golang.org/x/tools"

"github.com/daixiang0/gci"
)
4 changes: 2 additions & 2 deletions pkg/gci/internal/testdata/whitespace-test.out.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"fmt"

alias "github.com/daixiang0/gci"

"github.com/golang" // golang

alias "github.com/daixiang0/gci"
)
4 changes: 2 additions & 2 deletions pkg/gci/internal/testdata/with-above-comment-and-alias.out.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package main
import (
"fmt"

"github.com/daixiang0/gci"

// golang
_ "github.com/golang"

"github.com/daixiang0/gci"
)
4 changes: 2 additions & 2 deletions pkg/gci/internal/testdata/with-comment-and-alias.out.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main
import (
"fmt"

"github.com/daixiang0/gci"

_ "github.com/golang" // golang

"github.com/daixiang0/gci"
)
19 changes: 0 additions & 19 deletions pkg/gci/specificity/default.go

This file was deleted.

24 changes: 0 additions & 24 deletions pkg/gci/specificity/match.go

This file was deleted.

19 changes: 0 additions & 19 deletions pkg/gci/specificity/mismatch.go

This file was deleted.

26 changes: 0 additions & 26 deletions pkg/gci/specificity/specificity.go

This file was deleted.

29 changes: 0 additions & 29 deletions pkg/gci/specificity/specificity_test.go

This file was deleted.

19 changes: 0 additions & 19 deletions pkg/gci/specificity/standard.go

This file was deleted.

0 comments on commit a0b03a1

Please sign in to comment.