KTextEditor

include/ktexteditor/range.h
1 /* SPDX-License-Identifier: LGPL-2.0-or-later
2 
3  Copyright (C) 2003-2005 Hamish Rodda <[email protected]>
4  Copyright (C) 2001-2005 Christoph Cullmann <[email protected]>
5  Copyright (C) 2002 Christian Couder <[email protected]>
6  Copyright (C) 2001 Joseph Wenninger <[email protected]>
7  Copyright (C) 1999 Jochen Wilhelmy <[email protected]>
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Library General Public
11  License as published by the Free Software Foundation; either
12  version 2 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Library General Public License for more details.
18 
19  You should have received a copy of the GNU Library General Public License
20  along with this library; see the file COPYING.LIB. If not, write to
21  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22  Boston, MA 02110-1301, USA.
23 */
24 
25 #ifndef KTEXTEDITOR_RANGE_H
26 #define KTEXTEDITOR_RANGE_H
27 
28 #include <ktexteditor/cursor.h>
29 #include <ktexteditor_export.h>
30 
31 #include <QDebug>
32 #include <QtGlobal>
33 
34 namespace KTextEditor
35 {
57 class KTEXTEDITOR_EXPORT Range
58 {
59 public:
64  Q_DECL_CONSTEXPR Range() Q_DECL_NOEXCEPT
65  {
66  }
67 
75  Q_DECL_CONSTEXPR Range(const Cursor &start, const Cursor &end) Q_DECL_NOEXCEPT : m_start(qMin(start, end)), m_end(qMax(start, end))
76  {
77  }
78 
86  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)))
87  {
88  }
89 
97  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)))
98  {
99  }
100 
109  Q_DECL_CONSTEXPR Range(int startLine, int startColumn, int endLine, int endColumn) Q_DECL_NOEXCEPT : m_start(qMin(Cursor(startLine, startColumn), Cursor(endLine, endColumn))),
110  m_end(qMax(Cursor(startLine, startColumn), Cursor(endLine, endColumn)))
111  {
112  }
113 
117  Q_DECL_CONSTEXPR inline bool isValid() const Q_DECL_NOEXCEPT
118  {
119  return start().isValid() && end().isValid();
120  }
121 
125  Q_DECL_CONSTEXPR static Range invalid() Q_DECL_NOEXCEPT
126  {
128  }
129 
136  {
137  return QLatin1Char('[') + m_start.toString() + QLatin1String(", ") + m_end.toString() + QLatin1Char(']');
138  }
139 
146  static Range fromString(const QString &str) Q_DECL_NOEXCEPT
147  {
148  return fromString(str.leftRef(-1));
149  }
150 
157  static Range fromString(const QStringRef &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  void setBothLines(int line) Q_DECL_NOEXCEPT;
192 
198  void setBothColumns(int column) Q_DECL_NOEXCEPT;
199 
205  void setRange(const Range &range) Q_DECL_NOEXCEPT;
206 
217  void setRange(const Cursor &start, const Cursor &end) Q_DECL_NOEXCEPT;
218 
226  inline void setStart(const Cursor &start) Q_DECL_NOEXCEPT
227  {
228  if (start > end()) {
229  setRange(start, start);
230  } else {
231  setRange(start, end());
232  }
233  }
234 
242  inline void setEnd(const Cursor &end) Q_DECL_NOEXCEPT
243  {
244  if (end < start()) {
245  setRange(end, end);
246  } else {
247  setRange(start(), end);
248  }
249  }
250 
258  bool expandToRange(const Range &range) Q_DECL_NOEXCEPT;
259 
267  bool confineToRange(const Range &range) Q_DECL_NOEXCEPT;
268 
276  Q_DECL_CONSTEXPR inline bool onSingleLine() const Q_DECL_NOEXCEPT
277  {
278  return start().line() == end().line();
279  }
280 
287  Q_DECL_CONSTEXPR inline int numberOfLines() const Q_DECL_NOEXCEPT
288  {
289  return end().line() - start().line();
290  }
291 
298  Q_DECL_CONSTEXPR inline int columnWidth() const Q_DECL_NOEXCEPT
299  {
300  return end().column() - start().column();
301  }
302 
309  Q_DECL_CONSTEXPR inline bool isEmpty() const Q_DECL_NOEXCEPT
310  {
311  return start() == end();
312  }
313 
314  // BEGIN comparison functions
331  Q_DECL_CONSTEXPR inline bool contains(const Range &range) const Q_DECL_NOEXCEPT
332  {
333  return range.start() >= start() && range.end() <= end();
334  }
335 
343  Q_DECL_CONSTEXPR inline bool contains(const Cursor &cursor) const Q_DECL_NOEXCEPT
344  {
345  return cursor >= start() && cursor < end();
346  }
347 
355  Q_DECL_CONSTEXPR inline bool containsLine(int line) const Q_DECL_NOEXCEPT
356  {
357  return (line > start().line() || (line == start().line() && !start().column())) && line < end().line();
358  }
359 
367  Q_DECL_CONSTEXPR inline bool containsColumn(int column) const Q_DECL_NOEXCEPT
368  {
369  return column >= start().column() && column < end().column();
370  }
371 
379  Q_DECL_CONSTEXPR inline bool overlaps(const Range &range) const Q_DECL_NOEXCEPT
380  {
381  return (range.start() <= start()) ? (range.end() > start()) : (range.end() >= end()) ? (range.start() < end()) : contains(range);
382  }
383 
391  Q_DECL_CONSTEXPR inline bool overlapsLine(int line) const Q_DECL_NOEXCEPT
392  {
393  return line >= start().line() && line <= end().line();
394  }
395 
406  Q_DECL_CONSTEXPR inline bool overlapsColumn(int column) const Q_DECL_NOEXCEPT
407  {
408  return start().column() <= column && end().column() > column;
409  }
410 
420  Q_DECL_CONSTEXPR inline bool boundaryAtCursor(const Cursor &cursor) const Q_DECL_NOEXCEPT
421  {
422  return cursor == start() || cursor == end();
423  }
425  // END
426 
435  Q_DECL_CONSTEXPR inline Range intersect(const Range &range) const Q_DECL_NOEXCEPT
436  {
437  return ((!isValid() || !range.isValid() || *this > range || *this < range)) ? invalid() : Range(qMax(start(), range.start()), qMin(end(), range.end()));
438  }
439 
448  Q_DECL_CONSTEXPR inline Range encompass(const Range &range) const Q_DECL_NOEXCEPT
449  {
450  return (!isValid()) ? (range.isValid() ? range : invalid()) : (!range.isValid()) ? (*this) : Range(qMin(start(), range.start()), qMax(end(), range.end()));
451  }
452 
461  Q_DECL_CONSTEXPR inline friend Range operator+(const Range &r1, const Range &r2) Q_DECL_NOEXCEPT
462  {
463  return Range(r1.start() + r2.start(), r1.end() + r2.end());
464  }
465 
474  inline friend Range &operator+=(Range &r1, const Range &r2) Q_DECL_NOEXCEPT
475  {
476  r1.setRange(r1.start() + r2.start(), r1.end() + r2.end());
477  return r1;
478  }
479 
489  Q_DECL_CONSTEXPR inline friend Range operator-(const Range &r1, const Range &r2) Q_DECL_NOEXCEPT
490  {
491  return Range(r1.start() - r2.start(), r1.end() - r2.end());
492  }
493 
502  inline friend Range &operator-=(Range &r1, const Range &r2) Q_DECL_NOEXCEPT
503  {
504  r1.setRange(r1.start() - r2.start(), r1.end() - r2.end());
505  return r1;
506  }
507 
516  Q_DECL_CONSTEXPR inline friend Range operator&(const Range &r1, const Range &r2)Q_DECL_NOEXCEPT
517  {
518  return r1.intersect(r2);
519  }
520 
529  inline friend Range &operator&=(Range &r1, const Range &r2) Q_DECL_NOEXCEPT
530  {
531  r1.setRange(r1.intersect(r2));
532  return r1;
533  }
534 
543  Q_DECL_CONSTEXPR inline friend bool operator==(const Range &r1, const Range &r2) Q_DECL_NOEXCEPT
544  {
545  return r1.start() == r2.start() && r1.end() == r2.end();
546  }
547 
556  Q_DECL_CONSTEXPR inline friend bool operator!=(const Range &r1, const Range &r2) Q_DECL_NOEXCEPT
557  {
558  return r1.start() != r2.start() || r1.end() != 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.start() > r2.end();
573  }
574 
584  Q_DECL_CONSTEXPR inline friend bool operator<(const Range &r1, const Range &r2) Q_DECL_NOEXCEPT
585  {
586  return r1.end() < r2.start();
587  }
588 
592  inline friend QDebug operator<<(QDebug s, const Range &range)
593  {
594  s << "[" << range.start() << " -> " << range.end() << "]";
595  return s;
596  }
597 
598 private:
604  Cursor m_start;
605 
611  Cursor m_end;
612 };
613 
614 }
615 
616 Q_DECLARE_TYPEINFO(KTextEditor::Range, Q_MOVABLE_TYPE);
617 Q_DECLARE_METATYPE(KTextEditor::Range)
618 
619 
623 inline uint qHash(const KTextEditor::Range &range, uint seed = 0) Q_DECL_NOTHROW
624 {
625  return qHash(qMakePair(qHash(range.start()), qHash(range.end())), seed);
626 }
627 
628 namespace QTest
629 {
630 // forward declaration of template in qtestcase.h
631 template<typename T> char *toString(const T &);
632 
636 template<> KTEXTEDITOR_EXPORT char *toString(const KTextEditor::Range &range);
637 }
638 
639 #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:85
constexpr bool isValid() const Q_DECL_NOEXCEPT
Validity check.
static constexpr Cursor invalid() Q_DECL_NOEXCEPT
Returns an invalid cursor.
Definition: cursor.h:123
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:217
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:199
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 Aug 4 2020 22:56:42 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.