| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2013 Christoph Cullmann <cullmann@kde.org> |
| 3 | |
| 4 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 | */ |
| 6 | |
| 7 | #ifndef KATE_TEXTHISTORY_H |
| 8 | #define KATE_TEXTHISTORY_H |
| 9 | |
| 10 | #include <vector> |
| 11 | |
| 12 | #include <ktexteditor/movingcursor.h> |
| 13 | #include <ktexteditor/movingrange.h> |
| 14 | #include <ktexteditor/range.h> |
| 15 | |
| 16 | namespace Kate |
| 17 | { |
| 18 | class TextBuffer; |
| 19 | |
| 20 | /** |
| 21 | * Class representing the editing history of a TextBuffer |
| 22 | */ |
| 23 | class TextHistory |
| 24 | { |
| 25 | friend class TextBuffer; |
| 26 | friend class TextBlock; |
| 27 | |
| 28 | public: |
| 29 | /** |
| 30 | * Current revision, just relay the revision of the buffer |
| 31 | * @return current revision |
| 32 | */ |
| 33 | qint64 revision() const; |
| 34 | |
| 35 | /** |
| 36 | * Last revision the buffer got successful saved |
| 37 | * @return last revision buffer got saved, -1 if none |
| 38 | */ |
| 39 | qint64 lastSavedRevision() const |
| 40 | { |
| 41 | return m_lastSavedRevision; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Lock a revision, this will keep it around until released again. |
| 46 | * But all revisions will always be cleared on buffer clear() (and therefor load()) |
| 47 | * @param revision revision to lock |
| 48 | */ |
| 49 | void lockRevision(qint64 revision); |
| 50 | |
| 51 | /** |
| 52 | * Release a revision. |
| 53 | * @param revision revision to release |
| 54 | */ |
| 55 | void unlockRevision(qint64 revision); |
| 56 | |
| 57 | /** |
| 58 | * Transform a cursor from one revision to an other. |
| 59 | * @param line line number of the cursor to transform |
| 60 | * @param column column number of the cursor to transform |
| 61 | * @param insertBehavior behavior of this cursor on insert of text at its position |
| 62 | * @param fromRevision from this revision we want to transform |
| 63 | * @param toRevision to this revision we want to transform, default of -1 is current revision |
| 64 | */ |
| 65 | void transformCursor(int &line, int &column, KTextEditor::MovingCursor::InsertBehavior insertBehavior, qint64 fromRevision, qint64 toRevision = -1); |
| 66 | |
| 67 | /** |
| 68 | * Transform a range from one revision to an other. |
| 69 | * @param range range to transform |
| 70 | * @param insertBehaviors behavior of this range on insert of text at its position |
| 71 | * @param emptyBehavior behavior on becoming empty |
| 72 | * @param fromRevision from this revision we want to transform |
| 73 | * @param toRevision to this revision we want to transform, default of -1 is current revision |
| 74 | */ |
| 75 | void transformRange(KTextEditor::Range &range, |
| 76 | KTextEditor::MovingRange::InsertBehaviors insertBehaviors, |
| 77 | KTextEditor::MovingRange::EmptyBehavior emptyBehavior, |
| 78 | qint64 fromRevision, |
| 79 | qint64 toRevision = -1); |
| 80 | |
| 81 | private: |
| 82 | /** |
| 83 | * Class representing one entry in the editing history. |
| 84 | */ |
| 85 | class Entry |
| 86 | { |
| 87 | public: |
| 88 | /** |
| 89 | * transform cursor for this history entry |
| 90 | * @param line line number of the cursor to transform |
| 91 | * @param column column number of the cursor to transform |
| 92 | * @param moveOnInsert behavior of this cursor on insert of text at its position |
| 93 | */ |
| 94 | void transformCursor(int &line, int &column, bool moveOnInsert) const; |
| 95 | |
| 96 | /** |
| 97 | * reverse transform cursor for this history entry |
| 98 | * @param line line number of the cursor to transform |
| 99 | * @param column column number of the cursor to transform |
| 100 | * @param moveOnInsert behavior of this cursor on insert of text at its position |
| 101 | */ |
| 102 | void reverseTransformCursor(int &line, int &column, bool moveOnInsert) const; |
| 103 | |
| 104 | /** |
| 105 | * Types of entries, matching editing primitives of buffer and placeholder |
| 106 | */ |
| 107 | enum Type { |
| 108 | NoChange, |
| 109 | WrapLine, |
| 110 | UnwrapLine, |
| 111 | InsertText, |
| 112 | RemoveText |
| 113 | }; |
| 114 | |
| 115 | /** |
| 116 | * Default Constructor, invalidates all fields |
| 117 | */ |
| 118 | Entry() |
| 119 | { |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Reference counter, how often ist this entry referenced from the outside? |
| 124 | */ |
| 125 | unsigned int referenceCounter = 0; |
| 126 | |
| 127 | /** |
| 128 | * Type of change |
| 129 | */ |
| 130 | Type type = NoChange; |
| 131 | |
| 132 | /** |
| 133 | * line the change occurred |
| 134 | */ |
| 135 | int line = -1; |
| 136 | |
| 137 | /** |
| 138 | * column the change occurred |
| 139 | */ |
| 140 | int column = -1; |
| 141 | |
| 142 | /** |
| 143 | * length of change (length of insert or removed text) |
| 144 | */ |
| 145 | int length = -1; |
| 146 | |
| 147 | /** |
| 148 | * old line length (needed for unwrap and insert) |
| 149 | */ |
| 150 | int oldLineLength = -1; |
| 151 | }; |
| 152 | |
| 153 | /** |
| 154 | * Construct an empty text history. |
| 155 | * @param buffer buffer this text history belongs to |
| 156 | */ |
| 157 | |
| 158 | explicit TextHistory(TextBuffer &buffer); |
| 159 | |
| 160 | /** |
| 161 | * Destruct the text history |
| 162 | */ |
| 163 | |
| 164 | ~TextHistory(); |
| 165 | |
| 166 | /** |
| 167 | * Clear the edit history, this is done on clear() in buffer. |
| 168 | */ |
| 169 | |
| 170 | void clear(); |
| 171 | |
| 172 | /** |
| 173 | * Set current revision as last saved revision |
| 174 | */ |
| 175 | |
| 176 | void setLastSavedRevision(); |
| 177 | |
| 178 | /** |
| 179 | * Notify about wrap line at given cursor position. |
| 180 | * @param position line/column as cursor where to wrap |
| 181 | */ |
| 182 | |
| 183 | void wrapLine(const KTextEditor::Cursor position); |
| 184 | |
| 185 | /** |
| 186 | * Notify about unwrap given line. |
| 187 | * @param line line to unwrap |
| 188 | * @param oldLineLength text length of the line in front of this one before this unwrap |
| 189 | */ |
| 190 | |
| 191 | void unwrapLine(int line, int oldLineLength); |
| 192 | |
| 193 | /** |
| 194 | * Notify about insert text at given cursor position. |
| 195 | * @param position position where to insert text |
| 196 | * @param length text length to be inserted |
| 197 | * @param oldLineLength text length of the line before this insert |
| 198 | */ |
| 199 | |
| 200 | void insertText(const KTextEditor::Cursor position, int length, int oldLineLength); |
| 201 | |
| 202 | /** |
| 203 | * Notify about remove text at given range. |
| 204 | * @param range range of text to remove, must be on one line only. |
| 205 | * @param oldLineLength text length of the line before this remove |
| 206 | */ |
| 207 | |
| 208 | void removeText(KTextEditor::Range range, int oldLineLength); |
| 209 | |
| 210 | /** |
| 211 | * Generic function to add a entry to the history. Is used by the above functions for the different editing primitives. |
| 212 | * @param entry new entry to add |
| 213 | */ |
| 214 | |
| 215 | void addEntry(const Entry &entry); |
| 216 | |
| 217 | private: |
| 218 | /** |
| 219 | * TextBuffer this history belongs to |
| 220 | */ |
| 221 | TextBuffer &m_buffer; |
| 222 | |
| 223 | /** |
| 224 | * Last revision the buffer got saved |
| 225 | */ |
| 226 | qint64 m_lastSavedRevision; |
| 227 | |
| 228 | /** |
| 229 | * history of edits |
| 230 | * needs no sharing, small entries |
| 231 | */ |
| 232 | std::vector<Entry> m_historyEntries; |
| 233 | |
| 234 | /** |
| 235 | * offset for the first entry in m_history, to which revision it really belongs? |
| 236 | */ |
| 237 | qint64 m_firstHistoryEntryRevision; |
| 238 | }; |
| 239 | |
| 240 | } |
| 241 | |
| 242 | #endif |
| 243 | |