diff --git a/src/cursor.rs b/src/cursor.rs index 011641c..d0b46ab 100644 --- a/src/cursor.rs +++ b/src/cursor.rs @@ -56,8 +56,19 @@ impl CursorState { self.selection } - pub(crate) const fn desired_cursor_column(&self) -> Option { - self.desired_cursor_column + /// Gets (or inserts the current cursor column) the desired column to + /// navigate to on vertical cursor movement. + pub(crate) fn get_or_insert_desired_column( + &mut self, + text: RopeSlice<'_>, + text_width: NonZeroColumns, + ) -> Columns { + let cursor = self.cursor(); + + *self.desired_cursor_column.get_or_insert_with(|| { + let cursor_line = text.line_idx_containing_byte(cursor); + visual_cursor_position(text, cursor, cursor_line, text_width).left() + }) } pub(crate) const fn set_cursor(&mut self, index: ByteIndex) { @@ -75,10 +86,6 @@ impl CursorState { pub(crate) const fn clear_desired_column(&mut self) { self.desired_cursor_column = None; } - - pub(crate) const fn set_desired_cursor_column(&mut self, column: Columns) { - self.desired_cursor_column = Some(column); - } } #[derive(Debug, Clone, Copy, Default)] diff --git a/src/document/mod.rs b/src/document/mod.rs index cc505f3..054798c 100644 --- a/src/document/mod.rs +++ b/src/document/mod.rs @@ -455,7 +455,7 @@ impl Document { } if action.should_reset_desired_column() { - self.clear_desired_column(); + self.cursor_state.clear_desired_column(); } } @@ -511,14 +511,11 @@ impl Document { MovementAction::MoveDown => { match self.content_layout().max_text_width() { Some(width) => { - let target_column = self.desired_column(width); - move_cursor_down( - self.text.slice(..), - self.selection(), - target_column, - width, - action_count, - ) + let text = self.text.slice(..); + let target_column = + self.cursor_state.get_or_insert_desired_column(text, width); + + move_cursor_down(text, self.selection(), target_column, width, action_count) } None => self.selection(), } @@ -526,14 +523,11 @@ impl Document { MovementAction::MoveUp => { match self.content_layout().max_text_width() { Some(width) => { - let target_column = self.desired_column(width); - move_cursor_up( - self.text.slice(..), - self.selection(), - target_column, - width, - action_count, - ) + let text = self.text.slice(..); + let target_column = + self.cursor_state.get_or_insert_desired_column(text, width); + + move_cursor_up(text, self.selection(), target_column, width, action_count) } None => self.selection(), } @@ -670,33 +664,6 @@ impl Document { self.scroll_offset = cursor_line.saturating_sub(middle); } - /// Gets (or inserts the current cursor column) the desired column to - /// navigate to on vertical cursor movement. - fn desired_column(&mut self, max_width: NonZeroColumns) -> Columns { - if let Some(column) = self.cursor_state.desired_cursor_column() { - return column; - } - - let text = self.text.slice(..); - let cursor = self.cursor(); - - let line_start = text.line_start_byte(text.line_idx_containing_byte(cursor)); - - let column = text - .slice(line_start.value()..cursor.value()) - .chunks() - .map(text_width) - .sum::() - .map(|cols| cols % max_width.get().value()); - - self.cursor_state.set_desired_cursor_column(column); - column - } - - const fn clear_desired_column(&mut self) { - self.cursor_state.clear_desired_column(); - } - const fn event_fallback(&self, key_event: KeyEvent) -> Option { match self.mode() { Mode::Normal | Mode::Visual => None, diff --git a/src/ui.rs b/src/ui.rs index b395e03..eae4362 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -298,10 +298,6 @@ impl Columns { self.0 } - pub(crate) fn map(self, map_fn: impl FnOnce(usize) -> usize) -> Self { - Self(map_fn(self.0)) - } - #[must_use = "`saturating_sub` does not mutate the current value, but returns a new value"] pub(crate) fn saturating_sub(self, rhs: impl Into) -> Self { Self(self.0.saturating_sub(rhs.into()))