diff options
author | Pascal Kuthe | 2023-03-22 15:23:29 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2023-03-27 00:54:40 +0000 |
commit | 2af14a24abbd8de510f69e6569c18601533dc912 (patch) | |
tree | c43753ab9021b44f681f0a1a4ef1087216ce7566 /helix-core/src | |
parent | 9fac574178bb6b66675ffc72819a79dee25112df (diff) |
respect line annotations in char_idx_at_visual_row_offset
char_idx_at_visual_row_offset asssumed that a single line/block break
always corresponded to a vertical offset of 1. However conceal can hide
the line break (in which case the certical offset would be 0) and line
annotations (or softwrapped inlay hints at the end of the line) can insert
addtional vertical lines.
To correctly account for these cases we simply compute the visual offset
of the start of the next block from the previous block instead of the
visual offset of the block end. This means that the line breaks at the
end of the block (however many there may be) are automatically included
and we don't need to manually add 1 to the `row_offset` anymore.
Diffstat (limited to 'helix-core/src')
-rw-r--r-- | helix-core/src/position.rs | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/helix-core/src/position.rs b/helix-core/src/position.rs index c3233a34..3902b4d4 100644 --- a/helix-core/src/position.rs +++ b/helix-core/src/position.rs @@ -317,10 +317,11 @@ pub fn char_idx_at_visual_offset<'a>( text_fmt: &TextFormat, annotations: &TextAnnotations, ) -> (usize, usize) { + let mut pos = anchor; // convert row relative to visual line containing anchor to row relative to a block containing anchor (anchor may change) loop { let (visual_pos_in_block, block_char_offset) = - visual_offset_from_block(text, anchor, anchor, text_fmt, annotations); + visual_offset_from_block(text, anchor, pos, text_fmt, annotations); row_offset += visual_pos_in_block.row as isize; anchor = block_char_offset; if row_offset >= 0 { @@ -332,10 +333,10 @@ pub fn char_idx_at_visual_offset<'a>( break; } // the row_offset is negative so we need to look at the previous block - // set the anchor to the last char before the current block - // this char index is also always a line earlier so increase the row_offset by 1 + // set the anchor to the last char before the current block so that we can compute + // the distance of this block from the start of the previous block + pos = anchor; anchor -= 1; - row_offset += 1; } char_idx_at_visual_block_offset( |