1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 19:18:12 +00:00

LibGUI: Remove UndoStack's automatic command combo'ing

UndoStack will now merge adjacent commands *if they want to be merged*
instead of bundling everything you push onto it until you tell it
to "finalize the combo."

This uses less memory and gives applications full control over how
their undo stacks end up. :^)
This commit is contained in:
Andreas Kling 2021-05-08 21:08:41 +02:00
parent 81bc861085
commit 161568103e
5 changed files with 37 additions and 77 deletions

View file

@ -726,6 +726,8 @@ bool InsertTextCommand::merge_with(GUI::Command const& other)
auto& typed_other = static_cast<InsertTextCommand const&>(other);
if (m_range.end() != typed_other.m_range.start())
return false;
if (m_range.start().line() != m_range.end().line())
return false;
StringBuilder builder(m_text.length() + typed_other.m_text.length());
builder.append(m_text);
builder.append(typed_other.m_text);
@ -807,16 +809,18 @@ 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
if (m_range.start() != typed_other.m_range.end())
return false;
if (m_range.start().line() != m_range.end().line())
return false;
// 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;
return false;
}
@ -834,7 +838,7 @@ void RemoveTextCommand::undo()
void TextDocument::update_undo()
{
m_undo_stack.finalize_current_combo();
// FIXME: Maybe seal the last command somehow?
}
TextPosition TextDocument::insert_at(const TextPosition& position, const StringView& text, const Client* client)