jonathanBieler / Refactoring.jl

Refactoring tools for Julia language

Repository from Github https://github.comjonathanBieler/Refactoring.jlRepository from Github https://github.comjonathanBieler/Refactoring.jl

Continuity

hovi opened this issue · comments

This repo is great starting point for any kind of advanced tools for refactoring.
It doesn't work well when functions or arrays are involved. I don't mind working on this a little cause I think it can save us a lot of work in long run, but some help would be good.

Hey, which part doesn't work, the extract method or the search and replace ?

But otherwise the first step would be to write a test where the method fails and start from there, e.g. :

https://github.com/jonathanBieler/Refactoring.jl/blob/master/test/test_extract_method.jl

extract_method is not working for me, I haven't tried the other one.
Okay narrowed it down to problems with accessing multidimensional array, the failing test is here:
https://github.com/hovi/Refactoring.jl/blob/master/test/test_extract_method.jl#L147

Ha I see, I have a @test_broken there so I think this case never worked, I don't remember why... will have a look.

Edit : ha no, you added that.

The way the extract method works is that it first convert the AST into a typed AST where each node gets a type depending on wether it's an assignment (lhs = rhs), a reference (x[i]), etc. That allows to use multiple dispatch to figure out which variables are assigned, which makes the code much cleaner. But for some reason x[i,1] = 1 doesn't get transformed into a a reference type :

julia> Refactoring.econvert(quote x[i] = 1 end)
quote
    #= REPL[34]:1 =#
    Refactoring.EAssignment(Refactoring.ERef(:x, :i), 1)
end

julia> Refactoring.econvert(quote x[i,1] = 1 end)
quote
    #= REPL[35]:1 =#
    Refactoring.EAssignment(:(x[i, 1]), 1)
end

Hahah yeah I added that one :) any hints on how to move forward? I am not sure but I think there will be more because sometimes it was crashing (like this is) and sometimes it was instead generating function with 0 parameters.

This should solve the issue, at least multidimensional references seems to work now. I was allowing only references with two arguments before (x[i]).

921e4c5

julia> Refactoring.variables(Refactoring.econvert(quote x[a,b,c] = 1 end))
4-element Vector{Any}:
 :x
 :a
 :b
 :c

I wouldn't be surprised there's more issues though, it's hard to think of all the cases... I also tried to refactor the code a bit and add comments, I couldn't understand some of it anymore.

Looking good! I tried to feed it with big chunks of the dirtiest code I could find and everything worked so far, this will be huge time saver for us, thanks! Do you have "buy me a coffee" or anything?

Haha no, it's ok. How do you plan to use it ? I used to have it in my own editor but I'm using VS code now and it's a feature that I miss. It's just a pain in the ass (and error prone) to extract methods manually.

Even manually it will save a ton for us, because our methods are so huge with so many parameters so that's still nothing compared to "manual refactoring".

I commented on relevant VS code issue here:
julia-vscode/julia-vscode#2821

I created fast hacky integration that can be called using Ctrl+Shift+P on current selection, branched here:
https://github.com/hovi/julia-vscode/tree/extract-method