File indexing completed on 2024-04-28 03:57:54

0001 /*
0002     SPDX-FileCopyrightText: 2010 Christoph Cullmann <cullmann@kde.org>
0003 
0004     Based on code of the SmartCursor/Range by:
0005     SPDX-FileCopyrightText: 2003-2005 Hamish Rodda <rodda@kde.org>
0006 
0007     SPDX-License-Identifier: LGPL-2.0-or-later
0008 */
0009 
0010 #include "document.h"
0011 #include "documentcursor.h"
0012 #include "movingcursor.h"
0013 #include "movingrange.h"
0014 #include "movingrangefeedback.h"
0015 
0016 using namespace KTextEditor;
0017 
0018 // BEGIN MovingRange
0019 MovingRange::MovingRange() = default;
0020 
0021 MovingRange::~MovingRange() = default;
0022 
0023 void MovingRange::setRange(Cursor start, Cursor end)
0024 {
0025     // just use other function, KTextEditor::Range will handle some normalization
0026     setRange(Range(start, end));
0027 }
0028 
0029 bool MovingRange::overlaps(const Range &range) const
0030 {
0031     if (range.start() <= start()) {
0032         return range.end() > start();
0033     }
0034 
0035     else if (range.end() >= end()) {
0036         return range.start() < end();
0037     }
0038 
0039     else {
0040         return contains(range);
0041     }
0042 }
0043 
0044 QDebug KTextEditor::operator<<(QDebug s, const MovingRange *range)
0045 {
0046     if (range) {
0047         s << "[" << range->start() << " -> " << range->end() << "]";
0048     } else {
0049         s << "(null range)";
0050     }
0051     return s.space();
0052 }
0053 
0054 /**
0055  * qDebug() stream operator. Writes this range to the debug output in a nicely formatted way.
0056  * @param s debug stream
0057  * @param range range to print
0058  * @return debug stream
0059  */
0060 QDebug KTextEditor::operator<<(QDebug s, const MovingRange &range)
0061 {
0062     return s << &range;
0063 }
0064 // END MovingRange
0065 
0066 // BEGIN MovingCursor
0067 
0068 MovingCursor::MovingCursor()
0069 {
0070 }
0071 
0072 MovingCursor::~MovingCursor()
0073 {
0074 }
0075 
0076 void MovingCursor::setPosition(int line, int column)
0077 {
0078     // just use setPosition
0079     setPosition(Cursor(line, column));
0080 }
0081 
0082 void MovingCursor::setLine(int line)
0083 {
0084     // just use setPosition
0085     setPosition(line, column());
0086 }
0087 
0088 void MovingCursor::setColumn(int column)
0089 {
0090     // just use setPosition
0091     setPosition(line(), column);
0092 }
0093 
0094 bool MovingCursor::atStartOfLine() const
0095 {
0096     return isValidTextPosition() && column() == 0;
0097 }
0098 
0099 bool MovingCursor::atEndOfLine() const
0100 {
0101     return isValidTextPosition() && column() == document()->lineLength(line());
0102 }
0103 
0104 bool MovingCursor::atEndOfDocument() const
0105 {
0106     return *this == document()->documentEnd();
0107 }
0108 
0109 bool MovingCursor::atStartOfDocument() const
0110 {
0111     return line() == 0 && column() == 0;
0112 }
0113 
0114 bool MovingCursor::gotoNextLine()
0115 {
0116     // only touch valid cursors
0117     const bool ok = isValid() && (line() + 1 < document()->lines());
0118 
0119     if (ok) {
0120         setPosition(Cursor(line() + 1, 0));
0121     }
0122 
0123     return ok;
0124 }
0125 
0126 bool MovingCursor::gotoPreviousLine()
0127 {
0128     // only touch valid cursors
0129     bool ok = (line() > 0) && (column() >= 0);
0130 
0131     if (ok) {
0132         setPosition(Cursor(line() - 1, 0));
0133     }
0134 
0135     return ok;
0136 }
0137 
0138 bool MovingCursor::move(int chars, WrapBehavior wrapBehavior)
0139 {
0140     DocumentCursor dc(document(), toCursor());
0141 
0142     const bool success = dc.move(chars, static_cast<DocumentCursor::WrapBehavior>(wrapBehavior));
0143 
0144     if (success && dc.toCursor() != toCursor()) {
0145         setPosition(dc.toCursor());
0146     }
0147 
0148     return success;
0149 }
0150 
0151 bool MovingCursor::isValidTextPosition() const
0152 {
0153     return document()->isValidTextPosition(toCursor());
0154 }
0155 
0156 QDebug KTextEditor::operator<<(QDebug s, const MovingCursor *cursor)
0157 {
0158     if (cursor) {
0159         s.nospace() << "(" << cursor->line() << ", " << cursor->column() << ")";
0160     } else {
0161         s.nospace() << "(null cursor)";
0162     }
0163     return s.space();
0164 }
0165 
0166 QDebug KTextEditor::operator<<(QDebug s, const MovingCursor &cursor)
0167 {
0168     return s << &cursor;
0169 }
0170 // END MovingCursor
0171 
0172 // BEGIN MovingRangeFeedback
0173 MovingRangeFeedback::MovingRangeFeedback() = default;
0174 
0175 MovingRangeFeedback::~MovingRangeFeedback() = default;
0176 
0177 void MovingRangeFeedback::rangeEmpty(MovingRange *)
0178 {
0179 }
0180 
0181 void MovingRangeFeedback::rangeInvalid(MovingRange *)
0182 {
0183 }
0184 
0185 void MovingRangeFeedback::mouseEnteredRange(MovingRange *, View *)
0186 {
0187 }
0188 
0189 void MovingRangeFeedback::mouseExitedRange(MovingRange *, View *)
0190 {
0191 }
0192 
0193 void MovingRangeFeedback::caretEnteredRange(MovingRange *, View *)
0194 {
0195 }
0196 
0197 void MovingRangeFeedback::caretExitedRange(MovingRange *, View *)
0198 {
0199 }
0200 // END MovingRangeFeedback