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
14#include <ktexteditor/cursor.h>
15#include <ktexteditor_export.h>
16
17namespace KTextEditor
18{
19class View;
20}
21
22namespace Kate
23{
24class TextBuffer;
25class TextCursor;
26class TextRange;
27
28/**
29 * Class representing a text block.
30 * This is used to build up a Kate::TextBuffer.
31 * This class should only be used by TextBuffer/Cursor/Range.
32 */
34{
35 friend class TextBuffer;
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 * Flag all modified text lines as saved on disk.
188 */
190
191 /**
192 * Insert cursor into this block.
193 * @param cursor cursor to insert
194 */
196 {
197 auto it = std::lower_bound(m_cursors.begin(), m_cursors.end(), cursor);
198 if (it == m_cursors.end() || cursor != *it) {
199 m_cursors.insert(it, cursor);
200 }
201 }
202
203 /**
204 * Remove cursor from this block.
205 * @param cursor cursor to remove
206 */
208 {
209 auto it = std::lower_bound(m_cursors.begin(), m_cursors.end(), cursor);
210 if (it != m_cursors.end() && cursor == *it) {
211 m_cursors.erase(it);
212 }
213 }
214
215private:
216 /**
217 * parent text buffer
218 */
219 TextBuffer *m_buffer;
220
221 /**
222 * The index of this block in TextBuffer::m_blocks
223 */
224 int m_blockIndex;
225
226 /**
227 * Lines contained in this buffer.
228 * We need no sharing, use STL.
229 */
230 std::vector<Kate::TextLine> m_lines;
231
232 /**
233 * Set of cursors for this block.
234 */
235 std::vector<TextCursor *> m_cursors;
236};
237}
238
239#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 unwrapLine(int line, TextBlock *previousBlock, int fixStartLinesStartIndex)
Unwrap given line.
void splitBlock(int fromLine, TextBlock *newBlock)
Split given block.
TextBlock(TextBuffer *buffer, int blockIndex)
Construct an empty text block.
int lines() const
Number of lines in this block.
void debugPrint(int blockIndex) const
Debug output, print whole block content with line numbers and line length.
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.
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 Nov 1 2024 18:51:35 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.