KTextEditor

katetextline.cpp
1/*
2 SPDX-FileCopyrightText: 2010 Christoph Cullmann <cullmann@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "katetextline.h"
8
9namespace Kate
10{
11
13{
14 return nextNonSpaceChar(0);
15}
16
18{
19 return previousNonSpaceChar(m_text.length() - 1);
20}
21
23{
24 Q_ASSERT(pos >= 0);
25
26 for (int i = pos; i < m_text.length(); i++) {
27 if (!m_text[i].isSpace()) {
28 return i;
29 }
30 }
31
32 return -1;
33}
34
36{
37 if (pos >= m_text.length()) {
38 pos = m_text.length() - 1;
39 }
40
41 for (int i = pos; i >= 0; i--) {
42 if (!m_text[i].isSpace()) {
43 return i;
44 }
45 }
46
47 return -1;
48}
49
51{
52 if (firstChar() < 0) {
53 return string(0, length());
54 }
55
56 return string(0, firstChar());
57}
58
59int TextLine::indentDepth(int tabWidth) const
60{
61 int d = 0;
62 const int len = m_text.length();
63 const QChar *unicode = m_text.unicode();
64
65 for (int i = 0; i < len; ++i) {
66 if (unicode[i].isSpace()) {
67 if (unicode[i] == QLatin1Char('\t')) {
68 d += tabWidth - (d % tabWidth);
69 } else {
70 d++;
71 }
72 } else {
73 return d;
74 }
75 }
76
77 return d;
78}
79
80bool TextLine::matchesAt(int column, const QString &match) const
81{
82 if (column < 0) {
83 return false;
84 }
85
86 const int len = m_text.length();
87 const int matchlen = match.length();
88
89 if ((column + matchlen) > len) {
90 return false;
91 }
92
93 const QChar *unicode = m_text.unicode();
94 const QChar *matchUnicode = match.unicode();
95
96 for (int i = 0; i < matchlen; ++i) {
97 if (unicode[i + column] != matchUnicode[i]) {
98 return false;
99 }
100 }
101
102 return true;
103}
104
105int TextLine::toVirtualColumn(int column, int tabWidth) const
106{
107 if (column < 0) {
108 return 0;
109 }
110
111 int x = 0;
112 const int zmax = qMin(column, m_text.length());
113 const QChar *unicode = m_text.unicode();
114
115 for (int z = 0; z < zmax; ++z) {
116 if (unicode[z] == QLatin1Char('\t')) {
117 x += tabWidth - (x % tabWidth);
118 } else {
119 x++;
120 }
121 }
122
123 return x + column - zmax;
124}
125
126int TextLine::fromVirtualColumn(int column, int tabWidth) const
127{
128 if (column < 0) {
129 return 0;
130 }
131
132 const int zmax = qMin(m_text.length(), column);
133 const QChar *unicode = m_text.unicode();
134
135 int x = 0;
136 int z = 0;
137 for (; z < zmax; ++z) {
138 int diff = 1;
139 if (unicode[z] == QLatin1Char('\t')) {
140 diff = tabWidth - (x % tabWidth);
141 }
142
143 if (x + diff > column) {
144 break;
145 }
146 x += diff;
147 }
148
149 return z + qMax(column - x, 0);
150}
151
152int TextLine::virtualLength(int tabWidth) const
153{
154 int x = 0;
155 const int len = m_text.length();
156 const QChar *unicode = m_text.unicode();
157
158 for (int z = 0; z < len; ++z) {
159 if (unicode[z] == QLatin1Char('\t')) {
160 x += tabWidth - (x % tabWidth);
161 } else {
162 x++;
163 }
164 }
165
166 return x;
167}
168
169void TextLine::addAttribute(const Attribute &attribute)
170{
171 // try to append to previous range, if same attribute value
172 if (!m_attributesList.empty() && (m_attributesList.back().attributeValue == attribute.attributeValue)
173 && ((m_attributesList.back().offset + m_attributesList.back().length) == attribute.offset)) {
174 m_attributesList.back().length += attribute.length;
175 return;
176 }
177
178 m_attributesList.push_back(attribute);
179}
180
181int TextLine::attribute(int pos) const
182{
183 const auto found = std::upper_bound(m_attributesList.cbegin(), m_attributesList.cend(), pos, [](const int &p, const Attribute &x) {
184 return p < x.offset + x.length;
185 });
186 if (found != m_attributesList.cend() && found->offset <= pos && pos < (found->offset + found->length)) {
187 return found->attributeValue;
188 }
189 return 0;
190}
191
192}
Attribute storage.
int attribute(int pos) const
Gets the attribute at the given position use KRenderer::attributes to get the KTextAttribute for this...
QString leadingWhitespace() const
Leading whitespace of this line.
int virtualLength(int tabWidth) const
Returns the text length with each tab expanded into tabWidth characters.
int indentDepth(int tabWidth) const
Returns the indentation depth with each tab expanded into tabWidth characters.
QString string(int column, int length) const
Returns the substring with length beginning at the given column.
int previousNonSpaceChar(int pos) const
Find the position of the previous char that is not a space.
int length() const
Returns the line's length.
int lastChar() const
Returns the position of the last non-whitespace character.
int firstChar() const
Returns the position of the first non-whitespace character.
void addAttribute(const Attribute &attribute)
Add attribute to this line.
int toVirtualColumn(int column, int tabWidth) const
Returns the column with each tab expanded into tabWidth characters.
bool matchesAt(int column, const QString &match) const
Returns true, if match equals to the text at position column, otherwise returns false.
int nextNonSpaceChar(int pos) const
Find the position of the next char that is not a space.
int fromVirtualColumn(int column, int tabWidth) const
Returns the "real" column where each tab only counts one character.
qsizetype length() const const
const QChar * unicode() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:15:43 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.