KTextEditor

katetextcursor.cpp
1 /*
2  SPDX-FileCopyrightText: 2010 Christoph Cullmann <[email protected]>
3 
4  Based on code of the SmartCursor/Range by:
5  SPDX-FileCopyrightText: 2003-2005 Hamish Rodda <[email protected]>
6 
7  SPDX-License-Identifier: LGPL-2.0-or-later
8 */
9 
10 #include "katetextcursor.h"
11 #include "katedocument.h"
12 #include "katetextbuffer.h"
13 #include "katetextrange.h"
14 
15 namespace Kate
16 {
17 TextCursor::TextCursor(TextBuffer &buffer, const KTextEditor::Cursor position, InsertBehavior insertBehavior)
18  : m_buffer(buffer)
19  , m_range(nullptr)
20  , m_block(nullptr)
21  , m_line(-1)
22  , m_column(-1)
23  , m_moveOnInsert(insertBehavior == MoveOnInsert)
24 {
25  // init position
26  setPosition(position, true);
27 }
28 
29 TextCursor::TextCursor(TextBuffer &buffer, TextRange *range, const KTextEditor::Cursor position, InsertBehavior insertBehavior)
30  : m_buffer(buffer)
31  , m_range(range)
32  , m_block(nullptr)
33  , m_line(-1)
34  , m_column(-1)
35  , m_moveOnInsert(insertBehavior == MoveOnInsert)
36 {
37  // init position
38  setPosition(position, true);
39 }
40 
42 {
43  // remove cursor from block or buffer
44  if (m_block) {
45  m_block->removeCursor(this);
46  }
47 
48  // only cursors without range are here!
49  else if (!m_range) {
50  m_buffer.m_invalidCursors.remove(this);
51  }
52 }
53 
54 void TextCursor::setPosition(const TextCursor &position)
55 {
56  if (m_block && m_block != position.m_block) {
57  m_block->removeCursor(this);
58  }
59 
60  m_line = position.m_line;
61  m_column = position.m_column;
62 
63  m_block = position.m_block;
64  if (m_block) {
65  m_block->insertCursor(this);
66  }
67 }
68 
69 void TextCursor::setPosition(const KTextEditor::Cursor &position, bool init)
70 {
71  // any change or init? else do nothing
72  if (!init && position.line() == line()) {
73  // simple case: 1:1 equal
74  if (position.column() == m_column) {
75  return;
76  }
77 
78  // ok, too: both old and new column are valid, we can just adjust the column and be done
79  if (position.column() >= 0 && m_column >= 0) {
80  m_column = position.column();
81  return;
82  }
83 
84  // else: we need to handle the change in a more complex way, new or old column are not valid!
85  }
86 
87  // remove cursor from old block in any case
88  if (m_block) {
89  m_block->removeCursor(this);
90  }
91 
92  // first: validate the line and column, else invalid
93  if (position.column() < 0 || position.line() < 0 || position.line() >= m_buffer.lines()) {
94  if (!m_range) {
95  m_buffer.m_invalidCursors.insert(this);
96  }
97  m_block = nullptr;
98  m_line = m_column = -1;
99  return;
100  }
101 
102  // else, find block
103  TextBlock *block = m_buffer.blockForIndex(m_buffer.blockForLine(position.line()));
104  Q_ASSERT(block);
105 
106  // if cursor was invalid before, remove it from invalid cursor list
107  if (!m_range && !m_block && !init) {
108  Q_ASSERT(m_buffer.m_invalidCursors.contains(this));
109  m_buffer.m_invalidCursors.remove(this);
110  }
111 
112  // else: valid cursor
113  m_block = block;
114  m_line = position.line() - m_block->startLine();
115  m_column = position.column();
116  m_block->insertCursor(this);
117 }
118 
120 {
121  setPosition(position, false);
122 }
123 
124 int TextCursor::line() const
125 {
126  // invalid cursor have no block
127  if (!m_block) {
128  return -1;
129  }
130 
131  // else, calculate real line
132  return m_block->startLine() + m_line;
133 }
134 
136 {
137  return m_buffer.document();
138 }
139 
141 {
142  return m_range;
143 }
144 
145 }
constexpr int column() const Q_DECL_NOEXCEPT
Retrieve the column on which this cursor is situated.
Definition: cursor.h:215
Class representing a 'clever' text cursor.
void insertCursor(Kate::TextCursor *cursor)
Insert cursor into this block.
Class representing a text buffer.
InsertBehavior
Insert behavior of this cursor, should it stay if text is insert at its position or should it move.
Definition: movingcursor.h:64
int line() const override
Retrieve the line on which this cursor is situated.
The Cursor represents a position in a Document.
Definition: cursor.h:71
KTextEditor::Document * document() const override
Gets the document to which this cursor is bound.
~TextCursor() override
Destruct the text cursor.
void setPosition(const TextCursor &position)
Fast way to set the current cursor position to position.
Class representing a 'clever' text range.
Definition: katetextrange.h:36
int lines() const
Lines currently stored in this buffer.
int startLine() const
Start line of this block.
Definition: katetextblock.h:59
A range that is bound to a specific Document, and maintains its position.
Definition: movingrange.h:144
KTextEditor::MovingRange * range() const override
Get range this cursor belongs to, if any.
TextBlock * block() const
Get block this cursor belongs to, if any.
constexpr int line() const Q_DECL_NOEXCEPT
Retrieve the line on which this cursor is situated.
Definition: cursor.h:197
void removeCursor(Kate::TextCursor *cursor)
Remove cursor from this block.
A KParts derived class representing a text document.
Definition: document.h:185
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Feb 7 2023 03:56:05 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.