Skip to content

Fix \w not matching UTF-8 continuation bytes - #1001

Merged
Leonard Hecker (lhecker) merged 2 commits into
microsoft:mainfrom
AkiPoh:fix-syntax-highlighting-bug
Nov 2, 2026
Merged

Leonard Hecker (lhecker) merged 2 commits into
microsoft:mainfrom
AkiPoh:fix-syntax-highlighting-bug

Conversation

@AkiPoh

@AkiPoh AkiPoh (AkiPoh) commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

\w previously only included the leading bytes of multibyte sequences (0xC2-0xF4), so a match ended in the middle of a non-ASCII character. Because \w+ is a single non-backtracking charset instruction, rules like java's /(\w+)\s*(/ (for method definition highlighting) then failed outright, leaving non-ASCII identifiers unhighlighted.

Hence this commit adds the continuation bytes (0x80-0xBF) to the \w matching in regex.rs, to fix this issue.

See example that previously went unhighlighted in Java:

// "merkkienMäärä" was previously unhighlighted due to containing
// non-ASCII characters. After this commit this too is highlighted.
public static int merkkienMäärä (String merkkijono) {
}

// In comparison "numberOfCharacters" got previously highlighted
// because it only included solely ASCII-characters.
public static int numberOfCharacters (String text) {
}

Disclaimers and potential shortcomings:

  • AI was used to help me fix this bug that bothered me, so I'm not 100% sure this is the appropriate solution, but it seemed to work for me as solution.
  • The logic is bit naive, for example naïve—ok (with em dash in the middle) would match as single identifier with the addition of the (0x80-0xBF) range. But this should most often be fine, probably.

There's probably a better way to fix this, but I hope this PR is at least of some value to someone, at least in highlighting this issue -- pun intended.

@AkiPoh
AkiPoh (AkiPoh) force-pushed the fix-syntax-highlighting-bug branch from bffa0e4 to fa98ad2 Compare November 1, 2026 04:56
\w previously only included the leading bytes of multibyte sequences
(0xC2-0xF4), so a match ended in the middle of a non-ASCII character.
Because \w+ is a single non-backtracking charset instruction, rules
like java's /(\w+)\s*\(/ (for method definition highlighting) then
failed outright, leaving non-ASCII identifiers unhighlighted.

Hence this commit adds the continuation bytes (0x80-0xBF) to the \w
matching in regex.rs, to fix this issue.

See example that previously went unhighlighted in Java:

```java
// "merkkienMäärä" was previously unhighlighted due to containing
// non-ASCII characters. After this commit this too is highlighted.
public static int merkkienMäärä (String merkkijono) {
}

// In comparison "numberOfCharacters" got previously highlighted
// because it only included solely ASCII-characters.
public static int numberOfCharacters (String text) {
}
```
@AkiPoh
AkiPoh (AkiPoh) force-pushed the fix-syntax-highlighting-bug branch from fa98ad2 to d3468ac Compare November 1, 2026 04:57

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.

Leonard Hecker (@lhecker) huh, this seems like a reasonable catch

@lhecker Leonard Hecker (lhecker) left a comment

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.

Well, someone had a brain fart there. Someone is me.

The comments are a bit LLM-sloppy. Approved with some final touches. Thanks for the fix!

@lhecker
Leonard Hecker (lhecker) merged commit f31ea74 into microsoft:main Nov 2, 2026
6 checks passed
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