diff options
author | Ivan Tham | 2022-01-23 07:54:03 +0000 |
---|---|---|
committer | GitHub | 2022-01-23 07:54:03 +0000 |
commit | 759b850859727da9ef3e3c06cdab0300d242f1fe (patch) | |
tree | d2e20a8b0037a739715cc34030c323c45d89ae8f /helix-core | |
parent | 7d510429c564d216b4f1419b37a37cf7384f49c0 (diff) |
Allow specifying file start position (#445)
Like helix-term/src/commands.rs:3426:15
Diffstat (limited to 'helix-core')
-rw-r--r-- | helix-core/src/position.rs | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/helix-core/src/position.rs b/helix-core/src/position.rs index c6018ce6..93362c77 100644 --- a/helix-core/src/position.rs +++ b/helix-core/src/position.rs @@ -109,7 +109,10 @@ pub fn visual_coords_at_pos(text: RopeSlice, pos: usize, tab_width: usize) -> Po /// TODO: this should be changed to work in terms of visual row/column, not /// graphemes. pub fn pos_at_coords(text: RopeSlice, coords: Position, limit_before_line_ending: bool) -> usize { - let Position { row, col } = coords; + let Position { mut row, col } = coords; + if limit_before_line_ending { + row = row.min(text.len_lines() - 1); + }; let line_start = text.line_to_char(row); let line_end = if limit_before_line_ending { line_end_char_index(&text, row) @@ -290,5 +293,12 @@ mod test { assert_eq!(pos_at_coords(slice, (0, 0).into(), false), 0); assert_eq!(pos_at_coords(slice, (0, 1).into(), false), 1); assert_eq!(pos_at_coords(slice, (0, 2).into(), false), 2); + + // Test out of bounds. + let text = Rope::new(); + let slice = text.slice(..); + assert_eq!(pos_at_coords(slice, (10, 0).into(), true), 0); + assert_eq!(pos_at_coords(slice, (0, 10).into(), true), 0); + assert_eq!(pos_at_coords(slice, (10, 10).into(), true), 0); } } |