on master help sign in
Reuse visual_coords_at_pos function in view
commit5d14f56fa9822b18107a5a5efac98dfe9d08f8ce authorBlaž Hrastnik <blaz@mxxn.io> dateThu 3 Mar 2022, 17:41 (5 years ago) parent74a9dd5
3 files changed+17−22
helix-core/src/position.rs+14−10 helix-term/src/commands.rs+1 helix-view/src/view.rs+2−12
helix-core/src/position.rs+14−10
@@ -1,8 +1,9 @@
1+use std::borrow::Cow;
2+
13use crate::{
24 chars::char_is_line_ending,
3− graphemes::{ensure_grapheme_boundary_prev, RopeGraphemes},
5+ graphemes::{ensure_grapheme_boundary_prev, grapheme_width, RopeGraphemes},
46 line_ending::line_end_char_index,
5− unicode::width::UnicodeWidthChar,
67 RopeSlice,
78};
89
@@ -77,14 +78,17 @@ pub fn visual_coords_at_pos(text: RopeSlice, pos: usize, tab_width: usize) -> Po
7778
7879 let line_start = text.line_to_char(line);
7980 let pos = ensure_grapheme_boundary_prev(text, pos);
80− let col = text
81− .slice(line_start..pos)
82− .chars()
83− .flat_map(|c| match c {
84− '\t' => Some(tab_width),
85− c => UnicodeWidthChar::width(c),
86− })
87− .sum();
81+
82+ let mut col = 0;
83+
84+ for grapheme in RopeGraphemes::new(text.slice(line_start..pos)) {
85+ if grapheme == "\t" {
86+ col += tab_width;
87+ } else {
88+ let grapheme = Cow::from(grapheme);
89+ col += grapheme_width(&grapheme);
90+ }
91+ }
8892
8993 Position::new(line, col)
9094}
helix-term/src/commands.rs+1
@@ -1551,6 +1551,7 @@ fn search_impl(
15511551 };
15521552
15531553 doc.set_selection(view.id, selection);
1554+ // TODO: is_cursor_in_view does the same calculation as ensure_cursor_in_view
15541555 if view.is_cursor_in_view(doc, 0) {
15551556 view.ensure_cursor_in_view(doc, scrolloff);
15561557 } else {
helix-view/src/view.rs+2−12
@@ -204,19 +204,9 @@ impl View {
204204 return None;
205205 }
206206
207− let line_start = text.line_to_char(line);
208− let line_slice = text.slice(line_start..pos);
209− let mut col = 0;
210207 let tab_width = doc.tab_width();
211−
212− for grapheme in RopeGraphemes::new(line_slice) {
213− if grapheme == "\t" {
214− col += tab_width;
215− } else {
216− let grapheme = Cow::from(grapheme);
217− col += grapheme_width(&grapheme);
218− }
219− }
208+ // TODO: visual_coords_at_pos also does char_to_line which we ignore, can we reuse the call?
209+ let Position { col, .. } = visual_coords_at_pos(text, pos, tab_width);
220210
221211 // It is possible for underflow to occur if the buffer length is larger than the terminal width.
222212 let row = line.saturating_sub(self.offset.row);