rust-lang / rust-lang/rust-analyzer

[Proposal] Allow embedding other languages in macros

Open
#5,389 5 comments 21 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

E-hard S-unactionable
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

A lot of function-like procedural macros might have syntax alien to Rust (html! Yew macro comes to mind). Highlighing based on expansion is not always reliable as a lot of tokens might not end up in the final code.

So instead of trying to handle every case, I believe there should be an official way to extend rust-analyzer with custom language services and syntax highlights, this would allow library authors to offer better DX for their custom macros.

Proposal
A TextMate scope for macros (VSCode)

A custom scope like meta.macro-function.rust would allow syntax highlighting coming from somewhere else, since scope names cannot be dynamic, the scope should span the entire macro invocation (my_macro!(...)), this way rules can be matched based on the macro's name.

Registering external macro LSPs

External LSPs could be registered for macros and rust-analyzer would forward requests to them. In VSCode, this could be done via the extension API based on this stack overflow answer.

This way rust-analyzer can decide not to forward known macros it can handle, and there are no multiple LSPs processing the same document simultaneously, potentially clashing with each other.

Macros would be processed in the following order:

  • Known macros (e.g. println)
  • Registered LSP if found by the macro path
  • Best-effort based on expansion
Unresolved questions
  • I only considered VSCode, as I don't use or know the internals of other editors rust-analyzer supports.
  • Macros can be imported by a different name. Rust-analyzer could find out its original name/path, but this wouldn't work for syntax highlights.
  • Scrap the TextMate highlight support, and allow macro LSPs to provide semantic tokens.
  • This feature would further encourage people to write macros with custom alien syntax, but it's already happening anyway.

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

The proposal names no files, tests, or entry points; start by studying rust-analyzer's existing macro expansion and syntax-highlighting behavior, along with its VSCode integration. The unresolved questions about editor support, renamed macros, and semantic tokens need decisions before completion can be defined.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, vscode
Domain
developer-experience, devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.