Reuse visual_coords_at_pos function in view

pull/1747/head
Blaž Hrastnik 3 years ago
parent 74a9dd51ff
commit 5d14f56fa9
No known key found for this signature in database
GPG Key ID: 1238B9C4AD889640

@ -1,8 +1,9 @@
use std::borrow::Cow;
use crate::{ use crate::{
chars::char_is_line_ending, chars::char_is_line_ending,
graphemes::{ensure_grapheme_boundary_prev, RopeGraphemes}, graphemes::{ensure_grapheme_boundary_prev, grapheme_width, RopeGraphemes},
line_ending::line_end_char_index, line_ending::line_end_char_index,
unicode::width::UnicodeWidthChar,
RopeSlice, RopeSlice,
}; };
@ -77,14 +78,17 @@ pub fn visual_coords_at_pos(text: RopeSlice, pos: usize, tab_width: usize) -> Po
let line_start = text.line_to_char(line); let line_start = text.line_to_char(line);
let pos = ensure_grapheme_boundary_prev(text, pos); let pos = ensure_grapheme_boundary_prev(text, pos);
let col = text
.slice(line_start..pos) let mut col = 0;
.chars()
.flat_map(|c| match c { for grapheme in RopeGraphemes::new(text.slice(line_start..pos)) {
'\t' => Some(tab_width), if grapheme == "\t" {
c => UnicodeWidthChar::width(c), col += tab_width;
}) } else {
.sum(); let grapheme = Cow::from(grapheme);
col += grapheme_width(&grapheme);
}
}
Position::new(line, col) Position::new(line, col)
} }

@ -1551,6 +1551,7 @@ fn search_impl(
}; };
doc.set_selection(view.id, selection); doc.set_selection(view.id, selection);
// TODO: is_cursor_in_view does the same calculation as ensure_cursor_in_view
if view.is_cursor_in_view(doc, 0) { if view.is_cursor_in_view(doc, 0) {
view.ensure_cursor_in_view(doc, scrolloff); view.ensure_cursor_in_view(doc, scrolloff);
} else { } else {

@ -204,19 +204,9 @@ impl View {
return None; return None;
} }
let line_start = text.line_to_char(line);
let line_slice = text.slice(line_start..pos);
let mut col = 0;
let tab_width = doc.tab_width(); let tab_width = doc.tab_width();
// TODO: visual_coords_at_pos also does char_to_line which we ignore, can we reuse the call?
for grapheme in RopeGraphemes::new(line_slice) { let Position { col, .. } = visual_coords_at_pos(text, pos, tab_width);
if grapheme == "\t" {
col += tab_width;
} else {
let grapheme = Cow::from(grapheme);
col += grapheme_width(&grapheme);
}
}
// It is possible for underflow to occur if the buffer length is larger than the terminal width. // It is possible for underflow to occur if the buffer length is larger than the terminal width.
let row = line.saturating_sub(self.offset.row); let row = line.saturating_sub(self.offset.row);

Loading…
Cancel
Save