Repository: daixiang0/gci
Pull request #108 (2022-09-01 02:06:47)

support format blank
Signed-off-by: Loong <loong.dai@intel.com>


Patch:
diff --git a/pkg/gci/gci.go b/pkg/gci/gci.go
index 37c9ee1..2980e7d 100644
--- a/pkg/gci/gci.go
+++ b/pkg/gci/gci.go
@@ -4,6 +4,7 @@ import (
 	"bytes"
 	"errors"
 	"fmt"
+	goFormat "go/format"
 	"os"
 	"sync"
 
@@ -172,11 +173,6 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err
 		}
 	}
 
-	// remove breakline in the end
-	for body[len(body)-1] == utils.Linebreak {
-		body = body[:len(body)-1]
-	}
-
 	if tail[0] != utils.Linebreak {
 		body = append(body, utils.Linebreak)
 	}
@@ -192,6 +188,11 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err
 		i += copy(dist[i:], s)
 	}
 
+	dist, err = goFormat.Source(dist)
+	if err != nil {
+		return nil, nil, err
+	}
+
 	return src, dist, nil
 }
 


Test patch:
diff --git a/pkg/gci/internal/testdata/already-good.in.go b/pkg/gci/internal/testdata/already-good.in.go
index 89de7a0..26a4c1e 100644
--- a/pkg/gci/internal/testdata/already-good.in.go
+++ b/pkg/gci/internal/testdata/already-good.in.go
@@ -1,4 +1,5 @@
 package main
