@@ -19,10 +19,8 @@ function refactor(
19
19
column = 1 , row = 1 , startrow = 0 , context = " " ,
20
20
mod = " Main" ,
21
21
)
22
- expr = CSTParser. parse (context, true )
23
- bind = CSTParser. bindingof (expr)
24
-
25
22
# local refactor only if `old` is really a local binding
23
+ bind = CSTParser. bindingof (CSTParser. parse (context))
26
24
if bind === nothing || old != bind. name
27
25
try
28
26
refactored = localrefactor (old, new, path, column, row, startrow, context)
@@ -33,52 +31,75 @@ function refactor(
33
31
end
34
32
35
33
try
36
- refactored = localrefactor (old, new, path, column, row, startrow, context)
37
- isempty (refactored) || return Dict (:text => refactored)
34
+ mod = getmodule (mod)
35
+ val = getfield′ (mod, old)
36
+ result = globalrefactor (old, new, mod, val)
37
+ return result isa String ? Dict (:error => result) : Dict (:error => false )
38
38
catch err
39
39
@error err
40
40
end
41
41
42
- # try
43
- # globalrefactor(old, new, path, mod) && return nothing
44
- # catch err
45
- # @error err
46
- # end
47
-
48
- return Dict (:error => true , :msg => " no refactor" )
42
+ return Dict (:error => " Rename refactoring failed: `$old ` -> `$new `" )
49
43
end
50
44
51
- function localrefactor (old, new, path, column, row, startrow, context)
52
- old = first (split (old, ' .' )) # ignore dot accessors
53
- position = row - startrow
45
+ # local refactor
46
+ # --------------
54
47
55
- return if old ∈ map (l -> l[:name ], locals (context, position, column))
48
+ function localrefactor (old, new, path, column, row, startrow, context)
49
+ return if old ∈ map (l -> l[:name ], locals (context, row - startrow, column))
56
50
oldsym = Symbol (old)
57
- quote
58
- MacroTools. textwalk ($ context) do ex
59
- @capture (ex, $ oldsym) ? Symbol ($ new) : ex
60
- end
61
- end |> eval
51
+ newsym = Symbol (new)
52
+ MacroTools. textwalk (context) do sym
53
+ sym === oldsym ? newsym : sym
54
+ end
62
55
else
63
56
" "
64
57
end
65
58
end
66
59
67
- # mod = getmodule(m)
68
- # parentfile, modulefiles = modulefiles(mod)
69
- # sourcewalk("../packages/Atom/src/goto.jl") do x
70
- # isshort = MacroTools.isshortdef(x)
71
- # ex = MacroTools.shortdef(x)
72
- # if @capture(ex, locals(args__) = body_)
73
- # return if isshort
74
- # :(newlocals(args...) = body)
75
- # else
76
- # :(function newlocals(args...)
77
- # body
78
- # end)
79
- # end
80
- # end
81
- # return x
82
- # isstruct = MacroTools.isstructdef(x)
83
- # if @capture(x, struct )
84
- # end
60
+ # global refactor
61
+ # ---------------
62
+
63
+ globalrefactor (old, new, mod, @nospecialize (val)) = _globalrefactor (old, new, mod) # general case
64
+ function globalrefactor (old, new, mod, val:: Undefined )
65
+ Symbol (old) in keys (Docs. keywords) ?
66
+ " Keywords can't be renamed: `$old `" :
67
+ _globalrefactor (old, new, mod)
68
+ end
69
+
70
+ function _globalrefactor (old, new, mod)
71
+ entrypath, line = moduledefinition (mod)
72
+ files = modulefiles (entrypath)
73
+
74
+ with_logger (JunoProgressLogger ()) do
75
+ refactorfiles (old, new, mod, files)
76
+ end
77
+ end
78
+
79
+ function refactorfiles (old, new, obj, files)
80
+ id = " global_rename_refactor_progress"
81
+ @info " Start global rename refactoring" progress= 0 _id= id
82
+
83
+ oldsym = Symbol (old)
84
+ newsym = Symbol (new)
85
+ modulesyms = Set (Symbol .(Base. loaded_modules_array ()))
86
+ total = length (files)
87
+
88
+ for (i, file) ∈ enumerate (files)
89
+ @info " Refactoring: $file ($i / $total )" progress= i/ total _id= id
90
+ MacroTools. sourcewalk (file) do ex
91
+ return if ex === oldsym
92
+ 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)
98
+ else
99
+ ex
100
+ end
101
+ end
102
+ end
103
+
104
+ @info " Finish global rename refactoring" progress= 1 _id= id
105
+ end
0 commit comments