blob: 87a20b3eac4a3507464d0a018dbe8abf57fe0c3c [file] [log] [blame]
Upstream CL: https://go-review.googlesource.com/c/tools/+/321409
Modified to work against upstream Go.
From cdd84f3789f3ebbd1c55dc220bbbe4a77ab4024a Mon Sep 17 00:00:00 2001
From: Moekr <1143757638wlw@gmail.com>
Date: Thu, 20 May 2021 15:41:17 +0800
Subject: [PATCH] internal/imports: merge mergeable import groups
Merge two import groups if there is no comments between them.
This can make import blocks look more pretty.
---
diff -ur org_golang_x_tools.orig/internal/imports/fix_test.go org_golang_x_tools/internal/imports/fix_test.go
--- org_golang_x_tools.orig/internal/imports/fix_test.go 2023-04-03 15:16:24.808998544 +0200
+++ org_golang_x_tools/internal/imports/fix_test.go 2023-04-03 15:16:56.069290812 +0200
@@ -552,7 +552,6 @@
import (
"fmt"
-
"gu"
"manypackages.com/packagea"
@@ -643,15 +642,11 @@
import (
"fmt"
-
- renamed_packagea "manypackages.com/packagea"
-
"io"
-
- . "manypackages.com/packageb"
-
"strings"
+ renamed_packagea "manypackages.com/packagea"
+ . "manypackages.com/packageb"
_ "manypackages.com/packagec"
)
@@ -1183,6 +1178,62 @@
var _, _ = rand.Read, rand.NewZipf
`,
},
+ {
+ name: "comment_between_imports_simple",
+ in: `package main
+
+import (
+ _ "foo2"
+ // foo3 has side effects.
+ _ "foo3"
+ _ "foo1"
+)
+`,
+ out: `package main
+
+import (
+ _ "foo2"
+ // foo3 has side effects.
+ _ "foo1"
+ _ "foo3"
+)
+`,
+ },
+ {
+ name: "comment_between_imports_complex",
+ in: `package main
+
+import (
+ _ "foo1" // foo1, "std" package
+ _ "github.com/bar/foo1" // github.com/bar/foo1, third-party package
+ _ "local/foo1" // local/foo1, local package
+
+ _ "appengine"
+
+ _ "foo2"
+ // local/foo3 comment
+ _ "local/foo3"
+ _ "github.com/bar/foo2"
+)
+`,
+ out: `package main
+
+import (
+ _ "foo1" // foo1, "std" package
+ _ "foo2"
+ _ "local/foo1" // local/foo1, local package
+
+ _ "github.com/bar/foo1" // github.com/bar/foo1, third-party package
+
+ _ "appengine"
+
+ // local/foo3 comment
+ _ "local/foo3"
+
+ _ "github.com/bar/foo2"
+)
+`,
+ },
}
func TestSimpleCases(t *testing.T) {
Only in org_golang_x_tools/internal/imports: fix_test.go.orig
diff -ur org_golang_x_tools.orig/internal/imports/sortimports.go org_golang_x_tools/internal/imports/sortimports.go
--- org_golang_x_tools.orig/internal/imports/sortimports.go 2023-04-03 15:16:24.808998544 +0200
+++ org_golang_x_tools/internal/imports/sortimports.go 2023-04-03 15:20:31.361303687 +0200
@@ -38,15 +38,31 @@
continue
}
+ // Find comments.
+ commentLines := make([]int, len(f.Comments))
+ for j, comment := range f.Comments {
+ commentLines[j] = tokFile.Line(comment.Pos())
+ }
+
// Identify and sort runs of specs on successive lines.
i := 0
specs := d.Specs[:0]
for j, s := range d.Specs {
- if j > i && tokFile.Line(s.Pos()) > 1+tokFile.Line(d.Specs[j-1].End()) {
- // j begins a new run. End this one.
- specs = append(specs, sortSpecs(localPrefix, tokFile, f, d.Specs[i:j])...)
- i = j
+ if j > i {
+ curLine, prevLine := tokFile.Line(s.Pos()), tokFile.Line(d.Specs[j-1].End())
+ if curLine > 1+prevLine {
+ // j begins a new run.
+ for _, commentLine := range commentLines {
+ if prevLine < commentLine && commentLine < curLine {
+ // End this one if there is a comment before the new one.
+ specs = append(specs, sortSpecs(localPrefix, tokFile, f, d.Specs[i:j])...)
+ i = j
+ break
+ }
+ }
+ }
}
+
}
specs = append(specs, sortSpecs(localPrefix, tokFile, f, d.Specs[i:])...)
d.Specs = specs