Skip to content

Commit 4682c11

Browse files
committed
even better messaging
1 parent e618a16 commit 4682c11

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

src/refactor.jl

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,14 @@ function renamerefactor(
2323
mod = getmodule(mod)
2424

2525
# 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`")
26+
modulenote = if (obj = first(split(full, '.'))) != old
27+
if (parentmod = getfield′(mod, obj)) isa Module && parentmod != mod
28+
"**NOTE**: `$old` is defined in `$parentmod` -- you may need the same rename refactorings in that module as well."
29+
else
30+
return Dict(:warning => "Rename refactoring on a field isn't available: `$obj.$old`")
31+
end
32+
else
33+
""
2834
end
2935

3036
# local refactor only if `old` is really a local binding
@@ -34,20 +40,29 @@ function renamerefactor(
3440
refactored = localrefactor(old, new, path, column, row, startrow, context)
3541
isempty(refactored) || return Dict(
3642
:text => refactored,
37-
:success => "Local rename refactoring `$old` ⟹ `$new` succeeded"
43+
:success => "_Local_ rename refactoring `$old` ⟹ `$new` succeeded"
3844
)
3945
catch err
4046
@error err
4147
end
4248
end
4349

4450
try
45-
val = getfield′(mod, old)
46-
kind, description = globalrefactor(old, new, mod, val)
51+
val = getfield′(mod, full)
52+
# catch keyword renaming
53+
if val isa Undefined && Symbol(old) in keys(Docs.keywords)
54+
return Dict(:warning => "Keywords can't be renamed: `$old`")
55+
end
56+
# update modulenote
57+
if isempty(modulenote) && applicable(parentmodule, val) && (parentmod = parentmodule(val)) != mod
58+
modulenote =
59+
"**NOTE**: `$old` is defined in `$parentmod` -- you may need the same rename refactorings in that module as well."
60+
end
61+
kind, desc = globalrefactor(old, new, mod)
4762
return Dict(
48-
kind => description,
49-
:success => kind !== :info ? false :
50-
"Global rename refactoring `$old` ⟹ `$new` succeeded"
63+
kind => kind === :success ?
64+
join(("_Global_ rename refactoring `$old` ⟹ `$new` succeeded.", modulenote, desc), "\n\n") :
65+
desc
5166
)
5267
catch err
5368
@error err
@@ -74,14 +89,7 @@ end
7489
# global refactor
7590
# ---------------
7691

77-
globalrefactor(old, new, mod, @nospecialize(val)) = _globalrefactor(old, new, mod) # general case
78-
function globalrefactor(old, new, mod, val::Undefined)
79-
Symbol(old) in keys(Docs.keywords) ?
80-
(:warning, "Keywords can't be renamed: `$old`") :
81-
_globalrefactor(old, new, mod)
82-
end
83-
84-
function _globalrefactor(old, new, mod)
92+
function globalrefactor(old, new, mod)
8593
entrypath, line = moduledefinition(mod)
8694
files = modulefiles(entrypath)
8795

@@ -97,18 +105,18 @@ function refactorfiles(old, new, mod, files)
97105
oldsym = Symbol(old)
98106
newsym = Symbol(new)
99107
total = length(files)
100-
desc = ""
108+
109+
# TODO: enable line location information (the upstream needs to be enhanced)
110+
refactoredfiles = Set{String}()
101111

102112
for (i, file) enumerate(files)
103113
@info "Refactoring: $file ($i / $total)" progress=i/total _id=id
104114
MacroTools.sourcewalk(file) do ex
105115
return if ex === oldsym
116+
push!(refactoredfiles, fullpath(file))
106117
newsym
107118
elseif @capture(ex, m_.$oldsym) && getfield′(mod, Symbol(m)) isa Module
108-
# TODO: enable line location information (the upstream needs to be enhanced)
109-
file = fullpath(file)
110-
link = "atom://julia-client/?open=true&file=$(file)&line=0"
111-
desc *= "- `$m.$old` ⟹ `$m.$new` in [$file]($link)\n"
119+
push!(refactoredfiles, fullpath(file))
112120
Expr(:., m, newsym)
113121
else
114122
ex
@@ -118,5 +126,10 @@ function refactorfiles(old, new, mod, files)
118126

119127
@info "Finish global rename refactoring" progress=1 _id=id
120128

121-
(:info, isempty(desc) ? "" : "Refactorings across modules\n" * desc)
129+
return if !isempty(refactoredfiles)
130+
filelist = ("- [$file](atom://julia-client/?open=true&file=$(file)&line=0)" for file in refactoredfiles)
131+
(:success, string("Refactored files (all in `$mod` module):\n\n", join(filelist, '\n')))
132+
else
133+
(:warning, "No rename refactoring occured on `$old`")
134+
end
122135
end

0 commit comments

Comments
 (0)