Macro_rules autocompletion, is it only me?

I have always been kind of reluctant to work with macros in rust for one reason: lack of auto completion in the block.

How do you deal with that ?
I tend to write the "specialized" code first out of the macro rule block, and then once I know it's working, put this code in the macro_rules with the different incoming expressions/idents.. and blindly trust it's ok from then on.

If a modification has to be made, I extract the whole block, do the modifications, test a specialized version and then put it back in the macro.

I just can't iterate from inside the macro as the IDE leaves me alone (in the dark ?).

So much that I replace the macros with closures all the time. There must be something wrong with my approach.

I work in VSCode with an up to date Debian system, weekly updated nightly toolchain and latest rust analyzer.
Macro expansion is on.

Is there an option/ course of actions I missed somewhere ?

Thanks guys

1 Like

If I can write it as a closure (or more often, a generic function), I don't make a macro. I rarely have custom macros in expression context.

Other than that this is pretty much the same as my experience.

3 Likes

I once spent around half a decade working on a codebase in a hacked-together literate D toolchain that not only didn't have autocomplete, it didn't even have proper syntax highlighting. I either knew what I was writing, or looked up what I needed from the docs.

Auto complete is nice, but in no way required.

If I really want autocomplete for a bit of code, then just like you I copy it out of the macro body into a suitable "sandbox", edit it there, then paste it back into the macro.

Trying to do autocomplete inside a macro is too difficult for anyone to seriously bother with. Macros can perform arbitrary transformations on the input tokens, so the IDE has no way of knowing whether the code you're typing is really even real Rust code in the first place, let alone how to suggest what might come next.

There's probably a useful subset that could be carved out, but I haven't seen anything along those lines yet.

3 Likes

Ok so I am not the only one,

Thanks a lot guys, really appreciated!

I'm also in the camp of autocomplete being nice, but not required.

And my approach to macros is similar too: write a concrete version that works, and then macro-ize it.

Whether tweaks require a non-macro version or not depends on how major the tweak is. If it's not a complicated or large change I'm generally comfortable working with the macro directly. My intuition is that this is partially experience and familiarity with the language, but also partially not being dependent on autocomplete in a general sense.

Basically, if you're comfortable and "fluent" writing code without completion assistance, you miss it less when it's gone. It might slow you down, but it's not crippling .

4 Likes

I get that too, thanks quinedot

I dealt with this by switching from IntelliJ to Sublime Text so that I wouldn't be held back by considerations around whether an IDE could understand what I was doing.

Soon I will get to the next step: abandoning my corporeal form and ascending to a realm of Pure Code.

2 Likes

:grin:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.