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_export.h>
16 
17 #include <QDebug>
18 #include <QtGlobal>
19 
20 namespace KTextEditor
21 {
43 class KTEXTEDITOR_EXPORT Range
44 {
45 public:
50  Q_DECL_CONSTEXPR Range() Q_DECL_NOEXCEPT
51  {
52  }
53 
61  Q_DECL_CONSTEXPR Range(const Cursor &start, const Cursor &end) Q_DECL_NOEXCEPT : m_start(qMin(start, end)), m_end(qMax(start, end))
62  {
63  }
64 
72  Q_DECL_CONSTEXPR Range(const Cursor &start, int width) Q_DECL_NOEXCEPT : m_start(qMin(start, Cursor(start.line(), start.column() + width))), m_end(qMax(start, Cursor(start.line(), start.column() + width)))
73  {
74  }
75 
83  Q_DECL_CONSTEXPR Range(const Cursor &start, int endLine, int endColumn) Q_DECL_NOEXCEPT : m_start(qMin(start, Cursor(endLine, endColumn))), m_end(qMax(start, Cursor(endLine, endColumn)))
84  {
85  }
86 
95  Q_DECL_CONSTEXPR Range(int startLine, int startColumn, int endLine, int endColumn) Q_DECL_NOEXCEPT : m_start(qMin(Cursor(startLine, startColumn), Cursor(endLine, endColumn))),
96  m_end(qMax(Cursor(startLine, startColumn), Cursor(endLine, endColumn)))
97  {
98  }
99 
103  Q_DECL_CONSTEXPR inline bool isValid() const Q_DECL_NOEXCEPT
104  {
105  return start().isValid() && end().isValid();
106  }
107 
111  Q_DECL_CONSTEXPR static Range invalid() Q_DECL_NOEXCEPT
112  {
114  }
115 
122  {
123  return QLatin1Char('[') + m_start.toString() + QLatin1String(", ") + m_end.toString() + QLatin1Char(']');
124  }
125 
132  static Range fromString(const QString &str) Q_DECL_NOEXCEPT
133  {
134  return fromString(str.leftRef(-1));
135  }
136 
143  static Range fromString(const QStringRef &str) Q_DECL_NOEXCEPT;
144 
157  Q_DECL_CONSTEXPR inline Cursor start() const Q_DECL_NOEXCEPT
158  {
159  return m_start;
160  }
161 
167  Q_DECL_CONSTEXPR inline Cursor end() const Q_DECL_NOEXCEPT
168  {
169  return m_end;
170  }
171 
177  void setBothLines(int line) Q_DECL_NOEXCEPT;
178 
184  void setBothColumns(int column) Q_DECL_NOEXCEPT;
185 
191  void setRange(const Range &range) Q_DECL_NOEXCEPT;
192 
203  void setRange(const Cursor &start, const Cursor &end) Q_DECL_NOEXCEPT;
204 
212  inline void setStart(const Cursor &start) Q_DECL_NOEXCEPT
213  {
214  if (start > end()) {
215  setRange(start, start);
216  } else {
217  setRange(start, end());
218  }
219  }
220 
228  inline void setEnd(const Cursor &end) Q_DECL_NOEXCEPT
229  {
230  if (end < start()) {
231  setRange(end, end);
232  } else {
233  setRange(start(), end);
234  }
235  }
236 
244  bool expandToRange(const Range &range) Q_DECL_NOEXCEPT;
245 
253  bool confineToRange(const Range &range) Q_DECL_NOEXCEPT;
254 
262  Q_DECL_CONSTEXPR inline bool onSingleLine() const Q_DECL_NOEXCEPT
263  {
264  return start().line() == end().line();
265  }
266 
273  Q_DECL_CONSTEXPR inline int numberOfLines() const Q_DECL_NOEXCEPT
274  {
275  return end().line() - start().line();
276  }
277 
284  Q_DECL_CONSTEXPR inline int columnWidth() const Q_DECL_NOEXCEPT
285  {
286  return end().column() - start().column();
287  }
288 
295  Q_DECL_CONSTEXPR inline bool isEmpty() const Q_DECL_NOEXCEPT
296  {
297  return start() == end();
298  }
299 
300  // BEGIN comparison functions
317  Q_DECL_CONSTEXPR inline bool contains(const Range &range) const Q_DECL_NOEXCEPT
318  {
319  return range.start() >= start() && range.end() <= end();
320  }
321 
329  Q_DECL_CONSTEXPR inline bool contains(const Cursor &cursor) const Q_DECL_NOEXCEPT
330  {
331  return cursor >= start() && cursor < end();
332  }
333 
341  Q_DECL_CONSTEXPR inline bool containsLine(int line) const Q_DECL_NOEXCEPT
342  {
343  return (line > start().line() || (line == start().line() && !start().column())) && line < end().line();
344  }
345 
353  Q_DECL_CONSTEXPR inline bool containsColumn(int column) const Q_DECL_NOEXCEPT
354  {
355  return column >= start().column() && column < end().column();
356  }
357 
365  Q_DECL_CONSTEXPR inline bool overlaps(const Range &range) const Q_DECL_NOEXCEPT
366  {
367  return (range.start() <= start()) ? (range.end() > start()) : (range.end() >= end()) ? (range.start() < end()) : contains(range);
368  }
369 
377  Q_DECL_CONSTEXPR inline bool overlapsLine(int line) const Q_DECL_NOEXCEPT
378  {
379  return line >= start().line() && line <= end().line();
380  }
381 
392  Q_DECL_CONSTEXPR inline bool overlapsColumn(int column) const Q_DECL_NOEXCEPT
393  {
394  return start().column() <= column && end().column() > column;
395  }
396 
406  Q_DECL_CONSTEXPR inline bool boundaryAtCursor(const Cursor &cursor) const Q_DECL_NOEXCEPT
407  {
408  return cursor == start() || cursor == end();
409  }
411  // END
412 
421  Q_DECL_CONSTEXPR inline Range intersect(const Range &range) const Q_DECL_NOEXCEPT
422  {
423  return ((!isValid() || !range.isValid() || *this > range || *this < range)) ? invalid() : Range(qMax(start(), range.start()), qMin(end(), range.end()));
424  }
425 
434  Q_DECL_CONSTEXPR inline Range encompass(const Range &range) const Q_DECL_NOEXCEPT
435  {
436  return (!isValid()) ? (range.isValid() ? range : invalid()) : (!range.isValid()) ? (*this) : Range(qMin(start(), range.start()), qMax(end(), range.end()));
437  }
438 
447  Q_DECL_CONSTEXPR inline friend Range operator+(const Range &r1, const Range &r2) Q_DECL_NOEXCEPT
448  {
449  return Range(r1.start() + r2.start(), r1.end() + r2.end());
450  }
451 
460  inline friend Range &operator+=(Range &r1, const Range &r2) Q_DECL_NOEXCEPT
461  {
462  r1.setRange(r1.start() + r2.start(), r1.end() + r2.end());
463  return r1;
464  }
465 
475  Q_DECL_CONSTEXPR inline friend Range operator-(const Range &r1, const Range &r2) Q_DECL_NOEXCEPT
476  {
477  return Range(r1.start() - r2.start(), r1.end() - r2.end());
478  }
479 
488  inline friend Range &operator-=(Range &r1, const Range &r2) Q_DECL_NOEXCEPT
489  {
490  r1.setRange(r1.start() - r2.start(), r1.end() - r2.end());
491  return r1;
492  }
493 
502  Q_DECL_CONSTEXPR inline friend Range operator&(const Range &r1, const Range &r2) Q_DECL_NOEXCEPT
503  {
504  return r1.intersect(r2);
505  }
506 
515  inline friend Range &operator&=(Range &r1, const Range &r2) Q_DECL_NOEXCEPT
516  {
517  r1.setRange(r1.intersect(r2));
518  return r1;
519  }
520 
529  Q_DECL_CONSTEXPR inline friend bool operator==(const Range &r1, const Range &r2) Q_DECL_NOEXCEPT
530  {
531  return r1.start() == r2.start() && r1.end() == r2.end();
532  }
533 
542  Q_DECL_CONSTEXPR inline friend bool operator!=(const Range &r1, const Range &r2) Q_DECL_NOEXCEPT
543  {
544  return r1.start() != r2.start() || r1.end() != r2.end();
545  }
546 
556  Q_DECL_CONSTEXPR inline friend bool operator>(const Range &r1, const Range &r2) Q_DECL_NOEXCEPT
557  {
558  return r1.start() > r2.end();
559  }
560 
570  Q_DECL_CONSTEXPR inline friend bool operator<(const Range &r1, const Range &r2) Q_DECL_NOEXCEPT
571  {
572  return r1.end() < r2.start();
573  }
574 
578  inline friend QDebug operator<<(QDebug s, const Range &range)
579  {
580  s << "[" << range.start() << " -> " << range.end() << "]";
581  return s;
582  }
583 
584 private:
590  Cursor m_start;
591 
597  Cursor m_end;
598 };
599 
600 }
601 
602 Q_DECLARE_TYPEINFO(KTextEditor::Range, Q_MOVABLE_TYPE);
603 Q_DECLARE_METATYPE(KTextEditor::Range)
604 
605 
609 inline uint qHash(const KTextEditor::Range &range, uint seed = 0) Q_DECL_NOTHROW
610 {
611  return qHash(qMakePair(qHash(range.start()), qHash(range.end())), seed);
612 }
613 
614 namespace QTest
615 {
616 // forward declaration of template in qtestcase.h
617 template<typename T> char *toString(const T &);
618 
622 template<> KTEXTEDITOR_EXPORT char *toString(const KTextEditor::Range &range);
623 }
624 
625 #endif
constexpr friend Range operator+(const Range &r1, const Range &r2) Q_DECL_NOEXCEPT
Addition operator.
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.
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:203
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...
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:185
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-2020 The KDE developers.
Generated on Tue Oct 20 2020 23:03:17 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.