Repository navigation
Closed
Milestone
Description
Problem
The current byte offset calculation in src/panes/editor.rs:152-162 incorrectly handles files with CRLF (\r\n) line endings, causing syntax highlighting to appear at wrong columns.
Root Cause
// Current implementation (line 158-162)
content_lines
.iter()
.take(target_line)
.map(|line| line.len() + 1) // ❌ Assumes single-byte newline
.sum()The problem:
str::lines()removes line endings, so\r\nbecomes just the content (losing\r)- Adding
+ 1assumes only\n(1 byte) - On Windows files with
\r\n(2 bytes), this undercounts by 1 byte per line - Byte offset drifts, causing highlights to jump to wrong columns
Example
Original file: "foo\r\nbar\r\nbaz"
Bytes: f o o \r \n b a r \r \n b a z
Position: 0 1 2 3 4 5 6 7 8 9 10 11 12
lines()[0] = "foo" (len=3)
lines()[1] = "bar" (len=3)
lines()[2] = "baz" (len=3)
Current calculation for line 2:
offset = (3 + 1) + (3 + 1) = 8
Actual offset should be 10 ❌
Solution
Pre-calculate actual byte offsets from the original content before calling lines().
Implementation Sketch
1. Add line offset fields to EditorBuffer
pub struct EditorBuffer {
// ...
pub old_content_line_offsets: Vec<usize>,
pub new_content_line_offsets: Vec<usize>,
}2. Calculate offsets from original content
fn calculate_line_offsets(content: &str) -> Vec<usize> {
std::iter::once(0)
.chain(content.bytes().enumerate().filter_map(|(i, b)| {
if b == b'\n' {
Some(i + 1)
} else {
None
}
}))
.collect()
}3. Store offsets in SwitchFile step
AnimationStep::SwitchFile { old_content, new_content, .. } => {
// ...
self.buffer.old_content_line_offsets = calculate_line_offsets(&old_content);
self.buffer.new_content_line_offsets = calculate_line_offsets(&new_content);
}4. Use offsets instead of calculating from line lengths
fn calculate_byte_offset(
&self,
line_num: usize,
cursor_line: usize,
line_offset: isize,
line_offsets: &[usize], // ← Use pre-calculated offsets
) -> usize {
let target_line = if line_num > cursor_line {
((line_num as isize) - line_offset).max(0) as usize
} else {
line_num
};
line_offsets.get(target_line).copied().unwrap_or(0)
}Acceptance Criteria
- Syntax highlighting displays at correct columns for CRLF files
- Byte offset calculation is accurate regardless of line ending type
- No regression for LF-only files
- Test with mixed line endings (if applicable)
Related
- Related to Optimize syntax highlighting performance #20 (syntax highlighting optimization)
- Affects Windows users and files checked out with
core.autocrlf=true
Metadata
Metadata
Assignees
Labels
No labels
Activity
linear commentedon Nov 9, 2025
E-669