diff options
author | Andreas Kling <kling@serenityos.org> | 2021-05-08 16:52:08 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-05-08 22:17:51 +0200 |
commit | ff6bac4854f7b699d0faf9ccabf47afa053459b8 (patch) | |
tree | 803d3baee7b449aa0fa93b803c63fe427e27facf | |
parent | ff912946ae3e16c0d338199d77a42923a62a3f71 (diff) | |
download | serenity-ff6bac4854f7b699d0faf9ccabf47afa053459b8.zip |
LibGUI: Implement merging of TextDocument's InsertTextCommand
When performing multiple text insertions in a row, they will now
be merged into a single InsertTextCommand.
-rw-r--r-- | Userland/Libraries/LibGUI/TextDocument.cpp | 15 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/TextDocument.h | 1 |
2 files changed, 16 insertions, 0 deletions
diff --git a/Userland/Libraries/LibGUI/TextDocument.cpp b/Userland/Libraries/LibGUI/TextDocument.cpp index 0beef4170b..78a23a30b0 100644 --- a/Userland/Libraries/LibGUI/TextDocument.cpp +++ b/Userland/Libraries/LibGUI/TextDocument.cpp @@ -719,6 +719,21 @@ InsertTextCommand::InsertTextCommand(TextDocument& document, const String& text, { } +bool InsertTextCommand::merge_with(GUI::Command const& other) +{ + if (!is<InsertTextCommand>(other)) + return false; + auto& typed_other = static_cast<InsertTextCommand const&>(other); + if (m_range.end() != typed_other.m_range.start()) + return false; + StringBuilder builder(m_text.length() + typed_other.m_text.length()); + builder.append(m_text); + builder.append(typed_other.m_text); + m_text = builder.to_string(); + m_range.set_end(typed_other.m_range.end()); + return true; +} + void InsertTextCommand::perform_formatting(const TextDocument::Client& client) { const size_t tab_width = client.soft_tab_width(); diff --git a/Userland/Libraries/LibGUI/TextDocument.h b/Userland/Libraries/LibGUI/TextDocument.h index aac61254fb..f3a4c9a0cf 100644 --- a/Userland/Libraries/LibGUI/TextDocument.h +++ b/Userland/Libraries/LibGUI/TextDocument.h @@ -206,6 +206,7 @@ public: virtual void perform_formatting(const TextDocument::Client&) override; virtual void undo() override; virtual void redo() override; + virtual bool merge_with(GUI::Command const&) override; const String& text() const { return m_text; } const TextRange& range() const { return m_range; } |