From 7a8104e79b9c8b4bb63d9ad8386ed73125ca07a5 Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Tue, 7 Jun 2022 22:32:35 +0200 Subject: LibGUI: Add MoveLineUpOrDownCommand This allows lines moved by Ctrl+Shift+[Up, Down] to be registered as a command, i.e. cancellable by Ctrl+Z. This patch also introduces the usage of TextDocument::[take, insert]_line. Those functions forward changes to the visual lines and then avoid some data mismatch. Co-authored-by: Jorropo --- Userland/Libraries/LibGUI/EditingEngine.h | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) (limited to 'Userland/Libraries/LibGUI/EditingEngine.h') diff --git a/Userland/Libraries/LibGUI/EditingEngine.h b/Userland/Libraries/LibGUI/EditingEngine.h index 13c71a3dca..d6dacb1205 100644 --- a/Userland/Libraries/LibGUI/EditingEngine.h +++ b/Userland/Libraries/LibGUI/EditingEngine.h @@ -22,6 +22,8 @@ enum EngineType { Vim, }; +class MoveLineUpOrDownCommand; + class EditingEngine { AK_MAKE_NONCOPYABLE(EditingEngine); AK_MAKE_NONMOVABLE(EditingEngine); @@ -45,6 +47,8 @@ public: bool is_regular() const { return engine_type() == EngineType::Regular; } bool is_vim() const { return engine_type() == EngineType::Vim; } + void get_selection_line_boundaries(Badge, size_t& first_line, size_t& last_line); + protected: EditingEngine() = default; @@ -80,10 +84,27 @@ protected: void delete_char(); virtual EngineType engine_type() const = 0; +}; + +class MoveLineUpOrDownCommand : public TextDocumentUndoCommand { +public: + MoveLineUpOrDownCommand(TextDocument&, KeyEvent event, EditingEngine&); + virtual void undo() override; + virtual void redo() override; + bool merge_with(GUI::Command const&) override; + String action_text() const override; + + static bool valid_operation(EditingEngine& engine, VerticalDirection direction); private: - void move_selected_lines_up(); - void move_selected_lines_down(); + void move_lines(VerticalDirection); + TextRange retrieve_selection(VerticalDirection); + + KeyEvent m_event; + VerticalDirection m_direction; + EditingEngine& m_engine; + TextRange m_selection; + TextPosition m_cursor; }; } -- cgit v1.2.3