Skip to content

Fix escape sequence semantic highlighting#19975

Open
shenglol wants to merge 1 commit into
mainfrom
shenglol/issue-#18378
Open

Fix escape sequence semantic highlighting#19975
shenglol wants to merge 1 commit into
mainfrom
shenglol/issue-#18378

Conversation

@shenglol

@shenglol shenglol commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

Prevents semantic string tokens from covering escape sequences so TextMate escape scopes keep highlighting.

Fixes #18378

Microsoft Reviewers: Open in CodeFlow

Comment on lines +297 to +337
private static int? TryGetEscapeSequenceLength(string text, int position, int end)
{
if (text[position] != '\\' || position + 1 >= end)
{
return null;
}

return text[position + 1] switch
{
'n' or 'r' or 't' or '\\' or '\'' => 2,
'$' when position + 2 < end && text[position + 2] == '{' => 3,
'u' => TryGetUnicodeEscapeSequenceLength(text, position, end),
_ => null,
};
}

private static int? TryGetUnicodeEscapeSequenceLength(string text, int position, int end)
{
if (position + 3 >= end || text[position + 2] != '{')
{
return null;
}

var hexDigitCount = 0;
for (var current = position + 3; current < end; current++)
{
if (text[current] == '}')
{
return hexDigitCount > 0 ? current - position + 1 : null;
}

if (!Uri.IsHexDigit(text[current]))
{
return null;
}

hexDigitCount++;
}

return null;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't there code in the lexer we can reuse for this, instead of duplicating?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. The lexer validates escapes while producing string tokens and string values, but it does not retain sub-token source spans. Semantic highlighting needs those spans so it can leave gaps for the existing TextMate escape scopes, so there is not an existing lexer result to reuse here without adding a new lexer API just for highlighting.

@github-actions

Copy link
Copy Markdown
Contributor

Test this change out locally with the following install scripts (Action run 28404602368)

VSCode
  • Mac/Linux
    bash <(curl -Ls https://aka.ms/bicep/nightly-vsix.sh) --run-id 28404602368
  • Windows
    iex "& { $(irm https://aka.ms/bicep/nightly-vsix.ps1) } -RunId 28404602368"
Azure CLI
  • Mac/Linux
    bash <(curl -Ls https://aka.ms/bicep/nightly-cli.sh) --run-id 28404602368
  • Windows
    iex "& { $(irm https://aka.ms/bicep/nightly-cli.ps1) } -RunId 28404602368"

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

VS Code extension stops highlighting escape characters after a couple of seconds

2 participants