KTextEditor

utils/range.cpp
1 /*
2  SPDX-FileCopyrightText: 2016 Dominik Haumann <[email protected]>
3  SPDX-FileCopyrightText: 2007 Mirko Stocker <[email protected]>
4  SPDX-FileCopyrightText: 2003-2005 Hamish Rodda <[email protected]>
5  SPDX-FileCopyrightText: 2002 Christian Couder <[email protected]>
6  SPDX-FileCopyrightText: 2001, 2003 Christoph Cullmann <[email protected]>
7  SPDX-FileCopyrightText: 2001 Joseph Wenninger <[email protected]>
8  SPDX-FileCopyrightText: 1999 Jochen Wilhelmy <[email protected]>
9 
10  SPDX-License-Identifier: LGPL-2.0-or-later
11 */
12 
13 #include "range.h"
14 
15 using namespace KTextEditor;
16 
17 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
18 Range Range::fromString(const QStringRef &str) Q_DECL_NOEXCEPT
19 {
20  return fromString(QStringView(str));
21 }
22 #endif
23 
24 Range Range::fromString(QStringView str) Q_DECL_NOEXCEPT
25 {
26  const int startIndex = str.indexOf(QLatin1Char('['));
27  const int endIndex = str.indexOf(QLatin1Char(']'));
28  const int closeIndex = str.indexOf(QLatin1Char(')')); // end of first cursor
29 
30  if (startIndex < 0 || endIndex < 0 || closeIndex < 0 || closeIndex < startIndex || endIndex < closeIndex || endIndex < startIndex) {
31  return invalid();
32  }
33 
34  return Range(Cursor::fromString(str.mid(startIndex + 1, closeIndex - startIndex)), Cursor::fromString(str.mid(closeIndex + 2, endIndex - closeIndex - 2)));
35 }
36 
37 void Range::setRange(const Range &range) Q_DECL_NOEXCEPT
38 {
39  m_start = range.start();
40  m_end = range.end();
41 }
42 
43 void Range::setRange(const Cursor &start, const Cursor &end) Q_DECL_NOEXCEPT
44 {
45  if (start > end) {
46  setRange(Range(end, start));
47  } else {
48  setRange(Range(start, end));
49  }
50 }
51 
52 bool Range::confineToRange(const Range &range) Q_DECL_NOEXCEPT
53 {
54  if (start() < range.start()) {
55  if (end() > range.end()) {
56  setRange(range);
57  } else {
58  setStart(range.start());
59  }
60  } else if (end() > range.end()) {
61  setEnd(range.end());
62  } else {
63  return false;
64  }
65 
66  return true;
67 }
68 
69 bool Range::expandToRange(const Range &range) Q_DECL_NOEXCEPT
70 {
71  if (start() > range.start()) {
72  if (end() < range.end()) {
73  setRange(range);
74  } else {
75  setStart(range.start());
76  }
77  } else if (end() < range.end()) {
78  setEnd(range.end());
79  } else {
80  return false;
81  }
82 
83  return true;
84 }
85 
86 void Range::setBothLines(int line) Q_DECL_NOEXCEPT
87 {
88  setRange(Range(line, start().column(), line, end().column()));
89 }
90 
91 void KTextEditor::Range::setBothColumns(int column) Q_DECL_NOEXCEPT
92 {
93  setRange(Range(start().line(), column, end().line(), column));
94 }
95 
97 {
98  // parse format "[start, end]"
99  const int startIndex = str.indexOf(QLatin1Char('['));
100  const int endIndex = str.indexOf(QLatin1Char(']'));
101  const int commaIndex = str.indexOf(QLatin1Char(','));
102 
103  if (startIndex < 0 || endIndex < 0 || commaIndex < 0 || commaIndex < startIndex || endIndex < commaIndex || endIndex < startIndex) {
104  return invalid();
105  }
106 
107  bool ok1 = false;
108  bool ok2 = false;
109 
110  const int start = str.mid(startIndex + 1, commaIndex - startIndex - 1).toString().toInt(&ok1); // FIXME KF6: Qt 5.15.2 and higher: remove .toString()
111  const int end = str.mid(commaIndex + 1, endIndex - commaIndex - 1).toString().toInt(&ok2); // FIXME KF6: Qt 5.15.2 and higher: remove .toString()
112 
113  if (!ok1 || !ok2) {
114  return invalid();
115  }
116 
117  return {start, end};
118 }
119 
120 namespace QTest
121 {
122 // Cursor: template specialization for QTest::toString()
123 template<>
124 char *toString(const KTextEditor::Cursor &cursor)
125 {
126  QByteArray ba = "Cursor[" + QByteArray::number(cursor.line()) + ", " + QByteArray::number(cursor.column()) + ']';
127  return qstrdup(ba.data());
128 }
129 
130 // Range: template specialization for QTest::toString()
131 template<>
132 char *toString(const KTextEditor::Range &range)
133 {
134  QByteArray ba = "Range[";
135  ba += QByteArray::number(range.start().line()) + ", " + QByteArray::number(range.start().column()) + " - ";
136  ba += QByteArray::number(range.end().line()) + ", " + QByteArray::number(range.end().column());
137  ba += ']';
138  return qstrdup(ba.data());
139 }
140 
141 // LineRange: template specialization for QTest::toString()
142 template<>
143 char *toString(const KTextEditor::LineRange &range)
144 {
145  QByteArray ba = "LineRange[";
146  ba += QByteArray::number(range.start()) + ", " + QByteArray::number(range.end());
147  ba += ']';
148  return qstrdup(ba.data());
149 }
150 }
constexpr int column() const Q_DECL_NOEXCEPT
Retrieve the column on which this cursor is situated.
Definition: cursor.h:215
static Range fromString(QStringView str) Q_DECL_NOEXCEPT
Returns a Range created from the string str containing the format "[(start-line, start-column),...
Definition: utils/range.cpp:24
static Cursor fromString(QStringView str) Q_DECL_NOEXCEPT
Returns a Cursor created from the string str containing the format "(line, column)".
Definition: ktexteditor.cpp:51
Q_SCRIPTABLE Q_NOREPLY void start()
bool confineToRange(const Range &range) Q_DECL_NOEXCEPT
Confine this range if necessary to fit within range.
Definition: utils/range.cpp:52
QByteArray number(int n, int base)
An object representing lines from a start line to an end line.
Definition: linerange.h:37
An object representing a section of text, from one Cursor to another.
void setBothColumns(int column) Q_DECL_NOEXCEPT
Convenience function.
Definition: utils/range.cpp:91
constexpr Cursor end() const Q_DECL_NOEXCEPT
Get the end position of this range.
bool expandToRange(const Range &range) Q_DECL_NOEXCEPT
Expand this range if necessary to contain range.
Definition: utils/range.cpp:69
The Cursor represents a position in a Document.
Definition: cursor.h:71
char * toString(const T &value)
constexpr int start() const Q_DECL_NOEXCEPT
Get the start line of this line range.
Definition: linerange.h:105
void setRange(const Range &range) Q_DECL_NOEXCEPT
Set the start and end cursors to range.start() and range.end() respectively.
The KTextEditor namespace contains all the public API that is required to use the KTextEditor compone...
Definition: katetextblock.h:22
static LineRange fromString(QStringView str) Q_DECL_NOEXCEPT
Returns a LineRange created from the string str containing the format "[start line,...
Definition: utils/range.cpp:96
constexpr Cursor start() const Q_DECL_NOEXCEPT
Get the start position of this range.
void setBothLines(int line) Q_DECL_NOEXCEPT
Convenience function.
Definition: utils/range.cpp:86
constexpr int end() const Q_DECL_NOEXCEPT
Get the end line of this line range.
Definition: linerange.h:115
constexpr int line() const Q_DECL_NOEXCEPT
Retrieve the line on which this cursor is situated.
Definition: cursor.h:197
char * data()
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Mon Jun 27 2022 03:58:28 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.