Skip to content

Semantic highlighting does not distinguish escaped characters in string literals #2472

@Steffeeen

Description

@Steffeeen

Swift version

swift-6.2.3-RELEASE

Platform

macOS Sequoia 15.4.1

Editor

Visual Studio Code with Swift extension 2.16.0

Description

When using semantic highlighting, escaped characters in string literals are not highlighted in a separate color. The (default) TextMate grammar-based syntax highlighting creates separate tokens for escaped characters.

With semantic highlighting:

Image

Without semantic highlighting, using the TextMate grammar-based highlighting:

Image

VSCode token debug hover showing the different token types:

Image

I’m aware that sourcekit-lsp currently only forwards semantic tokens provided by sourcekitd / swift-syntax, and does not perform additional tokenization itself.

That said, it seems possible for this behavior to be implemented at the LSP level by augmenting or post-processing semantic tokens, even if the upstream tokens do not currently distinguish escaped characters.

However, I’m unsure whether this is the correct layer to address this issue. It may be more appropriate to implement this upstream. This would avoid splitting the tokenization logic across multiple projects. But I don't know which other projects consume the tokens and if it is thus possible / feasible to change this upstream.

I am open to working on this and creating a PR.

Steps to Reproduce

  1. Ensure semantic highlighting is turned on in VSCode: "editor.semanticHighlighting.enabled": true
  2. Open a Swift file with a string containing escaped characters, for example let a = "test \n \r \\ \' \""

Logging

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions