From c1e2710a0d70bf336400e514b31d28d7a2828438 Mon Sep 17 00:00:00 2001 From: Matthew Hall Date: Sat, 10 Jul 2021 22:24:03 +0100 Subject: [PATCH] VimEditingEngine: Add P command to put before cursor --- .../Libraries/LibGUI/VimEditingEngine.cpp | 21 +++++++++++++++++-- Userland/Libraries/LibGUI/VimEditingEngine.h | 3 ++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibGUI/VimEditingEngine.cpp b/Userland/Libraries/LibGUI/VimEditingEngine.cpp index 7d3bcedd4f6..6e2b4787872 100644 --- a/Userland/Libraries/LibGUI/VimEditingEngine.cpp +++ b/Userland/Libraries/LibGUI/VimEditingEngine.cpp @@ -926,6 +926,8 @@ bool VimEditingEngine::on_key_in_normal_mode(const KeyEvent& event) move_one_left(); return true; } + case (KeyCode::Key_P): + put_before(); default: break; } @@ -986,7 +988,7 @@ bool VimEditingEngine::on_key_in_normal_mode(const KeyEvent& event) m_previous_key = event.key(); return true; case (KeyCode::Key_P): - put(); + put_after(); return true; case (KeyCode::Key_PageUp): move_page_up(); @@ -1237,7 +1239,22 @@ void VimEditingEngine::yank(TextRange range) m_yank_buffer = m_editor->document().text_in_range(range); } -void VimEditingEngine::put() +void VimEditingEngine::put_before() +{ + if (m_yank_type == YankType::Line) { + move_to_logical_line_beginning(); + StringBuilder sb = StringBuilder(m_yank_buffer.length() + 1); + sb.append(m_yank_buffer); + sb.append_code_point(0x0A); + m_editor->insert_at_cursor_or_replace_selection(sb.to_string()); + m_editor->set_cursor({ m_editor->cursor().line(), m_editor->current_line().first_non_whitespace_column() }); + } else { + m_editor->insert_at_cursor_or_replace_selection(m_yank_buffer); + move_one_left(); + } +} + +void VimEditingEngine::put_after() { if (m_yank_type == YankType::Line) { move_to_logical_line_end(); diff --git a/Userland/Libraries/LibGUI/VimEditingEngine.h b/Userland/Libraries/LibGUI/VimEditingEngine.h index 44e10d531ef..19c1e388c01 100644 --- a/Userland/Libraries/LibGUI/VimEditingEngine.h +++ b/Userland/Libraries/LibGUI/VimEditingEngine.h @@ -166,7 +166,8 @@ private: String m_yank_buffer {}; void yank(YankType); void yank(TextRange); - void put(); + void put_before(); + void put_after(); TextPosition m_selection_start_position = {}; void update_selection_on_cursor_move();