Serge Bazanski | 144b786 | 2021-12-17 17:30:14 +0100 | [diff] [blame] | 1 | Upstream CL: https://go-review.googlesource.com/c/tools/+/321409 |
| 2 | |
| 3 | From cdd84f3789f3ebbd1c55dc220bbbe4a77ab4024a Mon Sep 17 00:00:00 2001 |
| 4 | From: Moekr <1143757638wlw@gmail.com> |
| 5 | Date: Thu, 20 May 2021 15:41:17 +0800 |
| 6 | Subject: [PATCH] internal/imports: merge mergeable import groups |
| 7 | |
| 8 | Merge two import groups if there is no comments between them. |
| 9 | This can make import blocks look more pretty. |
| 10 | |
| 11 | Fixes golang/go#20818 golang/go#28200 |
| 12 | |
| 13 | Change-Id: Ic02ca83bd778e6d5b5b3c15292cde4fba6c842a9 |
| 14 | --- |
| 15 | internal/imports/fix_test.go | 65 +++++++++++++++++++++++++++++---- |
| 16 | internal/imports/sortimports.go | 23 ++++++++++-- |
| 17 | 2 files changed, 77 insertions(+), 11 deletions(-) |
| 18 | |
| 19 | diff --git a/internal/imports/fix_test.go b/internal/imports/fix_test.go |
| 20 | index 005bf96e..5ff84cdd 100644 |
| 21 | --- a/internal/imports/fix_test.go |
| 22 | +++ b/internal/imports/fix_test.go |
| 23 | @@ -553,7 +553,6 @@ c = fmt.Printf |
| 24 | |
| 25 | import ( |
| 26 | "fmt" |
| 27 | - |
| 28 | "gu" |
| 29 | |
| 30 | "manypackages.com/packagea" |
| 31 | @@ -644,15 +643,11 @@ var _, _, _, _, _ = fmt.Errorf, io.Copy, strings.Contains, renamed_packagea.A, B |
| 32 | |
| 33 | import ( |
| 34 | "fmt" |
| 35 | - |
| 36 | - renamed_packagea "manypackages.com/packagea" |
| 37 | - |
| 38 | "io" |
| 39 | - |
| 40 | - . "manypackages.com/packageb" |
| 41 | - |
| 42 | "strings" |
| 43 | |
| 44 | + renamed_packagea "manypackages.com/packagea" |
| 45 | + . "manypackages.com/packageb" |
| 46 | _ "manypackages.com/packagec" |
| 47 | ) |
| 48 | |
| 49 | @@ -1151,6 +1146,62 @@ var _, _ = rand.Read, rand.NewZipf |
| 50 | import "math/rand" |
| 51 | |
| 52 | var _, _ = rand.Read, rand.NewZipf |
| 53 | +`, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "comment_between_imports_simple", |
| 57 | + in: `package main |
| 58 | + |
| 59 | +import ( |
| 60 | + _ "foo2" |
| 61 | + // foo3 has side effects. |
| 62 | + _ "foo3" |
| 63 | + _ "foo1" |
| 64 | +) |
| 65 | +`, |
| 66 | + out: `package main |
| 67 | + |
| 68 | +import ( |
| 69 | + _ "foo2" |
| 70 | + // foo3 has side effects. |
| 71 | + _ "foo1" |
| 72 | + _ "foo3" |
| 73 | +) |
| 74 | +`, |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "comment_between_imports_complex", |
| 78 | + in: `package main |
| 79 | + |
| 80 | +import ( |
| 81 | + _ "foo1" // foo1, "std" package |
| 82 | + _ "github.com/bar/foo1" // github.com/bar/foo1, third-party package |
| 83 | + _ "local/foo1" // local/foo1, local package |
| 84 | + |
| 85 | + _ "appengine" |
| 86 | + |
| 87 | + _ "foo2" |
| 88 | + // local/foo3 comment |
| 89 | + _ "local/foo3" |
| 90 | + _ "github.com/bar/foo2" |
| 91 | +) |
| 92 | +`, |
| 93 | + out: `package main |
| 94 | + |
| 95 | +import ( |
| 96 | + _ "foo1" // foo1, "std" package |
| 97 | + _ "foo2" |
| 98 | + _ "local/foo1" // local/foo1, local package |
| 99 | + |
| 100 | + _ "github.com/bar/foo1" // github.com/bar/foo1, third-party package |
| 101 | + |
| 102 | + _ "appengine" |
| 103 | + |
| 104 | + // local/foo3 comment |
| 105 | + _ "local/foo3" |
| 106 | + |
| 107 | + _ "github.com/bar/foo2" |
| 108 | +) |
| 109 | `, |
| 110 | }, |
| 111 | } |
| 112 | diff --git a/internal/imports/sortimports.go b/internal/imports/sortimports.go |
| 113 | index be8ffa25..25c5ca3f 100644 |
| 114 | --- a/internal/imports/sortimports.go |
| 115 | +++ b/internal/imports/sortimports.go |
| 116 | @@ -34,14 +34,29 @@ func sortImports(localPrefix string, fset *token.FileSet, f *ast.File) { |
| 117 | continue |
| 118 | } |
| 119 | |
| 120 | + // Find comments. |
| 121 | + commentLines := make([]int, len(f.Comments)) |
| 122 | + for j, comment := range f.Comments { |
| 123 | + commentLines[j] = fset.Position(comment.Pos()).Line |
| 124 | + } |
| 125 | + |
| 126 | // Identify and sort runs of specs on successive lines. |
| 127 | i := 0 |
| 128 | specs := d.Specs[:0] |
| 129 | for j, s := range d.Specs { |
| 130 | - if j > i && fset.Position(s.Pos()).Line > 1+fset.Position(d.Specs[j-1].End()).Line { |
| 131 | - // j begins a new run. End this one. |
| 132 | - specs = append(specs, sortSpecs(localPrefix, fset, f, d.Specs[i:j])...) |
| 133 | - i = j |
| 134 | + if j > i { |
| 135 | + curLine, prevLine := fset.Position(s.Pos()).Line, fset.Position(d.Specs[j-1].End()).Line |
| 136 | + if curLine > 1+prevLine { |
| 137 | + // j begins a new run. |
| 138 | + for _, commentLine := range commentLines { |
| 139 | + if prevLine < commentLine && commentLine < curLine { |
| 140 | + // End this one if there is a comment before the new one. |
| 141 | + specs = append(specs, sortSpecs(localPrefix, fset, f, d.Specs[i:j])...) |
| 142 | + i = j |
| 143 | + break |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | } |
| 148 | } |
| 149 | specs = append(specs, sortSpecs(localPrefix, fset, f, d.Specs[i:])...) |
| 150 | -- |
| 151 | 2.31.1 |
| 152 | |