on master help sign in
Reuse visual_coords_at_pos function in view
5d14f56Blaž Hrastnik
helix-core/src/position.rs+14−10
@@ -1,8 +1,9 @@
1+use std::borrow::Cow;
2+
1use crate::{
3use crate::{
2 chars::char_is_line_ending,
4 chars::char_is_line_ending,
3− graphemes::{ensure_grapheme_boundary_prev, RopeGraphemes},
5+ graphemes::{ensure_grapheme_boundary_prev, grapheme_width, RopeGraphemes},
4 line_ending::line_end_char_index,
6 line_ending::line_end_char_index,
5− unicode::width::UnicodeWidthChar,
6 RopeSlice,
7 RopeSlice,
7};
8};
8
9
@@ -77,14 +78,17 @@ pub fn visual_coords_at_pos(text: RopeSlice, pos: usize, tab_width: usize) -> Po
77
78
78 let line_start = text.line_to_char(line);
79 let line_start = text.line_to_char(line);
79 let pos = ensure_grapheme_boundary_prev(text, pos);
80 let pos = ensure_grapheme_boundary_prev(text, pos);
81+
80− let col = text
82+ let mut col = 0;
83+
81− .slice(line_start..pos)
84+ for grapheme in RopeGraphemes::new(text.slice(line_start..pos)) {
82− .chars()
85+ if grapheme == "\t" {
83− .flat_map(|c| match c {
84− '\t' => Some(tab_width),
86+ col += tab_width;
85− c => UnicodeWidthChar::width(c),
87+ } else {
86− })
88+ let grapheme = Cow::from(grapheme);
87− .sum();
89+ col += grapheme_width(&grapheme);
90+ }
91+ }
88
92
89 Position::new(line, col)
93 Position::new(line, col)
90}
94}
helix-term/src/commands.rs+1
@@ -1551,6 +1551,7 @@ fn search_impl(
1551 };
1551 };
1552
1552
1553 doc.set_selection(view.id, selection);
1553 doc.set_selection(view.id, selection);
1554+ // TODO: is_cursor_in_view does the same calculation as ensure_cursor_in_view
1554 if view.is_cursor_in_view(doc, 0) {
1555 if view.is_cursor_in_view(doc, 0) {
1555 view.ensure_cursor_in_view(doc, scrolloff);
1556 view.ensure_cursor_in_view(doc, scrolloff);
1556 } else {
1557 } else {
helix-view/src/view.rs+2−12
@@ -204,19 +204,9 @@ impl View {
204 return None;
204 return None;
205 }
205 }
206
206
207− let line_start = text.line_to_char(line);
208− let line_slice = text.slice(line_start..pos);
209− let mut col = 0;
210 let tab_width = doc.tab_width();
207 let tab_width = doc.tab_width();
211−
208+ // TODO: visual_coords_at_pos also does char_to_line which we ignore, can we reuse the call?
212− for grapheme in RopeGraphemes::new(line_slice) {
209+ let Position { col, .. } = visual_coords_at_pos(text, pos, tab_width);
213− if grapheme == "\t" {
214− col += tab_width;
215− } else {
216− let grapheme = Cow::from(grapheme);
217− col += grapheme_width(&grapheme);
218− }
219− }
220
210
221 // It is possible for underflow to occur if the buffer length is larger than the terminal width.
211 // It is possible for underflow to occur if the buffer length is larger than the terminal width.
222 let row = line.saturating_sub(self.offset.row);
212 let row = line.saturating_sub(self.offset.row);