Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A macro expansion visualizer tool would be nice! #2400

Open
gerdint opened this issue Feb 12, 2024 · 6 comments
Open

A macro expansion visualizer tool would be nice! #2400

gerdint opened this issue Feb 12, 2024 · 6 comments

Comments

@gerdint
Copy link
Contributor

gerdint commented Feb 12, 2024

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.

@gerdint gerdint changed the title Add macro expansion visualizer tool No macro expansion visualizer tool... Feb 12, 2024
@gerdint gerdint changed the title No macro expansion visualizer tool... A macro expansion visualizer tool would be nice! Feb 12, 2024
@gerdint
Copy link
Contributor Author

gerdint commented Feb 12, 2024

I think the "peek def"-kind of UI would be a nice, if it can be implemented.

@PEZ
Copy link
Collaborator

PEZ commented Feb 12, 2024

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.

@PEZ
Copy link
Collaborator

PEZ commented Feb 13, 2024

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:

  1. Replace the current form with the results.
  2. 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.

@gerdint
Copy link
Contributor Author

gerdint commented Feb 13, 2024

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.

@PEZ
Copy link
Collaborator

PEZ commented Feb 13, 2024

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:

image

@PEZ
Copy link
Collaborator

PEZ commented Feb 13, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants