A macro expansion visualizer tool would be nice!
A classic Lisp IDE feature is the ability to visualize expansion of macros. This gets more interesting due to the recursive nature of this problem but at least some basic 1st level-expand functionality (akin to clojure.core/macroexpand-1) would be nice (see below). So I'm thinking some command to macroexplanding forms (for the usual meaning/variations of "forms"). Bonus points for the ability to recursively expand more layers of macros, though a simple toggle for 1st level vs all levels would be a good start.
Example of prior art: https://docs.cider.mx/cider/debugging/macroexpansion.html Cursive also has a macro-expander, and DrRacket a particularly nice Macro debugger/stepper.
This is my current poor mans Custom REPL Commands-based implementation of this functionality:
{
"name": "Macroexpand-1 Current Form",
"key": "1",
"snippet": "(macroexpand-1 '$current-form)"
},
{
"name": "Macroexpand-all Current Form",
"key": "e",
"snippet": "(macroexpand-all '$current-form)"
},
{
"name": "Macroexpand Current Selection",
"key": "s",
"snippet": "(macroexpand '$selection)"
},
Basic UX question: Where to print the expanded form? A hover window would probably be nicest. Another dedicated window would be another option. The REPL is probably not much better than what the above commands give.
I think the "peek def"-kind of UI would be a nice, if it can be implemented.
Sounds awesome with support for this. I'll have a think about the UI. You could post about it in the Calva channel on slack too to hopefully get more ideas.
The REPL is probably not much better than what the above commands give
A bit unrelated: One thing that springs to mind, looking at how CIDER supports the macro expand tooling, is if we added some options to custom commands:
- Replace the current form with the results.
- Add the results to a rich comment form. And if it is performed in a rich comment form that option would cause the result to be added to the same comment form below whatever is being expanded.
Less unrelated: With such mechanisms in place, we could implemen macro expand tooling built-in using those.
Another option (we could support this in several ways) is to have a setting Include macroexpand in Hover? off, expand-1, expand-all when enabled will cause us to add the expansion to the regular hover. And a command for toggling this setting, similar to the command for toggling pretty print.
If we do both we would support the expand-again option naturally with the rich comment mechanism.
Expanding macros into comment forms sounds like a great first step. Another option to adding it below what was just expanded (when invoked from inside a RCF) would be to replace the form with its expansion, i.e. the recursive behaviour that Cider sports.
For the hovering, you mean when you hover any macro use you see the expansion there? That sounds sweet.
Ultimately, I think it would be nice to integrate the macroexpand-1, macroexpand-all and macroexpand functionality in the same UI, but which would require some more interaction.
For the hovering, you mean when you hover any macro use you see the expansion there?
Yes. That's the only hover we have available. But since it's already a bit crowded, it could be good to be able to toggle the behaviour quickly.
We can also add “buttons” (links) in the hover for macros, that when clicked will expand the macro to a rich comment. Similar to what we do for clojuredocs examples:
If you want to have a feel for how it would be to use a command that would put the expansion in a rich comment, try the command Calva: Add Rich Commment.