KTextEditor

katetextline.h
1 /* SPDX-License-Identifier: LGPL-2.0-or-later
2 
3  Copyright (C) 2010 Christoph Cullmann <[email protected]>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #ifndef KATE_TEXTLINE_H
22 #define KATE_TEXTLINE_H
23 
24 #include <QSharedPointer>
25 #include <QString>
26 #include <QVector>
27 
28 #include <ktexteditor_export.h>
29 
30 #include <KSyntaxHighlighting/State>
31 
32 namespace Kate
33 {
39 class KTEXTEDITOR_EXPORT TextLineData
40 {
44  friend class TextBlock;
45 
46 public:
50  class Attribute
51  {
52  public:
59  explicit Attribute(int _offset = 0, int _length = 0, short _attributeValue = 0)
60  : offset(_offset)
61  , length(_length)
62  , attributeValue(_attributeValue)
63  {
64  }
65 
69  int offset;
70 
74  int length;
75 
80  };
81 
85  class Folding
86  {
87  public:
93  Folding(int _offset, int _foldingValue)
94  : offset(_offset)
95  , foldingValue(_foldingValue)
96  {
97  }
98 
102  int offset = 0;
103 
107  int foldingValue = 0;
108  };
109 
113  enum Flags { flagAutoWrapped = 1, flagFoldingStartAttribute = 2, flagFoldingStartIndentation = 4, flagLineModified = 8, flagLineSavedOnDisk = 16 };
114 
118  TextLineData();
119 
124  explicit TextLineData(const QString &text);
125 
129  ~TextLineData();
130 
135  const QString &text() const
136  {
137  return m_text;
138  }
139 
144  int firstChar() const;
145 
150  int lastChar() const;
151 
158  int nextNonSpaceChar(int pos) const;
159 
166  int previousNonSpaceChar(int pos) const;
167 
174  inline QChar at(int column) const
175  {
176  if (column >= 0 && column < m_text.length()) {
177  return m_text[column];
178  }
179 
180  return QChar();
181  }
182 
188  inline QChar operator[](int column) const
189  {
190  if (column >= 0 && column < m_text.length()) {
191  return m_text[column];
192  }
193 
194  return QChar();
195  }
196 
197  inline void markAsModified(bool modified)
198  {
199  if (modified) {
200  m_flags |= flagLineModified;
201  m_flags &= (~flagLineSavedOnDisk);
202  } else {
203  m_flags &= (~flagLineModified);
204  }
205  }
206 
207  inline bool markedAsModified() const
208  {
209  return m_flags & flagLineModified;
210  }
211 
212  inline void markAsSavedOnDisk(bool savedOnDisk)
213  {
214  if (savedOnDisk) {
215  m_flags |= flagLineSavedOnDisk;
216  m_flags &= (~flagLineModified);
217  } else {
218  m_flags &= (~flagLineSavedOnDisk);
219  }
220  }
221 
222  inline bool markedAsSavedOnDisk() const
223  {
224  return m_flags & flagLineSavedOnDisk;
225  }
226 
231  bool markedAsFoldingStart() const
232  {
233  return m_flags & (flagFoldingStartAttribute | flagFoldingStartIndentation);
234  }
235 
240  {
241  m_flags &= ~(flagFoldingStartAttribute | flagFoldingStartIndentation);
242  }
243 
249  {
250  return m_flags & flagFoldingStartAttribute;
251  }
252 
258  {
259  return m_flags & flagFoldingStartIndentation;
260  }
261 
266  {
267  clearMarkedAsFoldingStart();
268  m_flags |= flagFoldingStartAttribute;
269  }
270 
275  {
276  clearMarkedAsFoldingStart();
277  m_flags |= flagFoldingStartIndentation;
278  }
279 
283  int length() const
284  {
285  return m_text.length();
286  }
287 
293  bool isAutoWrapped() const
294  {
295  return m_flags & flagAutoWrapped;
296  }
297 
302  const QString &string() const
303  {
304  return m_text;
305  }
306 
313  QString string(int column, int length) const
314  {
315  return m_text.mid(column, length);
316  }
317 
322  QString leadingWhitespace() const;
323 
327  int indentDepth(int tabWidth) const;
328 
332  int toVirtualColumn(int column, int tabWidth) const;
333 
338  int fromVirtualColumn(int column, int tabWidth) const;
339 
343  int virtualLength(int tabWidth) const;
344 
349  bool matchesAt(int column, const QString &match) const;
350 
354  bool startsWith(const QString &match) const
355  {
356  return m_text.startsWith(match);
357  }
358 
362  bool endsWith(const QString &match) const
363  {
364  return m_text.endsWith(match);
365  }
366 
372  {
373  return m_highlightingState;
374  }
375 
381  {
382  m_highlightingState = val;
383  }
384 
389  void addAttribute(const Attribute &attribute);
390 
395  {
396  m_attributesList.clear();
397  m_foldings.clear();
398  }
399 
405  {
406  return m_attributesList;
407  }
408 
413  const std::vector<Folding> &foldings() const
414  {
415  return m_foldings;
416  }
417 
423  void addFolding(int offset, int folding)
424  {
425  m_foldings.emplace_back(offset, folding);
426  }
427 
435  short attribute(int pos) const;
436 
441  void setAutoWrapped(bool wrapped)
442  {
443  if (wrapped) {
444  m_flags = m_flags | flagAutoWrapped;
445  } else {
446  m_flags = m_flags & ~flagAutoWrapped;
447  }
448  }
449 
450 private:
456  QString &textReadWrite()
457  {
458  return m_text;
459  }
460 
461 private:
465  QString m_text;
466 
470  QVector<Attribute> m_attributesList;
471 
475  std::vector<Folding> m_foldings;
476 
480  KSyntaxHighlighting::State m_highlightingState;
481 
485  unsigned int m_flags = 0;
486 };
487 
492 
493 }
494 
495 #endif
QChar operator[](int column) const
Same as at().
Definition: katetextline.h:188
bool isAutoWrapped() const
Returns true, if the line was automagically wrapped, otherwise returns false.
Definition: katetextline.h:293
void addFolding(int offset, int folding)
Add new folding at end of foldings stored in this line.
Definition: katetextline.h:423
Attribute storage.
Definition: katetextline.h:50
const KSyntaxHighlighting::State & highlightingState() const
context stack
Definition: katetextline.h:371
int length() const
Returns the line&#39;s length.
Definition: katetextline.h:283
const std::vector< Folding > & foldings() const
Accessor to foldings.
Definition: katetextline.h:413
Class representing a single text line.
Definition: katetextline.h:39
Attribute(int _offset=0, int _length=0, short _attributeValue=0)
Attribute constructor.
Definition: katetextline.h:59
bool markedAsFoldingStart() const
Is on this line a folding start?
Definition: katetextline.h:231
Folding(int _offset, int _foldingValue)
Construct folding.
Definition: katetextline.h:93
void markAsFoldingStartIndentation()
Mark as folding start line of an indentation based folding.
Definition: katetextline.h:274
QChar at(int column) const
Returns the character at the given column.
Definition: katetextline.h:174
const QVector< Attribute > & attributesList() const
Accessor to attributes.
Definition: katetextline.h:404
void setHighlightingState(const KSyntaxHighlighting::State &val)
Sets the syntax highlight context number.
Definition: katetextline.h:380
void markAsFoldingStartAttribute()
Mark as folding start line of an attribute based folding.
Definition: katetextline.h:265
bool startsWith(const QString &match) const
Returns true, if the line starts with match, otherwise returns false.
Definition: katetextline.h:354
short attributeValue
attribute value (to encode type of this range)
Definition: katetextline.h:79
bool markedAsFoldingStartAttribute() const
Is on this line a folding start per attribute?
Definition: katetextline.h:248
bool endsWith(const QString &match) const
Returns true, if the line ends with match, otherwise returns false.
Definition: katetextline.h:362
QString mid(int position, int n) const const
void clearMarkedAsFoldingStart()
Clear folding start status.
Definition: katetextline.h:239
bool markedAsFoldingStartIndentation() const
Is on this line a folding start per indentation?
Definition: katetextline.h:257
Flags
Flags of TextLineData.
Definition: katetextline.h:113
void clearAttributesAndFoldings()
Clear attributes and foldings of this line.
Definition: katetextline.h:394
const QString & string() const
Returns the complete text line (as a QString reference).
Definition: katetextline.h:302
void setAutoWrapped(bool wrapped)
set auto-wrapped property
Definition: katetextline.h:441
const QString & text() const
Accessor to the text contained in this line.
Definition: katetextline.h:135
QString string(int column, int length) const
Returns the substring with length beginning at the given column.
Definition: katetextline.h:313
Class representing a text block.
Definition: katetextblock.h:44
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat Jul 11 2020 22:57:09 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.