rust-lang / rust-lang/rust-analyzer
Does "Extract…" not work within macros?
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 16.9k
- Forks
- 2.2k
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 72
Description
Steps to reproduce:
-
cargo new <project> -
open
<project>/src/main.rsin VS Code with rust-analyzer:fn main() { println!("Hello, world!"); } -
select the string
"Hello, world!" -
right-click and select Refactor… from the context menu
Expected behavior:
- "Extract…" assists appear ("Extract into variable", "Extract into function")
Actual behavior:
- Only "Rewrite…" assists appear ("Rewrite as raw string", "Inline macro").
Notes:
-
I've verified that:
- "extract" works fine for strings in other contexts (e.g.
let s = "foo";) - the same behavior happens for non-strings in macros (e.g. I can't extract
2fromlet v = vec![1, 2, 3];)
- "extract" works fine for strings in other contexts (e.g.
-
I tried using "Inline macro" in hopes of expanding till I was able to extract the string, but this didn't work; the
println!call expands to{$crate::io::_print($crate::format_args_nl!("Hello, world!"));};which
rust-analyzerisn't happy with ($producesSyntax error: expected expression) — so I can't go on to expandformat_args_nl!as I would assume I'd need to to get to the string.
I'm new to both Rust and VS Code, so I may be missing something. Coming from CLion, though, where Rust plugin handles it just fine, it seems a little weird.
I'm guessing CLion's doing something a bit hacky, while rust-analyzer's extract assist is trying to guarantee correctness, but the std macros must be common enough, I wonder if it isn't worth a little special-casing.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the report from a new Cargo project by opening src/main.rs in VS Code with rust-analyzer and trying Refactor… on the string or a value inside a macro. Start by tracing rust-analyzer's extract-assist handling for macro arguments; done means the expected extraction assists appear without breaking the existing rewrite assists.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100