use iced::Task;
use crate::canvas_editor::editing::word;
use crate::canvas_editor::{ArrowDirection, CodeEditor, Message, VimMode};
impl CodeEditor {
pub(crate) fn vim_accepts_insert_input(&self) -> bool {
!self.vim_enabled || self.vim_state.mode() == VimMode::Insert
}
fn begin_navigation(&mut self, shift_pressed: bool) {
self.end_grouping_if_active();
if shift_pressed {
for cursor in self.cursors.as_mut_slice() {
if cursor.anchor.is_none() {
cursor.set_anchor();
}
}
} else {
self.clear_selection();
}
}
pub(crate) fn handle_arrow_key(
&mut self,
direction: ArrowDirection,
shift_pressed: bool,
) -> Task<Message> {
self.begin_navigation(shift_pressed);
self.move_cursor(direction);
self.finish_navigation_operation();
self.scroll_to_cursor()
}
pub(crate) fn handle_home(&mut self, shift_pressed: bool) -> Task<Message> {
self.begin_navigation(shift_pressed);
for cursor in self.cursors.as_mut_slice() {
let first =
word::first_non_blank(self.buffer.line(cursor.position.0));
cursor.position.1 =
if cursor.position.1 == first { 0 } else { first };
}
self.cursors.sort_and_merge();
self.finish_navigation_operation();
self.scroll_to_cursor()
}
pub(crate) fn handle_word_move(
&mut self,
forward: bool,
shift_pressed: bool,
) -> Task<Message> {
self.begin_navigation(shift_pressed);
for cursor in self.cursors.as_mut_slice() {
cursor.position = if forward {
word::word_right(&self.buffer, cursor.position)
} else {
word::word_left(&self.buffer, cursor.position)
};
}
self.cursors.sort_and_merge();
self.finish_navigation_operation();
self.scroll_to_cursor()
}
pub(crate) fn handle_end(&mut self, shift_pressed: bool) -> Task<Message> {
self.begin_navigation(shift_pressed);
for cursor in self.cursors.as_mut_slice() {
cursor.position.1 = self.buffer.line_len(cursor.position.0);
}
self.cursors.sort_and_merge();
self.finish_navigation_operation();
self.scroll_to_cursor()
}
pub(crate) fn handle_ctrl_home(&mut self) -> Task<Message> {
self.begin_navigation(false);
self.cursors.set_single((0, 0));
self.finish_navigation_operation();
self.scroll_to_cursor()
}
pub(crate) fn handle_ctrl_end(&mut self) -> Task<Message> {
self.begin_navigation(false);
let last_line = self.buffer.line_count().saturating_sub(1);
let last_col = self.buffer.line_len(last_line);
self.cursors.set_single((last_line, last_col));
self.finish_navigation_operation();
self.scroll_to_cursor()
}
pub(crate) fn handle_page_up(
&mut self,
shift_pressed: bool,
) -> Task<Message> {
self.begin_navigation(shift_pressed);
self.page_up();
self.finish_navigation_operation();
self.scroll_to_cursor()
}
pub(crate) fn handle_page_down(
&mut self,
shift_pressed: bool,
) -> Task<Message> {
self.begin_navigation(shift_pressed);
self.page_down();
self.finish_navigation_operation();
self.scroll_to_cursor()
}
pub(crate) fn handle_goto_position(
&mut self,
line: usize,
col: usize,
) -> Task<Message> {
self.end_grouping_if_active();
self.set_cursor(line, col)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_home_key() {
let mut editor = CodeEditor::new("hello world", "py");
editor.cursors.primary_mut().position = (0, 5); let _ = editor.update(&Message::Home(false));
assert_eq!(editor.cursors.primary_position(), (0, 0));
}
#[test]
fn test_end_key() {
let mut editor = CodeEditor::new("hello world", "py");
editor.cursors.primary_mut().position = (0, 0);
let _ = editor.update(&Message::End(false));
assert_eq!(editor.cursors.primary_position(), (0, 11)); }
#[test]
fn test_arrow_key_with_shift_creates_selection() {
let mut editor = CodeEditor::new("hello world", "py");
editor.cursors.primary_mut().position = (0, 0);
let _ = editor.update(&Message::ArrowKey(ArrowDirection::Right, true));
assert!(editor.cursors.primary().anchor.is_some());
assert!(editor.cursors.primary().has_selection());
}
#[test]
fn test_arrow_key_without_shift_clears_selection() {
let mut editor = CodeEditor::new("hello world", "py");
editor.cursors.primary_mut().anchor = Some((0, 0));
editor.cursors.primary_mut().position = (0, 5);
let _ = editor.update(&Message::ArrowKey(ArrowDirection::Right, false));
assert!(editor.cursors.primary().anchor.is_none());
assert!(!editor.cursors.primary().has_selection());
}
fn paged_editor() -> CodeEditor {
let content = (0..10)
.map(|index| format!("line{index}"))
.collect::<Vec<_>>()
.join("\n");
let mut editor = CodeEditor::new(&content, "py");
editor.viewport_height = editor.line_height * 3.0;
editor
}
#[test]
fn test_page_down_without_shift_clears_selection() {
let mut editor = paged_editor();
editor.cursors.primary_mut().anchor = Some((0, 0));
editor.cursors.primary_mut().position = (0, 2);
let _ = editor.update(&Message::PageDown(false));
assert_eq!(editor.cursors.primary_position(), (3, 2));
assert!(editor.cursors.primary().anchor.is_none());
assert!(!editor.cursors.primary().has_selection());
}
#[test]
fn test_page_up_without_shift_clears_selection() {
let mut editor = paged_editor();
editor.cursors.primary_mut().anchor = Some((9, 0));
editor.cursors.primary_mut().position = (5, 2);
let _ = editor.update(&Message::PageUp(false));
assert_eq!(editor.cursors.primary_position(), (2, 2));
assert!(editor.cursors.primary().anchor.is_none());
assert!(!editor.cursors.primary().has_selection());
}
#[test]
fn test_page_down_with_shift_creates_selection() {
let mut editor = paged_editor();
editor.cursors.primary_mut().position = (0, 0);
let _ = editor.update(&Message::PageDown(true));
assert_eq!(editor.cursors.primary_position(), (3, 0));
assert_eq!(editor.cursors.primary().anchor, Some((0, 0)));
assert!(editor.cursors.primary().has_selection());
}
#[test]
fn test_page_up_with_shift_creates_selection() {
let mut editor = paged_editor();
editor.cursors.primary_mut().position = (5, 0);
let _ = editor.update(&Message::PageUp(true));
assert_eq!(editor.cursors.primary_position(), (2, 0));
assert_eq!(editor.cursors.primary().anchor, Some((5, 0)));
assert!(editor.cursors.primary().has_selection());
}
#[test]
fn test_page_down_after_click_does_not_select() {
let mut editor = paged_editor();
let point = iced::Point::new(
editor.gutter_width() + 5.0 + editor.char_width * 2.0,
editor.line_height / 2.0,
);
let _ = editor.update(&Message::MouseClick(point));
let _ = editor.update(&Message::MouseRelease);
assert!(!editor.cursors.primary().has_selection());
let _ = editor.update(&Message::PageDown(false));
assert!(editor.cursors.primary().anchor.is_none());
assert!(!editor.cursors.primary().has_selection());
}
#[test]
fn test_shift_page_down_extends_every_cursor_that_stays_apart() {
let mut editor = paged_editor();
editor.cursors.set_single((0, 0));
editor.cursors.add_cursor((8, 0));
let _ = editor.update(&Message::PageDown(true));
let cursors: Vec<_> = editor
.cursors
.as_slice()
.iter()
.map(|cursor| (cursor.anchor, cursor.position))
.collect();
assert_eq!(
cursors,
vec![(Some((0, 0)), (3, 0)), (Some((8, 0)), (9, 0))]
);
}
#[test]
fn test_shift_page_down_merges_selections_that_overlap() {
let mut editor = paged_editor();
editor.cursors.set_single((0, 0));
editor.cursors.add_cursor((1, 0));
editor.cursors.add_cursor((2, 0));
let _ = editor.update(&Message::PageDown(true));
assert_eq!(editor.cursors.len(), 1);
assert_eq!(editor.cursors.primary().anchor, Some((0, 0)));
assert_eq!(editor.cursors.primary_position(), (5, 0));
}
#[test]
fn test_page_down_without_shift_keeps_the_cursors_it_does_not_merge() {
let mut editor = paged_editor();
editor.cursors.set_single((0, 0));
editor.cursors.add_cursor((3, 0));
editor.cursors.add_cursor((6, 0));
let _ = editor.update(&Message::PageDown(false));
let positions: Vec<_> = editor
.cursors
.as_slice()
.iter()
.map(|cursor| cursor.position)
.collect();
assert_eq!(positions, vec![(3, 0), (6, 0), (9, 0)]);
}
fn typing_editor() -> CodeEditor {
let mut editor = CodeEditor::new("line0\nline1\nline2", "py");
editor.request_focus();
editor.has_canvas_focus = true;
editor.focus_locked = false;
editor.cursors.primary_mut().position = (1, 2);
editor
}
#[test]
fn test_one_undo_after_navigating_keeps_the_earlier_typing() {
for message in [
Message::Home(false),
Message::End(false),
Message::CtrlHome,
Message::CtrlEnd,
Message::ArrowKey(ArrowDirection::Down, false),
Message::PageDown(false),
Message::PageUp(false),
] {
let mut editor = typing_editor();
let _ = editor.update(&Message::CharacterInput('A'));
let _ = editor.update(&message);
let after_move = editor.content();
let _ = editor.update(&Message::CharacterInput('B'));
assert_ne!(editor.content(), after_move);
let _ = editor.update(&Message::Undo);
assert_eq!(
editor.content(),
after_move,
"{message:?}: one undo did not stop at the navigation"
);
}
}
#[test]
fn test_navigating_closes_the_undo_group() {
for message in [
Message::Home(false),
Message::End(false),
Message::CtrlHome,
Message::CtrlEnd,
Message::ArrowKey(ArrowDirection::Up, false),
Message::PageDown(false),
Message::PageUp(false),
] {
let mut editor = typing_editor();
let _ = editor.update(&Message::CharacterInput('!'));
assert!(editor.is_grouping, "{message:?}: nothing to close");
let _ = editor.update(&message);
assert!(
!editor.is_grouping,
"{message:?} left the undo group open"
);
}
}
#[test]
fn test_ctrl_home_still_drops_the_selection() {
let mut editor = typing_editor();
editor.cursors.primary_mut().anchor = Some((0, 0));
assert!(editor.cursors.primary().has_selection());
let _ = editor.update(&Message::CtrlHome);
assert!(!editor.cursors.primary().has_selection());
assert_eq!(editor.cursors.primary_position(), (0, 0));
}
#[test]
fn test_ctrl_home() {
let mut editor = CodeEditor::new("line1\nline2\nline3", "py");
editor.cursors.primary_mut().position = (2, 5); let _ = editor.update(&Message::CtrlHome);
assert_eq!(editor.cursors.primary_position(), (0, 0)); }
#[test]
fn test_ctrl_end() {
let mut editor = CodeEditor::new("line1\nline2\nline3", "py");
editor.cursors.primary_mut().position = (0, 0); let _ = editor.update(&Message::CtrlEnd);
assert_eq!(editor.cursors.primary_position(), (2, 5)); }
#[test]
fn test_ctrl_home_clears_selection() {
let mut editor = CodeEditor::new("line1\nline2\nline3", "py");
editor.cursors.primary_mut().position = (2, 5);
editor.cursors.primary_mut().anchor = Some((0, 0));
editor.cursors.primary_mut().position = (2, 5);
let _ = editor.update(&Message::CtrlHome);
assert_eq!(editor.cursors.primary_position(), (0, 0));
assert!(editor.cursors.primary().anchor.is_none());
assert!(!editor.cursors.primary().has_selection());
}
#[test]
fn test_ctrl_end_clears_selection() {
let mut editor = CodeEditor::new("line1\nline2\nline3", "py");
editor.cursors.primary_mut().position = (0, 0);
editor.cursors.primary_mut().anchor = Some((0, 0));
editor.cursors.primary_mut().position = (1, 3);
let _ = editor.update(&Message::CtrlEnd);
assert_eq!(editor.cursors.primary_position(), (2, 5));
assert!(editor.cursors.primary().anchor.is_none());
assert!(!editor.cursors.primary().has_selection());
}
#[test]
fn test_goto_position_sets_cursor_and_clears_selection() {
let mut editor = CodeEditor::new("line1\nline2\nline3", "py");
editor.cursors.primary_mut().anchor = Some((0, 0));
editor.cursors.primary_mut().position = (1, 2);
let _ = editor.update(&Message::GotoPosition(1, 3));
assert_eq!(editor.cursors.primary_position(), (1, 3));
assert!(editor.cursors.primary().anchor.is_none());
assert!(!editor.cursors.primary().has_selection());
}
#[test]
fn test_goto_position_clamps_out_of_range() {
let mut editor = CodeEditor::new("a\nbb", "py");
let _ = editor.update(&Message::GotoPosition(99, 99));
assert_eq!(editor.cursors.primary_position(), (1, 2));
}
#[test]
fn test_navigation_ends_grouping() {
let mut editor = CodeEditor::new("hello", "py");
editor.request_focus();
editor.has_canvas_focus = true;
editor.focus_locked = false;
editor.cursors.primary_mut().position = (0, 5);
let _ = editor.update(&Message::CharacterInput('!'));
assert!(editor.is_grouping);
let _ = editor.update(&Message::ArrowKey(ArrowDirection::Left, false));
assert!(!editor.is_grouping);
let _ = editor.update(&Message::CharacterInput('?'));
assert!(editor.is_grouping);
editor.history.end_group();
let _ = editor.update(&Message::Undo);
assert_eq!(editor.buffer.line(0), "hello!");
let _ = editor.update(&Message::Undo);
assert_eq!(editor.buffer.line(0), "hello");
}
#[test]
fn test_home_goes_to_first_non_blank_then_to_column_zero() {
let mut editor = CodeEditor::new(" let x = 1;", "rs");
editor.cursors.primary_mut().position = (0, 10);
let _ = editor.update(&Message::Home(false));
assert_eq!(editor.cursors.primary_position(), (0, 4));
let _ = editor.update(&Message::Home(false));
assert_eq!(editor.cursors.primary_position(), (0, 0));
let _ = editor.update(&Message::Home(false));
assert_eq!(editor.cursors.primary_position(), (0, 4));
}
#[test]
fn test_shift_home_selects_to_first_non_blank() {
let mut editor = CodeEditor::new(" foo", "rs");
editor.cursors.primary_mut().position = (0, 5);
let _ = editor.update(&Message::Home(true));
assert_eq!(editor.cursors.primary_position(), (0, 2));
assert_eq!(editor.cursors.primary().anchor, Some((0, 5)));
}
#[test]
fn test_word_moves_skip_one_run_and_cross_lines() {
let mut editor = CodeEditor::new(
"foo.bar baz
qux",
"rs",
);
editor.cursors.primary_mut().position = (0, 0);
let mut stops = Vec::new();
for _ in 0..6 {
let _ = editor.update(&Message::WordRight(false));
stops.push(editor.cursors.primary_position());
}
assert_eq!(stops, [(0, 3), (0, 4), (0, 7), (0, 11), (1, 0), (1, 3)]);
let mut stops = Vec::new();
for _ in 0..6 {
let _ = editor.update(&Message::WordLeft(false));
stops.push(editor.cursors.primary_position());
}
assert_eq!(stops, [(1, 0), (0, 11), (0, 8), (0, 4), (0, 3), (0, 0)]);
}
#[test]
fn test_shift_word_move_extends_the_selection() {
let mut editor = CodeEditor::new("alpha beta gamma", "rs");
editor.cursors.primary_mut().position = (0, 0);
let _ = editor.update(&Message::WordRight(true));
let _ = editor.update(&Message::WordRight(true));
assert_eq!(editor.cursors.primary().anchor, Some((0, 0)));
assert_eq!(editor.cursors.primary_position(), (0, 10));
assert_eq!(editor.get_selected_text().as_deref(), Some("alpha beta"));
let _ = editor.update(&Message::WordLeft(false));
assert!(!editor.cursors.primary().has_selection());
assert_eq!(editor.cursors.primary_position(), (0, 6));
}
#[test]
fn test_word_move_moves_every_cursor() {
let mut editor = CodeEditor::new(
"one two
three four",
"rs",
);
editor.cursors.primary_mut().position = (0, 0);
editor.cursors.add_cursor((1, 0));
let _ = editor.update(&Message::WordRight(false));
let positions: Vec<_> =
editor.cursors.iter().map(|cursor| cursor.position).collect();
assert_eq!(positions, [(0, 3), (1, 5)]);
}
}