File: documentchangetracker.h

package info (click to toggle)
kdevelop 4%3A5.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 57,892 kB
  • sloc: cpp: 278,773; javascript: 3,558; python: 3,385; sh: 1,317; ansic: 689; xml: 273; php: 95; makefile: 40; lisp: 13; sed: 12
file content (258 lines) | stat: -rw-r--r-- 10,424 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
 * This file is part of KDevelop
 *
 * Copyright 2010 David Nolden <david.nolden.kdevelop@art-master.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Library General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#ifndef KDEVPLATFORM_DOCUMENTCHANGETRACKER_H
#define KDEVPLATFORM_DOCUMENTCHANGETRACKER_H

#include <language/languageexport.h>
#include <QExplicitlySharedDataPointer>
#include <QPointer>
#include <QPair>
#include <language/editor/rangeinrevision.h>
#include <serialization/indexedstring.h>

#include <KTextEditor/MovingRange>

namespace KTextEditor {
class Document;
class MovingRange;
class MovingInterface;
}

namespace KDevelop {
class DocumentChangeTracker;
/**
 * These objects belongs to the foreground, and thus can only be accessed from background threads if the foreground lock is held.
 * */

class RevisionLockerAndClearerPrivate;

/**
 * Helper class that locks a revision, and clears it on its destruction within the foreground thread.
 * Just delete it using deleteLater().
 * */
class KDEVPLATFORMLANGUAGE_EXPORT RevisionLockerAndClearer
    : public QSharedData
{
public:
    using Ptr = QExplicitlySharedDataPointer<RevisionLockerAndClearer>;

    ~RevisionLockerAndClearer();

    /**
     * Returns the revision number
     * */
    qint64 revision() const;

    /**
     * Whether the revision is still being held. It may have been lost due to document-reloads,
     * in which case the revision must not be used.
     * */
    bool valid() const;
    /**
     * Transform a range from this document revision to the given @p to.
     * */
    RangeInRevision transformToRevision(const RangeInRevision& range, const Ptr& to) const;

    /**
     * Transform a cursor from this document revision to the given @p to.
     * If a zero target revision is given, the transformation is done to the current document revision.
     * */
    CursorInRevision transformToRevision(const CursorInRevision& cursor, const Ptr& to,
                                         KTextEditor::MovingCursor::InsertBehavior behavior =
                                             KTextEditor::MovingCursor::StayOnInsert) const;

    /**
     * Transforms the given range from this revision into the current revision.
     */
    KTextEditor::Range transformToCurrentRevision(const RangeInRevision& range) const;

    /**
     * Transforms the given cursor from this revision into the current revision.
     */
    KTextEditor::Cursor transformToCurrentRevision(const CursorInRevision& cursor,
                                                   KTextEditor::MovingCursor::InsertBehavior behavior =
                                                       KTextEditor::MovingCursor::StayOnInsert) const;

    /**
     * Transform ranges from the given document revision @p from to the this one.
     * If a zero @p from revision is given, the transformation is done from the current document revision.
     * */
    RangeInRevision transformFromRevision(const RangeInRevision& range, const Ptr& from = Ptr()) const;
    /**
     * Transform ranges from the given document revision @p from to the this one.
     * If a zero @p from revision is given, the transformation is done from the current document revision.
     * */
    CursorInRevision transformFromRevision(const CursorInRevision& cursor,
                                           const Ptr& from = Ptr(),
                                           KTextEditor::MovingCursor::InsertBehavior behavior =
                                               KTextEditor::MovingCursor::StayOnInsert) const;

    /**
     * Transforms the given range from the current revision into this revision.
     */
    RangeInRevision transformFromCurrentRevision(const KTextEditor::Range& range) const;

    /**
     * Transforms the given cursor from the current revision into this revision.
     */
    CursorInRevision transformFromCurrentRevision(const KTextEditor::Cursor& cursor,
                                                  KTextEditor::MovingCursor::InsertBehavior behavior =
                                                      KTextEditor::MovingCursor::StayOnInsert) const;

private:
    friend class DocumentChangeTracker;

    RevisionLockerAndClearerPrivate* m_p;
};

using RevisionReference = RevisionLockerAndClearer::Ptr;

class KDEVPLATFORMLANGUAGE_EXPORT DocumentChangeTracker
    : public QObject
{
    Q_OBJECT

public:
    explicit DocumentChangeTracker(KTextEditor::Document* document);
    ~DocumentChangeTracker() override;

    /**
     * Completions of the users current edits that are supposed to complete
     * not-yet-finished statements, like for example for-loops for parsing.
     * */
    virtual QList<QPair<KTextEditor::Range, QString>> completions() const;

    /**
     * Resets the tracking to the current revision.
     * */
    virtual void reset();

    /**
     * Returns the document revision at which reset() was called last.
     *
     * The revision is being locked by the tracker in MovingInterface,
     * it will be unlocked as soon as reset() is called, so if you want to use
     * the revision afterwards, you have to lock it before calling reset.
     *
     * zero is returned if the revisions were invalidated after the last call.
     * */
    RevisionReference revisionAtLastReset() const;

    /**
     * Returns the current revision (which is not locked by the tracker)
     * */
    RevisionReference currentRevision();

    /**
     * Whether the changes that happened since the last reset are significant enough to require an update
     * */
    virtual bool needUpdate() const;

    /**
     * Returns the tracked document
     **/
    KTextEditor::Document* document() const;

    KTextEditor::MovingInterface* documentMovingInterface() const;

    /**
     * Returns the revision object which locks the revision representing the on-disk state.
     * Returns a zero object if the file is not on disk.
     * */
    RevisionReference diskRevision() const;

    /**
     * Returns whether the given revision is being current held, so that it can be used
     * for transformations in MovingInterface
     * */
    bool holdingRevision(qint64 revision) const;

    /**
     * Use this function to acquire a revision. As long as the returned object is stored somewhere,
     * the revision can be used for transformations in MovingInterface, and especially for
     * DocumentChangeTracker::transformBetweenRevisions.
     *
     * Returns a zero revision object if the revision could not be acquired (it wasn't held).
     * */
    RevisionReference acquireRevision(qint64 revision);

    /**
     * Safely maps the given range between the two given revisions.
     * The mapping is only done if both the from- and to- revision are held,
     * else the original range is returned.
     *
     * @warning: Make sure that you actually hold the referenced revisions, else no transformation will be done.
     * @note It is much less error-prone to use RevisionReference->transformToRevision() and RevisionReference->transformFromRevision() directly.
     * */
    RangeInRevision transformBetweenRevisions(RangeInRevision range, qint64 fromRevision, qint64 toRevision) const;
    CursorInRevision transformBetweenRevisions(CursorInRevision cursor, qint64 fromRevision, qint64 toRevision,
                                               KTextEditor::MovingCursor::InsertBehavior behavior =
                                                   KTextEditor::MovingCursor::StayOnInsert) const;

    KTextEditor::Range transformToCurrentRevision(RangeInRevision range, qint64 fromRevision) const;
    KTextEditor::Cursor transformToCurrentRevision(CursorInRevision cursor, qint64 fromRevision,
                                                   KTextEditor::MovingCursor::InsertBehavior behavior =
                                                       KTextEditor::MovingCursor::StayOnInsert) const;

    /// Transform the range from the current revision into the given one
    RangeInRevision transformToRevision(KTextEditor::Range range, qint64 toRevision) const;
    /// Transform the cursor from the current revision into the given one
    CursorInRevision transformToRevision(KTextEditor::Cursor cursor, qint64 toRevision,
                                         KTextEditor::MovingCursor::InsertBehavior behavior =
                                             KTextEditor::MovingCursor::StayOnInsert) const;

protected:
    RevisionReference m_revisionAtLastReset;
    bool m_needUpdate;
    QString m_currentCleanedInsertion;
    KTextEditor::Cursor m_lastInsertionPosition;
    KTextEditor::MovingRange* m_changedRange;

    KTextEditor::Document* m_document;
    KTextEditor::MovingInterface* m_moving;
    KDevelop::IndexedString m_url;

    void updateChangedRange(int delay);
    int recommendedDelay(KTextEditor::Document* doc, const KTextEditor::Range& range, const QString& text,
                         bool removal);

public Q_SLOTS:
    void textInserted(KTextEditor::Document* document, const KTextEditor::Cursor& position, const QString& inserted);
    void textRemoved(KTextEditor::Document* document, const KTextEditor::Range& range, const QString& oldText);
    void lineWrapped(KTextEditor::Document* document, const KTextEditor::Cursor& position);
    void lineUnwrapped(KTextEditor::Document* document, int line);

    void documentDestroyed(QObject*);
    void aboutToInvalidateMovingInterfaceContent (KTextEditor::Document* document);
    void documentSavedOrUploaded(KTextEditor::Document*, bool);

private:
    bool checkMergeTokens(const KTextEditor::Range& range);

    friend class RevisionLockerAndClearerPrivate;
    void lockRevision(qint64 revision);
    void unlockRevision(qint64 revision);

    QMap<qint64, int> m_revisionLocks;
};
}
#endif