Skip to content

Strict Rust highlighting is not a good fit inside macro invocations #51437

Description

@jordanhalase

Reproduction steps

  1. Start Zed with Rust syntax highlighting, Semantic Tokens OFF.
  2. Type in a valid arbitrary custom macro invocation: e.g., foo! { bar- <b"none"> }
  3. Tokens are not highlighted correctly (particularly the string literal)

Current vs. Expected behavior

Currently Zed treats syntax highlighting within a Rust macro as Rust code with hard-coded exceptions for view!, html! and now bsn!. These hard-coded exceptions are good for special cases, but for all other macros, syntax should be highlighted generically that won't break whatever syntax a programmer may come up with in a macro.

In a general sense, it is okay to highlight the innards of a macro invocation loosely as Rust, such for key words like let and while, known literals and identifiers, but treating it strictly as Rust leads to inconsistent highlighting issues. These can be fixed as follows:

  1. Keep all PUNCT tokens (@!#.,<>?/ etc.) the same color/style (< does not need a corresponding > in macros and may be grouped with any other PUNCT, e.g., Brainfuck)
    a. ()[]{} can remain styled differently because they are required to enclose each other.
    b. A literal immediately preceding ! can remain a unique color to look like an inner_macro! invocation
    c. A literal immediately preceding ( can remain a unique color to look like a function call
  2. Allow PUNCT tokens directly next to string literals without breaking the highlighting: e.g., <c"cstring", <"foo"
  3. b and c do NOT signify c-string/bytestrings if part of a literal, i.e., 123c"foo" tokenizes as 123c followed by "foo"
    (Zed currently highlights 123c"foo" incorrectly, with colors separated at 123 and c"foo")

If the above points are fixed, then Rust code written inside of macros will continue to look like Rust code highlighting, while also allowing developers to create any arbitrary custom macros without immediately breaking the highlighting.

Current

Image

Expected

(VS Code handles this perfectly)

Image

Zed version and system specs

Zed 0.227.1
9f9b707

0.227.1+stable.199.9f9b707d39d62468d4cdaed2d8eb95a847da4cf9

Windows 11/macOS Tahoe 26.3.1

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:tree-sitterSyntax highlighting and tree-sitterreach:many usersBugs that happen for at least a third of the users across all platforms and kinds of usageseverity:S3Papercuts, minor issues with a clear non-tedious workaround, cosmetic bugsstaleLabel used by `stale` actionstate:reproducibleVerified steps to reproduce included or someone on the team managed to reproduce

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions