use iced::advanced::input_method;
use iced::widget::canvas::Action;
use iced::{Point, Rectangle, keyboard, mouse};
use crate::canvas_editor::features::folding;
use crate::canvas_editor::features::vim::VimMode;
use crate::canvas_editor::{
ArrowDirection, CodeEditor, FOCUSED_EDITOR_ID, Message,
};
fn is_key_char(
key: &keyboard::Key,
modified_key: &keyboard::Key,
ch: &str,
) -> bool {
matches!(key, keyboard::Key::Character(c) if c.as_str() == ch)
|| matches!(modified_key, keyboard::Key::Character(c) if c.as_str() == ch)
}
fn vim_toggle_shortcut(
key: &keyboard::Key,
modified_key: &keyboard::Key,
modifiers: &keyboard::Modifiers,
) -> Option<Action<Message>> {
let command_pressed = modifiers.command() || modifiers.control();
if command_pressed
&& modifiers.alt()
&& !modifiers.shift()
&& is_key_char(key, modified_key, "v")
{
return Some(Action::publish(Message::ToggleVimMode).and_capture());
}
None
}
fn write_shortcut(
key: &keyboard::Key,
modified_key: &keyboard::Key,
modifiers: &keyboard::Modifiers,
) -> Option<Action<Message>> {
let command_pressed = modifiers.command() || modifiers.control();
if command_pressed
&& !modifiers.alt()
&& !modifiers.shift()
&& is_key_char(key, modified_key, "s")
{
return Some(Action::publish(Message::WriteRequested).and_capture());
}
None
}
fn clipboard_shortcut(
key: &keyboard::Key,
modified_key: &keyboard::Key,
modifiers: &keyboard::Modifiers,
) -> Option<Action<Message>> {
let command_pressed = modifiers.command() || modifiers.control();
if (command_pressed && is_key_char(key, modified_key, "c"))
|| (modifiers.control()
&& matches!(
key,
keyboard::Key::Named(keyboard::key::Named::Insert)
))
{
return Some(Action::publish(Message::Copy).and_capture());
}
if command_pressed && is_key_char(key, modified_key, "x") {
return Some(Action::publish(Message::Cut).and_capture());
}
if command_pressed && is_key_char(key, modified_key, "a") {
return Some(Action::publish(Message::SelectAll).and_capture());
}
if (command_pressed && is_key_char(key, modified_key, "v"))
|| (modifiers.shift()
&& matches!(
key,
keyboard::Key::Named(keyboard::key::Named::Insert)
))
{
return Some(Action::publish(Message::Paste(String::new())));
}
None
}
fn multi_cursor_shortcut(
key: &keyboard::Key,
modified_key: &keyboard::Key,
modifiers: &keyboard::Modifiers,
) -> Option<Action<Message>> {
let command_pressed = modifiers.command() || modifiers.control();
if command_pressed && is_key_char(key, modified_key, "d") {
return Some(
Action::publish(Message::SelectNextOccurrence).and_capture(),
);
}
if modifiers.control()
&& modifiers.alt()
&& matches!(key, keyboard::Key::Named(keyboard::key::Named::ArrowUp))
{
return Some(Action::publish(Message::AddCursorAbove).and_capture());
}
if modifiers.control()
&& modifiers.alt()
&& matches!(key, keyboard::Key::Named(keyboard::key::Named::ArrowDown))
{
return Some(Action::publish(Message::AddCursorBelow).and_capture());
}
None
}
fn editing_shortcut(
key: &keyboard::Key,
modified_key: &keyboard::Key,
modifiers: &keyboard::Modifiers,
) -> Option<Action<Message>> {
let command_pressed = modifiers.command() || modifiers.control();
if command_pressed && is_key_char(key, modified_key, "/") {
return Some(Action::publish(Message::ToggleComment).and_capture());
}
if modifiers.shift()
&& matches!(key, keyboard::Key::Named(keyboard::key::Named::Delete))
{
return Some(Action::publish(Message::DeleteSelection).and_capture());
}
None
}
fn line_move_shortcut(
key: &keyboard::Key,
modifiers: &keyboard::Modifiers,
) -> Option<Action<Message>> {
if !modifiers.alt() || modifiers.control() {
return None;
}
if matches!(key, keyboard::Key::Named(keyboard::key::Named::ArrowUp)) {
let message = if modifiers.shift() {
Message::DuplicateLineUp
} else {
Message::MoveLineUp
};
return Some(Action::publish(message).and_capture());
}
if matches!(key, keyboard::Key::Named(keyboard::key::Named::ArrowDown)) {
let message = if modifiers.shift() {
Message::DuplicateLineDown
} else {
Message::MoveLineDown
};
return Some(Action::publish(message).and_capture());
}
None
}
fn navigation_shortcut(
key: &keyboard::Key,
modifiers: &keyboard::Modifiers,
) -> Option<Action<Message>> {
let command_pressed = modifiers.command() || modifiers.control();
if command_pressed
&& matches!(key, keyboard::Key::Named(keyboard::key::Named::Home))
{
return Some(Action::publish(Message::CtrlHome).and_capture());
}
if command_pressed
&& matches!(key, keyboard::Key::Named(keyboard::key::Named::End))
{
return Some(Action::publish(Message::CtrlEnd).and_capture());
}
None
}
impl CodeEditor {
pub(crate) fn has_focus(&self) -> bool {
let focused_id =
FOCUSED_EDITOR_ID.load(std::sync::atomic::Ordering::Relaxed);
focused_id == self.editor_id
&& self.has_canvas_focus
&& !self.focus_locked
}
fn handle_keyboard_shortcuts(
&self,
key: &keyboard::Key,
modified_key: &keyboard::Key,
modifiers: &keyboard::Modifiers,
) -> Option<Action<Message>> {
vim_toggle_shortcut(key, modified_key, modifiers)
.or_else(|| write_shortcut(key, modified_key, modifiers))
.or_else(|| self.focus_navigation_shortcut(key, modifiers))
.or_else(|| clipboard_shortcut(key, modified_key, modifiers))
.or_else(|| self.history_shortcut(key, modified_key, modifiers))
.or_else(|| self.dialog_shortcut(key, modified_key, modifiers))
.or_else(|| self.escape_shortcut(key))
.or_else(|| multi_cursor_shortcut(key, modified_key, modifiers))
.or_else(|| editing_shortcut(key, modified_key, modifiers))
.or_else(|| line_move_shortcut(key, modifiers))
.or_else(|| navigation_shortcut(key, modifiers))
.or_else(|| self.folding_shortcut(key, modified_key, modifiers))
}
fn focus_navigation_shortcut(
&self,
key: &keyboard::Key,
modifiers: &keyboard::Modifiers,
) -> Option<Action<Message>> {
if matches!(key, keyboard::Key::Named(keyboard::key::Named::Tab))
&& modifiers.shift()
&& !self.search_state.is_open
{
return Some(
Action::publish(Message::FocusNavigationShiftTab).and_capture(),
);
}
None
}
fn history_shortcut(
&self,
key: &keyboard::Key,
modified_key: &keyboard::Key,
modifiers: &keyboard::Modifiers,
) -> Option<Action<Message>> {
let command_pressed = modifiers.command() || modifiers.control();
if command_pressed
&& !modifiers.shift()
&& is_key_char(key, modified_key, "z")
{
return Some(Action::publish(Message::Undo).and_capture());
}
if command_pressed
&& (is_key_char(key, modified_key, "y")
|| (modifiers.shift() && is_key_char(key, modified_key, "z")))
{
return Some(Action::publish(Message::Redo).and_capture());
}
if self.vim_enabled
&& self.vim_state.mode() == VimMode::Normal
&& modifiers.control()
&& !modifiers.shift()
&& is_key_char(key, modified_key, "r")
{
return Some(Action::publish(Message::Redo).and_capture());
}
None
}
fn dialog_shortcut(
&self,
key: &keyboard::Key,
modified_key: &keyboard::Key,
modifiers: &keyboard::Modifiers,
) -> Option<Action<Message>> {
let command_pressed = modifiers.command() || modifiers.control();
if command_pressed
&& is_key_char(key, modified_key, "f")
&& self.search_replace_enabled
{
return Some(Action::publish(Message::OpenSearch).and_capture());
}
if command_pressed
&& is_key_char(key, modified_key, "h")
&& self.search_replace_enabled
{
return Some(
Action::publish(Message::OpenSearchReplace).and_capture(),
);
}
if command_pressed && is_key_char(key, modified_key, "g") {
return Some(Action::publish(Message::OpenGotoLine).and_capture());
}
if matches!(key, keyboard::Key::Named(keyboard::key::Named::Tab))
&& self.search_state.is_open
{
let message = if modifiers.shift() {
Message::SearchDialogShiftTab
} else {
Message::SearchDialogTab
};
return Some(Action::publish(message).and_capture());
}
if matches!(key, keyboard::Key::Named(keyboard::key::Named::F3))
&& self.search_replace_enabled
{
let message = if modifiers.shift() {
Message::FindPrevious
} else {
Message::FindNext
};
return Some(Action::publish(message).and_capture());
}
None
}
fn escape_shortcut(&self, key: &keyboard::Key) -> Option<Action<Message>> {
if !matches!(key, keyboard::Key::Named(keyboard::key::Named::Escape)) {
return None;
}
let message = if self.goto_line_state.is_open {
Some(Message::CloseGotoLine)
} else if self.search_state.is_open {
Some(Message::CloseSearch)
} else if self.vim_enabled {
Some(Message::VimKey('\u{1b}'))
} else if self.cursors.is_multi() {
Some(Message::CloseSearch)
} else {
None
};
message.map(|message| Action::publish(message).and_capture())
}
fn folding_shortcut(
&self,
key: &keyboard::Key,
modified_key: &keyboard::Key,
modifiers: &keyboard::Modifiers,
) -> Option<Action<Message>> {
if !self.folding_enabled {
return None;
}
if modifiers.control() && is_key_char(key, modified_key, ".") {
return Some(
Action::publish(Message::ToggleFoldAtCursor).and_capture(),
);
}
if modifiers.control()
&& !modifiers.shift()
&& !modifiers.alt()
&& is_key_char(key, modified_key, "k")
{
return Some(Action::publish(Message::FoldAll).and_capture());
}
if modifiers.control()
&& !modifiers.shift()
&& !modifiers.alt()
&& is_key_char(key, modified_key, "j")
{
return Some(Action::publish(Message::UnfoldAll).and_capture());
}
None
}
fn printable_input_message(&self, ch: char) -> Message {
if self.vim_enabled && self.vim_state.mode() != VimMode::Insert {
Message::VimKey(ch)
} else {
Message::CharacterInput(ch)
}
}
fn handle_character_input(
&self,
key: &keyboard::Key,
modifiers: &keyboard::Modifiers,
text: Option<&str>,
) -> Option<Action<Message>> {
if !self.has_focus() {
return None;
}
if let Some(text_content) = text
&& !text_content.is_empty()
&& !modifiers.control()
&& !modifiers.alt()
{
if let Some(first_char) = text_content.chars().next()
&& !first_char.is_control()
{
return Some(
Action::publish(self.printable_input_message(first_char))
.and_capture(),
);
}
}
let message = match key {
keyboard::Key::Named(keyboard::key::Named::Backspace)
if !self.vim_enabled
|| self.vim_state.mode() == VimMode::Insert
|| self.vim_state.command_line_active() =>
{
if self.vim_state.command_line_active() {
Some(Message::VimKey('\u{8}'))
} else {
Some(Message::Backspace)
}
}
keyboard::Key::Named(keyboard::key::Named::Delete)
if !self.vim_enabled
|| self.vim_state.mode() == VimMode::Insert =>
{
Some(Message::Delete)
}
keyboard::Key::Named(keyboard::key::Named::Enter)
if !self.vim_enabled
|| self.vim_state.mode() == VimMode::Insert
|| self.vim_state.command_line_active() =>
{
if self.vim_state.command_line_active() {
Some(Message::VimKey('\n'))
} else {
Some(Message::Enter)
}
}
keyboard::Key::Named(keyboard::key::Named::Tab)
if !self.vim_enabled
|| self.vim_state.mode() == VimMode::Insert =>
{
if modifiers.shift() {
Some(Message::FocusNavigationShiftTab)
} else {
if self.search_state.is_open {
Some(Message::SearchDialogTab)
} else {
Some(Message::Tab)
}
}
}
keyboard::Key::Named(keyboard::key::Named::ArrowUp) => {
Some(Message::ArrowKey(ArrowDirection::Up, modifiers.shift()))
}
keyboard::Key::Named(keyboard::key::Named::ArrowDown) => {
Some(Message::ArrowKey(ArrowDirection::Down, modifiers.shift()))
}
keyboard::Key::Named(keyboard::key::Named::ArrowLeft) => {
Some(Message::ArrowKey(ArrowDirection::Left, modifiers.shift()))
}
keyboard::Key::Named(keyboard::key::Named::ArrowRight) => Some(
Message::ArrowKey(ArrowDirection::Right, modifiers.shift()),
),
keyboard::Key::Named(keyboard::key::Named::PageUp) => {
Some(Message::PageUp)
}
keyboard::Key::Named(keyboard::key::Named::PageDown) => {
Some(Message::PageDown)
}
keyboard::Key::Named(keyboard::key::Named::Home) => {
Some(Message::Home(modifiers.shift()))
}
keyboard::Key::Named(keyboard::key::Named::End) => {
Some(Message::End(modifiers.shift()))
}
_ => {
if !modifiers.control()
&& !modifiers.alt()
&& let keyboard::Key::Character(c) = key
&& !c.is_empty()
{
return c
.chars()
.next()
.map(|ch| self.printable_input_message(ch))
.map(|msg| Action::publish(msg).and_capture());
}
None
}
};
message.map(|msg| Action::publish(msg).and_capture())
}
pub(crate) fn handle_keyboard_event(
&self,
key: &keyboard::Key,
modified_key: &keyboard::Key,
modifiers: &keyboard::Modifiers,
text: &Option<iced::advanced::graphics::core::SmolStr>,
_bounds: Rectangle,
_cursor: &mouse::Cursor,
) -> Option<Action<Message>> {
if !self.has_focus() || self.focus_locked {
return None;
}
if self.ime_preedit.is_some()
&& !(modifiers.control() || modifiers.command())
{
return None;
}
if let Some(action) =
self.handle_keyboard_shortcuts(key, modified_key, modifiers)
{
return Some(action);
}
let text_str = text.as_ref().map(|s| s.as_str());
self.handle_character_input(key, modifiers, text_str)
}
pub(crate) fn fold_header_at_point(&self, point: Point) -> Option<usize> {
if !self.folding_enabled {
return None;
}
let margin_start = self.line_number_gutter_width();
if point.x < margin_start || point.x >= self.gutter_width() {
return None;
}
let visual_line_idx = (point.y / self.line_height) as usize;
let visual_lines = self.visual_lines_cached(self.viewport_width);
let visual_line = visual_lines.get(visual_line_idx)?;
if !visual_line.is_first_segment() {
return None;
}
folding::is_line_fold_header(&self.buffer, visual_line.logical_line)
.then_some(visual_line.logical_line)
}
pub(crate) fn handle_mouse_event(
&self,
event: &mouse::Event,
bounds: Rectangle,
cursor: &mouse::Cursor,
) -> Option<Action<Message>> {
match event {
mouse::Event::ButtonPressed(mouse::Button::Left) => {
cursor.position_in(bounds).map(|position| {
if let Some(header) = self.fold_header_at_point(position) {
return Action::publish(Message::ToggleFold(header))
.and_capture();
}
#[cfg(target_os = "macos")]
let is_jump_click = self.modifiers.get().command();
#[cfg(not(target_os = "macos"))]
let is_jump_click = self.modifiers.get().control();
if is_jump_click {
return Action::publish(Message::JumpClick(position));
}
if self.modifiers.get().alt() {
let message = if self.vim_enabled {
Message::MouseClick(position)
} else {
Message::AltClick(position)
};
return Action::publish(message).and_capture();
}
let click_count = self.classify_click(position);
match click_count {
2 => Action::publish(Message::DoubleClick(position))
.and_capture(),
3 => Action::publish(Message::TripleClick(position))
.and_capture(),
_ => Action::publish(Message::MouseClick(position)),
}
})
}
mouse::Event::ButtonPressed(mouse::Button::Right) => {
cursor.position_in(bounds).map(|position| {
Action::publish(Message::ContextMenuRequested(position))
.and_capture()
})
}
mouse::Event::CursorMoved { .. } => {
cursor.position_in(bounds).map(|position| {
if self.is_dragging {
Action::publish(Message::MouseDrag(position))
.and_capture()
} else {
Action::publish(Message::MouseHover(position))
}
})
}
mouse::Event::ButtonReleased(mouse::Button::Left) => {
if cursor.is_over(bounds) {
Some(Action::publish(Message::MouseRelease).and_capture())
} else {
None
}
}
_ => None,
}
}
pub(crate) fn handle_ime_event(
&self,
event: &input_method::Event,
_bounds: Rectangle,
_cursor: &mouse::Cursor,
) -> Option<Action<Message>> {
if !self.has_focus() || self.focus_locked {
return None;
}
if self.vim_enabled && self.vim_state.mode() != VimMode::Insert {
return None;
}
let message = match event {
input_method::Event::Opened => Message::ImeOpened,
input_method::Event::Preedit(content, selection) => {
Message::ImePreedit(content.clone(), selection.clone())
}
input_method::Event::Commit(content) => {
Message::ImeCommit(content.clone())
}
input_method::Event::Closed => Message::ImeClosed,
};
Some(Action::publish(message).and_capture())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::canvas_editor::metrics::compare_floats;
use iced::event;
use std::cmp::Ordering;
fn character(ch: &str) -> keyboard::Key {
keyboard::Key::Character(ch.into())
}
fn named(key: keyboard::key::Named) -> keyboard::Key {
keyboard::Key::Named(key)
}
fn shortcut(
editor: &CodeEditor,
key: &keyboard::Key,
modifiers: keyboard::Modifiers,
) -> Option<Message> {
editor
.handle_keyboard_shortcuts(key, key, &modifiers)
.and_then(|action| action.into_inner().0)
}
fn shortcut_status(
editor: &CodeEditor,
key: &keyboard::Key,
modifiers: keyboard::Modifiers,
) -> Option<event::Status> {
editor
.handle_keyboard_shortcuts(key, key, &modifiers)
.map(|action| action.into_inner().2)
}
#[test]
fn test_vim_navigation_keyboard_route_uses_dedicated_message() {
let mut editor = CodeEditor::new("abc", "txt").with_vim_enabled(true);
assert!(matches!(
editor.printable_input_message('l'),
Message::VimKey('l')
));
let _ = editor.vim_state.parse_key('i');
assert!(matches!(
editor.printable_input_message('x'),
Message::CharacterInput('x')
));
editor.set_vim_enabled(false);
assert!(matches!(
editor.printable_input_message('x'),
Message::CharacterInput('x')
));
editor.set_vim_enabled(true);
let key = keyboard::Key::Character("r".into());
let message = editor
.handle_keyboard_shortcuts(&key, &key, &keyboard::Modifiers::CTRL)
.map(|action| action.into_inner().0);
assert!(matches!(message, Some(Some(Message::Redo))));
}
#[test]
fn test_vim_command_line_routes_enter_and_backspace() {
let mut editor = CodeEditor::new("abc", "txt").with_vim_enabled(true);
editor.request_focus();
editor.has_canvas_focus = true;
editor.focus_locked = false;
let _ = editor.vim_state.parse_key('/');
let backspace = editor
.handle_character_input(
&keyboard::Key::Named(keyboard::key::Named::Backspace),
&keyboard::Modifiers::NONE,
None,
)
.map(|action| action.into_inner().0);
assert!(matches!(backspace, Some(Some(Message::VimKey('\u{8}')))));
let enter = editor
.handle_character_input(
&keyboard::Key::Named(keyboard::key::Named::Enter),
&keyboard::Modifiers::NONE,
None,
)
.map(|action| action.into_inner().0);
assert!(matches!(enter, Some(Some(Message::VimKey('\n')))));
}
#[test]
fn test_command_g_opens_goto_line_dialog() {
let editor = CodeEditor::new("one\ntwo", "rs");
let key = keyboard::Key::Character("g".into());
let message = editor
.handle_keyboard_shortcuts(
&key,
&key,
&keyboard::Modifiers::COMMAND,
)
.map(|action| action.into_inner().0);
assert!(matches!(message, Some(Some(Message::OpenGotoLine))));
}
#[test]
fn test_is_key_char_matches_base_or_modified_key() {
let dot = keyboard::Key::Character(".".into());
let semicolon = keyboard::Key::Character(";".into());
let g = keyboard::Key::Character("g".into());
assert!(is_key_char(&dot, &dot, "."));
assert!(is_key_char(&semicolon, &dot, "."));
assert!(!is_key_char(&g, &g, "."));
}
#[test]
fn test_ctrl_dot_toggles_fold_on_azerty_layout() {
let editor = CodeEditor::new("fn a() {\n}\n", "rs");
assert!(editor.folding_enabled);
let base_key = keyboard::Key::Character(";".into());
let modified_key = keyboard::Key::Character(".".into());
let message = editor
.handle_keyboard_shortcuts(
&base_key,
&modified_key,
&keyboard::Modifiers::CTRL,
)
.map(|action| action.into_inner().0);
assert!(matches!(message, Some(Some(Message::ToggleFoldAtCursor))));
}
#[test]
fn test_ctrl_slash_toggles_comment_on_azerty_layout() {
let editor = CodeEditor::new("fn a() {}\n", "rs");
let base_key = keyboard::Key::Character(":".into());
let modified_key = keyboard::Key::Character("/".into());
let message = editor
.handle_keyboard_shortcuts(
&base_key,
&modified_key,
&keyboard::Modifiers::CTRL,
)
.map(|action| action.into_inner().0);
assert!(matches!(message, Some(Some(Message::ToggleComment))));
}
#[test]
fn test_every_single_character_shortcut_consults_the_modified_key() {
let editor = CodeEditor::new("fn a() {\n}\n", "rs");
let ctrl = keyboard::Modifiers::CTRL;
let ctrl_alt = keyboard::Modifiers::CTRL | keyboard::Modifiers::ALT;
let unmatched = character("&");
type ShortcutCase =
(&'static str, keyboard::Modifiers, fn(&Message) -> bool);
let cases: [ShortcutCase; 15] = [
("v", ctrl_alt, |m| matches!(m, Message::ToggleVimMode)),
("s", ctrl, |m| matches!(m, Message::WriteRequested)),
("c", ctrl, |m| matches!(m, Message::Copy)),
("x", ctrl, |m| matches!(m, Message::Cut)),
("a", ctrl, |m| matches!(m, Message::SelectAll)),
("v", ctrl, |m| matches!(m, Message::Paste(_))),
("d", ctrl, |m| matches!(m, Message::SelectNextOccurrence)),
("z", ctrl, |m| matches!(m, Message::Undo)),
("y", ctrl, |m| matches!(m, Message::Redo)),
("f", ctrl, |m| matches!(m, Message::OpenSearch)),
("h", ctrl, |m| matches!(m, Message::OpenSearchReplace)),
("g", ctrl, |m| matches!(m, Message::OpenGotoLine)),
("k", ctrl, |m| matches!(m, Message::FoldAll)),
("j", ctrl, |m| matches!(m, Message::UnfoldAll)),
(".", ctrl, |m| matches!(m, Message::ToggleFoldAtCursor)),
];
for (ch, modifiers, expected) in cases {
let message = editor
.handle_keyboard_shortcuts(
&unmatched,
&character(ch),
&modifiers,
)
.and_then(|action| action.into_inner().0);
assert!(
message.as_ref().is_some_and(expected),
"shortcut for {ch:?} ignored `modified_key`, got {message:?}"
);
}
}
#[test]
fn test_ctrl_alt_k_does_not_fold_all() {
let editor = CodeEditor::new("fn a() {\n}\n", "rs");
assert!(editor.folding_enabled);
let key = keyboard::Key::Character("k".into());
let modifiers = keyboard::Modifiers::CTRL | keyboard::Modifiers::ALT;
let message = editor
.handle_keyboard_shortcuts(&key, &key, &modifiers)
.map(|action| action.into_inner().0);
assert!(!matches!(message, Some(Some(Message::FoldAll))));
}
#[test]
fn test_escape_with_nothing_to_close_is_not_captured() {
let editor = CodeEditor::new("abc", "txt");
assert!(!editor.goto_line_state.is_open);
assert!(!editor.search_state.is_open);
assert!(!editor.vim_enabled);
assert!(!editor.cursors.is_multi());
let key = keyboard::Key::Named(keyboard::key::Named::Escape);
let message = editor.handle_keyboard_shortcuts(
&key,
&key,
&keyboard::Modifiers::NONE,
);
assert!(message.is_none());
}
#[test]
fn test_escape_with_multi_cursor_and_nothing_else_open_is_captured() {
let mut editor = CodeEditor::new("one\ntwo\nthree", "txt");
editor.cursors.add_cursor((1, 0));
assert!(editor.cursors.is_multi());
let key = keyboard::Key::Named(keyboard::key::Named::Escape);
let message = editor
.handle_keyboard_shortcuts(&key, &key, &keyboard::Modifiers::NONE)
.map(|action| action.into_inner().0);
assert!(matches!(message, Some(Some(Message::CloseSearch))));
}
#[test]
fn test_clipboard_shortcuts_route_copy_cut_select_all_and_paste() {
let editor = CodeEditor::new("one\ntwo", "txt");
let ctrl = keyboard::Modifiers::CTRL;
assert!(matches!(
shortcut(&editor, &character("c"), ctrl),
Some(Message::Copy)
));
assert!(matches!(
shortcut(&editor, &character("x"), ctrl),
Some(Message::Cut)
));
assert!(matches!(
shortcut(&editor, &character("a"), ctrl),
Some(Message::SelectAll)
));
assert!(matches!(
shortcut(&editor, &character("v"), ctrl),
Some(Message::Paste(text)) if text.is_empty()
));
}
#[test]
fn test_clipboard_shortcuts_accept_the_insert_key_bindings() {
let editor = CodeEditor::new("one\ntwo", "txt");
let insert = named(keyboard::key::Named::Insert);
assert!(matches!(
shortcut(&editor, &insert, keyboard::Modifiers::CTRL),
Some(Message::Copy)
));
assert!(matches!(
shortcut(&editor, &insert, keyboard::Modifiers::SHIFT),
Some(Message::Paste(_))
));
}
#[test]
fn test_paste_is_the_only_clipboard_shortcut_left_uncaptured() {
let editor = CodeEditor::new("one", "txt");
let ctrl = keyboard::Modifiers::CTRL;
for ch in ["c", "x", "a"] {
assert!(
matches!(
shortcut_status(&editor, &character(ch), ctrl),
Some(event::Status::Captured)
),
"Ctrl+{ch} should capture the event"
);
}
assert!(matches!(
shortcut_status(&editor, &character("v"), ctrl),
Some(event::Status::Ignored)
));
}
#[test]
fn test_multi_cursor_shortcuts_select_occurrence_and_add_cursors() {
let editor = CodeEditor::new("one\ntwo\nthree", "txt");
let ctrl_alt = keyboard::Modifiers::CTRL | keyboard::Modifiers::ALT;
assert!(matches!(
shortcut(&editor, &character("d"), keyboard::Modifiers::CTRL),
Some(Message::SelectNextOccurrence)
));
assert!(matches!(
shortcut(&editor, &named(keyboard::key::Named::ArrowUp), ctrl_alt),
Some(Message::AddCursorAbove)
));
assert!(matches!(
shortcut(
&editor,
&named(keyboard::key::Named::ArrowDown),
ctrl_alt
),
Some(Message::AddCursorBelow)
));
}
#[test]
fn test_shift_delete_deletes_the_selection() {
let editor = CodeEditor::new("one\ntwo", "txt");
assert!(matches!(
shortcut(
&editor,
&named(keyboard::key::Named::Delete),
keyboard::Modifiers::SHIFT
),
Some(Message::DeleteSelection)
));
}
#[test]
fn test_line_move_shortcuts_move_and_duplicate_lines() {
let editor = CodeEditor::new("one\ntwo\nthree", "txt");
let alt = keyboard::Modifiers::ALT;
let shift_alt = keyboard::Modifiers::SHIFT | keyboard::Modifiers::ALT;
let up = named(keyboard::key::Named::ArrowUp);
let down = named(keyboard::key::Named::ArrowDown);
assert!(matches!(
shortcut(&editor, &up, alt),
Some(Message::MoveLineUp)
));
assert!(matches!(
shortcut(&editor, &down, alt),
Some(Message::MoveLineDown)
));
assert!(matches!(
shortcut(&editor, &up, shift_alt),
Some(Message::DuplicateLineUp)
));
assert!(matches!(
shortcut(&editor, &down, shift_alt),
Some(Message::DuplicateLineDown)
));
}
#[test]
fn test_control_alt_arrows_add_cursors_instead_of_moving_lines() {
let editor = CodeEditor::new("one\ntwo\nthree", "txt");
let ctrl_alt = keyboard::Modifiers::CTRL | keyboard::Modifiers::ALT;
assert!(!matches!(
shortcut(&editor, &named(keyboard::key::Named::ArrowUp), ctrl_alt),
Some(Message::MoveLineUp | Message::DuplicateLineUp)
));
assert!(!matches!(
shortcut(
&editor,
&named(keyboard::key::Named::ArrowDown),
ctrl_alt
),
Some(Message::MoveLineDown | Message::DuplicateLineDown)
));
}
#[test]
fn test_navigation_shortcuts_jump_to_document_bounds() {
let editor = CodeEditor::new("one\ntwo", "txt");
let ctrl = keyboard::Modifiers::CTRL;
assert!(matches!(
shortcut(&editor, &named(keyboard::key::Named::Home), ctrl),
Some(Message::CtrlHome)
));
assert!(matches!(
shortcut(&editor, &named(keyboard::key::Named::End), ctrl),
Some(Message::CtrlEnd)
));
}
#[test]
fn test_shift_tab_navigates_focus_unless_the_search_dialog_is_open() {
let mut editor = CodeEditor::new("one", "txt");
let tab = named(keyboard::key::Named::Tab);
let shift = keyboard::Modifiers::SHIFT;
assert!(!editor.search_state.is_open);
assert!(matches!(
shortcut(&editor, &tab, shift),
Some(Message::FocusNavigationShiftTab)
));
editor.search_state.is_open = true;
assert!(matches!(
shortcut(&editor, &tab, shift),
Some(Message::SearchDialogShiftTab)
));
assert!(matches!(
shortcut(&editor, &tab, keyboard::Modifiers::NONE),
Some(Message::SearchDialogTab)
));
}
#[test]
fn test_history_shortcuts_route_undo_and_both_redo_bindings() {
let editor = CodeEditor::new("one", "txt");
let ctrl = keyboard::Modifiers::CTRL;
let ctrl_shift = keyboard::Modifiers::CTRL | keyboard::Modifiers::SHIFT;
assert!(matches!(
shortcut(&editor, &character("z"), ctrl),
Some(Message::Undo)
));
assert!(matches!(
shortcut(&editor, &character("y"), ctrl),
Some(Message::Redo)
));
assert!(matches!(
shortcut(&editor, &character("z"), ctrl_shift),
Some(Message::Redo)
));
}
#[test]
fn test_dialog_shortcuts_open_dialogs_and_drive_find_next_previous() {
let editor = CodeEditor::new("one\ntwo", "txt");
let ctrl = keyboard::Modifiers::CTRL;
let f3 = named(keyboard::key::Named::F3);
assert!(editor.search_replace_enabled);
assert!(matches!(
shortcut(&editor, &character("f"), ctrl),
Some(Message::OpenSearch)
));
assert!(matches!(
shortcut(&editor, &character("h"), ctrl),
Some(Message::OpenSearchReplace)
));
assert!(matches!(
shortcut(&editor, &f3, keyboard::Modifiers::NONE),
Some(Message::FindNext)
));
assert!(matches!(
shortcut(&editor, &f3, keyboard::Modifiers::SHIFT),
Some(Message::FindPrevious)
));
}
#[test]
fn test_search_shortcuts_are_inert_when_search_replace_is_disabled() {
let mut editor = CodeEditor::new("one\ntwo", "txt");
editor.set_search_replace_enabled(false);
let ctrl = keyboard::Modifiers::CTRL;
assert!(shortcut(&editor, &character("f"), ctrl).is_none());
assert!(shortcut(&editor, &character("h"), ctrl).is_none());
assert!(
shortcut(
&editor,
&named(keyboard::key::Named::F3),
keyboard::Modifiers::NONE
)
.is_none()
);
assert!(matches!(
shortcut(&editor, &character("g"), ctrl),
Some(Message::OpenGotoLine)
));
}
#[test]
fn test_escape_closes_the_goto_line_dialog_before_the_search_dialog() {
let mut editor = CodeEditor::new("one\ntwo", "txt");
let escape = named(keyboard::key::Named::Escape);
let none = keyboard::Modifiers::NONE;
editor.goto_line_state.is_open = true;
assert!(matches!(
shortcut(&editor, &escape, none),
Some(Message::CloseGotoLine)
));
editor.search_state.is_open = true;
assert!(matches!(
shortcut(&editor, &escape, none),
Some(Message::CloseGotoLine)
));
editor.goto_line_state.is_open = false;
assert!(matches!(
shortcut(&editor, &escape, none),
Some(Message::CloseSearch)
));
}
#[test]
fn test_escape_reaches_vim_only_when_no_dialog_is_open() {
let mut editor = CodeEditor::new("one", "txt").with_vim_enabled(true);
let escape = named(keyboard::key::Named::Escape);
let none = keyboard::Modifiers::NONE;
assert!(matches!(
shortcut(&editor, &escape, none),
Some(Message::VimKey('\u{1b}'))
));
editor.search_state.is_open = true;
assert!(matches!(
shortcut(&editor, &escape, none),
Some(Message::CloseSearch)
));
}
#[test]
fn test_folding_shortcuts_toggle_fold_and_unfold_everything() {
let editor = CodeEditor::new("fn a() {\n}\n", "rs");
let ctrl = keyboard::Modifiers::CTRL;
assert!(editor.folding_enabled);
assert!(matches!(
shortcut(&editor, &character("."), ctrl),
Some(Message::ToggleFoldAtCursor)
));
assert!(matches!(
shortcut(&editor, &character("k"), ctrl),
Some(Message::FoldAll)
));
assert!(matches!(
shortcut(&editor, &character("j"), ctrl),
Some(Message::UnfoldAll)
));
}
#[test]
fn test_fold_all_and_unfold_all_exclude_shift() {
let editor = CodeEditor::new("fn a() {\n}\n", "rs");
let ctrl_shift = keyboard::Modifiers::CTRL | keyboard::Modifiers::SHIFT;
assert!(shortcut(&editor, &character("k"), ctrl_shift).is_none());
assert!(shortcut(&editor, &character("j"), ctrl_shift).is_none());
}
#[test]
fn test_folding_shortcuts_are_inert_when_folding_is_disabled() {
let editor =
CodeEditor::new("fn a() {\n}\n", "rs").with_folding_enabled(false);
let ctrl = keyboard::Modifiers::CTRL;
assert!(!editor.folding_enabled);
assert!(shortcut(&editor, &character("."), ctrl).is_none());
assert!(shortcut(&editor, &character("k"), ctrl).is_none());
assert!(shortcut(&editor, &character("j"), ctrl).is_none());
}
const MOUSE_BOUNDS: Rectangle =
Rectangle { x: 10.0, y: 20.0, width: 400.0, height: 300.0 };
fn cursor_at(x: f32, y: f32) -> mouse::Cursor {
mouse::Cursor::Available(Point::new(
MOUSE_BOUNDS.x + x,
MOUSE_BOUNDS.y + y,
))
}
fn mouse_message(
editor: &CodeEditor,
event: &mouse::Event,
cursor: &mouse::Cursor,
) -> Option<Message> {
editor
.handle_mouse_event(event, MOUSE_BOUNDS, cursor)
.and_then(|action| action.into_inner().0)
}
fn mouse_status(
editor: &CodeEditor,
event: &mouse::Event,
cursor: &mouse::Cursor,
) -> Option<event::Status> {
editor
.handle_mouse_event(event, MOUSE_BOUNDS, cursor)
.map(|action| action.into_inner().2)
}
fn message_position(message: &Option<Message>) -> Option<Point> {
match message {
Some(
Message::MouseClick(position)
| Message::MouseHover(position)
| Message::MouseDrag(position)
| Message::AltClick(position)
| Message::JumpClick(position)
| Message::ContextMenuRequested(position),
) => Some(*position),
_ => None,
}
}
fn assert_position(message: &Option<Message>, x: f32, y: f32) {
let position = message_position(message);
assert!(
position.is_some_and(|position| {
compare_floats(position.x, x) == Ordering::Equal
&& compare_floats(position.y, y) == Ordering::Equal
}),
"expected position ({x}, {y}), got {message:?}"
);
}
const LEFT_PRESS: mouse::Event =
mouse::Event::ButtonPressed(mouse::Button::Left);
#[test]
fn test_left_click_publishes_a_bounds_relative_position() {
let editor = CodeEditor::new("one\ntwo", "txt");
let message =
mouse_message(&editor, &LEFT_PRESS, &cursor_at(30.0, 40.0));
assert!(
matches!(message, Some(Message::MouseClick(_))),
"expected a MouseClick, got {message:?}"
);
assert_position(&message, 30.0, 40.0);
let editor = CodeEditor::new("one\ntwo", "txt");
assert!(matches!(
mouse_status(&editor, &LEFT_PRESS, &cursor_at(30.0, 40.0)),
Some(event::Status::Ignored)
));
}
#[test]
fn test_mouse_events_outside_the_canvas_are_left_alone() {
let editor = CodeEditor::new("one\ntwo", "txt");
let outside = mouse::Cursor::Available(Point::new(
MOUSE_BOUNDS.x + MOUSE_BOUNDS.width + 5.0,
MOUSE_BOUNDS.y + MOUSE_BOUNDS.height + 5.0,
));
let release = mouse::Event::ButtonReleased(mouse::Button::Left);
let moved = mouse::Event::CursorMoved { position: Point::ORIGIN };
for event in [&LEFT_PRESS, &release, &moved] {
assert!(
editor
.handle_mouse_event(event, MOUSE_BOUNDS, &outside)
.is_none(),
"{event:?} outside the canvas must not be handled"
);
assert!(
editor
.handle_mouse_event(
event,
MOUSE_BOUNDS,
&mouse::Cursor::Unavailable
)
.is_none(),
"{event:?} with no cursor must not be handled"
);
}
}
#[test]
fn test_right_click_requests_the_context_menu() {
let editor = CodeEditor::new("one\ntwo", "txt");
let right_press = mouse::Event::ButtonPressed(mouse::Button::Right);
let cursor = cursor_at(15.0, 25.0);
let message = mouse_message(&editor, &right_press, &cursor);
assert!(
matches!(message, Some(Message::ContextMenuRequested(_))),
"expected a ContextMenuRequested, got {message:?}"
);
assert_position(&message, 15.0, 25.0);
assert!(matches!(
mouse_status(&editor, &right_press, &cursor),
Some(event::Status::Captured)
));
}
#[test]
fn test_cursor_moved_hovers_when_idle_and_drags_while_dragging() {
let mut editor = CodeEditor::new("one\ntwo", "txt");
let moved = mouse::Event::CursorMoved {
position: Point::new(999.0, 999.0),
};
let cursor = cursor_at(50.0, 60.0);
assert!(!editor.is_dragging);
let message = mouse_message(&editor, &moved, &cursor);
assert!(
matches!(message, Some(Message::MouseHover(_))),
"expected a MouseHover, got {message:?}"
);
assert_position(&message, 50.0, 60.0);
assert!(matches!(
mouse_status(&editor, &moved, &cursor),
Some(event::Status::Ignored)
));
editor.is_dragging = true;
let message = mouse_message(&editor, &moved, &cursor);
assert!(
matches!(message, Some(Message::MouseDrag(_))),
"expected a MouseDrag, got {message:?}"
);
assert_position(&message, 50.0, 60.0);
assert!(matches!(
mouse_status(&editor, &moved, &cursor),
Some(event::Status::Captured)
));
}
#[test]
fn test_left_release_is_handled_only_over_the_canvas() {
let editor = CodeEditor::new("one\ntwo", "txt");
let release = mouse::Event::ButtonReleased(mouse::Button::Left);
assert!(matches!(
mouse_message(&editor, &release, &cursor_at(5.0, 5.0)),
Some(Message::MouseRelease)
));
assert!(matches!(
mouse_status(&editor, &release, &cursor_at(5.0, 5.0)),
Some(event::Status::Captured)
));
}
#[test]
fn test_alt_click_adds_a_cursor_unless_vim_is_enabled() {
let mut editor = CodeEditor::new("one\ntwo", "txt");
editor.modifiers.set(keyboard::Modifiers::ALT);
let cursor = cursor_at(70.0, 10.0);
let message = mouse_message(&editor, &LEFT_PRESS, &cursor);
assert!(
matches!(message, Some(Message::AltClick(_))),
"expected an AltClick, got {message:?}"
);
assert_position(&message, 70.0, 10.0);
editor.set_vim_enabled(true);
assert!(matches!(
mouse_message(&editor, &LEFT_PRESS, &cursor),
Some(Message::MouseClick(_))
));
}
#[test]
fn test_control_click_jumps_to_definition_without_capturing() {
let editor = CodeEditor::new("one\ntwo", "txt");
editor
.modifiers
.set(keyboard::Modifiers::CTRL | keyboard::Modifiers::COMMAND);
let cursor = cursor_at(80.0, 12.0);
let message = mouse_message(&editor, &LEFT_PRESS, &cursor);
assert!(
matches!(message, Some(Message::JumpClick(_))),
"expected a JumpClick, got {message:?}"
);
assert_position(&message, 80.0, 12.0);
assert!(matches!(
mouse_status(&editor, &LEFT_PRESS, &cursor),
Some(event::Status::Ignored)
));
}
#[test]
fn test_repeated_clicks_at_one_spot_escalate_to_double_then_triple() {
let editor = CodeEditor::new("one two three", "txt");
let cursor = cursor_at(90.0, 5.0);
assert!(matches!(
mouse_message(&editor, &LEFT_PRESS, &cursor),
Some(Message::MouseClick(_))
));
assert!(matches!(
mouse_message(&editor, &LEFT_PRESS, &cursor),
Some(Message::DoubleClick(_))
));
assert!(matches!(
mouse_message(&editor, &LEFT_PRESS, &cursor),
Some(Message::TripleClick(_))
));
assert!(matches!(
mouse_message(&editor, &LEFT_PRESS, &cursor),
Some(Message::MouseClick(_))
));
}
#[test]
fn test_a_click_far_from_the_previous_one_stays_a_single_click() {
let editor = CodeEditor::new("one two three", "txt");
assert!(matches!(
mouse_message(&editor, &LEFT_PRESS, &cursor_at(20.0, 5.0)),
Some(Message::MouseClick(_))
));
assert!(matches!(
mouse_message(&editor, &LEFT_PRESS, &cursor_at(120.0, 80.0)),
Some(Message::MouseClick(_))
));
}
#[test]
fn test_clicking_the_fold_chevron_toggles_the_block() {
let editor = CodeEditor::new("fn a() {\n body\n}\n", "rs");
assert!(editor.folding_enabled);
let x = f32::midpoint(
editor.line_number_gutter_width(),
editor.gutter_width(),
);
let y = editor.line_height * 0.5;
let cursor = cursor_at(x, y);
assert!(matches!(
mouse_message(&editor, &LEFT_PRESS, &cursor),
Some(Message::ToggleFold(0))
));
editor.modifiers.set(keyboard::Modifiers::ALT);
assert!(matches!(
mouse_message(&editor, &LEFT_PRESS, &cursor),
Some(Message::ToggleFold(0))
));
}
#[test]
fn test_clicking_the_text_area_is_not_a_fold_toggle() {
let editor = CodeEditor::new("fn a() {\n body\n}\n", "rs");
let cursor =
cursor_at(editor.gutter_width() + 20.0, editor.line_height * 0.5);
assert!(matches!(
mouse_message(&editor, &LEFT_PRESS, &cursor),
Some(Message::MouseClick(_))
));
}
#[test]
fn test_unhandled_mouse_events_are_ignored() {
let editor = CodeEditor::new("one\ntwo", "txt");
let cursor = cursor_at(30.0, 30.0);
let events = [
mouse::Event::ButtonPressed(mouse::Button::Middle),
mouse::Event::ButtonReleased(mouse::Button::Right),
mouse::Event::CursorEntered,
mouse::Event::CursorLeft,
mouse::Event::WheelScrolled {
delta: mouse::ScrollDelta::Lines { x: 0.0, y: -1.0 },
},
];
for event in &events {
assert!(
editor
.handle_mouse_event(event, MOUSE_BOUNDS, &cursor)
.is_none(),
"{event:?} should not be handled here"
);
}
}
}