1
1
handle (" renamerefactor" ) do data
2
2
@destruct [
3
- old ,
4
- full ,
5
- new ,
3
+ oldWord ,
4
+ fullWord ,
5
+ newWord ,
6
6
# local context
7
7
column || 1 ,
8
8
row || 1 ,
@@ -11,74 +11,71 @@ handle("renamerefactor") do data
11
11
# module context
12
12
mod || " Main" ,
13
13
] = data
14
- renamerefactor (old, full, new , column, row, startRow, context, mod)
14
+ renamerefactor (oldWord, fullWord, newWord , column, row, startRow, context, mod)
15
15
end
16
16
17
17
# NOTE: invalid identifiers will be caught by frontend
18
18
function renamerefactor (
19
- old, full, new ,
19
+ oldword, fullword, newword ,
20
20
column = 1 , row = 1 , startrow = 0 , context = " " ,
21
21
mod = " Main" ,
22
22
)
23
23
# catch keyword renaming
24
- iskeyword (old ) && return Dict (:warning => " Keywords can't be renamed: `$old `" )
24
+ iskeyword (oldword ) && return Dict (:warning => " Keywords can't be renamed: `$oldword `" )
25
25
26
26
mod = getmodule (mod)
27
- hstr = first (split (full , ' .' ))
27
+ hstr = first (split (fullword , ' .' ))
28
28
head = getfield′ (mod, hstr)
29
29
30
30
# catch field renaming
31
- hstr ≠ old && ! isa (head, Module) && return Dict (
32
- :warning => " Rename refactoring on a field isn't available: `$hstr .$old `"
31
+ hstr ≠ oldword && ! isa (head, Module) && return Dict (
32
+ :warning => " Rename refactoring on a field isn't available: `$hstr .$oldword `"
33
33
)
34
34
35
35
expr = CSTParser. parse (context)
36
+
36
37
bind = let
37
- if expr != = nothing
38
- items = toplevelitems (expr, context)
39
- ind = findfirst (item -> item isa ToplevelBinding, items)
40
- ind === nothing ? nothing : items[ind]. bind
41
- else
42
- nothing
43
- end
38
+ items = toplevelitems (context, expr)
39
+ ind = findfirst (item -> item isa ToplevelBinding, items)
40
+ ind === nothing ? nothing : items[ind]. bind
44
41
end
45
42
46
43
# local rename refactor if `old` isn't a toplevel binding
47
- if islocalrefactor (bind, old )
44
+ if islocalrefactor (bind, oldword )
48
45
try
49
- refactored = localrenamerefactor (old, new , column, row, startrow, context, expr)
46
+ refactored = localrenamerefactor (oldword, newword , column, row, startrow, context, expr)
50
47
return isempty (refactored) ?
51
48
# NOTE: global refactoring not on definition, e.g.: on a call site, will be caught here
52
- Dict (:info => contextdescription (old , mod, context)) :
49
+ Dict (:info => contextdescription (oldword , mod, context)) :
53
50
Dict (
54
51
:text => refactored,
55
- :success => " _Local_ rename refactoring `$old ` ⟹ `$new ` succeeded"
52
+ :success => " _Local_ rename refactoring `$oldword ` ⟹ `$newword ` succeeded"
56
53
)
57
54
catch err
58
- return Dict (:error => errdescription (old, new , err))
55
+ return Dict (:error => errdescription (oldword, newword , err))
59
56
end
60
57
end
61
58
62
59
# global rename refactor if the local rename refactor didn't happen
63
60
try
64
- kind, desc = globalrenamerefactor (old, new , mod, expr)
61
+ kind, desc = globalrenamerefactor (oldword, newword , mod, expr)
65
62
66
63
# make description
67
64
if kind === :success
68
- val = getfield′ (mod, full )
65
+ val = getfield′ (mod, fullword )
69
66
moddesc = if (head isa Module && head ≠ mod) ||
70
67
(applicable (parentmodule, val) && (head = parentmodule (val)) ≠ mod)
71
- moduledescription (old , head)
68
+ moduledescription (oldword , head)
72
69
else
73
70
" "
74
71
end
75
72
76
- desc = join ((" _Global_ rename refactoring `$mod .$old ` ⟹ `$mod .$new ` succeeded." , moddesc, desc), " \n\n " )
73
+ desc = join ((" _Global_ rename refactoring `$mod .$oldword ` ⟹ `$mod .$newword ` succeeded." , moddesc, desc), " \n\n " )
77
74
end
78
75
79
76
return Dict (kind => desc)
80
77
catch err
81
- return Dict (:error => errdescription (old, new , err))
78
+ return Dict (:error => errdescription (oldword, newword , err))
82
79
end
83
80
end
84
81
@@ -87,22 +84,22 @@ islocalrefactor(bind, name) = bind === nothing || name ≠ bind.name
87
84
# local refactor
88
85
# --------------
89
86
90
- function localrenamerefactor (old, new , column, row, startrow, context, expr)
87
+ function localrenamerefactor (oldword, newword , column, row, startrow, context, expr)
91
88
bindings = localbindings (expr, context)
92
89
line = row - startrow
93
- scope = currentscope (old , bindings, byteoffset (context, line, column))
90
+ scope = currentscope (oldword , bindings, byteoffset (context, line, column))
94
91
scope === nothing && return " "
95
92
96
93
currentcontext = scope. bindstr
97
- oldsym = Symbol (old)
98
- newsym = Symbol (new)
99
94
newcontext = MacroTools. textwalk (currentcontext) do sym
95
+ oldsym = Symbol (oldword)
96
+ newsym = Symbol (newword)
100
97
sym === oldsym ? newsym : sym
101
98
end
102
99
103
100
replace (context, currentcontext => newcontext)
104
101
end
105
- localrenamerefactor (old, new , column, row, startrow, context, expr:: Nothing ) = " "
102
+ localrenamerefactor (oldword, newword , column, row, startrow, context, expr:: Nothing ) = " "
106
103
107
104
function currentscope (name, bindings, byteoffset)
108
105
for binding in bindings
@@ -124,14 +121,14 @@ end
124
121
# global refactor
125
122
# ---------------
126
123
127
- function globalrenamerefactor (old, new , mod, expr)
124
+ function globalrenamerefactor (oldword, newword , mod, expr)
128
125
entrypath, _ = if mod == Main
129
126
MAIN_MODULE_LOCATION[]
130
127
else
131
128
moduledefinition (mod)
132
129
end
133
130
134
- files = modulefiles (entrypath)
131
+ files = modulefiles (string (mod), entrypath)
135
132
136
133
# catch refactorings on an unsaved / non-existing file
137
134
isempty (files) && return :warning , unsaveddescription ()
@@ -143,14 +140,14 @@ function globalrenamerefactor(old, new, mod, expr)
143
140
end
144
141
145
142
with_logger (JunoProgressLogger ()) do
146
- _globalrenamerefactor (old, new , mod, expr, files)
143
+ _globalrenamerefactor (oldword, newword , mod, expr, files)
147
144
end
148
145
end
149
146
150
- function _globalrenamerefactor (old, new , mod, expr, files)
147
+ function _globalrenamerefactor (oldword, newword , mod, expr, files)
151
148
ismacro = CSTParser. defines_macro (expr)
152
- oldsym = ismacro ? Symbol (" @" * old ) : Symbol (old )
153
- newsym = ismacro ? Symbol (" @" * new ) : Symbol (new )
149
+ oldsym = ismacro ? Symbol (" @" * oldword ) : Symbol (oldword )
150
+ newsym = ismacro ? Symbol (" @" * newword ) : Symbol (newword )
154
151
155
152
total = length (files)
156
153
# TODO : enable line location information (the upstream needs to be enhanced)
@@ -171,9 +168,9 @@ function _globalrenamerefactor(old, new, mod, expr, files)
171
168
push! (modifiedfiles, fullpath (file))
172
169
Expr (:., m, newsym)
173
170
# macro case
174
- elseif ismacro && @capture (ex, macro $ (Symbol (old ))(args__) body_ end )
171
+ elseif ismacro && @capture (ex, macro $ (Symbol (oldword ))(args__) body_ end )
175
172
push! (modifiedfiles, fullpath (file))
176
- Expr (:macro , :($ (Symbol (new ))($ (args... ))), :($ body))
173
+ Expr (:macro , :($ (Symbol (newword ))($ (args... ))), :($ body))
177
174
else
178
175
ex
179
176
end
@@ -185,7 +182,7 @@ function _globalrenamerefactor(old, new, mod, expr, files)
185
182
return if ! isempty (modifiedfiles)
186
183
:success , filesdescription (mod, modifiedfiles)
187
184
else
188
- :warning , " No rename refactoring occured on `$old ` in `$mod ` module."
185
+ :warning , " No rename refactoring occured on `$oldword ` in `$mod ` module."
189
186
end
190
187
end
191
188
@@ -239,9 +236,9 @@ function filesdescription(mod, files)
239
236
"""
240
237
end
241
238
242
- function errdescription (old, new , err)
239
+ function errdescription (oldword, newword , err)
243
240
"""
244
- Rename refactoring `$old ` ⟹ `$new ` failed.
241
+ Rename refactoring `$oldword ` ⟹ `$newword ` failed.
245
242
246
243
<details><summary>Error:</summary><pre><code>$(errmsg (err)) </code></p></details>
247
244
"""
0 commit comments