1
0
mirror of https://github.com/golang/go synced 2024-11-05 14:46:11 -07:00

refactor/mvpkg: rewrite import comments.

Fixes golang/go#10508.

Change-Id: Id9b0f12e1a81b3b16e167462fd3639a6c6c1e0bb
Reviewed-on: https://go-review.googlesource.com/15267
Reviewed-by: Alan Donovan <adonovan@google.com>
This commit is contained in:
David R. Jenni 2015-10-06 19:51:57 +02:00 committed by Alan Donovan
parent 78a4eef087
commit 13be4dfe75
2 changed files with 59 additions and 0 deletions

View File

@ -17,6 +17,7 @@ import (
"fmt" "fmt"
"go/ast" "go/ast"
"go/build" "go/build"
"go/token"
"log" "log"
"os" "os"
"os/exec" "os/exec"
@ -238,6 +239,22 @@ func (m *mover) move() error {
} }
newName := filepath.Base(m.to) newName := filepath.Base(m.to)
for _, f := range pkg.Files { 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 f.Name.Name = newName // change package decl
filesToUpdate[f] = true filesToUpdate[f] = true
} }
@ -338,6 +355,11 @@ func (m *mover) move() error {
return moveDirectory(m.fromDir, m.toDir) 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 { var moveDirectory = func(from, to string) error {
return os.Rename(from, to) return os.Rename(from, to)
} }

View File

@ -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 { for _, test := range tests {