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

`@niceexpand`

Open oxinabox opened this issue 6 years ago • 2 comments

I have this code that I use for expanding things. I think it is like prettify but when I tried using prettify it engaged animal names, which (unlike gensym_ids) removes the names of things that were created via gensym(:meaningful)

Not sure what to do with it, but it is handy. Would a PR be useful?

using MacroTools
macro niceexpand(code)
    code = Base.macroexpand(Main, code, recursive=false)
    code = MacroTools.postwalk(MacroTools.unblock, code)
    code = MacroTools.postwalk(code) do x
        isexpr(x, :escape) ? x.args[1] : x
    end
    code = MacroTools.gensym_ids(code)
    code = MacroTools.striplines(code)
    return QuoteNode(code)
end

oxinabox avatar Sep 02 '19 14:09 oxinabox

You can ask prettify not to alias like that via a kwarg. That's probably less necessary now that gensyms are printed in a sensible way.

MikeInnes avatar Sep 02 '19 14:09 MikeInnes

I've got a very similar macro on my side. Maybe we should name it @prettyexpand for consistency, and change prettify accordingly.

The animal names are definitely prettier, but they're losing variable name information. I would replace the gensyms like var"#3#elapsedtime" with ¤elapsedtime, or some similar Unicode character that Julia won't var.

cstjean avatar Nov 13 '20 11:11 cstjean