KTextEditor

utils/range.cpp
1/*
2 SPDX-FileCopyrightText: 2016 Dominik Haumann <dhaumann@kde.org>
3 SPDX-FileCopyrightText: 2007 Mirko Stocker <me@misto.ch>
4 SPDX-FileCopyrightText: 2003-2005 Hamish Rodda <rodda@kde.org>
5 SPDX-FileCopyrightText: 2002 Christian Couder <christian@kdevelop.org>
6 SPDX-FileCopyrightText: 2001, 2003 Christoph Cullmann <cullmann@kde.org>
7 SPDX-FileCopyrightText: 2001 Joseph Wenninger <jowenn@kde.org>
8 SPDX-FileCopyrightText: 1999 Jochen Wilhelmy <digisnap@cs.tu-berlin.de>
9
10 SPDX-License-Identifier: LGPL-2.0-or-later
11*/
12
13#include "range.h"
14
15#include <QByteArray>
16#include <QDebug>
17#include <QHash>
18#include <QString>
19
20using namespace KTextEditor;
21
23{
24 const int startIndex = str.indexOf(QLatin1Char('['));
25 const int endIndex = str.indexOf(QLatin1Char(']'));
26 const int closeIndex = str.indexOf(QLatin1Char(')')); // end of first cursor
27
28 if (startIndex < 0 || endIndex < 0 || closeIndex < 0 || closeIndex < startIndex || endIndex < closeIndex || endIndex < startIndex) {
29 return invalid();
30 }
31
32 return Range(Cursor::fromString(str.mid(startIndex + 1, closeIndex - startIndex)), Cursor::fromString(str.mid(closeIndex + 2, endIndex - closeIndex - 2)));
33}
34
35void Range::setRange(Range range) noexcept
36{
37 m_start = range.start();
38 m_end = range.end();
39}
40
42{
43 if (start > end) {
44 setRange(Range(end, start));
45 } else {
46 setRange(Range(start, end));
47 }
48}
49
50bool Range::confineToRange(Range range) noexcept
51{
52 if (start() < range.start()) {
53 if (end() > range.end()) {
54 setRange(range);
55 } else {
56 setStart(range.start());
57 }
58 } else if (end() > range.end()) {
59 setEnd(range.end());
60 } else {
61 return false;
62 }
63
64 return true;
65}
66
67bool Range::expandToRange(Range range) noexcept
68{
69 if (start() > range.start()) {
70 if (end() < range.end()) {
71 setRange(range);
72 } else {
73 setStart(range.start());
74 }
75 } else if (end() < range.end()) {
76 setEnd(range.end());
77 } else {
78 return false;
79 }
80
81 return true;
82}
83
84void Range::setBothLines(int line) noexcept
85{
86 setRange(Range(line, start().column(), line, end().column()));
87}
88
89void KTextEditor::Range::setBothColumns(int column) noexcept
90{
91 setRange(Range(start().line(), column, end().line(), column));
92}
93
95{
96 // parse format "[start, end]"
97 const int startIndex = str.indexOf(QLatin1Char('['));
98 const int endIndex = str.indexOf(QLatin1Char(']'));
99 const int commaIndex = str.indexOf(QLatin1Char(','));
100
101 if (startIndex < 0 || endIndex < 0 || commaIndex < 0 || commaIndex < startIndex || endIndex < commaIndex || endIndex < startIndex) {
102 return invalid();
103 }
104
105 bool ok1 = false;
106 bool ok2 = false;
107
108 const int start = str.mid(startIndex + 1, commaIndex - startIndex - 1).toInt(&ok1);
109 const int end = str.mid(commaIndex + 1, endIndex - commaIndex - 1).toInt(&ok2);
110
111 if (!ok1 || !ok2) {
112 return invalid();
113 }
114
115 return {start, end};
116}
117
119{
120 s << "[" << range.start() << " -> " << range.end() << "]";
121 return s;
122}
123
125{
126 s << "[" << range.start() << " -> " << range.end() << "]";
127 return s;
128}
129
130size_t KTextEditor::qHash(KTextEditor::LineRange range, size_t seed) noexcept
131{
132 return qHashMulti(seed, range.start(), range.end());
133}
134
135size_t KTextEditor::qHash(KTextEditor::Range range, size_t seed) noexcept
136{
137 return qHashMulti(seed, range.start(), range.end());
138}
139
141{
142 return QStringLiteral("[%1, %2]").arg(m_start.toString()).arg(m_end.toString());
143}
144
146{
147 return QStringLiteral("[%1, %2]").arg(m_start).arg(m_end);
148}
149
150namespace QTest
151{
152// Cursor: template specialization for QTest::toString()
153template<>
154char *toString(const KTextEditor::Cursor &cursor)
155{
156 QByteArray ba = "Cursor[" + QByteArray::number(cursor.line()) + ", " + QByteArray::number(cursor.column()) + ']';
157 return qstrdup(ba.data());
158}
159
160// Range: template specialization for QTest::toString()
161template<>
162char *toString(const KTextEditor::Range &range)
163{
164 QByteArray ba = "Range[";
165 ba += QByteArray::number(range.start().line()) + ", " + QByteArray::number(range.start().column()) + " - ";
166 ba += QByteArray::number(range.end().line()) + ", " + QByteArray::number(range.end().column());
167 ba += ']';
168 return qstrdup(ba.data());
169}
170
171// LineRange: template specialization for QTest::toString()
172template<>
173char *toString(const KTextEditor::LineRange &range)
174{
175 QByteArray ba = "LineRange[";
176 ba += QByteArray::number(range.start()) + ", " + QByteArray::number(range.end());
177 ba += ']';
178 return qstrdup(ba.data());
179}
180}
The Cursor represents a position in a Document.
Definition cursor.h:75
constexpr int column() const noexcept
Retrieve the column on which this cursor is situated.
Definition cursor.h:192
static Cursor fromString(QStringView str) noexcept
Returns a Cursor created from the string str containing the format "(line, column)".
QString toString() const
Returns the cursor position as string in the format "(line, column)".
constexpr int line() const noexcept
Retrieve the line on which this cursor is situated.
Definition cursor.h:174
An object representing lines from a start line to an end line.
Definition linerange.h:41
constexpr int start() const noexcept
Get the start line of this line range.
Definition linerange.h:105
QString toString() const
Returns the line range as string in the format "[start line, end line]".
constexpr int end() const noexcept
Get the end line of this line range.
Definition linerange.h:115
static LineRange fromString(QStringView str) noexcept
Returns a LineRange created from the string str containing the format "[start line,...
An object representing a section of text, from one Cursor to another.
constexpr Cursor end() const noexcept
Get the end position of this range.
constexpr Cursor start() const noexcept
Get the start position of this range.
bool confineToRange(Range range) noexcept
Confine this range if necessary to fit within range.
void setRange(Range range) noexcept
Set the start and end cursors to range.start() and range.end() respectively.
bool expandToRange(Range range) noexcept
Expand this range if necessary to contain range.
void setBothLines(int line) noexcept
Convenience function.
void setBothColumns(int column) noexcept
Convenience function.
static Range fromString(QStringView str) noexcept
Returns a Range created from the string str containing the format "[(start-line, start-column),...
QString toString() const
Returns the cursor position as string in the format "start-line:start-column,endl-line:end-column".
Q_SCRIPTABLE Q_NOREPLY void start()
char * toString(const EngineQuery &query)
The KTextEditor namespace contains all the public API that is required to use the KTextEditor compone...
KTEXTEDITOR_EXPORT QDebug operator<<(QDebug s, const MovingCursor *cursor)
qDebug() stream operator.
KTEXTEDITOR_EXPORT size_t qHash(KTextEditor::Cursor cursor, size_t seed=0) noexcept
QHash function for KTextEditor::Cursor.
char * data()
QByteArray number(int n, int base)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Feb 24 2024 20:00:58 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.