Skip to content

Commit 54c40e1

Browse files
committed
update to #215
1 parent 7c730aa commit 54c40e1

File tree

2 files changed

+47
-51
lines changed

2 files changed

+47
-51
lines changed

src/refactor.jl

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
handle("renamerefactor") do data
22
@destruct [
3-
old,
4-
full,
5-
new,
3+
oldWord,
4+
fullWord,
5+
newWord,
66
# local context
77
column || 1,
88
row || 1,
@@ -11,74 +11,71 @@ handle("renamerefactor") do data
1111
# module context
1212
mod || "Main",
1313
] = data
14-
renamerefactor(old, full, new, column, row, startRow, context, mod)
14+
renamerefactor(oldWord, fullWord, newWord, column, row, startRow, context, mod)
1515
end
1616

1717
# NOTE: invalid identifiers will be caught by frontend
1818
function renamerefactor(
19-
old, full, new,
19+
oldword, fullword, newword,
2020
column = 1, row = 1, startrow = 0, context = "",
2121
mod = "Main",
2222
)
2323
# 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`")
2525

2626
mod = getmodule(mod)
27-
hstr = first(split(full, '.'))
27+
hstr = first(split(fullword, '.'))
2828
head = getfield′(mod, hstr)
2929

3030
# 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`"
3333
)
3434

3535
expr = CSTParser.parse(context)
36+
3637
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
4441
end
4542

4643
# local rename refactor if `old` isn't a toplevel binding
47-
if islocalrefactor(bind, old)
44+
if islocalrefactor(bind, oldword)
4845
try
49-
refactored = localrenamerefactor(old, new, column, row, startrow, context, expr)
46+
refactored = localrenamerefactor(oldword, newword, column, row, startrow, context, expr)
5047
return isempty(refactored) ?
5148
# 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)) :
5350
Dict(
5451
:text => refactored,
55-
:success => "_Local_ rename refactoring `$old` ⟹ `$new` succeeded"
52+
:success => "_Local_ rename refactoring `$oldword` ⟹ `$newword` succeeded"
5653
)
5754
catch err
58-
return Dict(:error => errdescription(old, new, err))
55+
return Dict(:error => errdescription(oldword, newword, err))
5956
end
6057
end
6158

6259
# global rename refactor if the local rename refactor didn't happen
6360
try
64-
kind, desc = globalrenamerefactor(old, new, mod, expr)
61+
kind, desc = globalrenamerefactor(oldword, newword, mod, expr)
6562

6663
# make description
6764
if kind === :success
68-
val = getfield′(mod, full)
65+
val = getfield′(mod, fullword)
6966
moddesc = if (head isa Module && head mod) ||
7067
(applicable(parentmodule, val) && (head = parentmodule(val)) mod)
71-
moduledescription(old, head)
68+
moduledescription(oldword, head)
7269
else
7370
""
7471
end
7572

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")
7774
end
7875

7976
return Dict(kind => desc)
8077
catch err
81-
return Dict(:error => errdescription(old, new, err))
78+
return Dict(:error => errdescription(oldword, newword, err))
8279
end
8380
end
8481

@@ -87,22 +84,22 @@ islocalrefactor(bind, name) = bind === nothing || name ≠ bind.name
8784
# local refactor
8885
# --------------
8986

90-
function localrenamerefactor(old, new, column, row, startrow, context, expr)
87+
function localrenamerefactor(oldword, newword, column, row, startrow, context, expr)
9188
bindings = localbindings(expr, context)
9289
line = row - startrow
93-
scope = currentscope(old, bindings, byteoffset(context, line, column))
90+
scope = currentscope(oldword, bindings, byteoffset(context, line, column))
9491
scope === nothing && return ""
9592

9693
currentcontext = scope.bindstr
97-
oldsym = Symbol(old)
98-
newsym = Symbol(new)
9994
newcontext = MacroTools.textwalk(currentcontext) do sym
95+
oldsym = Symbol(oldword)
96+
newsym = Symbol(newword)
10097
sym === oldsym ? newsym : sym
10198
end
10299

103100
replace(context, currentcontext => newcontext)
104101
end
105-
localrenamerefactor(old, new, column, row, startrow, context, expr::Nothing) = ""
102+
localrenamerefactor(oldword, newword, column, row, startrow, context, expr::Nothing) = ""
106103

107104
function currentscope(name, bindings, byteoffset)
108105
for binding in bindings
@@ -124,14 +121,14 @@ end
124121
# global refactor
125122
# ---------------
126123

