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

Refactor and improve macro debugging/general utilities #5949

Open
RDambrosio016 opened this issue Sep 4, 2020 · 3 comments
Open

Refactor and improve macro debugging/general utilities #5949

RDambrosio016 opened this issue Sep 4, 2020 · 3 comments
Labels
A-ide general IDE features A-macro macro expansion E-hard fun A technically challenging issue with high impact S-unactionable Issue requires feedback, design decisions or is blocked on other work

Comments

@RDambrosio016
Copy link
Contributor

Rust analyzer's expand macro recursively command is nice, however, it is also greatly flawed and provides little to no utility for debugging more complex macros. This issue is aimed at refactoring it and turning it into a useful tool.

Flaws with expand macro recursively

  • No formatting is applied to the expanded code, most people dont care about formatting inside macros, so the output ends up looking pretty horrid.
  • The macro expander expands all the macros recursively (duh). This may seem useful, but it often just causes more confusion around what is coming from the main macro and what is recursively expanded, and it generally just outputs miscellaneous code you dont care about.
  • There is no way of knowing what code came from what binding, and the expansion does not show the macro either, so now you have to go back and forth to try and see what is what.

Proposal for refactoring the command

Instead of a simple text read only editor, we can make use of vsc's custom editor windows extension to make a prettier and more useful editor.
The changes would have to be made in steps:

  • Refactor hir::sema::expand to give more info about what code came from what binding, i suspect this will be the hardest part.
  • Implement a basic editor which colors code blocks depending on where they came from.
  • (maybe) implement highlighting in the actual source code when the editor is live.
  • Make an experimental command (not sure of how RA handles experimental features).
  • Deprecate the expand macro recursively command and eventually phase it out.

Challenges

  • Not all macros are pretty, some are very cursed, especially recursive macros, displaying them in a nice way is going to be a challenge.
  • Modeling what bindings created what code may be a challenge in the expansion code.
  • Good editor design is hard :(

So what might it look like? 👀

I am by no means a design guy, and i realize this is a blob of colors, but this is conceptually what it might look like:

Design is hard

If anyone has suggestions then please put them below.

@flodiebold flodiebold added the S-unactionable Issue requires feedback, design decisions or is blocked on other work label Dec 21, 2020
@matklad
Copy link
Member

matklad commented Mar 29, 2021

As a more x-editor alternative, we can implement a custom "macro explorer" virtual buffer with a special language type, where we can use custom syntax highlighting, assists, etc. That is, the 💡 Explore Macro Call opens a split editor with one-level macro expansion, where there's 💡 Expand on each specific recursive call, which changes the text in-place.

@matklad matklad added E-hard fun A technically challenging issue with high impact labels Mar 29, 2021
@matklad
Copy link
Member

matklad commented Mar 30, 2021

Actually, I think just "expand macro in-place" code action might improve usability a bunch?

@ahicks92
Copy link

As a blind user who happened to stumble on this and could certainly have used macro debugging tools the other day, going to just chime in that custom editors are difficult from an accessibility perspective and that the built-in VSCode pieces (generally) just work.

That's not to say you can't make an accessible editor, but most of the off-the-shelf things you'd grab off NPM or whatever else will usually be entirely inaccessible because they implement the entire thing with CSS and divs/spans/insert-your-favorite-inaccessible-technique. I'm very definitely a backend guy, so I can't provide an accessible option here I'm afraid, but VSCode had to put a tremendous amount of effort into getting their component to work reliably with screen readers by basically implementing a second mode where the thing is an actual multiline text field, then adding a whole other virtual paging thing on top to deal with some screen reader/chrome bugs that exist for very silly reasons. I think that's both a statement as to the complexity and a possible option being as I think their component is either separated or at least separable from VSCode. Also I think VSCode offers some sort of component library for extension authors that's supposed to be accessible, but i'm not sure on the details.

I think my ideal would be some sort of expansion inline, but I haven't given what the UX here would look like much thought and the version I'd want vs. the version someone sighted would want are probably very different.

bors added a commit that referenced this issue Jan 9, 2023
Add action to expand a declarative macro once, inline. Fixes #13598

This commit adds a new r-a method, `expandMacroInline`, which expands the macro that's currently selected. See  #13598 for the most applicable issue; though I suspect it'll resolve part of #5949 and make #11888 significantly easier).

The macro works like this:

![rust-analyser-feature](https://user-images.githubusercontent.com/10906982/208813167-3123e379-8fd5-4206-a4f4-5af1129565f9.gif)

I have 2 questions before this PR can be merged:

1. **Should we rustfmt the output?** The advantage of doing this is neater code. The disadvantages are we'd have to format the whole expr/stmt/block (since there's no point just formatting one part, especially over multiple lines), and maybe it moves the code around more in weird ways. My suggestion here is to start off by not doing any formatting; and if it appears useful we can decide to do formatting in a later release.
2.   **Is it worth solving the `$crate` hygiene issue now?** -- I think this PR is usable as of right now for some use-cases; but it is annoying that many common macros (i.e. `println!()`, `format!()`) can't be expanded further unless the user guesses the correct `$crate` value. The trouble with solving that issue is that I think it's complicated and imperfect. If we do solve it; we'd also need to either change the existing `expandMacro`/`expandMacroInline` commands; provide some option to allow/disallow `$crate` expanding; or come to some other compromise.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ide general IDE features A-macro macro expansion E-hard fun A technically challenging issue with high impact S-unactionable Issue requires feedback, design decisions or is blocked on other work
Projects
None yet
Development

No branches or pull requests

5 participants