KTextEditor

include/ktexteditor/range.h
1 /*
2  SPDX-FileCopyrightText: 2003-2005 Hamish Rodda <[email protected]>
3  SPDX-FileCopyrightText: 2001-2005 Christoph Cullmann <[email protected]>
4  SPDX-FileCopyrightText: 2002 Christian Couder <[email protected]>
5  SPDX-FileCopyrightText: 2001 Joseph Wenninger <[email protected]>
6  SPDX-FileCopyrightText: 1999 Jochen Wilhelmy <[email protected]>
7 
8  SPDX-License-Identifier: LGPL-2.0-or-later
9 */
10 
11 #ifndef KTEXTEDITOR_RANGE_H
12 #define KTEXTEDITOR_RANGE_H
13 
14 #include <ktexteditor/cursor.h>
15 #include <ktexteditor/linerange.h>
16 #include <ktexteditor_export.h>
17 
18 #include <QDebug>
19 #include <QtGlobal>
20 
21 namespace KTextEditor
22 {
44 class KTEXTEDITOR_EXPORT Range
45 {
46 public:
51  Q_DECL_CONSTEXPR Range() Q_DECL_NOEXCEPT
52  {
53  }
54 
62  Q_DECL_CONSTEXPR Range(const Cursor &start, const Cursor &end) Q_DECL_NOEXCEPT : m_start(qMin(start, end)), m_end(qMax(start, end))
63  {
64  }
65 
73  Q_DECL_CONSTEXPR Range(const Cursor &start, int width) Q_DECL_NOEXCEPT : m_start(qMin(start, Cursor(start.line(), start.column() + width))),
74  m_end(qMax(start, Cursor(start.line(), start.column() + width)))
75  {
76  }
77 
85  Q_DECL_CONSTEXPR Range(const Cursor &start, int endLine, int endColumn) Q_DECL_NOEXCEPT : m_start(qMin(start, Cursor(endLine, endColumn))),
86  m_end(qMax(start, Cursor(endLine, endColumn)))
87  {
88  }
89 
98  Q_DECL_CONSTEXPR Range(int startLine, int startColumn, int endLine, int endColumn) Q_DECL_NOEXCEPT
99  : m_start(qMin(Cursor(startLine, startColumn), Cursor(endLine, endColumn))),
100  m_end(qMax(Cursor(startLine, startColumn), Cursor(endLine, endColumn)))
101  {
102  }
103 
107  Q_DECL_CONSTEXPR inline bool isValid() const Q_DECL_NOEXCEPT
108  {
109  return start().isValid() && end().isValid();
110  }
111 
115  Q_DECL_CONSTEXPR static Range invalid() Q_DECL_NOEXCEPT
116  {
118  }
119 
126  {
127  return QLatin1Char('[') + m_start.toString() + QLatin1String(", ") + m_end.toString() + QLatin1Char(']');
128  }
129 
136  // TODO KF6: Remove this overload in favor of fromString(QStringView).
137  static Range fromString(const QString &str) Q_DECL_NOEXCEPT
138  {
139  return fromString(str.leftRef(-1));
140  }
141 
148  // TODO KF6: Remove this overload in favor of fromString(QStringView).
149  static Range fromString(const QStringRef &str) Q_DECL_NOEXCEPT;
150 
157  static Range fromString(QStringView str) Q_DECL_NOEXCEPT;
158 
171  Q_DECL_CONSTEXPR inline Cursor start() const Q_DECL_NOEXCEPT
172  {
173  return m_start;
174  }
175 
181  Q_DECL_CONSTEXPR inline Cursor end() const Q_DECL_NOEXCEPT
182  {
183  return m_end;
184  }
185 
191  Q_DECL_CONSTEXPR inline LineRange toLineRange() const Q_DECL_NOEXCEPT
192  {
193  return {start().line(), end().line()};
194  }
195 
201  void setBothLines(int line) Q_DECL_NOEXCEPT;
202 
208  void setBothColumns(int column) Q_DECL_NOEXCEPT;
209 
215  void setRange(const Range &range) Q_DECL_NOEXCEPT;
216 
227  void setRange(const Cursor &start, const Cursor &end) Q_DECL_NOEXCEPT;
228 
236  inline void setStart(const Cursor &start) Q_DECL_NOEXCEPT
237  {
238  if (start > end()) {
239  setRange(start, start);
240  } else {
241  setRange(start, end());
242  }
243  }
244 
252  inline void setEnd(const Cursor &end) Q_DECL_NOEXCEPT
253  {
254  if (end < start()) {
255  setRange(end, end);
256  } else {
257  setRange(start(), end);
258  }
259  }
260 
268  bool expandToRange(const Range &range) Q_DECL_NOEXCEPT;
269 
277  bool confineToRange(const Range &range) Q_DECL_NOEXCEPT;
278 
286  Q_DECL_CONSTEXPR inline bool onSingleLine() const Q_DECL_NOEXCEPT
287  {
288  return start().line() == end().line();
289  }
290 
297  Q_DECL_CONSTEXPR inline int numberOfLines() const Q_DECL_NOEXCEPT
298  {
299  return end().line() - start().line();
300  }
301 
308  Q_DECL_CONSTEXPR inline int columnWidth() const Q_DECL_NOEXCEPT
309  {
310  return end().column() - start().column();
311  }
312 
319  Q_DECL_CONSTEXPR inline bool isEmpty() const Q_DECL_NOEXCEPT
320  {
321  return start() == end();
322  }
323 
324  // BEGIN comparison functions
341  Q_DECL_CONSTEXPR inline bool contains(const Range &range) const Q_DECL_NOEXCEPT
342  {
343  return range.start() >= start() && range.end() <= end();
344  }
345 
353  Q_DECL_CONSTEXPR inline bool contains(const Cursor &cursor) const Q_DECL_NOEXCEPT
354  {
355  return cursor >= start() && cursor < end();
356  }
357 
365  Q_DECL_CONSTEXPR inline bool containsLine(int line) const Q_DECL_NOEXCEPT
366  {
367  return (line > start().line() || (line == start().line() && !start().column())) && line < end().line();
368  }
369 
377  Q_DECL_CONSTEXPR inline bool containsColumn(int column) const Q_DECL_NOEXCEPT
378  {
379  return column >= start().column() && column < end().column();
380  }
381 
389  Q_DECL_CONSTEXPR inline bool overlaps(const Range &range) const Q_DECL_NOEXCEPT
390  {
391  return (range.start() <= start()) ? (range.end() > start()) : (range.end() >= end()) ? (range.start() < end()) : contains(range);
392  }
393 
401  Q_DECL_CONSTEXPR inline bool overlapsLine(int line) const Q_DECL_NOEXCEPT
402  {
403  return line >= start().line() && line <= end().line();
404  }
405 
416  Q_DECL_CONSTEXPR inline bool overlapsColumn(int column) const Q_DECL_NOEXCEPT
417  {
418  return start().column() <= column && end().column() > column;
419  }
420 
430  Q_DECL_CONSTEXPR inline bool boundaryAtCursor(const Cursor &cursor) const Q_DECL_NOEXCEPT
431  {
432  return cursor == start() || cursor == end();
433  }
435  // END
436 
445  Q_DECL_CONSTEXPR inline Range intersect(const Range &range) const Q_DECL_NOEXCEPT
446  {
447  return ((!isValid() || !range.isValid() || *this > range || *this < range)) ? invalid() : Range(qMax(start(), range.start()), qMin(end(), range.end()));
448  }
449 
458  Q_DECL_CONSTEXPR inline Range encompass(const Range &range) const Q_DECL_NOEXCEPT
459  {
460  return (!isValid()) ? (range.isValid() ? range : invalid())
461  : (!range.isValid()) ? (*this)
462  : Range(qMin(start(), range.start()), qMax(end(), range.end()));
463  }
464 
473  Q_DECL_CONSTEXPR inline friend Range operator+(const Range &r1, const Range &r2) Q_DECL_NOEXCEPT
474  {
475  return Range(r1.start() + r2.start(), r1.end() + r2.end());
476  }
477 
486  inline friend Range &operator+=(Range &r1, const Range &r2) Q_DECL_NOEXCEPT
487  {
488  r1.setRange(r1.start() + r2.start(), r1.end() + r2.end());
489  return r1;
490  }
491 
501  Q_DECL_CONSTEXPR inline friend Range operator-(const Range &r1, const Range &r2) Q_DECL_NOEXCEPT
502  {
503  return Range(r1.start() - r2.start(), r1.end() - r2.end());
504  }
505 
514  inline friend Range &operator-=(Range &r1, const Range &r2) Q_DECL_NOEXCEPT
515  {
516  r1.setRange(r1.start() - r2.start(), r1.end() - r2.end());
517  return r1;
518  }
519 
528  Q_DECL_CONSTEXPR inline friend Range operator&(const Range &r1, const Range &r2) Q_DECL_NOEXCEPT
529  {
530  return r1.intersect(r2);
531  }
532 
541  inline friend Range &operator&=(Range &r1, const Range &r2) Q_DECL_NOEXCEPT
542  {
543  r1.setRange(r1.intersect(r2));
544  return r1;
545  }
546 
555  Q_DECL_CONSTEXPR inline friend bool operator==(const Range &r1, const Range &r2) Q_DECL_NOEXCEPT
556  {
557  return r1.start() == r2.start() && r1.end() == r2.end();
558  }
559 
568  Q_DECL_CONSTEXPR inline friend bool operator!=(const Range &r1, const Range &r2) Q_DECL_NOEXCEPT
569  {
570  return r1.start() != r2.start() || r1.end() != r2.end();
571  }
572 
582  Q_DECL_CONSTEXPR inline friend bool operator>(const Range &r1, const Range &r2) Q_DECL_NOEXCEPT
583  {
584  return r1.start() > r2.end();
585  }
586 
596  Q_DECL_CONSTEXPR inline friend bool operator<(const Range &r1, const Range &r2) Q_DECL_NOEXCEPT
597  {
598  return r1.end() < r2.start();
599  }
600 
604  inline friend QDebug operator<<(QDebug s, const Range &range)
605  {
606  s << "[" << range.start() << " -> " << range.end() << "]";
607  return s;
608  }
609 
610 private:
616  Cursor m_start;
617 
623  Cursor m_end;
624 };
625 
626 }
627 
628 Q_DECLARE_TYPEINFO(KTextEditor::Range, Q_MOVABLE_TYPE);
629 Q_DECLARE_METATYPE(KTextEditor::Range)
630 
631 
635 inline uint qHash(const KTextEditor::Range &range, uint seed = 0) Q_DECL_NOTHROW
636 {
637  return qHash(qMakePair(qHash(range.start()), qHash(range.end())), seed);
638 }
639 
640 namespace QTest
641 {
642 // forward declaration of template in qtestcase.h
643 template<typename T> char *toString(const T &);
644 
648 template<> KTEXTEDITOR_EXPORT char *toString(const KTextEditor::Range &range);
649 }
650 
651 #endif
constexpr friend Range operator+(const Range &r1, const Range &r2) Q_DECL_NOEXCEPT
Addition operator.
constexpr LineRange toLineRange() const Q_DECL_NOEXCEPT
Convert this Range to a LineRange.
void setStart(const Cursor &start) Q_DECL_NOEXCEPT
Set the start cursor to start.
constexpr Range(int startLine, int startColumn, int endLine, int endColumn) Q_DECL_NOEXCEPT
Constructor which creates a range from startLine, startColumn to endLine, endColumn.
constexpr bool isEmpty() const Q_DECL_NOEXCEPT
Returns true if this range contains no characters, ie.
constexpr bool boundaryAtCursor(const Cursor &cursor) const Q_DECL_NOEXCEPT
Check whether cursor is located at either of the start() or end() boundaries.
constexpr int numberOfLines() const Q_DECL_NOEXCEPT
Returns the number of lines separating the start() and end() positions.
void setEnd(const Cursor &end) Q_DECL_NOEXCEPT
Set the end cursor to end.
friend Range & operator&=(Range &r1, const Range &r2) Q_DECL_NOEXCEPT
Intersects r1 with r2 and assigns the result to r1.
constexpr Range(const Cursor &start, int width) Q_DECL_NOEXCEPT
Constructor which creates a single-line range from start, extending width characters along the same l...
The Cursor represents a position in a Document.
Definition: cursor.h:71
constexpr bool isValid() const Q_DECL_NOEXCEPT
Validity check.
static constexpr Cursor invalid() Q_DECL_NOEXCEPT
Returns an invalid cursor.
Definition: cursor.h:109
constexpr bool contains(const Range &range) const Q_DECL_NOEXCEPT
Check whether the this range wholly encompasses range.
constexpr friend bool operator<(const Range &r1, const Range &r2) Q_DECL_NOEXCEPT
Less than operator.
constexpr Range(const Cursor &start, const Cursor &end) Q_DECL_NOEXCEPT
Constructor which creates a range from start to end.
friend Range & operator+=(Range &r1, const Range &r2) Q_DECL_NOEXCEPT
Addition assignment operator.
An object representing lines from a start line to an end line.
Definition: linerange.h:37
constexpr friend Range operator-(const Range &r1, const Range &r2) Q_DECL_NOEXCEPT
Subtraction operator.
constexpr int column() const Q_DECL_NOEXCEPT
Retrieve the column on which this cursor is situated.
Definition: cursor.h:213
constexpr bool overlapsLine(int line) const Q_DECL_NOEXCEPT
Check whether the range overlaps at least part of line.
The KTextEditor namespace contains all the public API that is required to use the KTextEditor compone...
Definition: katetextblock.h:21
constexpr Cursor start() const Q_DECL_NOEXCEPT
Get the start position of this range.
constexpr bool overlaps(const Range &range) const Q_DECL_NOEXCEPT
Check whether the this range overlaps with range.
friend QDebug operator<<(QDebug s, const Range &range)
qDebug() stream operator.
An object representing a section of text, from one Cursor to another.
constexpr friend bool operator!=(const Range &r1, const Range &r2) Q_DECL_NOEXCEPT
Inequality operator.
friend Range & operator-=(Range &r1, const Range &r2) Q_DECL_NOEXCEPT
Subtraction assignment operator.
constexpr bool overlapsColumn(int column) const Q_DECL_NOEXCEPT
Check to see if this range overlaps column; that is, if column is between start().column() and end().column().
constexpr Range() Q_DECL_NOEXCEPT
Default constructor.
constexpr Range encompass(const Range &range) const Q_DECL_NOEXCEPT
Returns the smallest range which encompasses this range and the supplied range.
constexpr friend Range operator&(const Range &r1, const Range &r2) Q_DECL_NOEXCEPT
Intersects r1 and r2.
constexpr Range(const Cursor &start, int endLine, int endColumn) Q_DECL_NOEXCEPT
Constructor which creates a range from start, to endLine, endColumn.
KCALENDARCORE_EXPORT uint qHash(const KCalendarCore::Period &key)
constexpr Range intersect(const Range &range) const Q_DECL_NOEXCEPT
Intersects this range with another, returning the shared area of the two ranges.
constexpr bool contains(const Cursor &cursor) const Q_DECL_NOEXCEPT
Check to see if cursor is contained within this range, ie >= start() and < end(). ...
constexpr bool containsColumn(int column) const Q_DECL_NOEXCEPT
Check whether the range contains column.
constexpr friend bool operator>(const Range &r1, const Range &r2) Q_DECL_NOEXCEPT
Greater than operator.
QString toString() const
Returns the cursor position as string in the format "start-line:start-column,endl-line:end-column".
constexpr Cursor end() const Q_DECL_NOEXCEPT
Get the end position of this range.
static Range fromString(const QString &str) Q_DECL_NOEXCEPT
Returns a Range created from the string str containing the format "[(start-line, start-column), (endl-line:end-column)]".
constexpr bool containsLine(int line) const Q_DECL_NOEXCEPT
Returns true if this range wholly encompasses line.
constexpr int line() const Q_DECL_NOEXCEPT
Retrieve the line on which this cursor is situated.
Definition: cursor.h:195
static constexpr Range invalid() Q_DECL_NOEXCEPT
Returns an invalid range.
void setRange(const Range &range) Q_DECL_NOEXCEPT
Set the start and end cursors to range.start() and range.end() respectively.
constexpr int columnWidth() const Q_DECL_NOEXCEPT
Returns the number of columns separating the start() and end() positions.
constexpr bool onSingleLine() const Q_DECL_NOEXCEPT
Check whether this range is wholly contained within one line, ie.
constexpr friend bool operator==(const Range &r1, const Range &r2) Q_DECL_NOEXCEPT
Equality operator.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Jan 19 2021 23:00:17 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.