Files
zed/crates/language_core/src/highlight_map.rs
T
Finn EversandKirill Bulatov 123a29795d language_core: Introduce fallback highlights (#52575)
Self-Review Checklist:

- [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)
- [ ] Tests cover the new/changed behavior
- [ ] Performance impact has been considered and is acceptable

Release Notes:

- Added the option for highlights from languages to specify fallbacks.
That means that if you have a pattern with the captures `@second.capture
@first.capture`, Zed will first try resolving a highlight from your
theme for the code fragment using the first capture, then look for the
second capture if no match for the first capture could be found.

---------

Co-authored-by: Kirill Bulatov <kirill@zed.dev>
2026-04-02 12:52:42 +00:00

41 lines
1.1 KiB
Rust

use std::{num::NonZeroU32, sync::Arc};
#[derive(Clone, Debug)]
pub struct HighlightMap(Arc<[Option<HighlightId>]>);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct HighlightId(NonZeroU32);
impl HighlightId {
pub const TABSTOP_INSERT_ID: HighlightId = HighlightId(NonZeroU32::new(u32::MAX - 1).unwrap());
pub const TABSTOP_REPLACE_ID: HighlightId = HighlightId(NonZeroU32::new(u32::MAX - 2).unwrap());
pub fn new(capture_id: u32) -> Self {
Self(NonZeroU32::new(capture_id + 1).unwrap_or(NonZeroU32::MAX))
}
}
impl From<HighlightId> for usize {
fn from(value: HighlightId) -> Self {
value.0.get() as usize - 1
}
}
impl HighlightMap {
#[inline]
pub fn from_ids(highlight_ids: impl IntoIterator<Item = Option<HighlightId>>) -> Self {
Self(highlight_ids.into_iter().collect())
}
#[inline]
pub fn get(&self, capture_id: u32) -> Option<HighlightId> {
self.0.get(capture_id as usize).copied().flatten()
}
}
impl Default for HighlightMap {
fn default() -> Self {
Self(Arc::new([]))
}
}