Skip to content

Commit 486b619

Browse files
committed
local refactor only on the current scope
1 parent 517d645 commit 486b619

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

src/refactor.jl

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function renamerefactor(
3737
# local rename refactor if `old` isn't a toplevel binding
3838
if bind === nothing || old bind.name
3939
try
40-
refactored = localrefactor(old, new, path, column, row, startrow, context)
40+
refactored = localrefactor(old, new, path, column, row, startrow, context, expr)
4141
isempty(refactored) || return Dict(
4242
:text => refactored,
4343
:success => "_Local_ rename refactoring `$old` ⟹ `$new` succeeded"
@@ -86,16 +86,38 @@ end
8686
# local refactor
8787
# --------------
8888

89-
function localrefactor(old, new, path, column, row, startrow, context)
90-
return if old map(l -> l[:name], locals(context, row - startrow, column))
91-
oldsym = Symbol(old)
92-
newsym = Symbol(new)
93-
MacroTools.textwalk(context) do sym
94-
sym === oldsym ? newsym : sym
89+
function localrefactor(old, new, path, column, row, startrow, context, expr)
90+
bindings = local_bindings(expr, context)
91+
line = row - startrow
92+
scope = current_scope(old, bindings, byteoffset(context, line, column))
93+
scope === nothing && return ""
94+
95+
current_context = scope.bindstr
96+
oldsym = Symbol(old)
97+
newsym = Symbol(new)
98+
new_context = MacroTools.textwalk(current_context) do sym
99+
sym === oldsym ? newsym : sym
100+
end
101+
102+
replace(context, current_context => new_context)
103+
end
104+
105+
function current_scope(name, bindings, byteoffset)
106+
for binding in bindings
107+
isa(binding, LocalScope) || continue
108+
109+
scope = binding
110+
if byteoffset in scope.span &&
111+
any(bind -> bind isa LocalBinding && name == bind.name, scope.children)
112+
return scope
113+
else
114+
let scope = current_scope(name, scope.children, byteoffset)
115+
scope !== nothing && return scope
116+
end
95117
end
96-
else
97-
""
98118
end
119+
120+
return nothing
99121
end
100122

101123
# global refactor

0 commit comments

Comments
 (0)