Skip to content

Commit abbb84c

Browse files
committed
better messaging & suppress field renaming
1 parent e2da219 commit abbb84c

File tree

1 file changed

+35
-20
lines changed

1 file changed

+35
-20
lines changed

src/refactor.jl

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
handle("refactor") do data
1+
handle("renamerefactor") do data
22
@destruct [
33
old,
4+
full,
45
new,
56
path,
67
# local context
@@ -11,35 +12,48 @@ handle("refactor") do data
1112
# module context
1213
mod || "Main",
1314
] = data
14-
refactor(old, new, path, column, row, startRow, context, mod)
15+
renamerefactor(old, full, new, path, column, row, startRow, context, mod)
1516
end
1617

17-
function refactor(
18-
old, new, path,
18+
function renamerefactor(
19+
old, full, new, path,
1920
column = 1, row = 1, startrow = 0, context = "",
2021
mod = "Main",
2122
)
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+
2230
# local refactor only if `old` is really a local binding
2331
bind = CSTParser.bindingof(CSTParser.parse(context))
2432
if bind === nothing || old != bind.name
2533
try
2634
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+
)
2839
catch err
2940
@error err
3041
end
3142
end
3243

3344
try
34-
mod = getmodule(mod)
3545
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+
)
3852
catch err
3953
@error err
4054
end
4155

42-
return Dict(:error => "Rename refactoring failed: `$old` -> `$new`")
56+
return Dict(:error => "Rename refactoring `$old` `$new` failed")
4357
end
4458

4559
# local refactor
@@ -63,7 +77,7 @@ end
6377
globalrefactor(old, new, mod, @nospecialize(val)) = _globalrefactor(old, new, mod) # general case
6478
function globalrefactor(old, new, mod, val::Undefined)
6579
Symbol(old) in keys(Docs.keywords) ?
66-
"Keywords can't be renamed: `$old`" :
80+
(:warning, "Keywords can't be renamed: `$old`") :
6781
_globalrefactor(old, new, mod)
6882
end
6983

@@ -76,30 +90,31 @@ function _globalrefactor(old, new, mod)
7690
end
7791
end
7892

79-
function refactorfiles(old, new, obj, files)
93+
function refactorfiles(old, new, mod, files)
8094
id = "global_rename_refactor_progress"
8195
@info "Start global rename refactoring" progress=0 _id=id
8296

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 = ""
87102

88103
for (i, file) enumerate(files)
89104
@info "Refactoring: $file ($i / $total)" progress=i/total _id=id
90105
MacroTools.sourcewalk(file) do ex
91106
return if ex === oldsym
92107
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)
98111
else
99112
ex
100113
end
101114
end
102115
end
103116

104117
@info "Finish global rename refactoring" progress=1 _id=id
118+
119+
(:info, isempty(desc) ? "" : "Refactorings across modules\n\n" * desc)
105120
end

0 commit comments

Comments
 (0)