diff options
author | Andreas Kling <kling@serenityos.org> | 2021-05-08 16:53:08 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-05-08 22:17:51 +0200 |
commit | 81bc861085895216f48d43c42fc7c7dcd05ee1d1 (patch) | |
tree | 1cc9767e4c7e1269660b0691bf6936da46930ec2 /Userland/Libraries | |
parent | ff6bac4854f7b699d0faf9ccabf47afa053459b8 (diff) | |
download | serenity-81bc861085895216f48d43c42fc7c7dcd05ee1d1.zip |
LibGUI: Implement merging of TextDocument's RemoveTextCommand
When deleting text by pressing backspace, the commands will be merged
into a single RemoveTextCommand.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibGUI/TextDocument.cpp | 18 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/TextDocument.h | 1 |
2 files changed, 19 insertions, 0 deletions
diff --git a/Userland/Libraries/LibGUI/TextDocument.cpp b/Userland/Libraries/LibGUI/TextDocument.cpp index 78a23a30b0..bca690e3df 100644 --- a/Userland/Libraries/LibGUI/TextDocument.cpp +++ b/Userland/Libraries/LibGUI/TextDocument.cpp @@ -802,6 +802,24 @@ RemoveTextCommand::RemoveTextCommand(TextDocument& document, const String& text, { } +bool RemoveTextCommand::merge_with(GUI::Command const& other) +{ + if (!is<RemoveTextCommand>(other)) + return false; + auto& typed_other = static_cast<RemoveTextCommand const&>(other); + if (m_range.start() == typed_other.m_range.end()) { + // Merge backspaces + StringBuilder builder(m_text.length() + typed_other.m_text.length()); + builder.append(typed_other.m_text); + builder.append(m_text); + m_text = builder.to_string(); + m_range.set_start(typed_other.m_range.start()); + return true; + } + // FIXME: Merge forward-deletes + return false; +} + void RemoveTextCommand::redo() { m_document.remove(m_range); diff --git a/Userland/Libraries/LibGUI/TextDocument.h b/Userland/Libraries/LibGUI/TextDocument.h index f3a4c9a0cf..1d0319f077 100644 --- a/Userland/Libraries/LibGUI/TextDocument.h +++ b/Userland/Libraries/LibGUI/TextDocument.h @@ -221,6 +221,7 @@ public: virtual void undo() override; virtual void redo() override; const TextRange& range() const { return m_range; } + virtual bool merge_with(GUI::Command const&) override; private: String m_text; |