+
 import (
 	"fmt"
 
diff --git a/pkg/gci/internal/testdata/already-good.out.go b/pkg/gci/internal/testdata/already-good.out.go
index 89de7a0..26a4c1e 100644
--- a/pkg/gci/internal/testdata/already-good.out.go
+++ b/pkg/gci/internal/testdata/already-good.out.go
@@ -1,4 +1,5 @@
 package main
+
 import (
 	"fmt"
 
diff --git a/pkg/gci/internal/testdata/blank-format.cfg.yaml b/pkg/gci/internal/testdata/blank-format.cfg.yaml
new file mode 120000
index 0000000..b0db9e7
--- /dev/null
+++ b/pkg/gci/internal/testdata/blank-format.cfg.yaml
@@ -0,0 +1,1 @@
+common.cfg.yaml
\ No newline at end of file
diff --git a/pkg/gci/internal/testdata/blank-format.in.go b/pkg/gci/internal/testdata/blank-format.in.go
new file mode 100644
index 0000000..6712762
--- /dev/null
+++ b/pkg/gci/internal/testdata/blank-format.in.go
@@ -0,0 +1,9 @@
+package main
+import (
+	"fmt"
+
+  // comment
+	g  "github.com/golang"    // comment
+
+	"github.com/daixiang0/gci"
+)
diff --git a/pkg/gci/internal/testdata/blank-format.out.go b/pkg/gci/internal/testdata/blank-format.out.go
new file mode 100644
index 0000000..28402ae
--- /dev/null
+++ b/pkg/gci/internal/testdata/blank-format.out.go
@@ -0,0 +1,10 @@
+package main
+
+import (
+	"fmt"
+
+	// comment
+	g "github.com/golang" // comment
+
+	"github.com/daixiang0/gci"
+)
diff --git a/pkg/gci/internal/testdata/comment-in-the-tail.in.go b/pkg/gci/internal/testdata/comment-in-the-tail.in.go
index 2114dbd..9d0f407 100644
--- a/pkg/gci/internal/testdata/comment-in-the-tail.in.go
+++ b/pkg/gci/internal/testdata/comment-in-the-tail.in.go
@@ -1,4 +1,5 @@
 package main
+
 import (
 	"fmt"
 
@@ -7,7 +8,6 @@ import (
 	"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
index 2114dbd..9d0f407 100644
--- a/pkg/gci/internal/testdata/comment-in-the-tail.out.go
+++ b/pkg/gci/internal/testdata/comment-in-the-tail.out.go
@@ -1,4 +1,5 @@
 package main
+
 import (
 	"fmt"
 
@@ -7,7 +8,6 @@ import (
 	"github.com/daixiang0/gci"
 )
 
-
 type test int
 
 // test
diff --git a/pkg/gci/internal/testdata/custom-order.in.go b/pkg/gci/internal/testdata/custom-order.in.go
index 5768d92..012efe9 100644
--- a/pkg/gci/internal/testdata/custom-order.in.go
+++ b/pkg/gci/internal/testdata/custom-order.in.go
@@ -1,4 +1,5 @@
 package main
+
 import (
 	"fmt"
 
diff --git a/pkg/gci/internal/testdata/custom-order.out.go b/pkg/gci/internal/testdata/custom-order.out.go
index d55c3c0..74acb7f 100644
--- a/pkg/gci/internal/testdata/custom-order.out.go
+++ b/pkg/gci/internal/testdata/custom-order.out.go
@@ -1,4 +1,5 @@
 package main
+
 import (
 	"github.com/daixiang0/a"
 
diff --git a/pkg/gci/internal/testdata/default-order.in.go b/pkg/gci/internal/testdata/default-order.in.go
index 5768d92..012efe9 100644
--- a/pkg/gci/internal/testdata/default-order.in.go
+++ b/pkg/gci/internal/testdata/default-order.in.go
@@ -1,4 +1,5 @@
 package main
+
 import (
 	"fmt"
 
diff --git a/pkg/gci/internal/testdata/default-order.out.go b/pkg/gci/internal/testdata/default-order.out.go
index 5768d92..012efe9 100644
--- a/pkg/gci/internal/testdata/default-order.out.go
+++ b/pkg/gci/internal/testdata/default-order.out.go
@@ -1,4 +1,5 @@
 package main
+
 import (
 	"fmt"
 
diff --git a/pkg/gci/internal/testdata/dot-and-blank.in.go b/pkg/gci/internal/testdata/dot-and-blank.in.go
index 7ffaec3..ac8e762 100644
--- a/pkg/gci/internal/testdata/dot-and-blank.in.go
+++ b/pkg/gci/internal/testdata/dot-and-blank.in.go
@@ -1,4 +1,5 @@
 package main
+
 import (
 	"fmt"
 
diff --git a/pkg/gci/internal/testdata/dot-and-blank.out.go b/pkg/gci/internal/testdata/dot-and-blank.out.go
index b09c8f5..036a8a3 100644
--- a/pkg/gci/internal/testdata/dot-and-blank.out.go
+++ b/pkg/gci/internal/testdata/dot-and-blank.out.go
@@ -1,4 +1,5 @@
 package main
+
 import (
 	"fmt"
 
diff --git a/pkg/gci/internal/testdata/duplicate-imports.in.go b/pkg/gci/internal/testdata/duplicate-imports.in.go
index a443a33..0ef8fec 100644
--- a/pkg/gci/internal/testdata/duplicate-imports.in.go
+++ b/pkg/gci/internal/testdata/duplicate-imports.in.go
@@ -1,4 +1,5 @@
 package main
+
 import (
 	"fmt"
 
diff --git a/pkg/gci/internal/testdata/duplicate-imports.out.go b/pkg/gci/internal/testdata/duplicate-imports.out.go
index fbdf2a1..476a354 100644
--- a/pkg/gci/internal/testdata/duplicate-imports.out.go
+++ b/pkg/gci/internal/testdata/duplicate-imports.out.go
@@ -1,4 +1,5 @@
 package main
+
 import (
 	"fmt"
 
diff --git a/pkg/gci/internal/testdata/linebreak-no-custom.in.go b/pkg/gci/internal/testdata/linebreak-no-custom.in.go
index 4075701..f30e7d4 100644
--- a/pkg/gci/internal/testdata/linebreak-no-custom.in.go
+++ b/pkg/gci/internal/testdata/linebreak-no-custom.in.go
@@ -1,4 +1,5 @@
 package main
+
 import (
 	g "github.com/golang"
 
diff --git a/pkg/gci/internal/testdata/linebreak-no-custom.out.go b/pkg/gci/internal/testdata/linebreak-no-custom.out.go
index 848a140..8ca2577 100644
--- a/pkg/gci/internal/testdata/linebreak-no-custom.out.go
+++ b/pkg/gci/internal/testdata/linebreak-no-custom.out.go
@@ -1,4 +1,5 @@
 package main
+
 import (
 	"fmt"
 
diff --git a/pkg/gci/internal/testdata/linebreak.in.go b/pkg/gci/internal/testdata/linebreak.in.go
index d0524a8..7e0f7e7 100644
--- a/pkg/gci/internal/testdata/linebreak.in.go
+++ b/pkg/gci/internal/testdata/linebreak.in.go
@@ -1,4 +1,5 @@
 package main
+
 import (
 	g "github.com/golang"
 
diff --git a/pkg/gci/internal/testdata/linebreak.out.go b/pkg/gci/internal/testdata/linebreak.out.go
index 89de7a0..26a4c1e 100644
--- a/pkg/gci/internal/testdata/linebreak.out.go
+++ b/pkg/gci/internal/testdata/linebreak.out.go
@@ -1,4 +1,5 @@
 package main
+
 import (
 	"fmt"
 
diff --git a/pkg/gci/internal/testdata/mismatch-section.in.go b/pkg/gci/internal/testdata/mismatch-section.in.go
index 89de7a0..26a4c1e 100644
--- a/pkg/gci/internal/testdata/mismatch-section.in.go
+++ b/pkg/gci/internal/testdata/mismatch-section.in.go
@@ -1,4 +1,5 @@
 package main
+
 import (
 	"fmt"
 
diff --git a/pkg/gci/internal/testdata/mismatch-section.out.go b/pkg/gci/internal/testdata/mismatch-section.out.go
index 89de7a0..26a4c1e 100644
--- a/pkg/gci/internal/testdata/mismatch-section.out.go
+++ b/pkg/gci/internal/testdata/mismatch-section.out.go
@@ -1,4 +1,5 @@
 package main
+
 import (
 	"fmt"
 
diff --git a/pkg/gci/internal/testdata/multiple-custom.in.go b/pkg/gci/internal/testdata/multiple-custom.in.go
index 09fe674..e9302a3 100644
--- a/pkg/gci/internal/testdata/multiple-custom.in.go
+++ b/pkg/gci/internal/testdata/multiple-custom.in.go
@@ -1,4 +1,5 @@
 package main
+
 import (
 	"fmt"
 
diff --git a/pkg/gci/internal/testdata/multiple-custom.out.go b/pkg/gci/internal/testdata/multiple-custom.out.go
index b8fd750..88895fe 100644
--- a/pkg/gci/internal/testdata/multiple-custom.out.go
+++ b/pkg/gci/internal/testdata/multiple-custom.out.go
@@ -1,4 +1,5 @@
 package main
+
 import (
 	"fmt"
 
diff --git a/pkg/gci/internal/testdata/number-in-alias.in.go b/pkg/gci/internal/testdata/number-in-alias.in.go
index 91026c4..63b62ee 100644
--- a/pkg/gci/internal/testdata/number-in-alias.in.go
+++ b/pkg/gci/internal/testdata/number-in-alias.in.go
@@ -1,4 +1,5 @@
 package main
+
 import (
 	"fmt"
 
diff --git a/pkg/gci/internal/testdata/number-in-alias.out.go b/pkg/gci/internal/testdata/number-in-alias.out.go
index 91026c4..63b62ee 100644
--- a/pkg/gci/internal/testdata/number-in-alias.out.go
+++ b/pkg/gci/internal/testdata/number-in-alias.out.go
@@ -1,4 +1,5 @@
 package main
+
 import (
 	"fmt"
 
diff --git a/pkg/gci/internal/testdata/same-prefix-custom.in.go b/pkg/gci/internal/testdata/same-prefix-custom.in.go
index ca7c385..715019b 100644
--- a/pkg/gci/internal/testdata/same-prefix-custom.in.go
+++ b/pkg/gci/internal/testdata/same-prefix-custom.in.go
@@ -1,4 +1,5 @@
 package main
+
 import (
 	"fmt"
 
diff --git a/pkg/gci/internal/testdata/same-prefix-custom.out.go b/pkg/gci/internal/testdata/same-prefix-custom.out.go
index c52bc7d..b53a04d 100644
--- a/pkg/gci/internal/testdata/same-prefix-custom.out.go
+++ b/pkg/gci/internal/testdata/same-prefix-custom.out.go
@@ -1,4 +1,5 @@
 package main
+
 import (
 	"fmt"
 
diff --git a/pkg/gci/internal/testdata/simple-case.in.go b/pkg/gci/internal/testdata/simple-case.in.go
index 03e0dbb..960e78d 100644
--- a/pkg/gci/internal/testdata/simple-case.in.go
+++ b/pkg/gci/internal/testdata/simple-case.in.go
@@ -1,4 +1,5 @@
 package main
+
 import (
 	"golang.org/x/tools"
 
diff --git a/pkg/gci/internal/testdata/simple-case.out.go b/pkg/gci/internal/testdata/simple-case.out.go
index 1c0e49f..fd2408b 100644
--- a/pkg/gci/internal/testdata/simple-case.out.go
+++ b/pkg/gci/internal/testdata/simple-case.out.go
@@ -1,4 +1,5 @@
 package main
+
 import (
 	"fmt"
 
diff --git a/pkg/gci/internal/testdata/with-above-comment-and-alias.in.go b/pkg/gci/internal/testdata/with-above-comment-and-alias.in.go
index b95516d..948fca8 100644
--- a/pkg/gci/internal/testdata/with-above-comment-and-alias.in.go
+++ b/pkg/gci/internal/testdata/with-above-comment-and-alias.in.go
@@ -1,4 +1,5 @@
 package main
+
 import (
 	"fmt"
 	// golang
diff --git a/pkg/gci/internal/testdata/with-above-comment-and-alias.out.go b/pkg/gci/internal/testdata/with-above-comment-and-alias.out.go
index 13c89f9..14808a7 100644
--- a/pkg/gci/internal/testdata/with-above-comment-and-alias.out.go
+++ b/pkg/gci/internal/testdata/with-above-comment-and-alias.out.go
@@ -1,4 +1,5 @@
 package main
+
 import (
 	"fmt"
 
diff --git a/pkg/gci/internal/testdata/with-comment-and-alias.in.go b/pkg/gci/internal/testdata/with-comment-and-alias.in.go
index 57767c4..79791d9 100644
--- a/pkg/gci/internal/testdata/with-comment-and-alias.in.go
+++ b/pkg/gci/internal/testdata/with-comment-and-alias.in.go
@@ -1,4 +1,5 @@
 package main
+
 import (
 	"fmt"
 	_ "github.com/golang" // golang
diff --git a/pkg/gci/internal/testdata/with-comment-and-alias.out.go b/pkg/gci/internal/testdata/with-comment-and-alias.out.go
index 6b2c628..1d6b967 100644
--- a/pkg/gci/internal/testdata/with-comment-and-alias.out.go
+++ b/pkg/gci/internal/testdata/with-comment-and-alias.out.go
@@ -1,4 +1,5 @@
 package main
+
 import (
 	"fmt"