Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion crates/language/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
45 changes: 45 additions & 0 deletions crates/language/src/buffer_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
}
Loading