-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathkatetextrange.cpp
More file actions
303 lines (250 loc) · 10 KB
/
katetextrange.cpp
File metadata and controls
303 lines (250 loc) · 10 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
SPDX-FileCopyrightText: 2010 Christoph Cullmann <cullmann@kde.org>
Based on code of the SmartCursor/Range by:
SPDX-FileCopyrightText: 2003-2005 Hamish Rodda <rodda@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "katetextrange.h"
#include "katedocument.h"
#include "katetextbuffer.h"
namespace Kate
{
TextRange::TextRange(TextBuffer *buffer, KTextEditor::Range range, InsertBehaviors insertBehavior, EmptyBehavior emptyBehavior)
: m_buffer(buffer)
, m_start(buffer, this, range.start(), (insertBehavior & ExpandLeft) ? Kate::TextCursor::StayOnInsert : Kate::TextCursor::MoveOnInsert)
, m_end(buffer, this, range.end(), (insertBehavior & ExpandRight) ? Kate::TextCursor::MoveOnInsert : Kate::TextCursor::StayOnInsert)
, m_view(nullptr)
, m_feedback(nullptr)
, m_zDepth(0.0)
, m_attributeOnlyForViews(false)
, m_invalidateIfEmpty(emptyBehavior == InvalidateIfEmpty)
{
// check if range now invalid, there can happen no feedback, as m_feedback == 0
// only place where KTextEditor::LineRange::invalid() for old range makes sense, as we were yet not registered!
checkValidity();
// Add the range if it is multiline
if (spansMultipleBlocks()) {
m_buffer->addMultilineRange(this);
}
}
TextRange::~TextRange()
{
if (!m_buffer) {
return;
}
// reset feedback, don't want feedback during destruction
const bool hadFeedBack = m_feedback != nullptr;
const bool hadDynamicAttr = m_attribute
&& (m_attribute->dynamicAttribute(KTextEditor::Attribute::ActivateCaretIn) || m_attribute->dynamicAttribute(KTextEditor::Attribute::ActivateMouseIn));
const bool notifyDeletion = hadFeedBack || hadDynamicAttr;
m_feedback = nullptr;
// remove range from cached multiline ranges
const auto lineRange = toLineRange();
if (lineRange.isValid() && spansMultipleBlocks()) {
m_buffer->removeMultilineRange(this);
}
// trigger update, if we have attribute
// notify right view
// here we can ignore feedback, even with feedback, we want none if the range is deleted!
if (m_attribute || notifyDeletion) {
m_buffer->notifyAboutRangeChange(m_view, lineRange, /*needsRepaint=*/m_attribute != nullptr, /*deletedRange=*/notifyDeletion ? this : nullptr);
}
}
void TextRange::setInsertBehaviors(InsertBehaviors _insertBehaviors)
{
// nothing to do?
if (_insertBehaviors == insertBehaviors() || !m_buffer) {
return;
}
// modify cursors
m_start.setInsertBehavior((_insertBehaviors & ExpandLeft) ? KTextEditor::MovingCursor::StayOnInsert : KTextEditor::MovingCursor::MoveOnInsert);
m_end.setInsertBehavior((_insertBehaviors & ExpandRight) ? KTextEditor::MovingCursor::MoveOnInsert : KTextEditor::MovingCursor::StayOnInsert);
// notify world
if (m_attribute || m_feedback) {
m_buffer->notifyAboutRangeChange(m_view, toLineRange(), true /* we have a attribute */);
}
}
KTextEditor::MovingRange::InsertBehaviors TextRange::insertBehaviors() const
{
InsertBehaviors behaviors = DoNotExpand;
if (m_start.insertBehavior() == KTextEditor::MovingCursor::StayOnInsert) {
behaviors |= ExpandLeft;
}
if (m_end.insertBehavior() == KTextEditor::MovingCursor::MoveOnInsert) {
behaviors |= ExpandRight;
}
return behaviors;
}
void TextRange::setEmptyBehavior(EmptyBehavior emptyBehavior)
{
// nothing to do?
if (m_invalidateIfEmpty == (emptyBehavior == InvalidateIfEmpty)) {
return;
}
// remember value
m_invalidateIfEmpty = (emptyBehavior == InvalidateIfEmpty);
checkValidity();
}
void TextRange::setRange(KTextEditor::Range range)
{
// avoid work if nothing changed!
if (!m_buffer || range == toRange()) {
return;
}
// remember old line range
const auto oldLineRange = toLineRange();
const bool oldSpannedMultipleBlocks = spansMultipleBlocks();
// change start and end cursor
m_start.setPosition(range.start());
m_end.setPosition(range.end());
const bool newSpansMultipleBlocks = spansMultipleBlocks();
if (oldSpannedMultipleBlocks != newSpansMultipleBlocks) {
if (oldSpannedMultipleBlocks) {
m_buffer->removeMultilineRange(this);
} else {
m_buffer->addMultilineRange(this);
}
}
// check if range now invalid, don't emit feedback here, will be handled below
// otherwise you can't delete ranges in feedback!
checkValidity(false);
// no attribute or feedback set, be done
if (!m_attribute && !m_feedback) {
return;
}
// get full range
int startLineMin = oldLineRange.start();
if (oldLineRange.start() == -1 || (m_start.lineInternal() != -1 && m_start.lineInternal() < oldLineRange.start())) {
startLineMin = m_start.line();
}
int endLineMax = oldLineRange.end();
if (oldLineRange.end() == -1 || m_end.lineInternal() > oldLineRange.end()) {
endLineMax = m_end.lineInternal();
}
// notify buffer about attribute change, it will propagate the changes
// notify right view
m_buffer->notifyAboutRangeChange(m_view, {startLineMin, endLineMax}, m_attribute);
// perhaps need to notify stuff!
if (m_feedback) {
// do this last: may delete this range
if (!toRange().isValid()) {
m_feedback->rangeInvalid(this);
} else if (toRange().isEmpty()) {
m_feedback->rangeEmpty(this);
}
}
}
void TextRange::setRange(KTextEditor::Range range, KTextEditor::Attribute::Ptr attribute)
{
// FIXME: optimize
setRange(range);
setAttribute(attribute);
}
void TextRange::setRange(KTextEditor::Range range, KTextEditor::Attribute::Ptr attribute, qreal zDepth)
{
// FIXME: optimize
setRange(range);
setAttribute(attribute);
setZDepth(zDepth);
}
void TextRange::checkValidity(bool notifyAboutChange)
{
// in any case: reset the flag, to avoid multiple runs
m_isCheckValidityRequired = false;
KTextEditor::Cursor end(m_end.lineInternal(), m_end.columnInternal());
KTextEditor::Cursor start(m_start.lineInternal(), m_start.columnInternal());
// check if any cursor is invalid or the range is zero size and it should be invalidated then
if (!start.isValid() || !end.isValid() || (m_invalidateIfEmpty && end <= start)) {
m_start.setPosition(-1, -1);
m_end.setPosition(-1, -1);
start = end = KTextEditor::Cursor::invalid();
}
// for ranges which are allowed to become empty, normalize them, if the end has moved to the front of the start
if (!m_invalidateIfEmpty && end < start) {
m_end.setPosition(m_start);
}
// perhaps need to notify stuff!
if (notifyAboutChange && m_feedback && m_buffer) {
m_buffer->notifyAboutRangeChange(m_view, toLineRange(), false /* attribute not interesting here */);
// do this last: may delete this range
if (!toRange().isValid()) {
m_feedback->rangeInvalid(this);
} else if (toRange().isEmpty()) {
m_feedback->rangeEmpty(this);
}
}
}
void TextRange::setView(KTextEditor::View *view)
{
// nothing changes, nop
if (view == m_view || !m_buffer) {
return;
}
// remember the new attribute
m_view = view;
// notify buffer about attribute change, it will propagate the changes
// notify all views (can be optimized later)
if (m_attribute || m_feedback) {
m_buffer->notifyAboutRangeChange(nullptr, toLineRange(), m_attribute);
}
}
void TextRange::setAttribute(KTextEditor::Attribute::Ptr attribute)
{
// nothing changes, nop, only pointer compare
if (attribute == m_attribute || !m_buffer) {
return;
}
// remember the new attribute
const bool hadDynamicAttr = m_attribute
&& (m_attribute->dynamicAttribute(KTextEditor::Attribute::ActivateCaretIn) || m_attribute->dynamicAttribute(KTextEditor::Attribute::ActivateMouseIn));
m_attribute = attribute;
const bool stillHasDynAttr = m_attribute
&& (m_attribute->dynamicAttribute(KTextEditor::Attribute::ActivateCaretIn) || m_attribute->dynamicAttribute(KTextEditor::Attribute::ActivateMouseIn));
const bool notifyDeletion = hadDynamicAttr && !stillHasDynAttr;
// notify buffer about attribute change, it will propagate the changes
// notify right view
// if dyn attr was cleared, then notify about deleted range now because if this range is deleted
// its deletion will not be notified because it will have no dyn attr.
m_buffer->notifyAboutRangeChange(m_view,
toLineRange(),
true /* even for nullptr attribute, we had before one => repaint */,
notifyDeletion ? this : nullptr);
}
void TextRange::setFeedback(KTextEditor::MovingRangeFeedback *feedback)
{
// nothing changes, nop
if (feedback == m_feedback || !m_buffer) {
return;
}
// remember the new feedback object
const bool feedbackCleared = m_feedback && !feedback;
m_feedback = feedback;
// notify buffer about feedback change, it will propagate the changes
// notify right view
// if feedback was set to null, then notify about deleted range now because if this range is deleted
// its deletion will not be notified because it has no feedback.
m_buffer->notifyAboutRangeChange(m_view, toLineRange(), m_attribute, feedbackCleared ? this : nullptr);
}
void TextRange::setAttributeOnlyForViews(bool onlyForViews)
{
// just set the value, no need to trigger updates, printing is not interruptible
m_attributeOnlyForViews = onlyForViews;
}
void TextRange::setZDepth(qreal zDepth)
{
// nothing changes, nop
if (zDepth == m_zDepth || !m_buffer) {
return;
}
// remember the new attribute
m_zDepth = zDepth;
// notify buffer about attribute change, it will propagate the changes
if (m_attribute) {
m_buffer->notifyAboutRangeChange(m_view, toLineRange(), m_attribute);
}
}
KTextEditor::Document *Kate::TextRange::document() const
{
return m_buffer ? m_buffer->document() : nullptr;
}
}