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 | |
16 | using namespace KTextEditor; |
17 | |
18 | // BEGIN MovingRange |
19 | MovingRange::MovingRange() = default; |
20 | |
21 | MovingRange::~MovingRange() = default; |
22 | |
23 | void 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 | |
29 | bool 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 | |
44 | QDebug 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 | */ |
60 | QDebug KTextEditor::operator<<(QDebug s, const MovingRange &range) |
61 | { |
62 | return s << ⦥ |
63 | } |
64 | // END MovingRange |
65 | |
66 | // BEGIN MovingCursor |
67 | |
68 | MovingCursor::MovingCursor() |
69 | { |
70 | } |
71 | |
72 | MovingCursor::~MovingCursor() |
73 | { |
74 | } |
75 | |
76 | void MovingCursor::setPosition(int line, int column) |
77 | { |
78 | // just use setPosition |
79 | setPosition(Cursor(line, column)); |
80 | } |
81 | |
82 | void MovingCursor::setLine(int line) |
83 | { |
84 | // just use setPosition |
85 | setPosition(line, column: column()); |
86 | } |
87 | |
88 | void MovingCursor::setColumn(int column) |
89 | { |
90 | // just use setPosition |
91 | setPosition(line: line(), column); |
92 | } |
93 | |
94 | bool MovingCursor::atStartOfLine() const |
95 | { |
96 | return isValidTextPosition() && column() == 0; |
97 | } |
98 | |
99 | bool MovingCursor::atEndOfLine() const |
100 | { |
101 | return isValidTextPosition() && column() == document()->lineLength(line: line()); |
102 | } |
103 | |
104 | bool MovingCursor::atEndOfDocument() const |
105 | { |
106 | return *this == document()->documentEnd(); |
107 | } |
108 | |
109 | bool MovingCursor::atStartOfDocument() const |
110 | { |
111 | return line() == 0 && column() == 0; |
112 | } |
113 | |
114 | bool 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 | |
126 | bool 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 | |
138 | bool 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 | |
151 | bool MovingCursor::isValidTextPosition() const |
152 | { |
153 | return document()->isValidTextPosition(cursor: toCursor()); |
154 | } |
155 | |
156 | QDebug 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 | |
166 | QDebug KTextEditor::operator<<(QDebug s, const MovingCursor &cursor) |
167 | { |
168 | return s << &cursor; |
169 | } |
170 | // END MovingCursor |
171 | |
172 | // BEGIN MovingRangeFeedback |
173 | MovingRangeFeedback::MovingRangeFeedback() = default; |
174 | |
175 | MovingRangeFeedback::~MovingRangeFeedback() = default; |
176 | |
177 | void MovingRangeFeedback::rangeEmpty(MovingRange *) |
178 | { |
179 | } |
180 | |
181 | void MovingRangeFeedback::rangeInvalid(MovingRange *) |
182 | { |
183 | } |
184 | |
185 | void MovingRangeFeedback::mouseEnteredRange(MovingRange *, View *) |
186 | { |
187 | } |
188 | |
189 | void MovingRangeFeedback::mouseExitedRange(MovingRange *, View *) |
190 | { |
191 | } |
192 | |
193 | void MovingRangeFeedback::caretEnteredRange(MovingRange *, View *) |
194 | { |
195 | } |
196 | |
197 | void MovingRangeFeedback::caretExitedRange(MovingRange *, View *) |
198 | { |
199 | } |
200 | // END MovingRangeFeedback |
201 | |