summaryrefslogtreecommitdiff
path: root/helix-core/src/position.rs
diff options
context:
space:
mode:
authorBlaž Hrastnik2022-03-03 08:41:39 +0000
committerBlaž Hrastnik2022-03-04 00:36:31 +0000
commit5d14f56fa9822b18107a5a5efac98dfe9d08f8ce (patch)
treedcfa99543ac8b789d5566358c713a78db90a49bc /helix-core/src/position.rs
parent74a9dd51ffb7cd3c14d4c7b5502e4febad24caa0 (diff)
Reuse visual_coords_at_pos function in view
Diffstat (limited to 'helix-core/src/position.rs')
-rw-r--r--helix-core/src/position.rs24
1 files changed, 14 insertions, 10 deletions
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)
}