Introduce ChunkBitmaps struct for text, chars and tabs

The change introduces a dedicated struct to hold chunk bitmaps data
rather than using raw tuples.
This commit is contained in:
Anthony 2025-08-11 14:57:04 -04:00
parent 21d4af3810
commit ac0249bd13
3 changed files with 39 additions and 14 deletions

View file

@ -11,7 +11,7 @@ use std::{
sync::Arc, sync::Arc,
}; };
use sum_tree::{Bias, Cursor, Dimensions, SumTree}; use sum_tree::{Bias, Cursor, Dimensions, SumTree};
use text::{Patch, Rope}; use text::{ChunkBitmaps, Patch, Rope};
use ui::{ActiveTheme, IntoElement as _, ParentElement as _, Styled as _, div}; use ui::{ActiveTheme, IntoElement as _, ParentElement as _, Styled as _, div};
use super::{Highlights, custom_highlights::CustomHighlightsChunks, fold_map::ChunkRendererId}; use super::{Highlights, custom_highlights::CustomHighlightsChunks, fold_map::ChunkRendererId};
@ -247,7 +247,7 @@ pub struct InlayChunks<'a> {
buffer_chunk: Option<Chunk<'a>>, buffer_chunk: Option<Chunk<'a>>,
inlay_chunks: Option<text::ChunkWithBitmaps<'a>>, inlay_chunks: Option<text::ChunkWithBitmaps<'a>>,
/// text, char bitmap, tabs bitmap /// text, char bitmap, tabs bitmap
inlay_chunk: Option<(&'a str, u128, u128)>, inlay_chunk: Option<ChunkBitmaps<'a>>,
output_offset: InlayOffset, output_offset: InlayOffset,
max_output_offset: InlayOffset, max_output_offset: InlayOffset,
highlight_styles: HighlightStyles, highlight_styles: HighlightStyles,
@ -416,7 +416,11 @@ impl<'a> Iterator for InlayChunks<'a> {
let chunks = inlay.text.chunks_in_range(start.0..end.0); let chunks = inlay.text.chunks_in_range(start.0..end.0);
text::ChunkWithBitmaps(chunks) text::ChunkWithBitmaps(chunks)
}); });
let (inlay_chunk, chars, tabs) = self let ChunkBitmaps {
text: inlay_chunk,
chars,
tabs,
} = self
.inlay_chunk .inlay_chunk
.get_or_insert_with(|| inlay_chunks.next().unwrap()); .get_or_insert_with(|| inlay_chunks.next().unwrap());

View file

@ -4760,7 +4760,12 @@ impl<'a> Iterator for BufferChunks<'a> {
} }
self.diagnostic_endpoints = diagnostic_endpoints; self.diagnostic_endpoints = diagnostic_endpoints;
if let Some((chunk, tabs, chars_map)) = self.chunks.peek_tabs() { if let Some(ChunkBitmaps {
text: chunk,
chars: chars_map,
tabs,
}) = self.chunks.peek_tabs()
{
let chunk_start = self.range.start; let chunk_start = self.range.start;
let mut chunk_end = (self.chunks.offset() + chunk.len()) let mut chunk_end = (self.chunks.offset() + chunk.len())
.min(next_capture_start) .min(next_capture_start)
@ -4773,7 +4778,6 @@ impl<'a> Iterator for BufferChunks<'a> {
} }
} }
// todo! write a test for this
let slice = let slice =
&chunk[chunk_start - self.chunks.offset()..chunk_end - self.chunks.offset()]; &chunk[chunk_start - self.chunks.offset()..chunk_end - self.chunks.offset()];
let bit_end = chunk_end - self.chunks.offset(); let bit_end = chunk_end - self.chunks.offset();

View file

