Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Fix incorrect mask in BufferChunks::next (#57544)

Galen Elias18c98b021168
diff --git a/crates/language/src/buffer.rs b/crates/language/src/buffer.rs
index 57612012c84..2973414211b 100644
--- a/crates/language/src/buffer.rs
+++ b/crates/language/src/buffer.rs
@@ -5798,7 +5798,9 @@ impl<'a> Iterator for BufferChunks<'a> {
let slice = &chunk[bit_start..bit_end];
- let mask = 1u128.unbounded_shl(bit_end as u32).wrapping_sub(1);
+ let mask = 1u128
+ .unbounded_shl((bit_end - bit_start) as u32)
+ .wrapping_sub(1);
let tabs = (tabs >> bit_start) & mask;
let chars = (chars_map >> bit_start) & mask;
let newlines = (newlines >> bit_start) & mask;
diff --git a/crates/language/src/buffer_tests.rs b/crates/language/src/buffer_tests.rs
index 86fdf519268..691a6f9448a 100644
--- a/crates/language/src/buffer_tests.rs
+++ b/crates/language/src/buffer_tests.rs
@@ -4192,3 +4192,48 @@ fn test_random_chunk_bitmaps(cx: &mut App, mut rng: StdRng) {
}
}
}
+
+#[gpui::test]
+fn test_formatted_chunks(cx: &mut gpui::App) {
+ init_settings(cx, |_| {});
+ let buffer = cx.new(|cx| Buffer::local("use std::cmp::Eq;", cx).with_language(rust_lang(), cx));
+ let snapshot = buffer.read(cx).snapshot();
+
+ let chunks = snapshot.chunks(
+ 0..snapshot.len(),
+ LanguageAwareStyling {
+ tree_sitter: true,
+ diagnostics: false,
+ },
+ );
+
+ for chunk in chunks {
+ let chunk_text = chunk.text;
+ let chars_bitmap = chunk.chars;
+
+ // Verify chars bitmap
+ let char_indices = chunk_text
+ .char_indices()
+ .map(|(i, _)| i)
+ .collect::<Vec<_>>();
+
+ assert_eq!(char_indices.len() as u32, chars_bitmap.count_ones());
+
+ for byte_idx in 0..chunk_text.len() {
+ let should_have_bit = char_indices.contains(&byte_idx);
+ let has_bit = chars_bitmap & (1 << byte_idx) != 0;
+
+ if has_bit != should_have_bit {
+ eprintln!("Chunk text bytes: {:?}", chunk_text.as_bytes());
+ eprintln!("Char indices: {:?}", char_indices);
+ eprintln!("Chars bitmap: {:#b}", chars_bitmap);
+ }
+
+ assert_eq!(
+ has_bit, should_have_bit,
+ "Chars bitmap mismatch at byte index {} in chunk {:?}. Expected bit: {}, Got bit: {}",
+ byte_idx, chunk_text, should_have_bit, has_bit
+ );
+ }
+ }
+}