blob: 87a20b3eac4a3507464d0a018dbe8abf57fe0c3c [file] [log] [blame]
Serge Bazanski144b7862021-12-17 17:30:14 +01001Upstream CL: https://go-review.googlesource.com/c/tools/+/321409
2
Serge Bazanskia6d8b392023-04-03 15:23:57 +02003Modified to work against upstream Go.
4
Serge Bazanski144b7862021-12-17 17:30:14 +01005From cdd84f3789f3ebbd1c55dc220bbbe4a77ab4024a Mon Sep 17 00:00:00 2001
6From: Moekr <1143757638wlw@gmail.com>
7Date: Thu, 20 May 2021 15:41:17 +0800
8Subject: [PATCH] internal/imports: merge mergeable import groups
9
10Merge two import groups if there is no comments between them.
11This can make import blocks look more pretty.
12
Serge Bazanski144b7862021-12-17 17:30:14 +010013---
Serge Bazanskia6d8b392023-04-03 15:23:57 +020014diff -ur org_golang_x_tools.orig/internal/imports/fix_test.go org_golang_x_tools/internal/imports/fix_test.go
15--- org_golang_x_tools.orig/internal/imports/fix_test.go 2023-04-03 15:16:24.808998544 +0200
16+++ org_golang_x_tools/internal/imports/fix_test.go 2023-04-03 15:16:56.069290812 +0200
17@@ -552,7 +552,6 @@
Serge Bazanski144b7862021-12-17 17:30:14 +010018
19 import (
20 "fmt"
21-
22 "gu"
23
24 "manypackages.com/packagea"
Serge Bazanskia6d8b392023-04-03 15:23:57 +020025@@ -643,15 +642,11 @@
Serge Bazanski144b7862021-12-17 17:30:14 +010026
27 import (
28 "fmt"
29-
30- renamed_packagea "manypackages.com/packagea"
31-
32 "io"
33-
34- . "manypackages.com/packageb"
35-
36 "strings"
37
38+ renamed_packagea "manypackages.com/packagea"
39+ . "manypackages.com/packageb"
40 _ "manypackages.com/packagec"
41 )
42
Serge Bazanskia6d8b392023-04-03 15:23:57 +020043@@ -1183,6 +1178,62 @@
Serge Bazanski144b7862021-12-17 17:30:14 +010044 var _, _ = rand.Read, rand.NewZipf
Serge Bazanskia6d8b392023-04-03 15:23:57 +020045 `,
46 },
Serge Bazanski144b7862021-12-17 17:30:14 +010047+ {
48+ name: "comment_between_imports_simple",
49+ in: `package main
50+
51+import (
52+ _ "foo2"
53+ // foo3 has side effects.
54+ _ "foo3"
55+ _ "foo1"
56+)
57+`,
58+ out: `package main
59+
60+import (
61+ _ "foo2"
62+ // foo3 has side effects.
63+ _ "foo1"
64+ _ "foo3"
65+)
66+`,
67+ },
68+ {
69+ name: "comment_between_imports_complex",
70+ in: `package main
71+
72+import (
73+ _ "foo1" // foo1, "std" package
74+ _ "github.com/bar/foo1" // github.com/bar/foo1, third-party package
75+ _ "local/foo1" // local/foo1, local package
76+
77+ _ "appengine"
78+
79+ _ "foo2"
80+ // local/foo3 comment
81+ _ "local/foo3"
82+ _ "github.com/bar/foo2"
83+)
84+`,
85+ out: `package main
86+
87+import (
88+ _ "foo1" // foo1, "std" package
89+ _ "foo2"
90+ _ "local/foo1" // local/foo1, local package
91+
92+ _ "github.com/bar/foo1" // github.com/bar/foo1, third-party package
93+
94+ _ "appengine"
95+
96+ // local/foo3 comment
97+ _ "local/foo3"
98+
99+ _ "github.com/bar/foo2"
100+)
Serge Bazanskia6d8b392023-04-03 15:23:57 +0200101+`,
102+ },
Serge Bazanski144b7862021-12-17 17:30:14 +0100103 }
Serge Bazanskia6d8b392023-04-03 15:23:57 +0200104
105 func TestSimpleCases(t *testing.T) {
106Only in org_golang_x_tools/internal/imports: fix_test.go.orig
107diff -ur org_golang_x_tools.orig/internal/imports/sortimports.go org_golang_x_tools/internal/imports/sortimports.go
108--- org_golang_x_tools.orig/internal/imports/sortimports.go 2023-04-03 15:16:24.808998544 +0200
109+++ org_golang_x_tools/internal/imports/sortimports.go 2023-04-03 15:20:31.361303687 +0200
110@@ -38,15 +38,31 @@
Serge Bazanski144b7862021-12-17 17:30:14 +0100111 continue
112 }
113
114+ // Find comments.
115+ commentLines := make([]int, len(f.Comments))
116+ for j, comment := range f.Comments {
Serge Bazanskia6d8b392023-04-03 15:23:57 +0200117+ commentLines[j] = tokFile.Line(comment.Pos())
Serge Bazanski144b7862021-12-17 17:30:14 +0100118+ }
119+
120 // Identify and sort runs of specs on successive lines.
121 i := 0
122 specs := d.Specs[:0]
123 for j, s := range d.Specs {
Serge Bazanskia6d8b392023-04-03 15:23:57 +0200124- if j > i && tokFile.Line(s.Pos()) > 1+tokFile.Line(d.Specs[j-1].End()) {
Serge Bazanski144b7862021-12-17 17:30:14 +0100125- // j begins a new run. End this one.
Serge Bazanskia6d8b392023-04-03 15:23:57 +0200126- specs = append(specs, sortSpecs(localPrefix, tokFile, f, d.Specs[i:j])...)
Serge Bazanski144b7862021-12-17 17:30:14 +0100127- i = j
128+ if j > i {
Serge Bazanskia6d8b392023-04-03 15:23:57 +0200129+ curLine, prevLine := tokFile.Line(s.Pos()), tokFile.Line(d.Specs[j-1].End())
Serge Bazanski144b7862021-12-17 17:30:14 +0100130+ if curLine > 1+prevLine {
131+ // j begins a new run.
132+ for _, commentLine := range commentLines {
133+ if prevLine < commentLine && commentLine < curLine {
134+ // End this one if there is a comment before the new one.
Serge Bazanskia6d8b392023-04-03 15:23:57 +0200135+ specs = append(specs, sortSpecs(localPrefix, tokFile, f, d.Specs[i:j])...)
Serge Bazanski144b7862021-12-17 17:30:14 +0100136+ i = j
137+ break
138+ }
139+ }
140+ }
141 }
Serge Bazanskia6d8b392023-04-03 15:23:57 +0200142+
Serge Bazanski144b7862021-12-17 17:30:14 +0100143 }
Serge Bazanskia6d8b392023-04-03 15:23:57 +0200144 specs = append(specs, sortSpecs(localPrefix, tokFile, f, d.Specs[i:])...)
145 d.Specs = specs