@ -595,6 +595,15 @@ impl<'a> Cursor<'a> {
} }
} }
pub struct ChunkBitmaps<'a> {
/// A slice of text up to 128 bytes in size
pub text: &'a str,
/// Bitmap of character locations in text. LSB ordered
pub chars: u128,
/// Bitmap of tab locations in text. LSB ordered
pub tabs: u128,
}
#[derive(Clone)] #[derive(Clone)]
pub struct Chunks<'a> { pub struct Chunks<'a> {
chunks: sum_tree::Cursor<'a, Chunk, usize>, chunks: sum_tree::Cursor<'a, Chunk, usize>,
@ -757,7 +766,7 @@ impl<'a> Chunks<'a> {
} }
/// Returns bitmaps that represent character positions and tab positions /// Returns bitmaps that represent character positions and tab positions
pub fn peak_with_bitmaps(&self) -> Option<(&'a str, u128, u128)> { pub fn peak_with_bitmaps(&self) -> Option<ChunkBitmaps<'a>> {
if !self.offset_is_valid() { if !self.offset_is_valid() {
return None; return None;
} }
@ -779,7 +788,11 @@ impl<'a> Chunks<'a> {
let chars = (chunk.chars() & bitmask) >> slice_range.start; let chars = (chunk.chars() & bitmask) >> slice_range.start;
let tabs = (chunk.tabs & bitmask) >> slice_range.start; let tabs = (chunk.tabs & bitmask) >> slice_range.start;
Some((&chunk.text[slice_range.clone()], chars, tabs)) Some(ChunkBitmaps {
text: &chunk.text[slice_range.clone()],
chars,
tabs,
})
} }
pub fn peek(&self) -> Option<&'a str> { pub fn peek(&self) -> Option<&'a str> {
@ -802,7 +815,7 @@ impl<'a> Chunks<'a> {
Some(&chunk.text[slice_range]) Some(&chunk.text[slice_range])
} }
pub fn peek_tabs(&self) -> Option<(&'a str, u128, u128)> { pub fn peek_tabs(&self) -> Option<ChunkBitmaps<'a>> {
if !self.offset_is_valid() { if !self.offset_is_valid() {
return None; return None;
} }
@ -825,7 +838,11 @@ impl<'a> Chunks<'a> {
let shifted_tabs = chunk.tabs >> chunk_start_offset; let shifted_tabs = chunk.tabs >> chunk_start_offset;
let shifted_chars = chunk.chars() >> chunk_start_offset; let shifted_chars = chunk.chars() >> chunk_start_offset;
Some((slice_text, shifted_tabs, shifted_chars)) Some(ChunkBitmaps {
text: slice_text,
chars: shifted_chars,
tabs: shifted_tabs,
})
} }
pub fn lines(self) -> Lines<'a> { pub fn lines(self) -> Lines<'a> {
@ -877,23 +894,23 @@ pub struct ChunkWithBitmaps<'a>(pub Chunks<'a>);
impl<'a> Iterator for ChunkWithBitmaps<'a> { impl<'a> Iterator for ChunkWithBitmaps<'a> {
/// text, chars bitmap, tabs bitmap /// text, chars bitmap, tabs bitmap
type Item = (&'a str, u128, u128); type Item = ChunkBitmaps<'a>;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
let chunk = self.0.peak_with_bitmaps()?; let chunk_bitmaps = self.0.peak_with_bitmaps()?;
if self.0.reversed { if self.0.reversed {
self.0.offset -= chunk.0.len(); self.0.offset -= chunk_bitmaps.text.len();
if self.0.offset <= *self.0.chunks.start() { if self.0.offset <= *self.0.chunks.start() {
self.0.chunks.prev(); self.0.chunks.prev();
} }
} else { } else {
self.0.offset += chunk.0.len(); self.0.offset += chunk_bitmaps.text.len();
if self.0.offset >= self.0.chunks.end() { if self.0.offset >= self.0.chunks.end() {
self.0.chunks.next(); self.0.chunks.next();
} }
} }
Some(chunk) Some(chunk_bitmaps)
} }
} }