1/*
2 SPDX-FileCopyrightText: 2010 Christoph Cullmann <cullmann@kde.org>
3
4 Based on code of the SmartCursor/Range by:
5 SPDX-FileCopyrightText: 2003-2005 Hamish Rodda <rodda@kde.org>
6
7 SPDX-License-Identifier: LGPL-2.0-or-later
8*/
9
10#include "document.h"
11#include "documentcursor.h"
12#include "movingcursor.h"
13#include "movingrange.h"
14#include "movingrangefeedback.h"
15
16using namespace KTextEditor;
17
18// BEGIN MovingRange
19MovingRange::MovingRange() = default;
20
21MovingRange::~MovingRange() = default;
22
23void MovingRange::setRange(Cursor start, Cursor end)
24{
25 // just use other function, KTextEditor::Range will handle some normalization
26 setRange(Range(start, end));
27}
28
29bool MovingRange::overlaps(const Range &range) const
30{
31 if (range.start() <= start()) {
32 return range.end() > start();
33 }
34
35 else if (range.end() >= end()) {
36 return range.start() < end();
37 }
38
39 else {
40 return contains(range);
41 }
42}
43
44QDebug KTextEditor::operator<<(QDebug s, const MovingRange *range)
45{
46 if (range) {
47 s << "[" << range->start() << " -> " << range->end() << "]";
48 } else {
49 s << "(null range)";
50 }
51 return s.space();
52}
53
54/**
55 * qDebug() stream operator. Writes this range to the debug output in a nicely formatted way.
56 * @param s debug stream
57 * @param range range to print
58 * @return debug stream
59 */
60QDebug KTextEditor::operator<<(QDebug s, const MovingRange &range)
61{
62 return s << &range;
63}
64// END MovingRange
65
66// BEGIN MovingCursor
67
68MovingCursor::MovingCursor()
69{
70}
71
72MovingCursor::~MovingCursor()
73{
74}
75
76void MovingCursor::setPosition(int line, int column)
77{
78 // just use setPosition
79 setPosition(Cursor(line, column));
80}
81
82void MovingCursor::setLine(int line)
83{
84 // just use setPosition
85 setPosition(line, column: column());
86}
87
88void MovingCursor::setColumn(int column)
89{
90 // just use setPosition
91 setPosition(line: line(), column);
92}
93
94bool MovingCursor::atStartOfLine() const
95{
96 return isValidTextPosition() && column() == 0;
97}
98
99bool MovingCursor::atEndOfLine() const
100{
101 return isValidTextPosition() && column() == document()->lineLength(line: line());
102}
103
104bool MovingCursor::atEndOfDocument() const
105{
106 return *this == document()->documentEnd();
107}
108
109bool MovingCursor::atStartOfDocument() const
110{
111 return line() == 0 && column() == 0;
112}
113
114bool MovingCursor::gotoNextLine()
115{
116 // only touch valid cursors
117 const bool ok = isValid() && (line() + 1 < document()->lines());
118
119 if (ok) {
120 setPosition(Cursor(line() + 1, 0));
121 }
122
123 return ok;
124}
125
126bool MovingCursor::gotoPreviousLine()
127{
128 // only touch valid cursors
129 bool ok = (line() > 0) && (column() >= 0);
130
131 if (ok) {
132 setPosition(Cursor(line() - 1, 0));
133 }
134
135 return ok;
136}
137
138bool MovingCursor::move(int chars, WrapBehavior wrapBehavior)
139{
140 DocumentCursor dc(document(), toCursor());
141
142 const bool success = dc.move(chars, wrapBehavior: static_cast<DocumentCursor::WrapBehavior>(wrapBehavior));
143
144 if (success && dc.toCursor() != toCursor()) {
145 setPosition(dc.toCursor());
146 }
147
148 return success;
149}
150
151bool MovingCursor::isValidTextPosition() const
152{
153 return document()->isValidTextPosition(cursor: toCursor());
154}
155
156QDebug KTextEditor::operator<<(QDebug s, const MovingCursor *cursor)
157{
158 if (cursor) {
159 s.nospace() << "(" << cursor->line() << ", " << cursor->column() << ")";
160 } else {
161 s.nospace() << "(null cursor)";
162 }
163 return s.space();
164}
165
166QDebug KTextEditor::operator<<(QDebug s, const MovingCursor &cursor)
167{
168 return s << &cursor;
169}
170// END MovingCursor
171
172// BEGIN MovingRangeFeedback
173MovingRangeFeedback::MovingRangeFeedback() = default;
174
175MovingRangeFeedback::~MovingRangeFeedback() = default;
176
177void MovingRangeFeedback::rangeEmpty(MovingRange *)
178{
179}
180
181void MovingRangeFeedback::rangeInvalid(MovingRange *)
182{
183}
184
185void MovingRangeFeedback::mouseEnteredRange(MovingRange *, View *)
186{
187}
188
189void MovingRangeFeedback::mouseExitedRange(MovingRange *, View *)
190{
191}
192
193void MovingRangeFeedback::caretEnteredRange(MovingRange *, View *)
194{
195}
196
197void MovingRangeFeedback::caretExitedRange(MovingRange *, View *)
198{
199}
200// END MovingRangeFeedback
201

source code of ktexteditor/src/utils/movingapi.cpp