File indexing completed on 2024-04-28 15:30:57

0001 /*
0002     SPDX-FileCopyrightText: 2010 Christoph Cullmann <cullmann@kde.org>
0003     SPDX-FileCopyrightText: 2010-2018 Dominik Haumann <dhaumann@kde.org>
0004 
0005     Based on code of the SmartCursor/Range by:
0006     SPDX-FileCopyrightText: 2003-2005 Hamish Rodda <rodda@kde.org>
0007 
0008     SPDX-License-Identifier: LGPL-2.0-or-later
0009 */
0010 
0011 #include "movingcursor.h"
0012 #include "document.h"
0013 #include "documentcursor.h"
0014 
0015 using namespace KTextEditor;
0016 
0017 MovingCursor::MovingCursor()
0018 {
0019 }
0020 
0021 MovingCursor::~MovingCursor()
0022 {
0023 }
0024 
0025 void MovingCursor::setPosition(int line, int column)
0026 {
0027     // just use setPosition
0028     setPosition(Cursor(line, column));
0029 }
0030 
0031 void MovingCursor::setLine(int line)
0032 {
0033     // just use setPosition
0034     setPosition(line, column());
0035 }
0036 
0037 void MovingCursor::setColumn(int column)
0038 {
0039     // just use setPosition
0040     setPosition(line(), column);
0041 }
0042 
0043 bool MovingCursor::atStartOfLine() const
0044 {
0045     return isValidTextPosition() && column() == 0;
0046 }
0047 
0048 bool MovingCursor::atEndOfLine() const
0049 {
0050     return isValidTextPosition() && column() == document()->lineLength(line());
0051 }
0052 
0053 bool MovingCursor::atEndOfDocument() const
0054 {
0055     return *this == document()->documentEnd();
0056 }
0057 
0058 bool MovingCursor::atStartOfDocument() const
0059 {
0060     return line() == 0 && column() == 0;
0061 }
0062 
0063 bool MovingCursor::gotoNextLine()
0064 {
0065     // only touch valid cursors
0066     const bool ok = isValid() && (line() + 1 < document()->lines());
0067 
0068     if (ok) {
0069         setPosition(Cursor(line() + 1, 0));
0070     }
0071 
0072     return ok;
0073 }
0074 
0075 bool MovingCursor::gotoPreviousLine()
0076 {
0077     // only touch valid cursors
0078     bool ok = (line() > 0) && (column() >= 0);
0079 
0080     if (ok) {
0081         setPosition(Cursor(line() - 1, 0));
0082     }
0083 
0084     return ok;
0085 }
0086 
0087 bool MovingCursor::move(int chars, WrapBehavior wrapBehavior)
0088 {
0089     DocumentCursor dc(document(), toCursor());
0090 
0091     const bool success = dc.move(chars, static_cast<DocumentCursor::WrapBehavior>(wrapBehavior));
0092 
0093     if (success && dc.toCursor() != toCursor()) {
0094         setPosition(dc.toCursor());
0095     }
0096 
0097     return success;
0098 }