| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2011 Dominik Haumann <dhaumann@kde.org> |
| 3 | SPDX-FileCopyrightText: 2009-2010 Bernhard Beschow <bbeschow@cs.tu-berlin.de> |
| 4 | SPDX-FileCopyrightText: 2002 John Firebaugh <jfirebaugh@kde.org> |
| 5 | SPDX-FileCopyrightText: 2001 Christoph Cullmann <cullmann@kde.org> |
| 6 | SPDX-FileCopyrightText: 2001 Joseph Wenninger <jowenn@kde.org> |
| 7 | SPDX-FileCopyrightText: 2023 Waqar Ahmed <waqar.17a@gmail.com> |
| 8 | |
| 9 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 10 | */ |
| 11 | |
| 12 | #ifndef kate_undo_h |
| 13 | #define kate_undo_h |
| 14 | |
| 15 | #include <QList> |
| 16 | |
| 17 | #include <QBitArray> |
| 18 | #include <kateview.h> |
| 19 | #include <ktexteditor/range.h> |
| 20 | |
| 21 | class KateUndoManager; |
| 22 | namespace KTextEditor |
| 23 | { |
| 24 | class DocumentPrivate; |
| 25 | } |
| 26 | |
| 27 | class UndoItem |
| 28 | { |
| 29 | public: |
| 30 | enum UndoType { |
| 31 | editInsertText, |
| 32 | editRemoveText, |
| 33 | editWrapLine, |
| 34 | editUnWrapLine, |
| 35 | editInsertLine, |
| 36 | editRemoveLine, |
| 37 | editMarkLineAutoWrapped, |
| 38 | editInvalid |
| 39 | }; |
| 40 | |
| 41 | enum ModificationFlag { |
| 42 | UndoLine1Modified = 1, |
| 43 | UndoLine2Modified = 2, |
| 44 | UndoLine1Saved = 4, |
| 45 | UndoLine2Saved = 8, |
| 46 | RedoLine1Modified = 16, |
| 47 | RedoLine2Modified = 32, |
| 48 | RedoLine1Saved = 64, |
| 49 | RedoLine2Saved = 128 |
| 50 | }; |
| 51 | Q_DECLARE_FLAGS(ModificationFlags, ModificationFlag) |
| 52 | |
| 53 | UndoType type = editInvalid; |
| 54 | ModificationFlags lineModFlags; |
| 55 | int line = 0; |
| 56 | int col = 0; |
| 57 | QString text; |
| 58 | bool autowrapped = false; |
| 59 | bool newLine = false; |
| 60 | bool removeLine = false; |
| 61 | int len = 0; |
| 62 | }; |
| 63 | |
| 64 | /** |
| 65 | * Class to manage a group of undo items |
| 66 | */ |
| 67 | class KateUndoGroup |
| 68 | { |
| 69 | public: |
| 70 | /** |
| 71 | * Constructor |
| 72 | * @param manager KateUndoManager this undo group will belong to |
| 73 | */ |
| 74 | explicit KateUndoGroup(const KTextEditor::Cursor cursorPosition, |
| 75 | KTextEditor::Range selection, |
| 76 | const QList<KTextEditor::ViewPrivate::PlainSecondaryCursor> &); |
| 77 | |
| 78 | KateUndoGroup(const KateUndoGroup &) = delete; |
| 79 | KateUndoGroup &operator=(const KateUndoGroup &) = delete; |
| 80 | |
| 81 | KateUndoGroup(KateUndoGroup &&o) = default; |
| 82 | KateUndoGroup &operator=(KateUndoGroup &&o) = default; |
| 83 | |
| 84 | public: |
| 85 | /** |
| 86 | * Undo the contained undo items |
| 87 | */ |
| 88 | void undo(KateUndoManager *manager, KTextEditor::ViewPrivate *view); |
| 89 | |
| 90 | /** |
| 91 | * Redo the contained undo items |
| 92 | */ |
| 93 | void redo(KateUndoManager *manager, KTextEditor::ViewPrivate *view); |
| 94 | |
| 95 | void editEnd(const KTextEditor::Cursor cursorPosition, |
| 96 | KTextEditor::Range selectionRange, |
| 97 | const QList<KTextEditor::ViewPrivate::PlainSecondaryCursor> &secondaryCursors); |
| 98 | |
| 99 | /** |
| 100 | * merge this group with an other |
| 101 | * @param newGroup group to merge into this one |
| 102 | * @param complex set if a complex undo |
| 103 | * @return success |
| 104 | */ |
| 105 | bool merge(KateUndoGroup *newGroup, bool complex); |
| 106 | |
| 107 | /** |
| 108 | * set group as as savepoint. the next group will not merge with this one |
| 109 | */ |
| 110 | void safePoint(bool safePoint = true); |
| 111 | |
| 112 | /** |
| 113 | * is this undogroup empty? |
| 114 | */ |
| 115 | bool isEmpty() const |
| 116 | { |
| 117 | return m_items.empty(); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Change all LineSaved flags to LineModified of the line modification system. |
| 122 | */ |
| 123 | void flagSavedAsModified(); |
| 124 | |
| 125 | void markUndoAsSaved(QBitArray &lines); |
| 126 | void markRedoAsSaved(QBitArray &lines); |
| 127 | |
| 128 | /** |
| 129 | * Set the undo cursor to @p cursor. |
| 130 | */ |
| 131 | inline void setUndoCursor(const KTextEditor::Cursor cursor) |
| 132 | { |
| 133 | m_undoCursor = cursor; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Set the redo cursor to @p cursor. |
| 138 | */ |
| 139 | inline void setRedoCursor(const KTextEditor::Cursor cursor) |
| 140 | { |
| 141 | m_redoCursor = cursor; |
| 142 | } |
| 143 | |
| 144 | inline KTextEditor::Cursor redoCursor() const |
| 145 | { |
| 146 | return m_redoCursor; |
| 147 | } |
| 148 | |
| 149 | private: |
| 150 | /** |
| 151 | * singleType |
| 152 | * @return the type if it's only one type, or editInvalid if it contains multiple types. |
| 153 | */ |
| 154 | UndoItem::UndoType singleType() const; |
| 155 | |
| 156 | /** |
| 157 | * are we only of this type ? |
| 158 | * @param type type to query |
| 159 | * @return we contain only the given type |
| 160 | */ |
| 161 | bool isOnlyType(UndoItem::UndoType type) const; |
| 162 | |
| 163 | public: |
| 164 | /** |
| 165 | * add an undo item |
| 166 | * @param u item to add |
| 167 | */ |
| 168 | void addItem(UndoItem u); |
| 169 | |
| 170 | private: |
| 171 | /** |
| 172 | * list of items contained |
| 173 | */ |
| 174 | std::vector<UndoItem> m_items; |
| 175 | |
| 176 | /** |
| 177 | * prohibit merging with the next group |
| 178 | */ |
| 179 | bool m_safePoint = false; |
| 180 | /* |
| 181 | * Selection Range of primary cursor |
| 182 | */ |
| 183 | KTextEditor::Range m_undoSelection; |
| 184 | /* |
| 185 | * Selection Range of primary cursor |
| 186 | */ |
| 187 | KTextEditor::Range m_redoSelection; |
| 188 | |
| 189 | /** |
| 190 | * the cursor position of the active view before the edit step |
| 191 | */ |
| 192 | KTextEditor::Cursor m_undoCursor; |
| 193 | /** |
| 194 | * the cursor positions of the active view before the edit step |
| 195 | */ |
| 196 | QList<KTextEditor::ViewPrivate::PlainSecondaryCursor> m_undoSecondaryCursors; |
| 197 | |
| 198 | /** |
| 199 | * the cursor position of the active view after the edit step |
| 200 | */ |
| 201 | KTextEditor::Cursor m_redoCursor; |
| 202 | /** |
| 203 | * the cursor positions of the active view before the edit step |
| 204 | */ |
| 205 | QList<KTextEditor::ViewPrivate::PlainSecondaryCursor> m_redoSecondaryCursors; |
| 206 | }; |
| 207 | |
| 208 | #endif |
| 209 | |