@@ -317,10 +317,11 @@ pub fn char_idx_at_visual_offset<'a>(
317 text_fmt: &TextFormat,
317 text_fmt: &TextFormat,
318 annotations: &TextAnnotations,
318 annotations: &TextAnnotations,
319) -> (usize, usize) {
319) -> (usize, usize) {
320+ let mut pos = anchor;
320 // convert row relative to visual line containing anchor to row relative to a block containing anchor (anchor may change)
321 // convert row relative to visual line containing anchor to row relative to a block containing anchor (anchor may change)
322 let (visual_pos_in_block, block_char_offset) =
323 let (visual_pos_in_block, block_char_offset) =
323− visual_offset_from_block(text, anchor, anchor, text_fmt, annotations);
324+ visual_offset_from_block(text, anchor, pos, text_fmt, annotations);
324 row_offset += visual_pos_in_block.row as isize;
325 row_offset += visual_pos_in_block.row as isize;
325 anchor = block_char_offset;
326 anchor = block_char_offset;
326 if row_offset >= 0 {
327 if row_offset >= 0 {
@@ -332,10 +333,10 @@ pub fn char_idx_at_visual_offset<'a>(
334 // the row_offset is negative so we need to look at the previous block
335 // the row_offset is negative so we need to look at the previous block
335− // set the anchor to the last char before the current block
336+ // set the anchor to the last char before the current block so that we can compute
336− // this char index is also always a line earlier so increase the row_offset by 1
337+ // the distance of this block from the start of the previous block
337 anchor -= 1;
339 anchor -= 1;
341 char_idx_at_visual_block_offset(
342 char_idx_at_visual_block_offset(