From 8230bf89443781c7662e69dbd52457585ef4e7c2 Mon Sep 17 00:00:00 2001 From: Ariel Don Date: Thu, 15 Jul 2021 15:50:03 -0500 Subject: [PATCH] LibGUI: Add Ctrl-H to insert mode In Vim, Ctrl-H is effectively equivalent to backspace in insert mode, as it deletes the previous character. This commit implements method delete_previous_char() to TextEditor. delete_char() already exists in EditingEngine, but it deletes the next character rather than the previous. delete_previous_char() is then called from within VimEditingEngine. --- Userland/Libraries/LibGUI/TextEditor.cpp | 17 +++++++++++++++++ Userland/Libraries/LibGUI/TextEditor.h | 1 + Userland/Libraries/LibGUI/VimEditingEngine.cpp | 3 +++ 3 files changed, 21 insertions(+) diff --git a/Userland/Libraries/LibGUI/TextEditor.cpp b/Userland/Libraries/LibGUI/TextEditor.cpp index 7fcdec582e..a1b2cc7074 100644 --- a/Userland/Libraries/LibGUI/TextEditor.cpp +++ b/Userland/Libraries/LibGUI/TextEditor.cpp @@ -911,6 +911,23 @@ void TextEditor::delete_current_line() execute(document().text_in_range(erased_range), erased_range); } +void TextEditor::delete_previous_char() +{ + if (!is_editable()) + return; + + if (has_selection()) + return delete_selection(); + + TextRange to_erase({ m_cursor.line(), m_cursor.column() - 1 }, m_cursor); + if (m_cursor.column() == 0 && m_cursor.line() != 0) { + size_t prev_line_len = line(m_cursor.line() - 1).length(); + to_erase.set_start({ m_cursor.line() - 1, prev_line_len }); + } + + execute(document().text_in_range(to_erase), to_erase); +} + void TextEditor::do_delete() { if (!is_editable()) diff --git a/Userland/Libraries/LibGUI/TextEditor.h b/Userland/Libraries/LibGUI/TextEditor.h index 1a7830397a..6e1a1c0e9b 100644 --- a/Userland/Libraries/LibGUI/TextEditor.h +++ b/Userland/Libraries/LibGUI/TextEditor.h @@ -136,6 +136,7 @@ public: void do_delete(); void delete_current_line(); void delete_previous_word(); + void delete_previous_char(); void select_all(); virtual void undo(); virtual void redo(); diff --git a/Userland/Libraries/LibGUI/VimEditingEngine.cpp b/Userland/Libraries/LibGUI/VimEditingEngine.cpp index a3530f6d32..1057373539 100644 --- a/Userland/Libraries/LibGUI/VimEditingEngine.cpp +++ b/Userland/Libraries/LibGUI/VimEditingEngine.cpp @@ -799,6 +799,9 @@ bool VimEditingEngine::on_key_in_insert_mode(const KeyEvent& event) case KeyCode::Key_W: m_editor->delete_previous_word(); return true; + case KeyCode::Key_H: + m_editor->delete_previous_char(); + return true; default: break; }