KTextEditor

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

KDE's Doxygen guidelines are available online.