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 mut prefix = Chunk {
199 text: prefix,
200 chars,
201 tabs,
202 ..chunk.clone()
203 };
204
205 chunk.chars = chunk.chars.unbounded_shr(split_idx as u32);
206 chunk.tabs = chunk.tabs.unbounded_shr(split_idx as u32);
207 chunk.text = suffix;
208 if !self.active_highlights.is_empty() {
209 prefix.highlight_style = self
210 .active_highlights
211 .values()
212 .copied()
213 .reduce(|acc, active_highlight| acc.highlight(active_highlight));
214 }
215 Some(prefix)
216 }
217}
218
219impl PartialOrd for HighlightEndpoint {
220 fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
221 Some(self.cmp(other))
222 }
223}
224
225impl Ord for HighlightEndpoint {
226 fn cmp(&self, other: &Self) -> cmp::Ordering {
227 self.offset
228 .cmp(&other.offset)
229 .then_with(|| self.style.is_some().cmp(&other.style.is_some()))
230 .then_with(|| self.tag.cmp(&other.tag))
231 }
232}
233
234#[cfg(test)]
235mod tests {
236 use std::sync::Arc;
237
238 use super::*;
239 use crate::MultiBuffer;
240 use gpui::App;
241 use rand::prelude::*;
242 use util::RandomCharIter;
243
244 #[gpui::test(iterations = 100)]
245 fn test_random_chunk_bitmaps(cx: &mut App, mut rng: StdRng) {
246 // Generate random buffer using existing test infrastructure
247 let len = rng.random_range(10..10000);
248 let buffer = if rng.random() {
249 let text = RandomCharIter::new(&mut rng).take(len).collect::<String>();
250 MultiBuffer::build_simple(&text, cx)
251 } else {
252 MultiBuffer::build_random(&mut rng, cx)
253 };
254
255 let buffer_snapshot = buffer.read(cx).snapshot(cx);
256
257 // Create random highlights
258 let mut highlights = sum_tree::TreeMap::default();
259 let highlight_count = rng.random_range(1..10);
260
261 for _i in 0..highlight_count {
262 let style = HighlightStyle {
263 color: Some(gpui::Hsla {
264 h: rng.random::<f32>(),
265 s: rng.random::<f32>(),
266 l: rng.random::<f32>(),
267 a: 1.0,
268 }),
269 ..Default::default()
270 };
271
272 let mut ranges = Vec::new();
273 let range_count = rng.random_range(1..10);
274 let text = buffer_snapshot.text();
275 for _ in 0..range_count {
276 if buffer_snapshot.len() == MultiBufferOffset(0) {
277 continue;
278 }
279
280 let mut start = rng.random_range(
281 MultiBufferOffset(0)..=buffer_snapshot.len().saturating_sub_usize(10),
282 );
283
284 while !text.is_char_boundary(start.0) {
285 start = start.saturating_sub_usize(1);
286 }
287
288 let end_end = buffer_snapshot.len().min(start + 100usize);
289 let mut end = rng.random_range(start..=end_end);
290 while !text.is_char_boundary(end.0) {
291 end = end.saturating_sub_usize(1);
292 }
293
294 if start < end {
295 start = end;
296 }
297 let start_anchor = buffer_snapshot.anchor_before(start);
298 let end_anchor = buffer_snapshot.anchor_after(end);
299 ranges.push(start_anchor..end_anchor);
300 }
301
302 highlights.insert(HighlightKey::Editor, Arc::new((style, ranges)));
303 }
304
305 // Get all chunks and verify their bitmaps
306 let chunks = CustomHighlightsChunks::new(
307 MultiBufferOffset(0)..buffer_snapshot.len(),
308 false,
309 None,
310 None,
311 &buffer_snapshot,
312 );
313
314 for chunk in chunks {
315 let chunk_text = chunk.text;
316 let chars_bitmap = chunk.chars;
317 let tabs_bitmap = chunk.tabs;
318
319 // Check empty chunks have empty bitmaps
320 if chunk_text.is_empty() {
321 assert_eq!(
322 chars_bitmap, 0,
323 "Empty chunk should have empty chars bitmap"
324 );
325 assert_eq!(tabs_bitmap, 0, "Empty chunk should have empty tabs bitmap");
326 continue;
327 }
328
329 // Verify that chunk text doesn't exceed 128 bytes
330 assert!(
331 chunk_text.len() <= 128,
332 "Chunk text length {} exceeds 128 bytes",
333 chunk_text.len()
334 );
335
336 // Verify chars bitmap
337 let char_indices = chunk_text
338 .char_indices()
339 .map(|(i, _)| i)
340 .collect::<Vec<_>>();
341
342 for byte_idx in 0..chunk_text.len() {
343 let should_have_bit = char_indices.contains(&byte_idx);
344 let has_bit = chars_bitmap & (1 << byte_idx) != 0;
345
346 if has_bit != should_have_bit {
347 eprintln!("Chunk text bytes: {:?}", chunk_text.as_bytes());
348 eprintln!("Char indices: {:?}", char_indices);
349 eprintln!("Chars bitmap: {:#b}", chars_bitmap);
350 assert_eq!(
351 has_bit, should_have_bit,
352 "Chars bitmap mismatch at byte index {} in chunk {:?}. Expected bit: {}, Got bit: {}",
353 byte_idx, chunk_text, should_have_bit, has_bit
354 );
355 }
356 }
357
358 // Verify tabs bitmap
359 for (byte_idx, byte) in chunk_text.bytes().enumerate() {
360 let is_tab = byte == b'\t';
361 let has_bit = tabs_bitmap & (1 << byte_idx) != 0;
362
363 if has_bit != is_tab {
364 eprintln!("Chunk text bytes: {:?}", chunk_text.as_bytes());
365 eprintln!("Tabs bitmap: {:#b}", tabs_bitmap);
366 assert_eq!(
367 has_bit, is_tab,
368 "Tabs bitmap mismatch at byte index {} in chunk {:?}. Byte: {:?}, Expected bit: {}, Got bit: {}",
369 byte_idx, chunk_text, byte as char, is_tab, has_bit
370 );
371 }
372 }
373 }
374 }
375}