mirror of
https://github.com/golang/go
synced 2024-11-18 10:54:40 -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:
parent
78a4eef087
commit
13be4dfe75
@ -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)
|
||||
}
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user