Pārlūkot izejas kodu

LibGUI: Make undo work for TextDocument ReplaceAllTextCommand

The undo code here was just replacing the new text with the new text.
Now we actually save the old text and use that instead. :^)
Sam Atkins 2 gadi atpakaļ
vecāks
revīzija
a5ff6769f5

+ 4 - 3
Userland/Libraries/LibGUI/TextDocument.cpp

@@ -970,7 +970,8 @@ DeprecatedString InsertLineCommand::action_text() const
 
 
 ReplaceAllTextCommand::ReplaceAllTextCommand(GUI::TextDocument& document, DeprecatedString const& text, GUI::TextRange const& range, DeprecatedString const& action_text)
 ReplaceAllTextCommand::ReplaceAllTextCommand(GUI::TextDocument& document, DeprecatedString const& text, GUI::TextRange const& range, DeprecatedString const& action_text)
     : TextDocumentUndoCommand(document)
     : TextDocumentUndoCommand(document)
-    , m_text(text)
+    , m_original_text(document.text())
+    , m_new_text(text)
     , m_range(range)
     , m_range(range)
     , m_action_text(action_text)
     , m_action_text(action_text)
 {
 {
@@ -980,7 +981,7 @@ void ReplaceAllTextCommand::redo()
 {
 {
     m_document.remove(m_range);
     m_document.remove(m_range);
     m_document.set_all_cursors(m_range.start());
     m_document.set_all_cursors(m_range.start());
-    auto new_cursor = m_document.insert_at(m_range.start(), m_text, m_client);
+    auto new_cursor = m_document.insert_at(m_range.start(), m_new_text, m_client);
     m_range.set_end(new_cursor);
     m_range.set_end(new_cursor);
     m_document.set_all_cursors(new_cursor);
     m_document.set_all_cursors(new_cursor);
 }
 }
@@ -989,7 +990,7 @@ void ReplaceAllTextCommand::undo()
 {
 {
     m_document.remove(m_range);
     m_document.remove(m_range);
     m_document.set_all_cursors(m_range.start());
     m_document.set_all_cursors(m_range.start());
-    auto new_cursor = m_document.insert_at(m_range.start(), m_text);
+    auto new_cursor = m_document.insert_at(m_range.start(), m_original_text, m_client);
     m_range.set_end(new_cursor);
     m_range.set_end(new_cursor);
     m_document.set_all_cursors(new_cursor);
     m_document.set_all_cursors(new_cursor);
 }
 }

+ 4 - 3
Userland/Libraries/LibGUI/TextDocument.h

@@ -270,17 +270,18 @@ private:
 class ReplaceAllTextCommand final : public GUI::TextDocumentUndoCommand {
 class ReplaceAllTextCommand final : public GUI::TextDocumentUndoCommand {
 
 
 public:
 public:
-    ReplaceAllTextCommand(GUI::TextDocument& document, DeprecatedString const& text, GUI::TextRange const& range, DeprecatedString const& action_text);
+    ReplaceAllTextCommand(GUI::TextDocument& document, DeprecatedString const& new_text, GUI::TextRange const& range, DeprecatedString const& action_text);
     virtual ~ReplaceAllTextCommand() = default;
     virtual ~ReplaceAllTextCommand() = default;
     void redo() override;
     void redo() override;
     void undo() override;
     void undo() override;
     bool merge_with(GUI::Command const&) override;
     bool merge_with(GUI::Command const&) override;
     DeprecatedString action_text() const override;
     DeprecatedString action_text() const override;
-    DeprecatedString const& text() const { return m_text; }
+    DeprecatedString const& text() const { return m_new_text; }
     TextRange const& range() const { return m_range; }
     TextRange const& range() const { return m_range; }
 
 
 private:
 private:
-    DeprecatedString m_text;
+    DeprecatedString m_original_text;
+    DeprecatedString m_new_text;
     GUI::TextRange m_range;
     GUI::TextRange m_range;
     DeprecatedString m_action_text;
     DeprecatedString m_action_text;
 };
 };