KTextEditor

movingapi.cpp
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
22
24{
25 // just use other function, KTextEditor::Range will handle some normalization
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
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 */
61{
62 return s << &range;
63}
64// END MovingRange
65
66// BEGIN MovingCursor
67
71
75
76void MovingCursor::setPosition(int line, int column)
77{
78 // just use setPosition
80}
81
83{
84 // just use setPosition
86}
87
89{
90 // just use setPosition
92}
93
95{
96 return isValidTextPosition() && column() == 0;
97}
98
100{
101 return isValidTextPosition() && column() == document()->lineLength(line());
102}
103
105{
106 return *this == document()->documentEnd();
107}
108
110{
111 return line() == 0 && column() == 0;
112}
113
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
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{
141
142 const bool success = dc.move(chars, static_cast<DocumentCursor::WrapBehavior>(wrapBehavior));
143
144 if (success && dc.toCursor() != toCursor()) {
145 setPosition(dc.toCursor());
146 }
147
148 return success;
149}
150
155
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
167{
168 return s << &cursor;
169}
170// END MovingCursor
171
172// BEGIN MovingRangeFeedback
174
176
180
184
188
192
196
200// END MovingRangeFeedback
The Cursor represents a position in a Document.
Definition cursor.h:75
A Cursor which is bound to a specific Document.
bool move(int chars, WrapBehavior wrapBehavior=Wrap)
Moves the cursor chars character forward or backwards.
Cursor toCursor() const
Convert this clever cursor into a dumb one.
WrapBehavior
Wrap behavior for end of line treatement used in move().
virtual bool isValidTextPosition(KTextEditor::Cursor cursor) const =0
Get whether cursor is a valid text position.
virtual int lines() const =0
Get the count of lines of the document.
virtual int lineLength(int line) const =0
Get the length of a given line in characters.
virtual Cursor documentEnd() const =0
End position of the document.
A Cursor which is bound to a specific Document, and maintains its position.
bool atStartOfDocument() const
Determine if this cursor is located at line 0 and column 0.
bool move(int chars, WrapBehavior wrapBehavior=Wrap)
Moves the cursor chars character forward or backwards.
const Cursor toCursor() const
Convert this clever cursor into a dumb one.
bool isValid() const
Returns whether the current position of this cursor is a valid position, i.e.
WrapBehavior
Wrap behavior for end of line treatement used in move().
bool gotoNextLine()
Moves the cursor to the next line and sets the column to 0.
virtual Document * document() const =0
Gets the document to which this cursor is bound.
virtual ~MovingCursor()
Destruct the moving cursor.
Definition movingapi.cpp:72
bool gotoPreviousLine()
Moves the cursor to the previous line and sets the column to 0.
void setLine(int line)
Set the cursor line to line.
Definition movingapi.cpp:82
MovingCursor()
For inherited class only.
Definition movingapi.cpp:68
bool atEndOfLine() const
Determine if this cursor is located at the end of the current line.
Definition movingapi.cpp:99
virtual int column() const =0
Retrieve the column on which this cursor is situated.
bool atEndOfDocument() const
Determine if this cursor is located at the end of the last line in the document.
void setColumn(int column)
Set the cursor column to column.
Definition movingapi.cpp:88
bool isValidTextPosition() const
Check whether this MovingCursor is located at a valid text position.
virtual int line() const =0
Retrieve the line on which this cursor is situated.
bool atStartOfLine() const
Determine if this cursor is located at column 0 of a valid text line.
Definition movingapi.cpp:94
virtual void setPosition(KTextEditor::Cursor position)=0
Set the current cursor position to position.
MovingRangeFeedback()
Default constructor.
virtual void caretExitedRange(MovingRange *range, View *view)
The caret on view exited range.
virtual void mouseEnteredRange(MovingRange *range, View *view)
The mouse cursor on view entered range.
virtual void rangeInvalid(MovingRange *range)
The range is now invalid (ie.
virtual ~MovingRangeFeedback()
Virtual destructor.
virtual void rangeEmpty(MovingRange *range)
The range is now empty (ie.
virtual void caretEnteredRange(MovingRange *range, View *view)
The caret on view entered range.
virtual void mouseExitedRange(MovingRange *range, View *view)
The mouse cursor on view exited range.
A range that is bound to a specific Document, and maintains its position.
virtual const MovingCursor & start() const =0
Retrieve start cursor of this range, read-only.
virtual const MovingCursor & end() const =0
Retrieve end cursor of this range, read-only.
virtual ~MovingRange()
Destruct the moving range.
bool contains(const Range &range) const
Check whether the this range wholly encompasses range.
virtual void setRange(KTextEditor::Range range)=0
Set the range of this range.
MovingRange()
For inherited class only.
bool overlaps(const Range &range) const
Check whether the this range overlaps with range.
Definition movingapi.cpp:29
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.
A text widget with KXMLGUIClient that represents a Document.
Definition view.h:244
Q_SCRIPTABLE Q_NOREPLY void start()
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.
QDebug & nospace()
QDebug & space()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 11:56:00 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.