1
- handle (" refactor " ) do data
1
+ handle (" renamerefactor " ) do data
2
2
@destruct [
3
3
old,
4
+ full,
4
5
new,
5
6
path,
6
7
# local context
@@ -11,35 +12,48 @@ handle("refactor") do data
11
12
# module context
12
13
mod || " Main" ,
13
14
] = data
14
- refactor (old, new, path, column, row, startRow, context, mod)
15
+ renamerefactor (old, full , new, path, column, row, startRow, context, mod)
15
16
end
16
17
17
- function refactor (
18
- old, new, path,
18
+ function renamerefactor (
19
+ old, full, new, path,
19
20
column = 1 , row = 1 , startrow = 0 , context = " " ,
20
21
mod = " Main" ,
21
22
)
23
+ mod = getmodule (mod)
24
+
25
+ # catch field renaming
26
+ if (obj = first (split (full, ' .' ))) != old && ! isa (getfield′ (mod, obj), Module)
27
+ return Dict (:warning => " Rename refactoring on a field isn't available: `$obj .$old `" )
28
+ end
29
+
22
30
# local refactor only if `old` is really a local binding
23
31
bind = CSTParser. bindingof (CSTParser. parse (context))
24
32
if bind === nothing || old != bind. name
25
33
try
26
34
refactored = localrefactor (old, new, path, column, row, startrow, context)
27
- isempty (refactored) || return Dict (:text => refactored)
35
+ isempty (refactored) || return Dict (
36
+ :text => refactored,
37
+ :success => " Local rename refactoring `$old ` ⟹ `$new ` succeeded"
38
+ )
28
39
catch err
29
40
@error err
30
41
end
31
42
end
32
43
33
44
try
34
- mod = getmodule (mod)
35
45
val = getfield′ (mod, old)
36
- result = globalrefactor (old, new, mod, val)
37
- return result isa String ? Dict (:error => result) : Dict (:error => false )
46
+ kind, description = globalrefactor (old, new, mod, val)
47
+ return Dict (
48
+ kind => description,
49
+ :success => kind != = :info ? false :
50
+ " Global rename refactoring `$old ` ⟹ `$new ` succeeded"
51
+ )
38
52
catch err
39
53
@error err
40
54
end
41
55
42
- return Dict (:error => " Rename refactoring failed: `$old ` -> `$new `" )
56
+ return Dict (:error => " Rename refactoring `$old ` ⟹ `$new ` failed " )
43
57
end
44
58
45
59
# local refactor
63
77
globalrefactor (old, new, mod, @nospecialize (val)) = _globalrefactor (old, new, mod) # general case
64
78
function globalrefactor (old, new, mod, val:: Undefined )
65
79
Symbol (old) in keys (Docs. keywords) ?
66
- " Keywords can't be renamed: `$old `" :
80
+ ( :warning , " Keywords can't be renamed: `$old `" ) :
67
81
_globalrefactor (old, new, mod)
68
82
end
69
83
@@ -76,30 +90,31 @@ function _globalrefactor(old, new, mod)
76
90
end
77
91
end
78
92
79
- function refactorfiles (old, new, obj , files)
93
+ function refactorfiles (old, new, mod , files)
80
94
id = " global_rename_refactor_progress"
81
95
@info " Start global rename refactoring" progress= 0 _id= id
82
96
83
- oldsym = Symbol (old)
84
- newsym = Symbol (new)
85
- modulesyms = Set (Symbol .(Base. loaded_modules_array ()))
86
- total = length (files)
97
+ oldsym = Symbol (old)
98
+ newsym = Symbol (new)
99
+ total = length (files)
100
+
101
+ desc = " "
87
102
88
103
for (i, file) ∈ enumerate (files)
89
104
@info " Refactoring: $file ($i / $total )" progress= i/ total _id= id
90
105
MacroTools. sourcewalk (file) do ex
91
106
return if ex === oldsym
92
107
newsym
93
- elseif @capture (ex, obj_.$ oldsym)
94
- if obj in modulesyms
95
- @warn " Came across a global rename refactoring across different modules: `$obj .$old ` -> `$obj .$new `"
96
- end
97
- Expr (:., obj, newsym)
108
+ elseif @capture (ex, m_.$ oldsym) && getfield′ (mod, m) isa Module
109
+ desc *= " `$m .$old ` ⟹ `$m .$new ` in $(fullpath (file)) \n "
110
+ Expr (:., m, newsym)
98
111
else
99
112
ex
100
113
end
101
114
end
102
115
end
103
116
104
117
@info " Finish global rename refactoring" progress= 1 _id= id
118
+
119
+ (:info , isempty (desc) ? " " : " Refactorings across modules\n\n " * desc)
105
120
end
0 commit comments