diff --git a/crates/editor/src/display_map/inlay_map.rs b/crates/editor/src/display_map/inlay_map.rs index 50021c2a04..9a68598d1d 100644 --- a/crates/editor/src/display_map/inlay_map.rs +++ b/crates/editor/src/display_map/inlay_map.rs @@ -11,7 +11,7 @@ use std::{ sync::Arc, }; 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 super::{Highlights, custom_highlights::CustomHighlightsChunks, fold_map::ChunkRendererId}; @@ -247,7 +247,7 @@ pub struct InlayChunks<'a> { buffer_chunk: Option>, inlay_chunks: Option>, /// text, char bitmap, tabs bitmap - inlay_chunk: Option<(&'a str, u128, u128)>, + inlay_chunk: Option>, output_offset: InlayOffset, max_output_offset: InlayOffset, highlight_styles: HighlightStyles, @@ -416,7 +416,11 @@ impl<'a> Iterator for InlayChunks<'a> { let chunks = inlay.text.chunks_in_range(start.0..end.0); text::ChunkWithBitmaps(chunks) }); - let (inlay_chunk, chars, tabs) = self + let ChunkBitmaps { + text: inlay_chunk, + chars, + tabs, + } = self .inlay_chunk .get_or_insert_with(|| inlay_chunks.next().unwrap()); diff --git a/crates/language/src/buffer.rs b/crates/language/src/buffer.rs index 4aaa5d0fec..25b0231761 100644 --- a/crates/language/src/buffer.rs +++ b/crates/language/src/buffer.rs @@ -4760,7 +4760,12 @@ impl<'a> Iterator for BufferChunks<'a> { } 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 mut chunk_end = (self.chunks.offset() + chunk.len()) .min(next_capture_start) @@ -4773,7 +4778,6 @@ impl<'a> Iterator for BufferChunks<'a> { } } - // todo! write a test for this let slice = &chunk[chunk_start - self.chunks.offset()..chunk_end - self.chunks.offset()]; let bit_end = chunk_end - self.chunks.offset(); diff --git a/crates/rope/src/rope.rs b/crates/rope/src/rope.rs index 61ae6070e6..fd5d53d067 100644 --- a/crates/rope/src/rope.rs +++ b/crates/rope/src/rope.rs @@ -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)] pub struct Chunks<'a> { chunks: sum_tree::Cursor<'a, Chunk, usize>, @@ -757,7 +766,7 @@ impl<'a> Chunks<'a> { } /// 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> { if !self.offset_is_valid() { return None; } @@ -779,7 +788,11 @@ impl<'a> Chunks<'a> { let chars = (chunk.chars() & 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> { @@ -802,7 +815,7 @@ impl<'a> Chunks<'a> { Some(&chunk.text[slice_range]) } - pub fn peek_tabs(&self) -> Option<(&'a str, u128, u128)> { + pub fn peek_tabs(&self) -> Option> { if !self.offset_is_valid() { return None; } @@ -825,7 +838,11 @@ impl<'a> Chunks<'a> { let shifted_tabs = chunk.tabs >> 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> { @@ -877,23 +894,23 @@ pub struct ChunkWithBitmaps<'a>(pub Chunks<'a>); impl<'a> Iterator for ChunkWithBitmaps<'a> { /// text, chars bitmap, tabs bitmap - type Item = (&'a str, u128, u128); + type Item = ChunkBitmaps<'a>; fn next(&mut self) -> Option { - let chunk = self.0.peak_with_bitmaps()?; + let chunk_bitmaps = self.0.peak_with_bitmaps()?; 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() { self.0.chunks.prev(); } } else { - self.0.offset += chunk.0.len(); + self.0.offset += chunk_bitmaps.text.len(); if self.0.offset >= self.0.chunks.end() { self.0.chunks.next(); } } - Some(chunk) + Some(chunk_bitmaps) } }