aboutsummaryrefslogtreecommitdiff
path: root/imports
diff options
context:
space:
mode:
authorLE Manh Cuong <cuong.manhle.vn@gmail.com>2018-07-01 22:28:45 +0700
committerBrad Fitzpatrick <bradfitz@golang.org>2018-07-02 20:27:37 +0000
commitb23eb6252fad4550577208636764807058a5331a (patch)
treedd26a6788ada5cfb97adec5de70aa0bb8c0885c3 /imports
parent1c99e1239a0c8b0e59c34000995c4e319b7702ca (diff)
downloadgolang-x-tools-b23eb6252fad4550577208636764807058a5331a.tar.gz
imports: fixup comments on import lines correctly
The current implementation uses the added import specs EndPos to fixup the comments position after import specs is sorted. If two or more import specs have the same EndPos, a comment associated with one of them is always added to the last import spec. This commit uses the current import spec position to compute new position for next import spec. So there is never two or more specs have the same EndPos. Fixes golang/go#23709 Change-Id: I60ace9431d871e94a2b3d90892aa80d0671aeea0 Reviewed-on: https://go-review.googlesource.com/121878 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'imports')
-rw-r--r--imports/fix_test.go31
-rw-r--r--imports/sortimports.go7
2 files changed, 36 insertions, 2 deletions
diff --git a/imports/fix_test.go b/imports/fix_test.go
index e2873008c..654d65596 100644
--- a/imports/fix_test.go
+++ b/imports/fix_test.go
@@ -890,6 +890,33 @@ func main() {
}
`,
},
+
+ {
+ name: "issue #23709",
+ in: `package main
+
+import (
+ "math" // fun
+)
+
+func main() {
+ x := math.MaxInt64
+ fmt.Println(strings.Join(",", []string{"hi"}), x)
+}`,
+ out: `package main
+
+import (
+ "fmt"
+ "math" // fun
+ "strings"
+)
+
+func main() {
+ x := math.MaxInt64
+ fmt.Println(strings.Join(",", []string{"hi"}), x)
+}
+`,
+ },
}
func TestFixImports(t *testing.T) {
@@ -1643,8 +1670,8 @@ func TestImportPathToNameGoPathParse(t *testing.T) {
func TestIgnoreConfiguration(t *testing.T) {
testConfig{
gopathFiles: map[string]string{
- ".goimportsignore": "# comment line\n\n example.net", // tests comment, blank line, whitespace trimming
- "example.net/pkg/pkg.go": "package pkg\nconst X = 1",
+ ".goimportsignore": "# comment line\n\n example.net", // tests comment, blank line, whitespace trimming
+ "example.net/pkg/pkg.go": "package pkg\nconst X = 1",
"otherwise-longer-so-worse.example.net/foo/pkg/pkg.go": "package pkg\nconst X = 1",
},
}.test(t, func(t *goimportTest) {
diff --git a/imports/sortimports.go b/imports/sortimports.go
index 653afc517..93711565a 100644
--- a/imports/sortimports.go
+++ b/imports/sortimports.go
@@ -167,11 +167,18 @@ func sortSpecs(fset *token.FileSet, f *ast.File, specs []ast.Spec) []ast.Spec {
}
s.Path.ValuePos = pos[i].Start
s.EndPos = pos[i].End
+ nextSpecPos := pos[i].End
+
for _, g := range importComment[s] {
for _, c := range g.List {
c.Slash = pos[i].End
+ nextSpecPos = c.End()
}
}
+ if i < len(specs)-1 {
+ pos[i+1].Start = nextSpecPos
+ pos[i+1].End = nextSpecPos
+ }
}
sort.Sort(byCommentPos(comments))