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

LibGUI: Don't merge TextDocumentUndoCommand if too much time has elapsed

This commit is contained in:
ForLoveOfCats 2022-04-24 20:35:59 -04:00 committed by Andreas Kling
parent ee81c1b07a
commit 2adc5efe2b
2 changed files with 15 additions and 3 deletions

View file

@ -762,7 +762,7 @@ String InsertTextCommand::action_text() const
bool InsertTextCommand::merge_with(GUI::Command const& other) bool InsertTextCommand::merge_with(GUI::Command const& other)
{ {
if (!is<InsertTextCommand>(other)) if (!is<InsertTextCommand>(other) || commit_time_expired())
return false; return false;
auto const& typed_other = static_cast<InsertTextCommand const&>(other); auto const& typed_other = static_cast<InsertTextCommand const&>(other);
@ -780,6 +780,7 @@ bool InsertTextCommand::merge_with(GUI::Command const& other)
m_text = builder.to_string(); m_text = builder.to_string();
m_range.set_end(typed_other.m_range.end()); m_range.set_end(typed_other.m_range.end());
m_timestamp = Time::now_monotonic();
return true; return true;
} }
@ -862,19 +863,24 @@ String RemoveTextCommand::action_text() const
bool RemoveTextCommand::merge_with(GUI::Command const& other) bool RemoveTextCommand::merge_with(GUI::Command const& other)
{ {
if (!is<RemoveTextCommand>(other)) if (!is<RemoveTextCommand>(other) || commit_time_expired())
return false; return false;
auto& typed_other = static_cast<RemoveTextCommand const&>(other);
auto const& typed_other = static_cast<RemoveTextCommand const&>(other);
if (m_range.start() != typed_other.m_range.end()) if (m_range.start() != typed_other.m_range.end())
return false; return false;
if (m_range.start().line() != m_range.end().line()) if (m_range.start().line() != m_range.end().line())
return false; return false;
// Merge backspaces // Merge backspaces
StringBuilder builder(m_text.length() + typed_other.m_text.length()); StringBuilder builder(m_text.length() + typed_other.m_text.length());
builder.append(typed_other.m_text); builder.append(typed_other.m_text);
builder.append(m_text); builder.append(m_text);
m_text = builder.to_string(); m_text = builder.to_string();
m_range.set_start(typed_other.m_range.start()); m_range.set_start(typed_other.m_range.start());
m_timestamp = Time::now_monotonic();
return true; return true;
} }

View file

@ -12,6 +12,7 @@
#include <AK/NonnullRefPtr.h> #include <AK/NonnullRefPtr.h>
#include <AK/Optional.h> #include <AK/Optional.h>
#include <AK/RefCounted.h> #include <AK/RefCounted.h>
#include <AK/Time.h>
#include <AK/Utf32View.h> #include <AK/Utf32View.h>
#include <LibCore/Forward.h> #include <LibCore/Forward.h>
#include <LibGUI/Command.h> #include <LibGUI/Command.h>
@ -25,6 +26,8 @@
namespace GUI { namespace GUI {
constexpr Time COMMAND_COMMIT_TIME = Time::from_milliseconds(400);
struct TextDocumentSpan { struct TextDocumentSpan {
TextRange range; TextRange range;
Gfx::TextAttributes attributes; Gfx::TextAttributes attributes;
@ -202,6 +205,9 @@ public:
} }
protected: protected:
bool commit_time_expired() const { return Time::now_monotonic() - m_timestamp >= COMMAND_COMMIT_TIME; }
Time m_timestamp = Time::now_monotonic();
TextDocument& m_document; TextDocument& m_document;
TextDocument::Client const* m_client { nullptr }; TextDocument::Client const* m_client { nullptr };
}; };