From 13be4dfe75fd7b383cf1c753873f118a97c4aeb4 Mon Sep 17 00:00:00 2001 From: "David R. Jenni" Date: Tue, 6 Oct 2015 19:51:57 +0200 Subject: [PATCH] refactor/mvpkg: rewrite import comments. Fixes golang/go#10508. Change-Id: Id9b0f12e1a81b3b16e167462fd3639a6c6c1e0bb Reviewed-on: https://go-review.googlesource.com/15267 Reviewed-by: Alan Donovan --- refactor/rename/mvpkg.go | 22 +++++++++++++++++++++ refactor/rename/mvpkg_test.go | 37 +++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/refactor/rename/mvpkg.go b/refactor/rename/mvpkg.go index bb0d9b0630..4eb8da6b4b 100644 --- a/refactor/rename/mvpkg.go +++ b/refactor/rename/mvpkg.go @@ -17,6 +17,7 @@ import ( "fmt" "go/ast" "go/build" + "go/token" "log" "os" "os/exec" @@ -238,6 +239,22 @@ func (m *mover) move() error { } newName := filepath.Base(m.to) for _, f := range pkg.Files { + // Update all import comments. + for _, cg := range f.Comments { + c := cg.List[0] + if c.Slash >= f.Name.End() && + sameLine(m.iprog.Fset, c.Slash, f.Name.End()) && + (f.Decls == nil || c.Slash < f.Decls[0].Pos()) { + if strings.HasPrefix(c.Text, `// import "`) { + c.Text = `// import "` + m.to + `"` + break + } + if strings.HasPrefix(c.Text, `/* import "`) { + c.Text = `/* import "` + m.to + `" */` + break + } + } + } f.Name.Name = newName // change package decl filesToUpdate[f] = true } @@ -338,6 +355,11 @@ func (m *mover) move() error { return moveDirectory(m.fromDir, m.toDir) } +// sameLine reports whether two positions in the same file are on the same line. +func sameLine(fset *token.FileSet, x, y token.Pos) bool { + return fset.Position(x).Line == fset.Position(y).Line +} + var moveDirectory = func(from, to string) error { return os.Rename(from, to) } diff --git a/refactor/rename/mvpkg_test.go b/refactor/rename/mvpkg_test.go index 61fa354cff..77037197f8 100644 --- a/refactor/rename/mvpkg_test.go +++ b/refactor/rename/mvpkg_test.go @@ -238,6 +238,43 @@ var _ bar.T `, }, }, + // package import comments + { + ctxt: fakeContext(map[string][]string{"foo": {`package foo // import "baz"`}}), + from: "foo", to: "bar", + want: map[string]string{"/go/src/bar/0.go": `package bar // import "bar" +`}, + }, + { + ctxt: fakeContext(map[string][]string{"foo": {`package foo /* import "baz" */`}}), + from: "foo", to: "bar", + want: map[string]string{"/go/src/bar/0.go": `package bar /* import "bar" */ +`}, + }, + { + ctxt: fakeContext(map[string][]string{"foo": {`package foo // import "baz"`}}), + from: "foo", to: "bar", + want: map[string]string{"/go/src/bar/0.go": `package bar // import "bar" +`}, + }, + { + ctxt: fakeContext(map[string][]string{"foo": {`package foo +// import " this is not an import comment`}}), + from: "foo", to: "bar", + want: map[string]string{"/go/src/bar/0.go": `package bar + +// import " this is not an import comment +`}, + }, + { + ctxt: fakeContext(map[string][]string{"foo": {`package foo +/* import " this is not an import comment */`}}), + from: "foo", to: "bar", + want: map[string]string{"/go/src/bar/0.go": `package bar + +/* import " this is not an import comment */ +`}, + }, } for _, test := range tests {