~ubuntu-branches/ubuntu/vivid/kate/vivid-updates

« back to all changes in this revision

Viewing changes to part/buffer/katetextline.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2014-12-04 16:49:41 UTC
  • mfrom: (1.6.6)
  • Revision ID: package-import@ubuntu.com-20141204164941-l3qbvsly83hhlw2v
Tags: 4:14.11.97-0ubuntu1
* New upstream release
* Update build-deps and use pkg-kde v3 for Qt 5 build
* kate-data now kate5-data for co-installability

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
}