127-
function globalrenamerefactor(old, new, mod, expr)
124+
function globalrenamerefactor(oldword, newword, mod, expr)
128125
entrypath, _ = if mod == Main
129126
MAIN_MODULE_LOCATION[]
130127
else
131128
moduledefinition(mod)
132129
end
133130

134-
files = modulefiles(entrypath)
131+
files = modulefiles(string(mod), entrypath)
135132

136133
# catch refactorings on an unsaved / non-existing file
137134
isempty(files) && return :warning, unsaveddescription()
@@ -143,14 +140,14 @@ function globalrenamerefactor(old, new, mod, expr)
143140
end
144141

145142
with_logger(JunoProgressLogger()) do
146-
_globalrenamerefactor(old, new, mod, expr, files)
143+
_globalrenamerefactor(oldword, newword, mod, expr, files)
147144
end
148145
end
149146

150-
function _globalrenamerefactor(old, new, mod, expr, files)
147+
function _globalrenamerefactor(oldword, newword, mod, expr, files)
151148
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)
154151

155152
total = length(files)
156153
# TODO: enable line location information (the upstream needs to be enhanced)
@@ -171,9 +168,9 @@ function _globalrenamerefactor(old, new, mod, expr, files)
171168
push!(modifiedfiles, fullpath(file))
172169
Expr(:., m, newsym)
173170
# macro case
174-
elseif ismacro && @capture(ex, macro $(Symbol(old))(args__) body_ end)
171+
elseif ismacro && @capture(ex, macro $(Symbol(oldword))(args__) body_ end)
175172
push!(modifiedfiles, fullpath(file))
176-
Expr(:macro, :($(Symbol(new))($(args...))), :($body))
173+
Expr(:macro, :($(Symbol(newword))($(args...))), :($body))
177174
else
178175
ex
179176
end
@@ -185,7 +182,7 @@ function _globalrenamerefactor(old, new, mod, expr, files)
185182
return if !isempty(modifiedfiles)
186183
:success, filesdescription(mod, modifiedfiles)
187184
else
188-
:warning, "No rename refactoring occured on `$old` in `$mod` module."
185+
:warning, "No rename refactoring occured on `$oldword` in `$mod` module."
189186
end
190187
end
191188

@@ -239,9 +236,9 @@ function filesdescription(mod, files)
239236
"""
240237
end
241238

242-
function errdescription(old, new, err)
239+
function errdescription(oldword, newword, err)
243240
"""
244-
Rename refactoring `$old` ⟹ `$new` failed.
241+
Rename refactoring `$oldword` ⟹ `$newword` failed.
245242
246243
<details><summary>Error:</summary><pre><code>$(errmsg(err))</code></p></details>
247244
"""

src/static/toplevel.jl

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ struct ToplevelTupleH <: ToplevelItem
2525
end
2626

2727
"""
28-
toplevelitems(text; kwargs...)::Vector{ToplevelItem}
28+
toplevelitems(text::String; kwargs...)::Vector{ToplevelItem}
29+
toplevelitems(text::String, expr::CSTParser.EXPR; kwargs...)::Vector{ToplevelItem}
2930
3031
Finds and returns toplevel "item"s (call and binding) in `text`.
3132
@@ -34,16 +35,14 @@ keyword arguments:
3435
other than `mod`, otherwise enter into every module.
3536
- `inmod::Bool`: if `true`, don't include toplevel items until it enters into `mod`.
3637
"""
37-
function toplevelitems(text; kwargs...)
38-
parsed = CSTParser.parse(text, true)
39-
_toplevelitems(text, parsed; kwargs...)
40-
end
38+
toplevelitems(text::String; kwargs...) = _toplevelitems(text, CSTParser.parse(text, true); kwargs...)
39+
toplevelitems(text::String, expr::CSTParser.EXPR; kwargs...) = _toplevelitems(text, expr; kwargs...)
40+
toplevelitems(text::String, expr::Nothing; kwargs...) = ToplevelItem[]
4141

4242
function _toplevelitems(
43-
text, expr,
44-
items::Vector{ToplevelItem} = Vector{ToplevelItem}(), line = 1, pos = 1;
45-
mod::Union{Nothing, String} = nothing,
46-
inmod::Bool = false,
43+
text::String, expr::CSTParser.EXPR,
44+
items::Vector{ToplevelItem} = ToplevelItem[], line::Int = 1, pos::Int = 1;
45+
mod::Union{Nothing, String} = nothing, inmod::Bool = false,
4746
)
4847
shouldadd = mod === nothing || inmod
4948

0 commit comments

Comments
 (0)