Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'helix-core/src/position.rs')
| -rw-r--r-- | helix-core/src/position.rs | 26 |
1 files changed, 11 insertions, 15 deletions
diff --git a/helix-core/src/position.rs b/helix-core/src/position.rs index e70cb949..1b378911 100644 --- a/helix-core/src/position.rs +++ b/helix-core/src/position.rs @@ -4,12 +4,10 @@ use std::{ ops::{Add, AddAssign, Sub, SubAssign}, }; -use helix_stdx::rope::RopeSliceExt; - use crate::{ chars::char_is_line_ending, doc_formatter::{DocumentFormatter, TextFormat}, - graphemes::{ensure_grapheme_boundary_prev, grapheme_width}, + graphemes::{ensure_grapheme_boundary_prev, grapheme_width, RopeGraphemes}, line_ending::line_end_char_index, text_annotations::TextAnnotations, RopeSlice, @@ -89,6 +87,11 @@ impl From<(usize, usize)> for Position { } } +impl From<Position> for tree_sitter::Point { + fn from(pos: Position) -> Self { + Self::new(pos.row, pos.col) + } +} /// Convert a character index to (line, column) coordinates. /// /// column in `char` count which can be used for row:column display in @@ -98,7 +101,7 @@ pub fn coords_at_pos(text: RopeSlice, pos: usize) -> Position { let line_start = text.line_to_char(line); let pos = ensure_grapheme_boundary_prev(text, pos); - let col = text.slice(line_start..pos).graphemes().count(); + let col = RopeGraphemes::new(text.slice(line_start..pos)).count(); Position::new(line, col) } @@ -123,7 +126,7 @@ pub fn visual_coords_at_pos(text: RopeSlice, pos: usize, tab_width: usize) -> Po let mut col = 0; - for grapheme in text.slice(line_start..pos).graphemes() { + for grapheme in RopeGraphemes::new(text.slice(line_start..pos)) { if grapheme == "\t" { col += tab_width - (col % tab_width); } else { @@ -262,14 +265,7 @@ pub fn visual_offset_from_anchor( pub fn pos_at_coords(text: RopeSlice, coords: Position, limit_before_line_ending: bool) -> usize { let Position { mut row, col } = coords; if limit_before_line_ending { - let lines = text.len_lines() - 1; - - row = row.min(if crate::line_ending::get_line_ending(&text).is_some() { - // if the last line is empty, don't jump to it - lines - 1 - } else { - lines - }); + row = row.min(text.len_lines() - 1); }; let line_start = text.line_to_char(row); let line_end = if limit_before_line_ending { @@ -279,7 +275,7 @@ pub fn pos_at_coords(text: RopeSlice, coords: Position, limit_before_line_ending }; let mut col_char_offset = 0; - for (i, g) in text.slice(line_start..line_end).graphemes().enumerate() { + for (i, g) in RopeGraphemes::new(text.slice(line_start..line_end)).enumerate() { if i == col { break; } @@ -310,7 +306,7 @@ pub fn pos_at_visual_coords(text: RopeSlice, coords: Position, tab_width: usize) let mut col_char_offset = 0; let mut cols_remaining = col; - for grapheme in text.slice(line_start..line_end).graphemes() { + for grapheme in RopeGraphemes::new(text.slice(line_start..line_end)) { let grapheme_width = if grapheme == "\t" { tab_width - ((col - cols_remaining) % tab_width) } else { |