KTextEditor

cursor.h
1 /*
2  SPDX-FileCopyrightText: 2003-2005 Hamish Rodda <[email protected]>
3  SPDX-FileCopyrightText: 2001-2005 Christoph Cullmann <[email protected]>
4  SPDX-FileCopyrightText: 2014 Dominik Haumann <[email protected]>
5  SPDX-FileCopyrightText: 2002 Christian Couder <[email protected]>
6  SPDX-FileCopyrightText: 2001 Joseph Wenninger <[email protected]>
7  SPDX-FileCopyrightText: 1999 Jochen Wilhelmy <[email protected]>
8 
9  SPDX-License-Identifier: LGPL-2.0-or-later
10 */
11 
12 #ifndef KTEXTEDITOR_CURSOR_H
13 #define KTEXTEDITOR_CURSOR_H
14 
15 #include <ktexteditor_export.h>
16 
17 #include <QDebug>
18 #include <QtGlobal>
19 
20 namespace KTextEditor
21 {
22 class Document;
23 class Range;
24 
71 class KTEXTEDITOR_EXPORT Cursor
72 {
73 public:
77  Q_DECL_CONSTEXPR Cursor() Q_DECL_NOEXCEPT
78  {
79  }
80 
87  Q_DECL_CONSTEXPR Cursor(int line, int column) Q_DECL_NOEXCEPT : m_line(line), m_column(column)
88  {
89  }
90 
99  Q_DECL_CONSTEXPR inline bool isValid() const Q_DECL_NOEXCEPT
100  {
101  return m_line >= 0 && m_column >= 0;
102  }
103 
109  Q_DECL_CONSTEXPR static Cursor invalid() Q_DECL_NOEXCEPT
110  {
111  return Cursor(-1, -1);
112  }
113 
117  Q_DECL_CONSTEXPR static Cursor start() Q_DECL_NOEXCEPT
118  {
119  return Cursor();
120  }
121 
127  {
128  return QLatin1Char('(') + QString::number(m_line) + QLatin1String(", ") + QString::number(m_column) + QLatin1Char(')');
129  }
130 
137  static Cursor fromString(const QString &str) Q_DECL_NOEXCEPT
138  {
139  return fromString(str.leftRef(-1));
140  }
141 
148  static Cursor fromString(const QStringRef &str) Q_DECL_NOEXCEPT;
149 
161  inline void setPosition(const Cursor &position) Q_DECL_NOEXCEPT
162  {
163  m_line = position.m_line;
164  m_column = position.m_column;
165  }
166 
175  inline void setPosition(int line, int column) Q_DECL_NOEXCEPT
176  {
177  m_line = line;
178  m_column = column;
179  }
180 
185  Q_DECL_CONSTEXPR inline int line() const Q_DECL_NOEXCEPT
186  {
187  return m_line;
188  }
189 
194  inline void setLine(int line) Q_DECL_NOEXCEPT
195  {
196  m_line = line;
197  }
198 
203  Q_DECL_CONSTEXPR inline int column() const Q_DECL_NOEXCEPT
204  {
205  return m_column;
206  }
207 
212  inline void setColumn(int column) Q_DECL_NOEXCEPT
213  {
214  m_column = column;
215  }
216 
221  Q_DECL_CONSTEXPR inline bool atStartOfLine() const Q_DECL_NOEXCEPT
222  {
223  return m_column == 0;
224  }
225 
230  Q_DECL_CONSTEXPR inline bool atStartOfDocument() const Q_DECL_NOEXCEPT
231  {
232  return m_line == 0 && m_column == 0;
233  }
234 
240  inline void position(int &line, int &column) const Q_DECL_NOEXCEPT
241  {
242  line = m_line;
243  column = m_column;
244  }
246 
253  Q_DECL_CONSTEXPR inline friend Cursor operator+(const Cursor &c1, const Cursor &c2) Q_DECL_NOEXCEPT
254  {
255  return Cursor(c1.line() + c2.line(), c1.column() + c2.column());
256  }
257 
264  inline friend Cursor &operator+=(Cursor &c1, const Cursor &c2) Q_DECL_NOEXCEPT
265  {
266  c1.setPosition(c1.line() + c2.line(), c1.column() + c2.column());
267  return c1;
268  }
269 
278  Q_DECL_CONSTEXPR inline friend Cursor operator-(const Cursor &c1, const Cursor &c2) Q_DECL_NOEXCEPT
279  {
280  return Cursor(c1.line() - c2.line(), c1.column() - c2.column());
281  }
282 
289  inline friend Cursor &operator-=(Cursor &c1, const Cursor &c2) Q_DECL_NOEXCEPT
290  {
291  c1.setPosition(c1.line() - c2.line(), c1.column() - c2.column());
292  return c1;
293  }
294 
305  Q_DECL_CONSTEXPR inline friend bool operator==(const Cursor &c1, const Cursor &c2) Q_DECL_NOEXCEPT
306  {
307  return c1.line() == c2.line() && c1.column() == c2.column();
308  }
309 
316  Q_DECL_CONSTEXPR inline friend bool operator!=(const Cursor &c1, const Cursor &c2) Q_DECL_NOEXCEPT
317  {
318  return !(c1 == c2);
319  }
320 
328  Q_DECL_CONSTEXPR inline friend bool operator>(const Cursor &c1, const Cursor &c2) Q_DECL_NOEXCEPT
329  {
330  return c1.line() > c2.line() || (c1.line() == c2.line() && c1.m_column > c2.m_column);
331  }
332 
340  Q_DECL_CONSTEXPR inline friend bool operator>=(const Cursor &c1, const Cursor &c2) Q_DECL_NOEXCEPT
341  {
342  return c1.line() > c2.line() || (c1.line() == c2.line() && c1.m_column >= c2.m_column);
343  }
344 
352  Q_DECL_CONSTEXPR inline friend bool operator<(const Cursor &c1, const Cursor &c2) Q_DECL_NOEXCEPT
353  {
354  return !(c1 >= c2);
355  }
356 
364  Q_DECL_CONSTEXPR inline friend bool operator<=(const Cursor &c1, const Cursor &c2) Q_DECL_NOEXCEPT
365  {
366  return !(c1 > c2);
367  }
368 
372  inline friend QDebug operator<<(QDebug s, const Cursor &cursor)
373  {
374  s.nospace() << "(" << cursor.line() << ", " << cursor.column() << ")";
375  return s.space();
376  }
377 
378 private:
384  int m_line = 0;
385 
391  int m_column = 0;
392 };
393 
394 } // namespace KTextEditor
395 
396 Q_DECLARE_TYPEINFO(KTextEditor::Cursor, Q_MOVABLE_TYPE);
397 Q_DECLARE_METATYPE(KTextEditor::Cursor)
398 
399 
403 inline uint qHash(const KTextEditor::Cursor &cursor, uint seed = 0) Q_DECL_NOTHROW
404 {
405  return qHash(qMakePair(cursor.line(), cursor.column()), seed);
406 }
407 
408 namespace QTest
409 {
410 // forward declaration of template in qtestcase.h
411 template<typename T> char *toString(const T &);
412 
416 template<> KTEXTEDITOR_EXPORT char *toString(const KTextEditor::Cursor &cursor);
417 }
418 
419 #endif
constexpr Cursor() Q_DECL_NOEXCEPT
The default constructor creates a cursor at position (0, 0).
Definition: cursor.h:77
constexpr bool atStartOfLine() const Q_DECL_NOEXCEPT
Determine if this cursor is located at the start of a line (= at column 0).
Definition: cursor.h:221
void setPosition(const Cursor &position) Q_DECL_NOEXCEPT
Set the current cursor position to position.
Definition: cursor.h:161
QDebug & nospace()
static constexpr Cursor start() Q_DECL_NOEXCEPT
Returns a cursor representing the start of any document - i.e., line 0, column 0. ...
Definition: cursor.h:117
constexpr friend bool operator!=(const Cursor &c1, const Cursor &c2) Q_DECL_NOEXCEPT
Inequality operator.
Definition: cursor.h:316
The Cursor represents a position in a Document.
Definition: cursor.h:71
constexpr friend bool operator==(const Cursor &c1, const Cursor &c2) Q_DECL_NOEXCEPT
Equality operator.
Definition: cursor.h:305
static constexpr Cursor invalid() Q_DECL_NOEXCEPT
Returns an invalid cursor.
Definition: cursor.h:109
constexpr bool isValid() const Q_DECL_NOEXCEPT
Returns whether the current position of this cursor is a valid position (line + column must both be >...
Definition: cursor.h:99
friend QDebug operator<<(QDebug s, const Cursor &cursor)
qDebug() stream operator.
Definition: cursor.h:372
QString number(int n, int base)
constexpr friend bool operator<(const Cursor &c1, const Cursor &c2) Q_DECL_NOEXCEPT
Less than operator.
Definition: cursor.h:352
constexpr int column() const Q_DECL_NOEXCEPT
Retrieve the column on which this cursor is situated.
Definition: cursor.h:203
constexpr friend Cursor operator-(const Cursor &c1, const Cursor &c2) Q_DECL_NOEXCEPT
Subtraction operator.
Definition: cursor.h:278
The KTextEditor namespace contains all the public API that is required to use the KTextEditor compone...
void setPosition(int line, int column) Q_DECL_NOEXCEPT
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: cursor.h:175
constexpr bool atStartOfDocument() const Q_DECL_NOEXCEPT
Determine if this cursor is located at the start of a document (= at position (0, 0))...
Definition: cursor.h:230
KCALENDARCORE_EXPORT uint qHash(const KCalendarCore::Period &key)
QDebug & space()
void position(int &line, int &column) const Q_DECL_NOEXCEPT
Get both the line and column of the cursor position.
Definition: cursor.h:240
friend Cursor & operator+=(Cursor &c1, const Cursor &c2) Q_DECL_NOEXCEPT
Addition assignment operator.
Definition: cursor.h:264
constexpr friend bool operator<=(const Cursor &c1, const Cursor &c2) Q_DECL_NOEXCEPT
Less than or equal to operator.
Definition: cursor.h:364
constexpr friend bool operator>(const Cursor &c1, const Cursor &c2) Q_DECL_NOEXCEPT
Greater than operator.
Definition: cursor.h:328
constexpr friend Cursor operator+(const Cursor &c1, const Cursor &c2) Q_DECL_NOEXCEPT
Addition operator.
Definition: cursor.h:253
friend Cursor & operator-=(Cursor &c1, const Cursor &c2) Q_DECL_NOEXCEPT
Subtraction assignment operator.
Definition: cursor.h:289
constexpr int line() const Q_DECL_NOEXCEPT
Retrieve the line on which this cursor is situated.
Definition: cursor.h:185
static Cursor fromString(const QString &str) Q_DECL_NOEXCEPT
Returns a Cursor created from the string str containing the format "(line, column)".
Definition: cursor.h:137
void setLine(int line) Q_DECL_NOEXCEPT
Set the cursor line to line.
Definition: cursor.h:194
constexpr friend bool operator>=(const Cursor &c1, const Cursor &c2) Q_DECL_NOEXCEPT
Greater than or equal to operator.
Definition: cursor.h:340
void setColumn(int column) Q_DECL_NOEXCEPT
Set the cursor column to column.
Definition: cursor.h:212
constexpr Cursor(int line, int column) Q_DECL_NOEXCEPT
This constructor creates a cursor initialized with line and column.
Definition: cursor.h:87
QString toString() const
Returns the cursor position as string in the format "(line, column)".
Definition: cursor.h:126
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Nov 29 2020 22:57:04 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.