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 * Append to outRanges addresses of all ranges in this block which might intersect the given line.
163 * @param line line to check intersection
164 * @param view only return ranges associated with given view
165 * @param rangesWithAttributeOnly ranges with attributes only?
166 * @param outRanges where to append results
167 */
168 KTEXTEDITOR_NO_EXPORT void rangesForLine(int line, KTextEditor::View *view, bool rangesWithAttributeOnly, QList<TextRange *> &outRanges) const;
169
170 /**
171 * Flag all modified text lines as saved on disk.
172 */
174
175 /**
176 * Insert cursor into this block.
177 * @param cursor cursor to insert
178 */
180 {
181 auto it = std::lower_bound(m_cursors.begin(), m_cursors.end(), cursor);
182 if (it == m_cursors.end() || cursor != *it) {
183 m_cursors.insert(it, cursor);
184 }
185 }
186
187 /**
188 * Remove cursor from this block.
189 * @param cursor cursor to remove
190 */
192 {
193 auto it = std::lower_bound(m_cursors.begin(), m_cursors.end(), cursor);
194 if (it != m_cursors.end() && cursor == *it) {
195 m_cursors.erase(it);
196 }
197 }
198
199private:
200 /**
201 * parent text buffer
202 */
203 TextBuffer *m_buffer;
204
205 /**
206 * The index of this block in TextBuffer::m_blocks
207 */
208 int m_blockIndex;
209
210 /**
211 * Lines contained in this buffer.
212 * We need no sharing, use STL.
213 */
214 std::vector<Kate::TextLine> m_lines;
215
216 /**
217 * Set of cursors for this block.
218 */
219 std::vector<TextCursor *> m_cursors;
220};
221}
222
223#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 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.
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_NO_EXPORT void rangesForLine(int line, KTextEditor::View *view, bool rangesWithAttributeOnly, QList< TextRange * > &outRanges) const
Append to outRanges addresses of 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 Sat Dec 21 2024 17:01:55 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.