custom_highlights.rs

  1use collections::BTreeMap;
  2use gpui::HighlightStyle;
  3use language::Chunk;
  4use multi_buffer::{MultiBufferChunks, MultiBufferOffset, MultiBufferSnapshot, ToOffset as _};
  5use std::{
  6    cmp,
  7    iter::{self, Peekable},
  8    ops::Range,
  9    vec,
 10};
 11
 12use crate::display_map::{HighlightKey, SemanticTokensHighlights, TextHighlights};
 13
 14pub struct CustomHighlightsChunks<'a> {
 15    buffer_chunks: MultiBufferChunks<'a>,
 16    buffer_chunk: Option<Chunk<'a>>,
 17    offset: MultiBufferOffset,
 18    multibuffer_snapshot: &'a MultiBufferSnapshot,
 19
 20    highlight_endpoints: Peekable<vec::IntoIter<HighlightEndpoint>>,
 21    active_highlights: BTreeMap<HighlightKey, HighlightStyle>,
 22    text_highlights: Option<&'a TextHighlights>,
 23    semantic_token_highlights: Option<&'a SemanticTokensHighlights>,
 24}
 25
 26#[derive(Debug, Copy, Clone, Eq, PartialEq)]
 27struct HighlightEndpoint {
 28    offset: MultiBufferOffset,
 29    tag: HighlightKey,
 30    style: Option<HighlightStyle>,
 31}
 32
 33impl<'a> CustomHighlightsChunks<'a> {
 34    #[ztracing::instrument(skip_all)]
 35    pub fn new(
 36        range: Range<MultiBufferOffset>,
 37        language_aware: bool,
 38        text_highlights: Option<&'a TextHighlights>,
 39        semantic_token_highlights: Option<&'a SemanticTokensHighlights>,
 40        multibuffer_snapshot: &'a MultiBufferSnapshot,
 41    ) -> Self {
 42        Self {
 43            buffer_chunks: multibuffer_snapshot.chunks(range.clone(), language_aware),
 44            buffer_chunk: None,
 45            offset: range.start,
 46            text_highlights,
 47            highlight_endpoints: create_highlight_endpoints(
 48                &range,
 49                text_highlights,
 50                semantic_token_highlights,
 51                multibuffer_snapshot,
 52            ),
 53            active_highlights: Default::default(),
 54            multibuffer_snapshot,
 55            semantic_token_highlights,
 56        }
 57    }
 58
 59    #[ztracing::instrument(skip_all)]
 60    pub fn seek(&mut self, new_range: Range<MultiBufferOffset>) {
 61        self.highlight_endpoints = create_highlight_endpoints(
 62            &new_range,
 63            self.text_highlights,
 64            self.semantic_token_highlights,
 65            self.multibuffer_snapshot,
 66        );
 67        self.offset = new_range.start;
 68        self.buffer_chunks.seek(new_range);
 69        self.buffer_chunk.take();
 70        self.active_highlights.clear()
 71    }
 72}
 73
 74fn create_highlight_endpoints(
 75    range: &Range<MultiBufferOffset>,
 76    text_highlights: Option<&TextHighlights>,
 77    semantic_token_highlights: Option<&SemanticTokensHighlights>,
 78    buffer: &MultiBufferSnapshot,
 79) -> iter::Peekable<vec::IntoIter<HighlightEndpoint>> {
 80    let mut highlight_endpoints = Vec::new();
 81    if let Some(text_highlights) = text_highlights {
 82        let start = buffer.anchor_after(range.start);
 83        let end = buffer.anchor_after(range.end);
 84        for (&tag, text_highlights) in text_highlights.iter() {
 85            let style = text_highlights.0;
 86            let ranges = &text_highlights.1;
 87
 88            let start_ix = ranges
 89                .binary_search_by(|probe| probe.end.cmp(&start, buffer).then(cmp::Ordering::Less))
 90                .unwrap_or_else(|i| i);
 91            let end_ix = ranges[start_ix..]
 92                .binary_search_by(|probe| {
 93                    probe.start.cmp(&end, buffer).then(cmp::Ordering::Greater)
 94                })
 95                .unwrap_or_else(|i| i);
 96
 97            highlight_endpoints.reserve(2 * end_ix);
 98
 99            for range in &ranges[start_ix..][..end_ix] {
100                let start = range.start.to_offset(buffer);
101                let end = range.end.to_offset(buffer);
102                if start == end {
103                    continue;
104                }
105                highlight_endpoints.push(HighlightEndpoint {
106                    offset: start,
107                    tag,
108                    style: Some(style),
109                });
110                highlight_endpoints.push(HighlightEndpoint {
111                    offset: end,
112                    tag,
113                    style: None,
114                });
115            }
116        }
117    }
118    if let Some(semantic_token_highlights) = semantic_token_highlights {
119        let start = buffer.anchor_after(range.start);
120        let end = buffer.anchor_after(range.end);
121        for buffer_id in buffer.buffer_ids_for_range(range.clone()) {
122            let Some((semantic_token_highlights, interner)) =
123                semantic_token_highlights.get(&buffer_id)
124            else {
125                continue;
126            };
127            let start_ix = semantic_token_highlights
128                .binary_search_by(|probe| {
129                    probe
130                        .range
131                        .end
132                        .cmp(&start, buffer)
133                        .then(cmp::Ordering::Less)
134                })
135                .unwrap_or_else(|i| i);
136            for token in &semantic_token_highlights[start_ix..] {
137                if token.range.start.cmp(&end, buffer).is_ge() {
138                    break;
139                }
140
141                let start = token.range.start.to_offset(buffer);
142                let end = token.range.end.to_offset(buffer);
143                if start == end {
144                    continue;
145                }
146                highlight_endpoints.push(HighlightEndpoint {
147                    offset: start,
148                    tag: HighlightKey::SemanticToken,
149                    style: Some(interner[token.style]),
150                });
151                highlight_endpoints.push(HighlightEndpoint {
152                    offset: end,
153                    tag: HighlightKey::SemanticToken,
154                    style: None,
155                });
156            }
157        }
158    }
159    highlight_endpoints.sort();
160    highlight_endpoints.into_iter().peekable()
161}
162
163impl<'a> Iterator for CustomHighlightsChunks<'a> {
164    type Item = Chunk<'a>;
165
166    #[ztracing::instrument(skip_all)]
167    fn next(&mut self) -> Option<Self::Item> {
168        let mut next_highlight_endpoint = MultiBufferOffset(usize::MAX);
169        while let Some(endpoint) = self.highlight_endpoints.peek().copied() {
170            if endpoint.offset <= self.offset {
171                if let Some(style) = endpoint.style {
172                    self.active_highlights.insert(endpoint.tag, style);
173                } else {
174                    self.active_highlights.remove(&endpoint.tag);
175                }
176                self.highlight_endpoints.next();
177            } else {
178                next_highlight_endpoint = endpoint.offset;
179                break;
180            }
181        }
182
183        let chunk = match &mut self.buffer_chunk {
184            Some(it) => it,
185            slot => slot.insert(self.buffer_chunks.next()?),
186        };
187        while chunk.text.is_empty() {
188            *chunk = self.buffer_chunks.next()?;
189        }
190
191        let split_idx = chunk.text.len().min(next_highlight_endpoint - self.offset);
192        let (prefix, suffix) = chunk.text.split_at(split_idx);
193        self.offset += prefix.len();
194
195        let mask = 1u128.unbounded_shl(split_idx as u32).wrapping_sub(1);
196        let chars = chunk.chars & mask;
197        let tabs = chunk.tabs & mask;
198        let newlines = chunk.newlines & mask;
199        let mut prefix = Chunk {
200            text: prefix,
201            chars,
202            tabs,
203            newlines,
204            ..chunk.clone()
205        };
206
207        chunk.chars = chunk.chars.unbounded_shr(split_idx as u32);
208        chunk.tabs = chunk.tabs.unbounded_shr(split_idx as u32);
209        chunk.newlines = chunk.newlines.unbounded_shr(split_idx as u32);
210        chunk.text = suffix;
211        if !self.active_highlights.is_empty() {
212            prefix.highlight_style = self
213                .active_highlights
214                .values()
215                .copied()
216                .reduce(|acc, active_highlight| acc.highlight(active_highlight));
217        }
218        Some(prefix)
219    }
220}
221
222impl PartialOrd for HighlightEndpoint {
223    fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
224        Some(self.cmp(other))
225    }
226}
227
228impl Ord for HighlightEndpoint {
229    fn cmp(&self, other: &Self) -> cmp::Ordering {
230        self.offset
231            .cmp(&other.offset)
232            .then_with(|| self.style.is_some().cmp(&other.style.is_some()))
233            .then_with(|| self.tag.cmp(&other.tag))
234    }
235}
236
237#[cfg(test)]
238mod tests {
239    use std::sync::Arc;
240
241    use super::*;
242    use crate::MultiBuffer;
243    use gpui::App;
244    use rand::prelude::*;
245    use util::RandomCharIter;
246
247    #[gpui::test(iterations = 100)]
248    fn test_random_chunk_bitmaps(cx: &mut App, mut rng: StdRng) {
249        // Generate random buffer using existing test infrastructure
250        let len = rng.random_range(10..10000);
251        let buffer = if rng.random() {
252            let text = RandomCharIter::new(&mut rng).take(len).collect::<String>();
253            MultiBuffer::build_simple(&text, cx)
254        } else {
255            MultiBuffer::build_random(&mut rng, cx)
256        };
257
258        let buffer_snapshot = buffer.read(cx).snapshot(cx);
259
260        // Create random highlights
261        let mut highlights = sum_tree::TreeMap::default();
262        let highlight_count = rng.random_range(1..10);
263
264        for _i in 0..highlight_count {
265            let style = HighlightStyle {
266                color: Some(gpui::Hsla {
267                    h: rng.random::<f32>(),
268                    s: rng.random::<f32>(),
269                    l: rng.random::<f32>(),
270                    a: 1.0,
271                }),
272                ..Default::default()
273            };
274
275            let mut ranges = Vec::new();
276            let range_count = rng.random_range(1..10);
277            let text = buffer_snapshot.text();
278            for _ in 0..range_count {
279                if buffer_snapshot.len() == MultiBufferOffset(0) {
280                    continue;
281                }
282
283                let mut start = rng.random_range(
284                    MultiBufferOffset(0)..=buffer_snapshot.len().saturating_sub_usize(10),
285                );
286
287                while !text.is_char_boundary(start.0) {
288                    start = start.saturating_sub_usize(1);
289                }
290
291                let end_end = buffer_snapshot.len().min(start + 100usize);
292                let mut end = rng.random_range(start..=end_end);
293                while !text.is_char_boundary(end.0) {
294                    end = end.saturating_sub_usize(1);
295                }
296
297                if start < end {
298                    start = end;
299                }
300                let start_anchor = buffer_snapshot.anchor_before(start);
301                let end_anchor = buffer_snapshot.anchor_after(end);
302                ranges.push(start_anchor..end_anchor);
303            }
304
305            highlights.insert(HighlightKey::Editor, Arc::new((style, ranges)));
306        }
307
308        // Get all chunks and verify their bitmaps
309        let chunks = CustomHighlightsChunks::new(
310            MultiBufferOffset(0)..buffer_snapshot.len(),
311            false,
312            None,
313            None,
314            &buffer_snapshot,
315        );
316
317        for chunk in chunks {
318            let chunk_text = chunk.text;
319            let chars_bitmap = chunk.chars;
320            let tabs_bitmap = chunk.tabs;
321
322            // Check empty chunks have empty bitmaps
323            if chunk_text.is_empty() {
324                assert_eq!(
325                    chars_bitmap, 0,
326                    "Empty chunk should have empty chars bitmap"
327                );
328                assert_eq!(tabs_bitmap, 0, "Empty chunk should have empty tabs bitmap");
329                continue;
330            }
331
332            // Verify that chunk text doesn't exceed 128 bytes
333            assert!(
334                chunk_text.len() <= 128,
335                "Chunk text length {} exceeds 128 bytes",
336                chunk_text.len()
337            );
338
339            // Verify chars bitmap
340            let char_indices = chunk_text
341                .char_indices()
342                .map(|(i, _)| i)
343                .collect::<Vec<_>>();
344
345            for byte_idx in 0..chunk_text.len() {
346                let should_have_bit = char_indices.contains(&byte_idx);
347                let has_bit = chars_bitmap & (1 << byte_idx) != 0;
348
349                if has_bit != should_have_bit {
350                    eprintln!("Chunk text bytes: {:?}", chunk_text.as_bytes());
351                    eprintln!("Char indices: {:?}", char_indices);
352                    eprintln!("Chars bitmap: {:#b}", chars_bitmap);
353                    assert_eq!(
354                        has_bit, should_have_bit,
355                        "Chars bitmap mismatch at byte index {} in chunk {:?}. Expected bit: {}, Got bit: {}",
356                        byte_idx, chunk_text, should_have_bit, has_bit
357                    );
358                }
359            }
360
361            // Verify tabs bitmap
362            for (byte_idx, byte) in chunk_text.bytes().enumerate() {
363                let is_tab = byte == b'\t';
364                let has_bit = tabs_bitmap & (1 << byte_idx) != 0;
365
366                if has_bit != is_tab {
367                    eprintln!("Chunk text bytes: {:?}", chunk_text.as_bytes());
368                    eprintln!("Tabs bitmap: {:#b}", tabs_bitmap);
369                    assert_eq!(
370                        has_bit, is_tab,
371                        "Tabs bitmap mismatch at byte index {} in chunk {:?}. Byte: {:?}, Expected bit: {}, Got bit: {}",
372                        byte_idx, chunk_text, byte as char, is_tab, has_bit
373                    );
374                }
375            }
376        }
377    }
378}