/
rename.go
182 lines (156 loc) · 3.65 KB
/
rename.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright 2011 John Asmuth. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"os"
"go/ast"
"strings"
"path/filepath"
"rog-go.googlecode.com/hg/exp/go/types"
)
func RenameCmd(args []string) (err os.Error) {
if len(args) != 3 {
return MakeErr("Usage: gorf [flags] rename <path> [<type>.]<old name> <new name>")
}
path, oldname, newname := filepath.Clean(args[0]), args[1], args[2]
if oldnametoks := strings.Split(oldname, ".", 2); len(oldnametoks) == 2 {
return FieldCmd([]string{path, oldnametoks[0], oldnametoks[1], newname})
}
if !IsLegalIdentifier(oldname) {
return MakeErr("Old name %s is not a legal identifier", oldname)
}
if !IsLegalIdentifier(newname) {
return MakeErr("New name %s is not a legal identifier", newname)
}
if oldname == newname {
return MakeErr("Old name and new name are the same")
}
err = ScanAllForImports(LocalRoot)
if err != nil {
return
}
PreloadImportedBy(path)
defer func() {
if err != nil {
UndoCmd([]string{})
}
}()
if PackageTops[path] == nil {
return MakeErr("No local package found in %s", path)
}
pkg := LocalImporter(path)
updated := false
var Obj *ast.Object
for fpath, file := range pkg.Files {
fdl := DeclFinder{oldname:oldname, newname:newname}
ast.Walk(&fdl, file)
if fdl.NameExists {
return MakeErr("Name %s already exists", newname)
}
Obj = fdl.Obj
if Obj != nil {
updated = true
}
if updated {
RenameInFile(file, newname, Obj)
RewriteSource(fpath, file)
}
}
if updated {
err = RenameInAll(path, newname, Obj)
}
return
}
type DeclFinder struct {
oldname, newname string
NameExists bool
Obj *ast.Object
Node ast.Node
Name *ast.Ident
}
func (this *DeclFinder) Visit(node ast.Node) ast.Visitor {
if this.Obj != nil {
return nil
}
switch n := node.(type) {
case *ast.BlockStmt:
return nil
case *ast.ValueSpec:
for _, name := range n.Names {
if name.Name == this.newname {
this.NameExists = true
return nil
}
if name.Name == this.oldname {
//this.Name = name
this.Obj, _ = types.ExprType(name, LocalImporter)
this.Node = node
return nil
}
}
return nil
case *ast.FuncDecl:
if n.Name.Name == this.newname {
this.NameExists = true
}
if n.Name.Name == this.oldname {
//this.Name = n.Name
this.Node = node
this.Obj, _ = types.ExprType(n.Name, LocalImporter)
}
return nil
case *ast.TypeSpec:
if n.Name.Name == this.newname {
this.NameExists = true
}
if n.Name.Name == this.oldname {
//this.Name = n.Name
this.Node = node
this.Obj, _ = types.ExprType(n.Name, LocalImporter)
}
return nil
}
return this
}
func RenameInAll(path string, newname string, Obj *ast.Object) (err os.Error) {
for _, ip := range ImportedBy[QuotePath(path)] {
ipkg := LocalImporter(ip)
for fpath, file := range ipkg.Files {
if RenameInFile(file, newname, Obj) {
err = RewriteSource(fpath, file)
if err != nil {
return
}
}
}
}
return
}
func RenameInFile(file *ast.File, NewName string, Obj *ast.Object) bool {
rw := RenameWalker{NewName:NewName, Obj:Obj}
ast.Walk(&rw, file)
return rw.Updated
}
type RenameWalker struct {
NewName string
Updated bool
Obj *ast.Object
}
func (this *RenameWalker) Visit(node ast.Node) ast.Visitor {
switch n := node.(type) {
case *ast.Ident:
obj, _ := types.ExprType(n, LocalImporter)
if obj == this.Obj {
this.Updated = true
n.Name = this.NewName
}
case *ast.SelectorExpr:
obj, _ := types.ExprType(n, LocalImporter)
if obj == this.Obj {
this.Updated = true
n.Sel.Name = this.NewName
}
}
return this
}