File indexing completed on 2024-04-21 03:57:45

0001 /*
0002     SPDX-FileCopyrightText: 2011 Dominik Haumann <dhaumann@kde.org>
0003     SPDX-FileCopyrightText: 2009-2010 Bernhard Beschow <bbeschow@cs.tu-berlin.de>
0004     SPDX-FileCopyrightText: 2002 John Firebaugh <jfirebaugh@kde.org>
0005     SPDX-FileCopyrightText: 2001 Christoph Cullmann <cullmann@kde.org>
0006     SPDX-FileCopyrightText: 2001 Joseph Wenninger <jowenn@kde.org>
0007     SPDX-FileCopyrightText: 2023 Waqar Ahmed <waqar.17a@gmail.com>
0008 
0009     SPDX-License-Identifier: LGPL-2.0-or-later
0010 */
0011 
0012 #ifndef kate_undo_h
0013 #define kate_undo_h
0014 
0015 #include <QList>
0016 
0017 #include <QBitArray>
0018 #include <kateview.h>
0019 #include <ktexteditor/range.h>
0020 
0021 class KateUndoManager;
0022 namespace KTextEditor
0023 {
0024 class DocumentPrivate;
0025 }
0026 
0027 class UndoItem
0028 {
0029 public:
0030     enum UndoType { editInsertText, editRemoveText, editWrapLine, editUnWrapLine, editInsertLine, editRemoveLine, editMarkLineAutoWrapped, editInvalid };
0031 
0032     enum ModificationFlag {
0033         UndoLine1Modified = 1,
0034         UndoLine2Modified = 2,
0035         UndoLine1Saved = 4,
0036         UndoLine2Saved = 8,
0037         RedoLine1Modified = 16,
0038         RedoLine2Modified = 32,
0039         RedoLine1Saved = 64,
0040         RedoLine2Saved = 128
0041     };
0042     Q_DECLARE_FLAGS(ModificationFlags, ModificationFlag)
0043 
0044     UndoType type = editInvalid;
0045     ModificationFlags lineModFlags;
0046     int line = 0;
0047     int col = 0;
0048     QString text;
0049     bool autowrapped = false;
0050     bool newLine = false;
0051     bool removeLine = false;
0052     int len = 0;
0053 };
0054 
0055 /**
0056  * Class to manage a group of undo items
0057  */
0058 class KateUndoGroup
0059 {
0060 public:
0061     /**
0062      * Constructor
0063      * @param manager KateUndoManager this undo group will belong to
0064      */
0065     explicit KateUndoGroup(const KTextEditor::Cursor cursorPosition,
0066                            KTextEditor::Range selection,
0067                            const QList<KTextEditor::ViewPrivate::PlainSecondaryCursor> &);
0068 
0069     KateUndoGroup(const KateUndoGroup &) = delete;
0070     KateUndoGroup &operator=(const KateUndoGroup &) = delete;
0071 
0072     KateUndoGroup(KateUndoGroup &&o) = default;
0073     KateUndoGroup &operator=(KateUndoGroup &&o) = default;
0074 
0075 public:
0076     /**
0077      * Undo the contained undo items
0078      */
0079     void undo(KateUndoManager *manager, KTextEditor::ViewPrivate *view);
0080 
0081     /**
0082      * Redo the contained undo items
0083      */
0084     void redo(KateUndoManager *manager, KTextEditor::ViewPrivate *view);
0085 
0086     void editEnd(const KTextEditor::Cursor cursorPosition,
0087                  KTextEditor::Range selectionRange,
0088                  const QList<KTextEditor::ViewPrivate::PlainSecondaryCursor> &secondaryCursors);
0089 
0090     /**
0091      * merge this group with an other
0092      * @param newGroup group to merge into this one
0093      * @param complex set if a complex undo
0094      * @return success
0095      */
0096     bool merge(KateUndoGroup *newGroup, bool complex);
0097 
0098     /**
0099      * set group as as savepoint. the next group will not merge with this one
0100      */
0101     void safePoint(bool safePoint = true);
0102 
0103     /**
0104      * is this undogroup empty?
0105      */
0106     bool isEmpty() const
0107     {
0108         return m_items.empty();
0109     }
0110 
0111     /**
0112      * Change all LineSaved flags to LineModified of the line modification system.
0113      */
0114     void flagSavedAsModified();
0115 
0116     void markUndoAsSaved(QBitArray &lines);
0117     void markRedoAsSaved(QBitArray &lines);
0118 
0119     /**
0120      * Set the undo cursor to @p cursor.
0121      */
0122     inline void setUndoCursor(const KTextEditor::Cursor cursor)
0123     {
0124         m_undoCursor = cursor;
0125     }
0126 
0127     /**
0128      * Set the redo cursor to @p cursor.
0129      */
0130     inline void setRedoCursor(const KTextEditor::Cursor cursor)
0131     {
0132         m_redoCursor = cursor;
0133     }
0134 
0135     inline KTextEditor::Cursor redoCursor() const
0136     {
0137         return m_redoCursor;
0138     }
0139 
0140 private:
0141     /**
0142      * singleType
0143      * @return the type if it's only one type, or editInvalid if it contains multiple types.
0144      */
0145     UndoItem::UndoType singleType() const;
0146 
0147     /**
0148      * are we only of this type ?
0149      * @param type type to query
0150      * @return we contain only the given type
0151      */
0152     bool isOnlyType(UndoItem::UndoType type) const;
0153 
0154 public:
0155     /**
0156      * add an undo item
0157      * @param u item to add
0158      */
0159     void addItem(UndoItem u);
0160 
0161 private:
0162     /**
0163      * list of items contained
0164      */
0165     std::vector<UndoItem> m_items;
0166 
0167     /**
0168      * prohibit merging with the next group
0169      */
0170     bool m_safePoint = false;
0171     /*
0172      * Selection Range of primary cursor
0173      */
0174     KTextEditor::Range m_undoSelection;
0175     /*
0176      * Selection Range of primary cursor
0177      */
0178     KTextEditor::Range m_redoSelection;
0179 
0180     /**
0181      * the cursor position of the active view before the edit step
0182      */
0183     KTextEditor::Cursor m_undoCursor;
0184     /**
0185      * the cursor positions of the active view before the edit step
0186      */
0187     QList<KTextEditor::ViewPrivate::PlainSecondaryCursor> m_undoSecondaryCursors;
0188 
0189     /**
0190      * the cursor position of the active view after the edit step
0191      */
0192     KTextEditor::Cursor m_redoCursor;
0193     /**
0194      * the cursor positions of the active view before the edit step
0195      */
0196     QList<KTextEditor::ViewPrivate::PlainSecondaryCursor> m_redoSecondaryCursors;
0197 };
0198 
0199 #endif