rust-lang / rust-lang/rust-analyzer

Injected rustdoc code blocks are not highlighted when used with a proc-macro attribute

Open
#21,025 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-highlighting A-macro C-feature
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

This is related to #4170 but I'm hoping it might be possible to implement without larger architectural changes? Since many other RA features (including highlighting e.g. for fn parameters) can "look through" macros like this, I wonder if it's possible to add the same functionality to semantic token injection for rustdoc code blocks.

Example pmacro/lib.rs

use proc_macro::TokenStream;

// trivially emit the same tokens without modification
#[proc_macro_attribute]
pub fn noop(_attr: TokenStream, tokens: TokenStream) -> TokenStream {
    tokens
}

lib.rs:

use pmacro::noop;

#[noop]
/// ```
/// println!("xyz");
/// ```
pub fn foo() {}

The resulting [EXPANSION].rs looks like

// Recursive expansion of noop macro
// ==================================

#[doc = " ```"]
#[doc = " println!(\"xyz\");"]
#[doc = " ```"]
pub fn foo() {}

In this case, I expect the println! code block to be highlighted with *.injected semantic tokens, but the actual result is that nothing is highlighted as long as the proc macro attribute is present. I also tried moving the #[noop] attribute after the doc comments, but the result is the same either way.

Image

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue using the proc-macro example in pmacro/lib.rs and the annotated function in lib.rs. Start by examining semantic token injection for rustdoc code blocks across the generated [EXPANSION].rs output. Done means the println! code block receives *.injected semantic tokens even when the #[noop] proc-macro attribute is present.

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
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.