KTextEditor

katetextcursor.h
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 #ifndef KATE_TEXTCURSOR_H
11 #define KATE_TEXTCURSOR_H
12 
13 #include <ktexteditor/movingcursor.h>
14 
15 #include "katetextblock.h"
16 #include <ktexteditor_export.h>
17 
18 namespace Kate
19 {
20 class TextBuffer;
21 class TextBlock;
22 class TextRange;
23 
24 /**
25  * Class representing a 'clever' text cursor.
26  * It will automagically move if the text inside the buffer it belongs to is modified.
27  * By intention no subclass of KTextEditor::Cursor, must be converted manually.
28  */
29 class KTEXTEDITOR_EXPORT TextCursor final : public KTextEditor::MovingCursor
30 {
31  // range wants direct access to some internals
32  friend class TextRange;
33 
34  // this is a friend, because this is needed to efficiently transfer cursors from on to an other block
35  friend class TextBlock;
36 
37 private:
38  /**
39  * Construct a text cursor with given range as parent, private, used by TextRange constructor only.
40  * @param buffer text buffer this cursor belongs to
41  * @param range text range this cursor is part of
42  * @param position wanted cursor position, if not valid for given buffer, will lead to invalid cursor
43  * @param insertBehavior behavior of this cursor on insert of text at its position
44  */
45  TextCursor(TextBuffer &buffer, TextRange *range, const KTextEditor::Cursor position, InsertBehavior insertBehavior);
46 
47 public:
48  /**
49  * Construct a text cursor.
50  * @param buffer text buffer this cursor belongs to
51  * @param position wanted cursor position, if not valid for given buffer, will lead to invalid cursor
52  * @param insertBehavior behavior of this cursor on insert of text at its position
53  */
54  TextCursor(TextBuffer &buffer, const KTextEditor::Cursor position, InsertBehavior insertBehavior);
55 
56  /**
57  * Destruct the text cursor
58  */
59  ~TextCursor() override;
60 
61  /**
62  * Set insert behavior.
63  * @param insertBehavior new insert behavior
64  */
65  void setInsertBehavior(InsertBehavior insertBehavior) override
66  {
67  m_moveOnInsert = insertBehavior == MoveOnInsert;
68  }
69 
70  /**
71  * Get current insert behavior.
72  * @return current insert behavior
73  */
74  InsertBehavior insertBehavior() const override
75  {
76  return m_moveOnInsert ? MoveOnInsert : StayOnInsert;
77  }
78 
79  /**
80  * Gets the document to which this cursor is bound.
81  * \return a pointer to the document
82  */
83  KTextEditor::Document *document() const override;
84 
85  /**
86  * Fast way to set the current cursor position to \e position.
87  *
88  * \param position new cursor position
89  */
90  void setPosition(const TextCursor &position);
91 
92  /**
93  * Set the current cursor position to \e position.
94  *
95  * \param position new cursor position
96  */
97  void setPosition(const KTextEditor::Cursor &position) override;
98 
99  /**
100  * \overload
101  *
102  * Set the cursor position to \e line and \e column.
103  *
104  * \param line new cursor line
105  * \param column new cursor column
106  */
107  void setPosition(int line, int column)
108  {
110  }
111 
112  /**
113  * Retrieve the line on which this cursor is situated.
114  * \return line number, where 0 is the first line.
115  */
116  int line() const override;
117 
118  /**
119  * Non-virtual version of line(), which is faster.
120  * Inlined for fast access (especially in KateTextBuffer::rangesForLine
121  * \return line number, where 0 is the first line.
122  */
123  int lineInternal() const
124  {
125  // invalid cursor have no block
126  if (!m_block) {
127  return -1;
128  }
129 
130  // else, calculate real line
131  return m_block->startLine() + m_line;
132  }
133 
134  /**
135  * Retrieve the column on which this cursor is situated.
136  * \return column number, where 0 is the first column.
137  */
138  int column() const override
139  {
140  return m_column;
141  }
142 
143  /**
144  * Non-virtual version of column(), which is faster.
145  * \return column number, where 0 is the first column.
146  * */
147  int columnInternal() const
148  {
149  return m_column;
150  }
151 
152  /**
153  * Get range this cursor belongs to, if any
154  * @return range this pointer is part of, else 0
155  */
156  KTextEditor::MovingRange *range() const override;
157 
158  /**
159  * Get range this cursor belongs to, if any
160  * @return range this pointer is part of, else 0
161  */
163  {
164  return m_range;
165  }
166 
167  /**
168  * Get block this cursor belongs to, if any
169  * @return block this pointer is part of, else 0
170  */
171  TextBlock *block() const
172  {
173  return m_block;
174  }
175 
176  /**
177  * Get offset into block this cursor belongs to, if any
178  * @return offset into block this pointer is part of, else -1
179  */
180  int lineInBlock() const
181  {
182  if (m_block) {
183  return m_line;
184  }
185  return -1;
186  }
187 
188 private:
189  /**
190  * no copy constructor, don't allow this to be copied.
191  */
192  TextCursor(const TextCursor &);
193 
194  /**
195  * no assignment operator, no copying around.
196  */
197  TextCursor &operator=(const TextCursor &);
198 
199  /**
200  * Set the current cursor position to \e position.
201  * Internal helper to allow the same code be used for constructor and
202  * setPosition.
203  *
204  * @param position new cursor position
205  * @param init is this the initial setup of the position in the constructor?
206  */
207  void setPosition(const KTextEditor::Cursor &position, bool init);
208 
209 private:
210  /**
211  * parent text buffer
212  * is a reference, and no pointer, as this must always exist and can't change
213  */
214  TextBuffer &m_buffer;
215 
216  /**
217  * range this cursor belongs to
218  * may be null, then no range owns this cursor
219  * can not change after initial assignment
220  */
221  TextRange *const m_range;
222 
223  /**
224  * parent text block, valid cursors always belong to a block, else they are invalid.
225  */
226  TextBlock *m_block;
227 
228  /**
229  * line, offset in block, or -1
230  */
231  int m_line;
232 
233  /**
234  * column
235  */
236  int m_column;
237 
238  /**
239  * should this cursor move on insert
240  */
241  bool m_moveOnInsert;
242 };
243 
244 }
245 
246 #endif
int columnInternal() const
Non-virtual version of column(), which is faster.
Class representing a 'clever' text cursor.
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 lineInternal() const
Non-virtual version of line(), which is faster.
int lineInBlock() const
Get offset into block this cursor belongs to, if any.
void setInsertBehavior(InsertBehavior insertBehavior) override
Set insert behavior.
The Cursor represents a position in a Document.
Definition: cursor.h:71
void setPosition(int line, int column)
This is an overloaded member function, provided for convenience. It differs from the above function o...
int column() const override
Retrieve the column on which this cursor is situated.
Class representing a 'clever' text range.
Definition: katetextrange.h:36
Class representing a text block.
Definition: katetextblock.h:40
A range that is bound to a specific Document, and maintains its position.
Definition: movingrange.h:144
TextBlock * block() const
Get block this cursor belongs to, if any.
virtual void setPosition(const KTextEditor::Cursor &position)=0
Set the current cursor position to position.
Kate::TextRange * kateRange() const
Get range this cursor belongs to, if any.
A Cursor which is bound to a specific Document, and maintains its position.
Definition: movingcursor.h:54
InsertBehavior insertBehavior() const override
Get current insert behavior.
A KParts derived class representing a text document.
Definition: document.h:185
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Mon Aug 15 2022 03:55:03 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.