1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 20:07:35 +00:00

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 <jorropo.pgm@gmail.com>
This commit is contained in:
Lucas CHOLLET 2022-06-07 22:32:35 +02:00 committed by Linus Groh
parent cf693136e2
commit 7a8104e79b
3 changed files with 145 additions and 55 deletions

View file

@ -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<MoveLineUpOrDownCommand>, 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;
};
}