• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

Kate

  • kde-4.14
  • applications
  • kate
  • part
  • buffer
katetextline.cpp
Go to the documentation of this file.
1 /* This file is part of the Kate project.
2  *
3  * Copyright (C) 2010 Christoph Cullmann <cullmann@kde.org>
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 #include "katetextline.h"
22 
23 namespace Kate {
24 
25 TextLineData::TextLineData ()
26  : m_flags (0)
27 {
28 }
29 
30 TextLineData::TextLineData (const QString &text)
31  : m_text (text)
32  , m_flags (0)
33 {
34 }
35 
36 TextLineData::~TextLineData ()
37 {
38 }
39 
40 int TextLineData::firstChar() const
41 {
42  return nextNonSpaceChar(0);
43 }
44 
45 int TextLineData::lastChar() const
46 {
47  return previousNonSpaceChar(m_text.length() - 1);
48 }
49 
50 int TextLineData::nextNonSpaceChar (int pos) const
51 {
52  Q_ASSERT (pos >= 0);
53 
54  for(int i = pos; i < m_text.length(); i++)
55  if (!m_text[i].isSpace())
56  return i;
57 
58  return -1;
59 }
60 
61 int TextLineData::previousNonSpaceChar (int pos) const
62 {
63  if (pos >= m_text.length())
64  pos = m_text.length() - 1;
65 
66  for(int i = pos; i >= 0; i--)
67  if (!m_text[i].isSpace())
68  return i;
69 
70  return -1;
71 }
72 
73 QString TextLineData::leadingWhitespace() const
74 {
75  if (firstChar() < 0)
76  return string(0, length());
77 
78  return string(0, firstChar());
79 }
80 
81 int TextLineData::indentDepth (int tabWidth) const
82 {
83  int d = 0;
84  const int len = m_text.length();
85  const QChar *unicode = m_text.unicode();
86 
87  for(int i = 0; i < len; ++i)
88  {
89  if(unicode[i].isSpace())
90  {
91  if (unicode[i] == QLatin1Char('\t'))
92  d += tabWidth - (d % tabWidth);
93  else
94  d++;
95  }
96  else
97  return d;
98  }
99 
100  return d;
101 }
102 
103 bool TextLineData::matchesAt(int column, const QString& match) const
104 {
105  if (column < 0)
106  return false;
107 
108  const int len = m_text.length();
109  const int matchlen = match.length();
110 
111  if ((column + matchlen) > len)
112  return false;
113 
114  const QChar *unicode = m_text.unicode();
115  const QChar *matchUnicode = match.unicode();
116 
117  for (int i=0; i < matchlen; ++i)
118  if (unicode[i+column] != matchUnicode[i])
119  return false;
120 
121  return true;
122 }
123 
124 int TextLineData::toVirtualColumn (int column, int tabWidth) const
125 {
126  if (column < 0)
127  return 0;
128 
129  int x = 0;
130  const int zmax = qMin(column, m_text.length());
131  const QChar *unicode = m_text.unicode();
132 
133  for ( int z = 0; z < zmax; ++z)
134  {
135  if (unicode[z] == QLatin1Char('\t'))
136  x += tabWidth - (x % tabWidth);
137  else
138  x++;
139  }
140 
141  return x + column - zmax;
142 }
143 
144 int TextLineData::fromVirtualColumn (int column, int tabWidth) const
145 {
146  if (column < 0)
147  return 0;
148 
149  const int zmax = qMin(m_text.length(), column);
150  const QChar *unicode = m_text.unicode();
151 
152  int x = 0;
153  int z = 0;
154  for (; z < zmax; ++z)
155  {
156  int diff = 1;
157  if (unicode[z] == QLatin1Char('\t'))
158  diff = tabWidth - (x % tabWidth);
159 
160  if (x + diff > column)
161  break;
162  x += diff;
163  }
164 
165  return z + qMax(column - x, 0);
166 }
167 
168 int TextLineData::virtualLength (int tabWidth) const
169 {
170  int x = 0;
171  const int len = m_text.length();
172  const QChar *unicode = m_text.unicode();
173 
174  for ( int z = 0; z < len; ++z)
175  {
176  if (unicode[z] == QLatin1Char('\t'))
177  x += tabWidth - (x % tabWidth);
178  else
179  x++;
180  }
181 
182  return x;
183 }
184 
185 void TextLineData::addAttribute (const Attribute &attribute)
186 {
187  // try to append to previous range, if no folding info + same attribute value
188  if ((attribute.foldingValue == 0) && !m_attributesList.isEmpty() && (m_attributesList.back().foldingValue == 0)
189  && (m_attributesList.back().attributeValue == attribute.attributeValue)
190  && ((m_attributesList.back().offset + m_attributesList.back().length) == attribute.offset))
191  {
192  m_attributesList.back().length += attribute.length;
193  return;
194  }
195 
196  m_attributesList.append (attribute);
197 }
198 
199 }
Kate::TextLineData::nextNonSpaceChar
int nextNonSpaceChar(int pos) const
Find the position of the next char that is not a space.
Definition: katetextline.cpp:50
katetextline.h
Kate::TextLineData::fromVirtualColumn
int fromVirtualColumn(int column, int tabWidth) const
Returns the "real" column where each tab only counts one character.
Definition: katetextline.cpp:144
Kate::TextLineData::indentDepth
int indentDepth(int tabWidth) const
Returns the indentation depth with each tab expanded into tabWidth characters.
Definition: katetextline.cpp:81
Kate::TextLineData::Attribute
Attribute storage.
Definition: katetextline.h:52
Kate::TextLineData::Attribute::foldingValue
short foldingValue
folding value (begin/end type)
Definition: katetextline.h:87
QChar
Kate::TextLineData::TextLineData
TextLineData()
Construct an empty text line.
Definition: katetextline.cpp:25
Kate::TextLineData::Attribute::length
int length
length
Definition: katetextline.h:77
Kate::TextLineData::length
int length() const
Returns the line's length.
Definition: katetextline.h:266
Kate::TextLineData::addAttribute
void addAttribute(const Attribute &attribute)
Add attribute to this line.
Definition: katetextline.cpp:185
Kate::TextLineData::~TextLineData
~TextLineData()
Destruct the text line.
Definition: katetextline.cpp:36
Kate::TextLineData::leadingWhitespace
QString leadingWhitespace() const
Leading whitespace of this line.
Definition: katetextline.cpp:73
QString
Kate::TextLineData::Attribute::attributeValue
short attributeValue
attribute value (to encode type of this range)
Definition: katetextline.h:82
QLatin1Char
Kate::TextLineData::virtualLength
int virtualLength(int tabWidth) const
Returns the text length with each tab expanded into tabWidth characters.
Definition: katetextline.cpp:168
Kate::TextLineData::toVirtualColumn
int toVirtualColumn(int column, int tabWidth) const
Returns the column with each tab expanded into tabWidth characters.
Definition: katetextline.cpp:124
Kate::TextLineData::previousNonSpaceChar
int previousNonSpaceChar(int pos) const
Find the position of the previous char that is not a space.
Definition: katetextline.cpp:61
QString::unicode
const QChar * unicode() const
Kate::TextLineData::firstChar
int firstChar() const
Returns the position of the first non-whitespace character.
Definition: katetextline.cpp:40
QString::length
int length() const
Kate::TextLineData::Attribute::offset
int offset
offset
Definition: katetextline.h:72
Kate::TextLineData::string
const QString & string() const
Returns the complete text line (as a QString reference).
Definition: katetextline.h:286
Kate::TextLineData::matchesAt
bool matchesAt(int column, const QString &match) const
Returns true, if match equals to the text at position column, otherwise returns false.
Definition: katetextline.cpp:103
Kate::TextLineData::lastChar
int lastChar() const
Returns the position of the last non-whitespace character.
Definition: katetextline.cpp:45
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:58 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Kate

Skip menu "Kate"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Konsole

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal