• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

Kate

  • kde-4.14
  • applications
  • kate
  • part
  • buffer
katetextrange.cpp
Go to the documentation of this file.
1 /* This file is part of the Kate project.
2  *
3  * Copyright (C) 2010 Christoph Cullmann <cullmann@kde.org>
4  *
5  * Based on code of the SmartCursor/Range by:
6  * Copyright (C) 2003-2005 Hamish Rodda <rodda@kde.org>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public License
19  * along with this library; see the file COPYING.LIB. If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 
24 #include "katetextrange.h"
25 #include "katetextbuffer.h"
26 
27 namespace Kate {
28 
29 TextRange::TextRange (TextBuffer &buffer, const KTextEditor::Range &range, InsertBehaviors insertBehavior, EmptyBehavior emptyBehavior)
30  : m_buffer (buffer)
31  , m_start (buffer, this, range.start(), (insertBehavior & ExpandLeft) ? Kate::TextCursor::StayOnInsert : Kate::TextCursor::MoveOnInsert)
32  , m_end (buffer, this, range.end(), (insertBehavior & ExpandRight) ? Kate::TextCursor::MoveOnInsert : Kate::TextCursor::StayOnInsert)
33  , m_view (0)
34  , m_feedback (0)
35  , m_zDepth (0.0)
36  , m_attributeOnlyForViews (false)
37  , m_invalidateIfEmpty (emptyBehavior == InvalidateIfEmpty)
38 {
39  // remember this range in buffer
40  m_buffer.m_ranges.insert (this);
41 
42  // check if range now invalid, there can happen no feedback, as m_feedback == 0
43  checkValidity ();
44 }
45 
46 TextRange::~TextRange ()
47 {
51  m_feedback = 0;
52 
53  // remove range from m_ranges
54  fixLookup (m_start.line(), m_end.line(), -1, -1);
55 
56  // remove this range from the buffer
57  m_buffer.m_ranges.remove (this);
58 
64  if (m_attribute)
65  m_buffer.notifyAboutRangeChange (m_view, m_start.line(), m_end.line(), true /* we have a attribute */);
66 }
67 
68 void TextRange::setInsertBehaviors (InsertBehaviors _insertBehaviors)
69 {
73  if (_insertBehaviors == insertBehaviors ())
74  return;
75 
79  m_start.setInsertBehavior ((_insertBehaviors & ExpandLeft) ? KTextEditor::MovingCursor::StayOnInsert : KTextEditor::MovingCursor::MoveOnInsert);
80  m_end.setInsertBehavior ((_insertBehaviors & ExpandRight) ? KTextEditor::MovingCursor::MoveOnInsert : KTextEditor::MovingCursor::StayOnInsert);
81 
85  if (m_attribute || m_feedback)
86  m_buffer.notifyAboutRangeChange (m_view, m_start.line(), m_end.line(), true /* we have a attribute */);
87 }
88 
89 KTextEditor::MovingRange::InsertBehaviors TextRange::insertBehaviors () const
90 {
91  InsertBehaviors behaviors = DoNotExpand;
92 
93  if (m_start.insertBehavior() == KTextEditor::MovingCursor::StayOnInsert)
94  behaviors |= ExpandLeft;
95 
96  if (m_end.insertBehavior() == KTextEditor::MovingCursor::MoveOnInsert)
97  behaviors |= ExpandRight;
98 
99  return behaviors;
100 }
101 
102 void TextRange::setEmptyBehavior (EmptyBehavior emptyBehavior)
103 {
107  if (m_invalidateIfEmpty == (emptyBehavior == InvalidateIfEmpty))
108  return;
109 
113  m_invalidateIfEmpty = (emptyBehavior == InvalidateIfEmpty);
114 
118  if (end() <= start())
119  setRange (KTextEditor::Range::invalid());
120 }
121 
122 void TextRange::setRange (const KTextEditor::Range &range)
123 {
124  // avoid work if nothing changed!
125  if (range == toRange())
126  return;
127 
128  // remember old line range
129  int oldStartLine = m_start.line();
130  int oldEndLine = m_end.line();
131 
132  // change start and end cursor
133  m_start.setPosition (range.start ());
134  m_end.setPosition (range.end ());
135 
136  // check if range now invalid, don't emit feedback here, will be handled below
137  // otherwise you can't delete ranges in feedback!
138  checkValidity (oldStartLine, oldEndLine, false);
139 
140  // no attribute or feedback set, be done
141  if (!m_attribute && !m_feedback)
142  return;
143 
144  // get full range
145  int startLineMin = oldStartLine;
146  if (oldStartLine == -1 || (m_start.line() != -1 && m_start.line() < oldStartLine))
147  startLineMin = m_start.line();
148 
149  int endLineMax = oldEndLine;
150  if (oldEndLine == -1 || m_end.line() > oldEndLine)
151  endLineMax = m_end.line();
152 
157  m_buffer.notifyAboutRangeChange (m_view, startLineMin, endLineMax, m_attribute);
158 
159  // perhaps need to notify stuff!
160  if (m_feedback) {
161  // do this last: may delete this range
162  if (!toRange().isValid())
163  m_feedback->rangeInvalid (this);
164  else if (toRange().isEmpty())
165  m_feedback->rangeEmpty (this);
166  }
167 }
168 
169 void TextRange::checkValidity (int oldStartLine, int oldEndLine, bool notifyAboutChange)
170 {
174  if (!m_start.isValid() || !m_end.isValid() || (m_invalidateIfEmpty && m_end <= m_start)) {
175  m_start.setPosition (-1, -1);
176  m_end.setPosition (-1, -1);
177  }
178 
182  if (!m_invalidateIfEmpty && m_end < m_start)
183  m_end.setPosition (m_start);
184 
185  // fix lookup
186  fixLookup (oldStartLine, oldEndLine, m_start.line(), m_end.line());
187 
188  // perhaps need to notify stuff!
189  if (notifyAboutChange && m_feedback) {
190  m_buffer.notifyAboutRangeChange (m_view, m_start.line(), m_end.line(), false /* attribute not interesting here */);
191 
192  // do this last: may delete this range
193  if (!toRange().isValid())
194  m_feedback->rangeInvalid (this);
195  else if (toRange().isEmpty())
196  m_feedback->rangeEmpty (this);
197  }
198 }
199 
200 void TextRange::fixLookup (int oldStartLine, int oldEndLine, int startLine, int endLine)
201 {
202  // nothing changed?
203  if (oldStartLine == startLine && oldEndLine == endLine)
204  return;
205 
206  // now, not both can be invalid
207  Q_ASSERT (oldStartLine >= 0 || startLine >= 0);
208  Q_ASSERT (oldEndLine >= 0 || endLine >= 0);
209 
210  // get full range
211  int startLineMin = oldStartLine;
212  if (oldStartLine == -1 || (startLine != -1 && startLine < oldStartLine))
213  startLineMin = startLine;
214 
215  int endLineMax = oldEndLine;
216  if (oldEndLine == -1 || endLine > oldEndLine)
217  endLineMax = endLine;
218 
219  // get start block
220  int blockIndex = m_buffer.blockForLine (startLineMin);
221  Q_ASSERT (blockIndex >= 0);
222 
223  // remove this range from m_ranges
224  for (; blockIndex < m_buffer.m_blocks.size(); ++blockIndex) {
225  // get block
226  TextBlock *block = m_buffer.m_blocks[blockIndex];
227 
228  // either insert or remove range
229  if ((endLine < block->startLine()) || (startLine >= (block->startLine() + block->lines())))
230  block->removeRange (this);
231  else
232  block->updateRange (this);
233 
234  // ok, reached end block
235  if (endLineMax < (block->startLine() + block->lines()))
236  return;
237  }
238 
239  // we should not be here, really, then endLine is wrong
240  Q_ASSERT (false);
241 }
242 
243 void TextRange::setView (KTextEditor::View *view)
244 {
248  if (view == m_view)
249  return;
250 
254  m_view = view;
255 
260  if (m_attribute || m_feedback)
261  m_buffer.notifyAboutRangeChange (0, m_start.line(), m_end.line(), m_attribute);
262 }
263 
264 void TextRange::setAttribute ( KTextEditor::Attribute::Ptr attribute )
265 {
269  m_attribute = attribute;
270 
275  m_buffer.notifyAboutRangeChange (m_view, m_start.line(), m_end.line(), m_attribute);
276 }
277 
278 void TextRange::setFeedback (KTextEditor::MovingRangeFeedback *feedback)
279 {
283  if (feedback == m_feedback)
284  return;
285 
289  m_feedback = feedback;
290 
295  m_buffer.notifyAboutRangeChange (m_view, m_start.line(), m_end.line(), m_attribute);
296 }
297 
298 void TextRange::setAttributeOnlyForViews (bool onlyForViews)
299 {
303  m_attributeOnlyForViews = onlyForViews;
304 }
305 
306 void TextRange::setZDepth (qreal zDepth)
307 {
311  if (zDepth == m_zDepth)
312  return;
313 
317  m_zDepth = zDepth;
318 
322  if (m_attribute)
323  m_buffer.notifyAboutRangeChange (m_view, m_start.line(), m_end.line(), m_attribute);
324 }
325 
326 KTextEditor::Document *Kate::TextRange::document () const
327 {
328  return m_buffer.document();
329 }
330 
331 }
Kate::TextRange::feedback
KTextEditor::MovingRangeFeedback * feedback() const
Gets the active MovingRangeFeedback for this range.
Definition: katetextrange.h:195
Kate::TextRange::start
const KTextEditor::MovingCursor & start() const
Retrieve start cursor of this range, read-only.
Definition: katetextrange.h:119
katetextbuffer.h
Kate::TextRange::~TextRange
~TextRange()
Destruct the text block.
Definition: katetextrange.cpp:46
Kate::TextRange::insertBehaviors
InsertBehaviors insertBehaviors() const
Get current insert behaviors.
Definition: katetextrange.cpp:89
Kate::TextRange::document
KTextEditor::Document * document() const
Gets the document to which this range is bound.
Definition: katetextrange.cpp:326
katetextrange.h
Kate::TextCursor::setPosition
void setPosition(const TextCursor &position)
Fast way to set the current cursor position to position.
Definition: katetextcursor.cpp:64
Kate::TextRange::setEmptyBehavior
void setEmptyBehavior(EmptyBehavior emptyBehavior)
Set if this range will invalidate itself if it becomes empty.
Definition: katetextrange.cpp:102
Kate::TextRange::TextRange
TextRange(TextBuffer &buffer, const KTextEditor::Range &range, InsertBehaviors insertBehavior, EmptyBehavior emptyBehavior=AllowEmpty)
Construct a text range.
Definition: katetextrange.cpp:29
Kate::TextRange::setView
void setView(KTextEditor::View *view)
Sets the currently active view for this range.
Definition: katetextrange.cpp:243
Kate::TextCursor::line
int line() const
Retrieve the line on which this cursor is situated.
Definition: katetextcursor.cpp:132
Kate::TextRange::toRange
const KTextEditor::Range toRange() const
Convert this clever range into a dumb one.
Definition: katetextrange.h:143
Kate::TextRange::setAttribute
void setAttribute(KTextEditor::Attribute::Ptr attribute)
Sets the currently active attribute for this range.
Definition: katetextrange.cpp:264
Kate::TextRange::zDepth
qreal zDepth() const
Gets the current Z-depth of this range.
Definition: katetextrange.h:226
Kate::TextRange::setInsertBehaviors
void setInsertBehaviors(InsertBehaviors insertBehaviors)
Set insert behaviors.
Definition: katetextrange.cpp:68
Kate::TextRange::setFeedback
void setFeedback(KTextEditor::MovingRangeFeedback *feedback)
Sets the currently active MovingRangeFeedback for this range.
Definition: katetextrange.cpp:278
Kate::TextRange::end
const KTextEditor::MovingCursor & end() const
Retrieve end cursor of this range, read-only.
Definition: katetextrange.h:131
Kate::TextCursor
Class representing a 'clever' text cursor.
Definition: katetextcursor.h:43
Kate::TextRange::setRange
void setRange(const KTextEditor::Range &range)
Set the range of this range.
Definition: katetextrange.cpp:122
Kate::TextCursor::insertBehavior
InsertBehavior insertBehavior() const
Get current insert behavior.
Definition: katetextcursor.h:84
Kate::TextRange::setZDepth
void setZDepth(qreal zDepth)
Set the current Z-depth of this range.
Definition: katetextrange.cpp:306
Kate::TextRange::TextBlock
friend class TextBlock
Definition: katetextrange.h:48
Kate::TextRange::attribute
KTextEditor::Attribute::Ptr attribute() const
Gets the active Attribute for this range.
Definition: katetextrange.h:173
Kate::TextBuffer
Class representing a text buffer.
Definition: katetextbuffer.h:48
Kate::TextCursor::setInsertBehavior
void setInsertBehavior(InsertBehavior insertBehavior)
Set insert behavior.
Definition: katetextcursor.h:78
Kate::TextRange::setAttributeOnlyForViews
void setAttributeOnlyForViews(bool onlyForViews)
Set if this range's attribute is only visible in views, not for example prints.
Definition: katetextrange.cpp:298
Kate::TextRange::view
KTextEditor::View * view() const
Gets the active view for this range.
Definition: katetextrange.h:156
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:58 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Kate

Skip menu "Kate"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Konsole

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal