KTextEditor

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

KDE's Doxygen guidelines are available online.