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

source code of ktexteditor/src/undo/kateundo.h