Skip to content

Commit

Permalink
Merge pull request #833 from gwenn/fix
Browse files Browse the repository at this point in the history
Fix merge error
  • Loading branch information
gwenn authored Dec 14, 2024
2 parents b4814bd + 7521e9b commit dbcb7c0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
10 changes: 5 additions & 5 deletions src/line_buffer.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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')
Expand All @@ -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
Expand Down

0 comments on commit dbcb7c0

Please sign in to comment.