mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 08:47:34 +00:00
LibGUI: Implement merging of TextDocument's RemoveTextCommand
When deleting text by pressing backspace, the commands will be merged into a single RemoveTextCommand.
This commit is contained in:
parent
ff6bac4854
commit
81bc861085
2 changed files with 19 additions and 0 deletions
|
@ -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()
|
void RemoveTextCommand::redo()
|
||||||
{
|
{
|
||||||
m_document.remove(m_range);
|
m_document.remove(m_range);
|
||||||
|
|
|
@ -221,6 +221,7 @@ public:
|
||||||
virtual void undo() override;
|
virtual void undo() override;
|
||||||
virtual void redo() override;
|
virtual void redo() override;
|
||||||
const TextRange& range() const { return m_range; }
|
const TextRange& range() const { return m_range; }
|
||||||
|
virtual bool merge_with(GUI::Command const&) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
String m_text;
|
String m_text;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue