KTextEditor

katetextcursor.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 "katetextcursor.h"
11#include "katedocument.h"
12#include "katetextblock.h"
13#include "katetextbuffer.h"
14#include "katetextrange.h"
15
16namespace Kate
17{
18TextCursor::TextCursor(TextBuffer &buffer, const KTextEditor::Cursor position, InsertBehavior insertBehavior)
19 : m_buffer(buffer)
20 , m_moveOnInsert(insertBehavior == MoveOnInsert)
21{
22 // init position
23 setPosition(position, true);
24}
25
26TextCursor::TextCursor(TextBuffer &buffer, TextRange *range, KTextEditor::Cursor position, InsertBehavior insertBehavior)
27 : m_buffer(buffer)
28 , m_range(range)
29 , m_moveOnInsert(insertBehavior == MoveOnInsert)
30{
31 // init position
32 setPosition(position, true);
33}
34
36{
37 // remove cursor from block or buffer
38 if (m_block) {
39 m_block->removeCursor(this);
40 }
41
42 // only cursors without range are here!
43 else if (!m_range) {
44 m_buffer.m_invalidCursors.remove(this);
45 }
46}
47
49{
50 if (m_block && m_block != position.m_block) {
51 m_block->removeCursor(this);
52 }
53
54 m_line = position.m_line;
55 m_column = position.m_column;
56
57 m_block = position.m_block;
58 if (m_block) {
59 m_block->insertCursor(this);
60 }
61}
62
63void TextCursor::setPosition(KTextEditor::Cursor position, bool init)
64{
65 // any change or init? else do nothing
66 if (!init && position.line() == line()) {
67 // simple case: 1:1 equal
68 if (position.column() == m_column) {
69 return;
70 }
71
72 // ok, too: both old and new column are valid, we can just adjust the column and be done
73 if (position.column() >= 0 && m_column >= 0) {
74 m_column = position.column();
75 return;
76 }
77
78 // else: we need to handle the change in a more complex way, new or old column are not valid!
79 }
80
81 // first: validate the line and column, else invalid
82 if (!position.isValid() || position.line() >= m_buffer.lines()) {
83 if (!m_range) {
84 m_buffer.m_invalidCursors.insert(this);
85 }
86 if (m_block) {
87 m_block->removeCursor(this);
88 }
89 m_block = nullptr;
90 m_line = m_column = -1;
91 return;
92 }
93
94 // find new block if m_block doesn't contain the line or if the block is null
95 TextBlock *oldBlock = m_block;
96 int startLine = oldBlock ? oldBlock->startLine() : -1;
97 if (!oldBlock || position.line() < startLine || position.line() >= startLine + oldBlock->lines()) {
98 if (oldBlock) {
99 oldBlock->removeCursor(this);
100 }
101 m_block = m_buffer.m_blocks[m_buffer.blockForLine(position.line())];
102 Q_ASSERT(m_block);
103 m_block->insertCursor(this);
104 startLine = m_block->startLine();
105 }
106
107 // if cursor was invalid before, remove it from invalid cursor list
108 if (!m_range && !oldBlock && !init) {
109 Q_ASSERT(m_buffer.m_invalidCursors.contains(this));
110 m_buffer.m_invalidCursors.remove(this);
111 }
112
113 // else: valid cursor
114 m_line = position.line() - startLine;
115 m_column = position.column();
116}
117
119{
120 return m_buffer.document();
121}
122
124{
125 return m_range;
126}
127
129{
130 setPosition(position, false);
131}
132
133void Kate::TextCursor::setPosition(int line, int column)
134{
135 setPosition(KTextEditor::Cursor(line, column), false);
136}
137}
The Cursor represents a position in a Document.
Definition cursor.h:75
constexpr int column() const noexcept
Retrieve the column on which this cursor is situated.
Definition cursor.h:192
constexpr bool isValid() const noexcept
Returns whether the current position of this cursor is a valid position (line + column must both be >...
Definition cursor.h:102
constexpr int line() const noexcept
Retrieve the line on which this cursor is situated.
Definition cursor.h:174
A KParts derived class representing a text document.
Definition document.h:284
InsertBehavior
Insert behavior of this cursor, should it stay if text is insert at its position or should it move.
A range that is bound to a specific Document, and maintains its position.
void removeCursor(Kate::TextCursor *cursor)
Remove cursor from this block.
int startLine() const
Start line of this block.
void insertCursor(Kate::TextCursor *cursor)
Insert cursor into this block.
Class representing a text buffer.
int lines() const
Lines currently stored in this buffer.
Class representing a 'clever' text cursor.
KTextEditor::MovingRange * range() const override
Get range this cursor belongs to, if any.
KTextEditor::Document * document() const override
Gets the document to which this cursor is bound.
void setPosition(const TextCursor &position)
Fast way to set the current cursor position to position.
int line() const override
Retrieve the line on which this cursor is situated.
~TextCursor() override
Destruct the text cursor.
Class representing a 'clever' text range.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:15:43 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.