KTextEditor

katetextblock.h
1/*
2 SPDX-FileCopyrightText: 2010 Christoph Cullmann <cullmann@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#ifndef KATE_TEXTBLOCK_H
8#define KATE_TEXTBLOCK_H
9
10#include "katetextline.h"
11
12#include <QList>
13#include <QSet>
14#include <QVarLengthArray>
15
16#include <ktexteditor/cursor.h>
17#include <ktexteditor_export.h>
18
19namespace KTextEditor
20{
21class View;
22}
23
24namespace Kate
25{
26class TextBuffer;
27class TextCursor;
28class TextRange;
29
30/**
31 * Class representing a text block.
32 * This is used to build up a Kate::TextBuffer.
33 * This class should only be used by TextBuffer/Cursor/Range.
34 */
36{
37public:
38 /**
39 * Construct an empty text block.
40 * @param buffer parent text buffer
41 * @param startLine start line of this block
42 */
43 TextBlock(TextBuffer *buffer, int blockIndex);
44
45 /**
46 * Destruct the text block
47 */
48 ~TextBlock();
49
50 /**
51 * Start line of this block.
52 * @return start line of this block
53 */
54 KTEXTEDITOR_EXPORT int startLine() const;
55
56 void setBlockIndex(int index)
57 {
58 m_blockIndex = index;
59 }
60
61 /**
62 * Retrieve a text line.
63 * @param line wanted line number
64 * @return text line
65 */
66 TextLine line(int line) const;
67
68 /**
69 * Transfer all non text attributes for the given line from the given text line to the one in the block.
70 * @param line line number to set attributes
71 * @param textLine line reference to get attributes from
72 */
73 void setLineMetaData(int line, const TextLine &textLine);
74
75 /**
76 * Retrieve length for @p line.
77 * @param line wanted line number
78 * @return length of line
79 */
80 int lineLength(int line) const
81 {
82 Q_ASSERT(line >= startLine() && (line - startLine()) < lines());
83 return m_lines[line - startLine()].length();
84 }
85
86 /**
87 * Append a new line with given text.
88 * @param textOfLine text of the line to append
89 */
90 void appendLine(const QString &textOfLine);
91
92 /**
93 * Clear the lines.
94 */
95 void clearLines();
96
97 /**
98 * Number of lines in this block.
99 * @return number of lines
100 */
101 int lines() const
102 {
103 return static_cast<int>(m_lines.size());
104 }
105
106 /**
107 * Retrieve text of block.
108 * @param text for this block, lines separated by '\n'
109 */
110 void text(QString &text) const;
111
112 /**
113 * Wrap line at given cursor position.
114 * @param position line/column as cursor where to wrap
115 * @param fixStartLinesStartIndex start index to fix start lines, normally this is this block
116 */
117 void wrapLine(const KTextEditor::Cursor position, int fixStartLinesStartIndex);
118
119 /**
120 * Unwrap given line.
121 * @param line line to unwrap
122 * @param previousBlock previous block, if any, if we unwrap first line in block, we need to have this
123 * @param fixStartLinesStartIndex start index to fix start lines, normally this is this block or the previous one
124 */
125 void unwrapLine(int line, TextBlock *previousBlock, int fixStartLinesStartIndex);
126
127 /**
128 * Insert text at given cursor position.
129 * @param position position where to insert text
130 * @param text text to insert
131 */
132 void insertText(const KTextEditor::Cursor position, const QString &text);
133
134 /**
135 * Remove text at given range.
136 * @param range range of text to remove, must be on one line only.
137 * @param removedText will be filled with removed text
138 */
139 void removeText(KTextEditor::Range range, QString &removedText);
140
141 /**
142 * Debug output, print whole block content with line numbers and line length
143 * @param blockIndex index of this block in buffer
144 */
145 void debugPrint(int blockIndex) const;
146
147 /**
148 * Split given block. All lines starting from @p fromLine will
149 * be moved to it, together with the cursors belonging to it.
150 * @param fromLine line from which to split
151 * @param newBlock The block to which the data will be moved after splitting
152 */
153 void splitBlock(int fromLine, TextBlock *newBlock);
154
155 /**
156 * Merge this block with given one, the given one must be a direct predecessor.
157 * @param targetBlock block to merge with
158 */
159 void mergeBlock(TextBlock *targetBlock);
160
161 /**
162 * Delete the block content, delete all lines and delete all cursors not bound to ranges.
163 * This is used in destructor of TextBuffer, for fast cleanup. Only stuff remaining afterwards are cursors which are
164 * part of a range, TextBuffer will delete them itself...
165 */
166 void deleteBlockContent();
167
168 /**
169 * Clear the block content, delete all lines, move all cursors not bound to range to given block at 0,0.
170 * This is used by clear() of TextBuffer.
171 * @param targetBlock empty target block for cursors
172 */
173 void clearBlockContent(TextBlock *targetBlock);
174
175 /**
176 * Return all ranges in this block which might intersect the given line.
177 * @param line line to check intersection
178 * @param view only return ranges associated with given view
179 * @param rangesWithAttributeOnly ranges with attributes only?
180 * @return list of possible candidate ranges
181 */
182 KTEXTEDITOR_EXPORT QList<TextRange *> rangesForLine(int line, KTextEditor::View *view, bool rangesWithAttributeOnly) const;
183
184 KTEXTEDITOR_NO_EXPORT void rangesForLine(int line, KTextEditor::View *view, bool rangesWithAttributeOnly, QList<TextRange *> &outRanges) const;
185
186 /**
187 * Is the given range contained in this block?
188 * @param range range to check for
189 * @return contained in this blocks mapping?
190 */
191 bool containsRange(TextRange *range) const
192 {
193 return m_cachedLineForRanges.find(range) != m_cachedLineForRanges.end() || m_uncachedRanges.contains(range);
194 }
195
196 /**
197 * Flag all modified text lines as saved on disk.
198 */
200
201 /**
202 * Insert cursor into this block.
203 * @param cursor cursor to insert
204 */
206 {
207 m_cursors.insert(cursor);
208 }
209
210 /**
211 * Remove cursor from this block.
212 * @param cursor cursor to remove
213 */
215 {
216 m_cursors.remove(cursor);
217 }
218
219 /**
220 * Update a range from this block.
221 * Will move the range to right set, either cached for one-line ranges or not.
222 * @param range range to update
223 */
224 void updateRange(TextRange *range);
225
226 /**
227 * Remove a range from this block.
228 * @param range range to remove
229 */
230 void removeRange(TextRange *range);
231
232 /**
233 * Returns the size of this block i.e.,
234 * the count of QChars it has + number of new lines
235 */
236 int blockSize() const
237 {
238 return m_blockSize + m_lines.size();
239 }
240
241private:
242 /**
243 * Return all ranges in this block which might intersect the given line and only span one line.
244 * For them an internal fast lookup cache is hold.
245 * @param line line to check intersection
246 * @return set of ranges
247 */
248 const QVarLengthArray<TextRange *, 6> *cachedRangesForLine(int line) const
249 {
250 line -= startLine();
251 if (line >= 0 && (size_t)line < m_cachedRangesForLine.size()) {
252 return &m_cachedRangesForLine[line];
253 } else {
254 return nullptr;
255 }
256 }
257
258private:
259 /**
260 * parent text buffer
261 */
262 TextBuffer *m_buffer;
263
264 /**
265 * The index of this block in TextBuffer::m_blocks
266 */
267 int m_blockIndex;
268
269 /**
270 * Lines contained in this buffer.
271 * We need no sharing, use STL.
272 */
273 std::vector<Kate::TextLine> m_lines;
274
275 /**
276 * size of block i.e., number of QChars
277 */
278 int m_blockSize = 0;
279
280 /**
281 * Set of cursors for this block.
282 * using QSet is better than unordered_set for perf reasons
283 */
284 QSet<TextCursor *> m_cursors;
285
286 /**
287 * Contains for each line-offset the ranges that were cached into it.
288 * These ranges are fully contained by the line.
289 */
290 std::vector<QVarLengthArray<TextRange *, 6>> m_cachedRangesForLine;
291
292 /**
293 * Maps for each cached range the line into which the range was cached.
294 */
295 QHash<TextRange *, int> m_cachedLineForRanges;
296
297 /**
298 * This contains all the ranges that are not cached.
299 */
300 QVarLengthArray<TextRange *, 1> m_uncachedRanges;
301};
302
303}
304
305#endif
The Cursor represents a position in a Document.
Definition cursor.h:75
An object representing a section of text, from one Cursor to another.
A text widget with KXMLGUIClient that represents a Document.
Definition view.h:244
Class representing a text block.
void clearBlockContent(TextBlock *targetBlock)
Clear the block content, delete all lines, move all cursors not bound to range to given block at 0,...
void removeCursor(Kate::TextCursor *cursor)
Remove cursor from this block.
KTEXTEDITOR_EXPORT int startLine() const
Start line of this block.
void wrapLine(const KTextEditor::Cursor position, int fixStartLinesStartIndex)
Wrap line at given cursor position.
void deleteBlockContent()
Delete the block content, delete all lines and delete all cursors not bound to ranges.
int lineLength(int line) const
Retrieve length for line.
void insertText(const KTextEditor::Cursor position, const QString &text)
Insert text at given cursor position.
void mergeBlock(TextBlock *targetBlock)
Merge this block with given one, the given one must be a direct predecessor.
void removeText(KTextEditor::Range range, QString &removedText)
Remove text at given range.
KTEXTEDITOR_EXPORT QList< TextRange * > rangesForLine(int line, KTextEditor::View *view, bool rangesWithAttributeOnly) const
Return all ranges in this block which might intersect the given line.
void text(QString &text) const
Retrieve text of block.
void appendLine(const QString &textOfLine)
Append a new line with given text.
void setLineMetaData(int line, const TextLine &textLine)
Transfer all non text attributes for the given line from the given text line to the one in the block.
void clearLines()
Clear the lines.
~TextBlock()
Destruct the text block.
void markModifiedLinesAsSaved()
Flag all modified text lines as saved on disk.
TextLine line(int line) const
Retrieve a text line.
void updateRange(TextRange *range)
Update a range from this block.
void unwrapLine(int line, TextBlock *previousBlock, int fixStartLinesStartIndex)
Unwrap given line.
void splitBlock(int fromLine, TextBlock *newBlock)
Split given block.
bool containsRange(TextRange *range) const
Is the given range contained in this block?
TextBlock(TextBuffer *buffer, int blockIndex)
Construct an empty text block.
int lines() const
Number of lines in this block.
int blockSize() const
Returns the size of this block i.e., the count of QChars it has + number of new lines.
void debugPrint(int blockIndex) const
Debug output, print whole block content with line numbers and line length.
void removeRange(TextRange *range)
Remove a range from this block.
void insertCursor(Kate::TextCursor *cursor)
Insert cursor into this block.
Class representing a text buffer.
Class representing a 'clever' text cursor.
Class representing a single text line.
Class representing a 'clever' text range.
The KTextEditor namespace contains all the public API that is required to use the KTextEditor compone...
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Sep 6 2024 12:09:44 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.