From cbced2a11f538c26c267ddd8497af2b3c65eac04 Mon Sep 17 00:00:00 2001 From: gwenn Date: Sat, 14 Dec 2024 15:33:46 +0100 Subject: [PATCH 1/2] Fix merge error --- src/edit.rs | 2 +- src/layout.rs | 3 ++- src/line_buffer.rs | 10 +++++----- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/edit.rs b/src/edit.rs index 9d85ab6b5..6dc21928d 100644 --- a/src/edit.rs +++ b/src/edit.rs @@ -11,7 +11,7 @@ use crate::hint::Hint; use crate::history::SearchDirection; use crate::keymap::{Anchor, At, CharSearch, Cmd, Movement, RepeatCount, Word}; use crate::keymap::{InputState, Invoke, Refresher}; -use crate::layout::{cwidh, Layout, Position}; +use crate::layout::{cwidh, gcount, Layout, Position}; use crate::line_buffer::{ ChangeListener, DeleteListener, Direction, LineBuffer, NoListener, WordAction, MAX_LINE, }; diff --git a/src/layout.rs b/src/layout.rs index 4e73a4c8a..ba8d603f7 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -2,11 +2,12 @@ use std::cmp::Ordering; /// Height, width pub type Unit = u16; - +/// Character width / number of columns pub(crate) fn cwidh(c: char) -> Unit { use unicode_width::UnicodeWidthChar; Unit::try_from(c.width().unwrap_or(0)).unwrap() } +/// String width / number of columns pub(crate) fn swidth(s: &str) -> Unit { use unicode_width::UnicodeWidthStr; Unit::try_from(s.width()).unwrap() diff --git a/src/line_buffer.rs b/src/line_buffer.rs index 6c7974b7d..38c8812ff 100644 --- a/src/line_buffer.rs +++ b/src/line_buffer.rs @@ -1,6 +1,6 @@ //! Line buffer with current cursor position use crate::keymap::{At, CharSearch, Movement, RepeatCount, Word}; -use crate::layout::Layout; +use crate::layout::{swidth, Layout}; use std::cmp::min; use std::fmt; use std::iter; @@ -588,7 +588,7 @@ impl LineBuffer { pub fn move_to_line_up(&mut self, n: RepeatCount, layout: &Layout) -> bool { match self.buf[..self.pos].rfind('\n') { Some(off) => { - let column = self.buf[off + 1..self.pos].graphemes(true).count(); + let column = swidth(&self.buf[off + 1..self.pos]); let mut dest_start = self.buf[..off].rfind('\n').map_or(0, |n| n + 1); let mut dest_end = off; @@ -606,7 +606,7 @@ impl LineBuffer { }; let gidx = self.buf[dest_start..dest_end] .grapheme_indices(true) - .nth(column.saturating_sub(offset)); + .nth(column.saturating_sub(offset) as usize); self.pos = gidx.map_or(off, |(idx, _)| dest_start + idx); // if there's no enough columns true @@ -669,7 +669,7 @@ impl LineBuffer { } else { 0 }; - let column = self.buf[line_start..self.pos].graphemes(true).count() + offset; + let column = swidth(&self.buf[line_start..self.pos]) + offset; let mut dest_start = self.pos + off + 1; let mut dest_end = self.buf[dest_start..] .find('\n') @@ -685,7 +685,7 @@ impl LineBuffer { } self.pos = self.buf[dest_start..dest_end] .grapheme_indices(true) - .nth(column) + .nth(column as usize) .map_or(dest_end, |(idx, _)| dest_start + idx); // if there's no enough columns debug_assert!(self.pos <= self.buf.len()); true From 7521e9b55318a05764b8057bb1496f2b5f88acd5 Mon Sep 17 00:00:00 2001 From: gwenn Date: Sat, 14 Dec 2024 15:37:33 +0100 Subject: [PATCH 2/2] Oops --- src/edit.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/edit.rs b/src/edit.rs index 6dc21928d..9d85ab6b5 100644 --- a/src/edit.rs +++ b/src/edit.rs @@ -11,7 +11,7 @@ use crate::hint::Hint; use crate::history::SearchDirection; use crate::keymap::{Anchor, At, CharSearch, Cmd, Movement, RepeatCount, Word}; use crate::keymap::{InputState, Invoke, Refresher}; -use crate::layout::{cwidh, gcount, Layout, Position}; +use crate::layout::{cwidh, Layout, Position}; use crate::line_buffer::{ ChangeListener, DeleteListener, Direction, LineBuffer, NoListener, WordAction, MAX_LINE, };