|
| 1 | +handle("refactor") do data |
| 2 | + @destruct [ |
| 3 | + old, |
| 4 | + new, |
| 5 | + path, |
| 6 | + # local context |
| 7 | + column || 1, |
| 8 | + row || 1, |
| 9 | + startRow || 0, |
| 10 | + context || "", |
| 11 | + # module context |
| 12 | + mod || "Main", |
| 13 | + ] = data |
| 14 | + refactor(old, new, path, column, row, startRow, context, mod) |
| 15 | +end |
| 16 | + |
| 17 | +function refactor( |
| 18 | + old, new, path, |
| 19 | + column = 1, row = 1, startrow = 0, context = "", |
| 20 | + mod = "Main", |
| 21 | +) |
| 22 | + expr = CSTParser.parse(context, true) |
| 23 | + bind = CSTParser.bindingof(expr) |
| 24 | + |
| 25 | + # local refactor only if `old` is really a local binding |
| 26 | + if bind === nothing || old != bind.name |
| 27 | + try |
| 28 | + refactored = localrefactor(old, new, path, column, row, startrow, context) |
| 29 | + isempty(refactored) || return Dict(:text => refactored) |
| 30 | + catch err |
| 31 | + @error err |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + try |
| 36 | + refactored = localrefactor(old, new, path, column, row, startrow, context) |
| 37 | + isempty(refactored) || return Dict(:text => refactored) |
| 38 | + catch err |
| 39 | + @error err |
| 40 | + end |
| 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") |
| 49 | +end |
| 50 | + |
| 51 | +function localrefactor(old, new, path, column, row, startrow, context) |
| 52 | + old = first(split(old, '.')) # ignore dot accessors |
| 53 | + position = row - startrow |
| 54 | + |
| 55 | + return if old ∈ map(l -> l[:name], locals(context, position, column)) |
| 56 | + oldsym = Symbol(old) |
| 57 | + quote |
| 58 | + MacroTools.textwalk($context) do ex |
| 59 | + @capture(ex, $oldsym) ? Symbol($new) : ex |
| 60 | + end |
| 61 | + end |> eval |
| 62 | + else |
| 63 | + "" |
| 64 | + end |
| 65 | +end |
| 66 | + |
| 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 |
0 commit comments