Repository: daixiang0/gci (go)

Issue:
Prefixes with capital letters are ignored
With the following config:
```
sections:
  - Standard
  - Default
  - Prefix(github.com/Azure)
```

I would expect that the imports are formatted as so:
```
import (
	"fmt"

	"github.com/golang"

	"github.com/Azure/go-autorest/autorest"
)
```

However, as of v0.4.0 it appears that having a capital letter in the prefix, in this case the `A` in `Azure`, results in the prefix being ignored. So instead I am getting:
```
import (
	"fmt"

	"github.com/Azure/go-autorest/autorest"
	"github.com/golang"
)
```

Pull request:
fix: prefix with upper case letters
Fixes #79

Patch:
diff --git a/main.go b/main.go
index 9496b2e..bb25437 100644
--- a/main.go
+++ b/main.go
@@ -6,7 +6,7 @@ import (
 	"github.com/daixiang0/gci/cmd/gci"
 )
 
-var version = "0.3"
+var version = "0.4.2"
 
 func main() {
 	e := gci.NewExecutor(version)
diff --git a/pkg/gci/gci.go b/pkg/gci/gci.go
index 3c92775..e19bad3 100644
--- a/pkg/gci/gci.go
+++ b/pkg/gci/gci.go
@@ -122,7 +122,7 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err
 		return src, src, nil
 	}
 
-	imports, headEnd, tailStart, tailEnd, err := parse.ParseFile(src)
+	imports, headEnd, tailStart, err := parse.ParseFile(src, file.Path())
 	if err != nil {
 		if errors.Is(err, parse.NoImportError{}) {
 			return src, src, nil
@@ -141,7 +141,7 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err
 	}
 
 	head := src[:headEnd]
-	tail := src[tailStart:tailEnd]
+	tail := src[tailStart:]
 
 	// sort for custom sections
 	allKeys := make([]string, 0, len(result))
diff --git a/pkg/parse/parse.go b/pkg/parse/parse.go
index 738cdd2..df0af3d 100644
--- a/pkg/parse/parse.go
+++ b/pkg/parse/parse.go
@@ -64,22 +64,31 @@ func getImports(imp *ast.ImportSpec) (start, end int, name string) {
 	return
 }
 
-func ParseFile(src []byte) (ImportList, int, int, int, error) {
-	parserMode := parser.Mode(0)
-	parserMode |= parser.ParseComments
+func ParseFile(src []byte, filename string) (ImportList, int, int, error) {
 	fileSet := token.NewFileSet()
-	f, err := parser.ParseFile(fileSet, "", src, parserMode)
+	f, err := parser.ParseFile(fileSet, filename, src, parser.ParseComments)
 	if err != nil {
-		return nil, 0, 0, 0, err
+		return nil, 0, 0, err
 	}
 
 	if len(f.Imports) == 0 {
-		return nil, 0, 0, 0, NoImportError{}
+		return nil, 0, 0, NoImportError{}
 	}
 
+	var headEnd int
+	var tailStart int
+
 	var data ImportList
-	for _, imp := range f.Imports {
+	for i, imp := range f.Imports {
 		start, end, name := getImports(imp)
+
+		if i == 0 {
+			headEnd = start
+		}
+		if i == len(f.Imports)-1 {
+			tailStart = end
+		}
+
 		data = append(data, &GciImports{
 			Start: start,
 			End:   end,
@@ -88,15 +97,11 @@ func ParseFile(src []byte) (ImportList, int, int, int, error) {
 		})
 	}
 
-	headEnd, _, _ := getImports(f.Imports[0])
-	_, tailStart, _ := getImports(f.Imports[len(f.Imports)-1])
-	tailEnd := f.Decls[len(f.Decls)-1].End()
-
 	sort.Sort(data)
-	return data, headEnd, tailStart, int(tailEnd), nil
+	return data, headEnd, tailStart, nil
 }
 
-// isGenerated reports whether the source file is generated code.
+// IsGeneratedFileByComment reports whether the source file is generated code.
 // Using a bit laxer rules than https://golang.org/s/generatedcode to
 // match more generated code.
 // Taken from https://github.com/golangci/golangci-lint.
diff --git a/pkg/section/parser.go b/pkg/section/parser.go
index aa87bc1..94e32af 100644
--- a/pkg/section/parser.go
+++ b/pkg/section/parser.go
@@ -25,10 +25,10 @@ func Parse(data []string) (SectionList, error) {
 			list = append(list, Standard{})
 		} else if s == "newline" {
 			list = append(list, NewLine{})
-		} else if strings.HasPrefix(s, "prefix(") && len(s) > 8 {
-			list = append(list, Custom{s[7 : len(s)-1]})
-		} else if strings.HasPrefix(s, "commentline(") && len(s) > 13 {
-			list = append(list, Custom{s[12 : len(s)-1]})
+		} else if strings.HasPrefix(s, "prefix(") && len(d) > 8 {
+			list = append(list, Custom{d[7 : len(d)-1]})
+		} else if strings.HasPrefix(s, "commentline(") && len(d) > 13 {
+			list = append(list, Custom{d[12 : len(d)-1]})
 		} else {
 			errString += fmt.Sprintf(" %s", s)
 		}


Test patch:
diff --git a/pkg/gci/internal/testdata/comment-in-the-tail.cfg.yaml b/pkg/gci/internal/testdata/comment-in-the-tail.cfg.yaml
new file mode 100644
index 0000000..e666ab9
--- /dev/null
+++ b/pkg/gci/internal/testdata/comment-in-the-tail.cfg.yaml
@@ -0,0 +1,4 @@
+sections:
+  - Standard
+  - Default
+  - Prefix(github.com/daixiang0)
diff --git a/pkg/gci/internal/testdata/comment-in-the-tail.in.go b/pkg/gci/internal/testdata/comment-in-the-tail.in.go
new file mode 100644
index 0000000..2114dbd
--- /dev/null
+++ b/pkg/gci/internal/testdata/comment-in-the-tail.in.go
@@ -0,0 +1,13 @@
+package main
+import (
+	"fmt"
+
+	g "github.com/golang"
+
+	"github.com/daixiang0/gci"
+)
+
+
+type test int
+
+// test
diff --git a/pkg/gci/internal/testdata/comment-in-the-tail.out.go b/pkg/gci/internal/testdata/comment-in-the-tail.out.go
new file mode 100644
index 0000000..2114dbd
--- /dev/null
+++ b/pkg/gci/internal/testdata/comment-in-the-tail.out.go
@@ -0,0 +1,13 @@
+package main
+import (
+	"fmt"
+
+	g "github.com/golang"
+
+	"github.com/daixiang0/gci"
+)
+
+
+type test int
+
+// test
diff --git a/pkg/section/parser_test.go b/pkg/section/parser_test.go
index 66b121d..b8c64b7 100644
--- a/pkg/section/parser_test.go
+++ b/pkg/section/parser_test.go
@@ -25,6 +25,16 @@ func TestParse(t *testing.T) {
 			expectedSection: SectionList{Custom{"go"}},
 			expectedError:   nil,
 		},
+		{
+			input:           []string{"prefix(go-UPPER-case)"},
+			expectedSection: SectionList{Custom{"go-UPPER-case"}},
+			expectedError:   nil,
+		},
+		{
+			input:           []string{"PREFIX(go-UPPER-case)"},
+			expectedSection: SectionList{Custom{"go-UPPER-case"}},
+			expectedError:   nil,
+		},
 		{
 			input:           []string{"prefix("},
 			expectedSection: nil,