Pluto.jl icon indicating copy to clipboard operation
Pluto.jl copied to clipboard

Add macrocall cell_id to LineNumberNodes inside expanded expr

Open disberd opened this issue 1 year ago • 1 comments

In current Pluto the methods are deleted only if the file entry in the method signature is marked with the cell_id of the cell where that method is defined (by appending #==#$(cell_id) to the LineNumberNode). This is done to avoid deleting method signatures that were not defined within the notebook, see: https://github.com/fonsp/Pluto.jl/blob/2158cab91132ae337e68fb0b708dd6cc46305b88/src/runner/PlutoRunner.jl#L739-L750

This breaks for methods defined inside the expression return by a macro, as the LineNumberNodes there refer to the file where the macro was defined, and no cell_id is attached there by Pluto, making deleting those methods impossible.

If the function being defined inside the macro does not depend on types defined within the macro itself, the problem is avoided as the new method definition simply overwrites the previous one. The problem arises when both a type and a method on that type is defined within a macro expression, see https://github.com/fonsp/Pluto.jl/issues/2127.

What this PR does, is going into the expanded macro expression inside try_macroexpand and appending the cell_id of the point where the macro is called to the LineNumberNodes. To differentiate this from the standard cell_id insertion, the prefix I used for this procedure is #@#==# rather than #==#.

This seemed the cleanest approach to fix https://github.com/fonsp/Pluto.jl/issues/2127, and it provides information of the cell calling the macro which might be useful for other things.

I am open discuss alternative implementation if this is not considered acceptable.

Closes https://github.com/fonsp/Pluto.jl/issues/2127

disberd avatar Aug 07 '22 10:08 disberd

Try this Pull Request!

Open Julia and type:

julia> import Pkg
julia> Pkg.activate(temp=true)
julia> Pkg.add(url="https://github.com/disberd/Pluto.jl", rev="delete_macro_methods")
julia> using Pluto

github-actions[bot] avatar Aug 07 '22 10:08 github-actions[bot]

Interesting approach! Are the error locations from macros still clickable when the error is thrown for a macro in the notebook ?

macro m()
    quote
        throw("boom")
    end
end

@m() # what does the stack trace looks like?

Pangoraw avatar Aug 10 '22 12:08 Pangoraw

@Pangoraw I don't seem to be able to get clickable errors from macros with the code you pasted even on vanilla (latest) Pluto: image

If I put the the throw inside a function defined in the macro and call it still within the macro I get the jump-to links: image They did not work with the original PR but the latest push fixed that

disberd avatar Aug 10 '22 14:08 disberd

Very cool! :confetti_ball:

Pangoraw avatar Aug 17 '22 16:08 Pangoraw