1
/* This file is part of the Kate project.
3
* Copyright (C) 2010 Christoph Cullmann <cullmann@kde.org>
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.
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.
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.
21
#include "katetextline.h"
25
TextLineData::TextLineData ()
30
TextLineData::TextLineData (const QString &text)
36
TextLineData::~TextLineData ()
40
int TextLineData::firstChar() const
42
return nextNonSpaceChar(0);
45
int TextLineData::lastChar() const
47
return previousNonSpaceChar(m_text.length() - 1);
50
int TextLineData::nextNonSpaceChar (int pos) const
54
for(int i = pos; i < m_text.length(); i++)
55
if (!m_text[i].isSpace())
61
int TextLineData::previousNonSpaceChar (int pos) const
63
if (pos >= m_text.length())
64
pos = m_text.length() - 1;
66
for(int i = pos; i >= 0; i--)
67
if (!m_text[i].isSpace())
73
QString TextLineData::leadingWhitespace() const
76
return string(0, length());
78
return string(0, firstChar());
81
int TextLineData::indentDepth (int tabWidth) const
84
const int len = m_text.length();
85
const QChar *unicode = m_text.unicode();
87
for(int i = 0; i < len; ++i)
89
if(unicode[i].isSpace())
91
if (unicode[i] == QLatin1Char('\t'))
92
d += tabWidth - (d % tabWidth);
103
bool TextLineData::matchesAt(int column, const QString& match) const
108
const int len = m_text.length();
109
const int matchlen = match.length();
111
if ((column + matchlen) > len)
114
const QChar *unicode = m_text.unicode();
115
const QChar *matchUnicode = match.unicode();
117
for (int i=0; i < matchlen; ++i)
118
if (unicode[i+column] != matchUnicode[i])
124
int TextLineData::toVirtualColumn (int column, int tabWidth) const
130
const int zmax = qMin(column, m_text.length());
131
const QChar *unicode = m_text.unicode();
133
for ( int z = 0; z < zmax; ++z)
135
if (unicode[z] == QLatin1Char('\t'))
136
x += tabWidth - (x % tabWidth);
141
return x + column - zmax;
144
int TextLineData::fromVirtualColumn (int column, int tabWidth) const
149
const int zmax = qMin(m_text.length(), column);
150
const QChar *unicode = m_text.unicode();
154
for (; z < zmax; ++z)
157
if (unicode[z] == QLatin1Char('\t'))
158
diff = tabWidth - (x % tabWidth);
160
if (x + diff > column)
165
return z + qMax(column - x, 0);
168
int TextLineData::virtualLength (int tabWidth) const
171
const int len = m_text.length();
172
const QChar *unicode = m_text.unicode();
174
for ( int z = 0; z < len; ++z)
176
if (unicode[z] == QLatin1Char('\t'))
177
x += tabWidth - (x % tabWidth);
185
void TextLineData::addAttribute (const Attribute &attribute)
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))
192
m_attributesList.back().length += attribute.length;
196
m_attributesList.append (attribute);