language: Improve highlight map resolution#52183
Merged
MrSubidubi merged 9 commits intomainfrom Mar 25, 2026
Merged
Conversation
Co-authored-by: MrSubidubi <[email protected]> Co-authored-by: Gaauwe Rombouts <[email protected]>
Co-authored-by: MrSubidubi <[email protected]> Co-authored-by: Gaauwe Rombouts <[email protected]>
Co-authored-by: Gaauwe Rombouts <[email protected]> Co-authored-by: MrSubidubi <[email protected]>
Co-authored-by: Gaauwe Rombouts <[email protected]> Co-authored-by: MrSubidubi <[email protected]>
Co-authored-by: Gaauwe Rombouts <[email protected]> Co-authored-by: MrSubidubi <[email protected]>
📏 PR Size: 520 lines changed (Size L)Please note: this PR exceeds the 400 LOC soft limit.
|
osiewicz
approved these changes
Mar 25, 2026
piper-of-dawn
pushed a commit
to piper-of-dawn/zed
that referenced
this pull request
Apr 25, 2026
This PR refactors the highlight map capture name resolution to be faster
and more predictable. Speficically,
- it changes the capture name matching to explicit prefix matching
(e.g., `function.call.whatever.jsx` will now be matched by only
`function`, `function.call`, `function.call.whatever` and
`function.call.whatever.jsx`). This matches the behavior VSCode has
- resolving highlights is now much more efficient, as we now look up
captures in a BTreeMap as opposed to searching in a Vector for these.
This substantially improves the performance for resolving capture names
against themes. With the benchmark added here for creating the
HighlightMap, we see quite some improvements:
```
Running benches/highlight_map.rs (target/release/deps/highlight_map-f99da68650aac85b)
HighlightMap::new/small_captures/small_theme
time: [161.90 ns 162.70 ns 163.55 ns]
change: [-39.027% -38.352% -37.742%] (p = 0.00 < 0.05)
Performance has improved.
Found 3 outliers among 100 measurements (3.00%)
3 (3.00%) high mild
HighlightMap::new/small_captures/large_theme
time: [231.37 ns 233.02 ns 234.70 ns]
change: [-91.570% -91.516% -91.464%] (p = 0.00 < 0.05)
Performance has improved.
HighlightMap::new/large_captures/small_theme
time: [991.82 ns 994.94 ns 998.50 ns]
change: [-50.670% -50.443% -50.220%] (p = 0.00 < 0.05)
Performance has improved.
Found 5 outliers among 100 measurements (5.00%)
5 (5.00%) high mild
HighlightMap::new/large_captures/large_theme
time: [1.6528 µs 1.6650 µs 1.6784 µs]
change: [-91.684% -91.637% -91.593%] (p = 0.00 < 0.05)
Performance has improved.
Found 1 outliers among 100 measurements (1.00%)
1 (1.00%) low mild
```
For large themes and many capture names, the revised approach is much
faster.
With that in place, we can also add better fallbacks whenever we change
tokens, since e.g. a change from `@variable` to `@preproc` would
previously cause tokens to not be highlighted at all, whereas now we can
add fallbacks for such cases more efficiently. I'll add this later on to
this PR.
## Self-Review Checklist
<!-- Check before requesting review: -->
- [X] I've reviewed my own diff for quality, security, and reliability
- [ ] Unsafe blocks (if any) have justifying comments
- [ ] The content is consistent with the [UI/UX
checklist](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)
- [X] Tests cover the new/changed behavior
- [X] Performance impact has been considered and is acceptable
Release Notes:
- Improved resolution speed of theme highlight capture names. This might
change highlighting in some rare edge cases, but should overall make
highlighting more predicatable. Theme captures will now follow a strict
prefix matching, so e.g. function.call.decorator.jsx` will now be
matched by only `function`, `function.call`, `function.call.decorator`
and `function.call.decorator.jsx` with the most specific capture always
taking precedence.
---------
Co-authored-by: Piotr Osiewicz <[email protected]>
Co-authored-by: Gaauwe Rombouts <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR refactors the highlight map capture name resolution to be faster and more predictable. Speficically,
function.call.whatever.jsxwill now be matched by onlyfunction,function.call,function.call.whateverandfunction.call.whatever.jsx). This matches the behavior VSCode hasThis substantially improves the performance for resolving capture names against themes. With the benchmark added here for creating the HighlightMap, we see quite some improvements:
For large themes and many capture names, the revised approach is much faster.
With that in place, we can also add better fallbacks whenever we change tokens, since e.g. a change from
@variableto@preprocwould previously cause tokens to not be highlighted at all, whereas now we can add fallbacks for such cases more efficiently. I'll add this later on to this PR.Self-Review Checklist
Release Notes:
will now be matched by onlyfunction,function.call,function.call.decoratorandfunction.call.decorator.jsx` with the most specific capture always taking precedence.