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

KDE's Doxygen guidelines are available online.