Skip to content

Fix byte offset calculation for CRLF line endings #23

Closed

Description

@unhappychoice

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:

  1. str::lines() removes line endings, so \r\n becomes just the content (losing \r)
  2. Adding + 1 assumes only \n (1 byte)
  3. On Windows files with \r\n (2 bytes), this undercounts by 1 byte per line
  4. 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

Activity

  1. linear commented on Nov 9, 2025

    @linear
  2. added this to the v0.1.0 milestone on Nov 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions