Skip to content

[C#] Fix various string literal highlighting issues#4484

Merged
deathaxe merged 15 commits intosublimehq:masterfrom
deathaxe:pr/cs/refactor-strings
Apr 2, 2026
Merged

[C#] Fix various string literal highlighting issues#4484
deathaxe merged 15 commits intosublimehq:masterfrom
deathaxe:pr/cs/refactor-strings

Conversation

@deathaxe
Copy link
Copy Markdown
Collaborator

Addresses #4468 "Multi-line" strings.

This PR is another iteration of refactoring and improving C# string literals.

It ...

  1. contains changes to structure in order to organize related contexts in a more tree like structure, grouped by string literal types.
  2. renames string contexts by their language specific types. "Long strings" become "Verbatim Strings".
  3. removes invalid patterns for e.g. @$"""...""". Those are just verbatim strings starting with escaped quotes.
  4. fixes various escaping issues
  5. adds various placeholder and interpolation related bailouts to handle incomplete expressions.
  6. add support for $$$"""...""" and $$$$"""...""" raw strings
  7. adjusts string scopes to align with Python

deathaxe added 11 commits March 28, 2026 18:59
This commit ...

1. removes `_literal` suffix from normal raw-strings to match context naming
   scheme with `inside_string` and `inside_long_string`.
2. renames `_long` to `_verbatim` as C# spec uses the later term for string
   literals starting with `@"`
This commit removes patterns of an unsupported raw string literals.

Verbatim strings may start with escaped quotes `""`, which makes them look
like verbatim raw string literals.

C# 11 and 12 raise compiler error, only if `@` is combined with multiple `$` chars, such as `@$$" ... "`.
This commit removes `string_escaped` import from raw strings
as those are interpreted literal.
This commit renames `_escaped` to `_escapes` in context names,
to denote their no-popping behavior via plural.
This commit ...

1. restricts allowed number of subsequent `{` or `}`, depending on amount
   of `$` a raw string literal starts with.

2. adds dedicated `raw_string_placeholder_format` without `string_escapes`
   context.
1. sort string contexts by quoted, verbatim and raw strings
2. move extended placeholders contexts to to related string content contexts
   as interpolation contexts are organized this way.
3. include `string_placeholders` and `string_placeholder_escapes` separately.
This commit adds and tweaks interpolation bailouts in double quoted strings.
This commit changes scope to avoid confusion between verbatim and raw strings.
This commit scopes raw strings `string.quoted.block`, because...

1. they may be enclosed by any number of double quotes, which renders `.triple`
   scope name a bit inaccurate.
2. Python also scopes multi-line strings this way.
@deathaxe deathaxe marked this pull request as draft March 29, 2026 12:36
This commit reverts some bailout related changes to fix support
of double quoted strings within interpolation expressions.
@deathaxe deathaxe marked this pull request as ready for review March 29, 2026 13:08
@deathaxe
Copy link
Copy Markdown
Collaborator Author

This PR seems to blow up PowerShell syntax due to context sanity limit being exceeded.

In Python we explicitly excluded SQL syntax highlighting in interpolated strings to discourage interpolated queries for security reasons.

Should we therefore remove SQL highlighting from C#'s verbatim format strings? It would resolve PowerShell.

@michaelblyons
Copy link
Copy Markdown
Collaborator

michaelblyons commented Mar 30, 2026

PowerShell explicitly disables SQL in nested C#. If this PR renames one of those contexts, it needs a change in PowerShell, too.

Or do #4416 (comment)

@deathaxe
Copy link
Copy Markdown
Collaborator Author

Gotcha! Actually, all are renamed. Luckily those are cheap enough to have both versions in overridng syntax. I had to rename them as I wasn't able to wrap my head around the old naming vs. naming in specs.

This commit helps 3rd-party syntaxes to extend C# and add common patterns
to string literals by adding a `string_prototype` context.
@michaelblyons
Copy link
Copy Markdown
Collaborator

Good catch, BTW, on

  1. removes invalid patterns for e.g. @$"""...""". Those are just verbatim strings starting with escaped quotes.

Comment thread C#/C#.sublime-syntax
Comment thread C#/C#.sublime-syntax
@michaelblyons
Copy link
Copy Markdown
Collaborator

michaelblyons commented Apr 1, 2026

(I wrote most of this a couple days ago but forgot to post.)

For casual lurkers, C# somewhat annoyingly calls its """ strings "Raw" and its older @" strings "Verbatim." Deathaxe has chosen here to set """ to string.quoted.double.block like Python uses and to create a new string.quoted.double.verbatim scope for @".

I originally held off on approval for a while because I wanted to consider leaving string.quoted.double.verbatim as .raw. But if "raw" is actually C#'s name for another kind of string, that might be misleading. There are arguable trade-offs both ways.

@deathaxe
Copy link
Copy Markdown
Collaborator Author

deathaxe commented Apr 1, 2026

I am also not very confident about the best solution as raw is basically used for various verbatim terms in other languages as well. On the other hand these kind of detailed scopes is barely standardized and at some point it should be possible to denote language specific requirements. Suggestions wellcome.

@deathaxe deathaxe merged commit 9905f52 into sublimehq:master Apr 2, 2026
2 checks passed
@deathaxe deathaxe deleted the pr/cs/refactor-strings branch April 2, 2026 10:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants