From 5d14f56fa9822b18107a5a5efac98dfe9d08f8ce Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Thu, 3 Mar 2022 17:41:39 +0900 Subject: Reuse visual_coords_at_pos function in view --- helix-core/src/position.rs | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'helix-core/src/position.rs') diff --git a/helix-core/src/position.rs b/helix-core/src/position.rs index 93362c77..17908d7a 100644 --- a/helix-core/src/position.rs +++ b/helix-core/src/position.rs @@ -1,8 +1,9 @@ +use std::borrow::Cow; + use crate::{ 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, - unicode::width::UnicodeWidthChar, 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 pos = ensure_grapheme_boundary_prev(text, pos); - let col = text - .slice(line_start..pos) - .chars() - .flat_map(|c| match c { - '\t' => Some(tab_width), - c => UnicodeWidthChar::width(c), - }) - .sum(); + + let mut col = 0; + + for grapheme in RopeGraphemes::new(text.slice(line_start..pos)) { + if grapheme == "\t" { + col += tab_width; + } else { + let grapheme = Cow::from(grapheme); + col += grapheme_width(&grapheme); + } + } Position::new(line, col) } -- cgit v1.2.3-70-g09d2