Kate
katetextline.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "katetextline.h"
00024 #include "katerenderer.h"
00025
00026 #include <kglobal.h>
00027 #include <kdebug.h>
00028
00029 #include <QtCore/QRegExp>
00030
00031 KateTextLine::KateTextLine ()
00032 : m_flags(0)
00033 {
00034 }
00035
00036 KateTextLine::KateTextLine (const QChar *data, int length)
00037 : m_text (data, length), m_flags(0)
00038 {
00039 }
00040
00041 KateTextLine::~KateTextLine()
00042 {
00043 }
00044
00045 void KateTextLine::insertText (int pos, const QString& insText)
00046 {
00047
00048 if (insText.length() == 0)
00049 return;
00050
00051 m_text.insert (pos, insText);
00052 }
00053
00054 void KateTextLine::removeText (uint pos, uint delLen)
00055 {
00056
00057 if (delLen == 0)
00058 return;
00059
00060 uint textLen = m_text.length();
00061
00062 if (textLen == 0)
00063 return;
00064
00065 if (pos >= textLen)
00066 return;
00067
00068 if ((pos + delLen) > textLen)
00069 delLen = textLen - pos;
00070
00071 m_text.remove (pos, delLen);
00072 }
00073
00074 void KateTextLine::truncate(int newLen)
00075 {
00076 if (newLen < 0)
00077 newLen = 0;
00078
00079 if (newLen < m_text.length())
00080 m_text.truncate (newLen);
00081 }
00082
00083 int KateTextLine::nextNonSpaceChar(uint pos) const
00084 {
00085 const int len = m_text.length();
00086 const QChar *unicode = m_text.unicode();
00087
00088 for(int i = pos; i < len; i++)
00089 {
00090 if(!unicode[i].isSpace())
00091 return i;
00092 }
00093
00094 return -1;
00095 }
00096
00097 int KateTextLine::previousNonSpaceChar(int pos) const
00098 {
00099 const int len = m_text.length();
00100 const QChar *unicode = m_text.unicode();
00101
00102 if (pos < 0)
00103 pos = 0;
00104
00105 if (pos >= len)
00106 pos = len - 1;
00107
00108 for(int i = pos; i >= 0; i--)
00109 {
00110 if(!unicode[i].isSpace())
00111 return i;
00112 }
00113
00114 return -1;
00115 }
00116
00117 int KateTextLine::firstChar() const
00118 {
00119 return nextNonSpaceChar(0);
00120 }
00121
00122 int KateTextLine::lastChar() const
00123 {
00124 return previousNonSpaceChar(m_text.length() - 1);
00125 }
00126
00127 int KateTextLine::indentDepth (int tabWidth) const
00128 {
00129 int d = 0;
00130 const int len = m_text.length();
00131 const QChar *unicode = m_text.unicode();
00132
00133 for(int i = 0; i < len; ++i)
00134 {
00135 if(unicode[i].isSpace())
00136 {
00137 if (unicode[i] == QChar('\t'))
00138 d += tabWidth - (d % tabWidth);
00139 else
00140 d++;
00141 }
00142 else
00143 return d;
00144 }
00145
00146 return d;
00147 }
00148
00149 bool KateTextLine::matchesAt(int column, const QString& match) const
00150 {
00151 if (column < 0)
00152 return false;
00153
00154 const int len = m_text.length();
00155 const int matchlen = match.length();
00156
00157 if ((column + matchlen) > len)
00158 return false;
00159
00160 const QChar *unicode = m_text.unicode();
00161 const QChar *matchUnicode = match.unicode();
00162
00163 for (int i=0; i < matchlen; ++i)
00164 if (unicode[i+column] != matchUnicode[i])
00165 return false;
00166
00167 return true;
00168 }
00169
00170 int KateTextLine::toVirtualColumn (int column, int tabWidth) const
00171 {
00172 if (column < 0)
00173 return 0;
00174
00175 int x = 0;
00176 const int zmax = qMin(column, m_text.length());
00177 const QChar *unicode = m_text.unicode();
00178
00179 for ( int z = 0; z < zmax; ++z)
00180 {
00181 if (unicode[z] == QChar('\t'))
00182 x += tabWidth - (x % tabWidth);
00183 else
00184 x++;
00185 }
00186
00187 return x;
00188 }
00189
00190 int KateTextLine::fromVirtualColumn (int column, int tabWidth) const
00191 {
00192 if (column < 0)
00193 return 0;
00194
00195 const int zmax = qMin(m_text.length(), column);
00196 const QChar *unicode = m_text.unicode();
00197
00198 int x = 0;
00199 int z = 0;
00200 for (; z < zmax; ++z)
00201 {
00202 int diff = 1;
00203 if (unicode[z] == QChar('\t'))
00204 diff = tabWidth - (x % tabWidth);
00205
00206 if (x + diff > column)
00207 break;
00208 x += diff;
00209 }
00210
00211 return z;
00212 }
00213
00214 int KateTextLine::virtualLength (int tabWidth) const
00215 {
00216 int x = 0;
00217 const int len = m_text.length();
00218 const QChar *unicode = m_text.unicode();
00219
00220 for ( int z = 0; z < len; ++z)
00221 {
00222 if (unicode[z] == QChar('\t'))
00223 x += tabWidth - (x % tabWidth);
00224 else
00225 x++;
00226 }
00227
00228 return x;
00229 }
00230
00231 bool KateTextLine::searchText (uint startCol, uint endCol, const QString &text, uint *foundAtCol,
00232 uint *matchLen, bool casesensitive, bool backwards) const
00233 {
00234 int index;
00235
00236
00237
00238 int l = text.length();
00239
00240 if (backwards)
00241 {
00242 int col = -1;
00243 int start_col=startCol-m_text.length();
00244
00245
00246
00247
00248
00249 do {
00250 index = m_text.lastIndexOf( text, col, casesensitive?Qt::CaseSensitive:Qt::CaseInsensitive);
00251 col--;
00252
00253
00254
00255 } while ( (col>=start_col) && (index >= (int)startCol) && ((l + index) > (int)endCol) );
00256 }
00257 else
00258 index = m_text.indexOf (text, startCol, casesensitive?Qt::CaseSensitive:Qt::CaseInsensitive);
00259
00260 if (index > -1)
00261 {
00262 if ( (index>=(int)startCol) && ( (index+l)<=(int)endCol) ) {
00263 if (foundAtCol)
00264 (*foundAtCol) = index;
00265 if (matchLen)
00266 (*matchLen)=text.length();
00267 return true;
00268 }
00269 }
00270
00271 return false;
00272 }
00273
00274 bool KateTextLine::searchText (uint startCol, const QRegExp ®exp, uint *foundAtCol, uint *matchLen, bool backwards) const
00275 {
00276 int index;
00277
00278 if (backwards)
00279 {
00280 int col = startCol;
00281
00282
00283 if ( col == (int) m_text.length() ) ++startCol;
00284 do {
00285 index = regexp.lastIndexIn (m_text, col);
00286 col--;
00287 } while ( col >= 0 && regexp.matchedLength() + index >= (int)startCol );
00288 }
00289 else
00290 index = regexp.indexIn (m_text, startCol);
00291
00292 if (index > -1)
00293 {
00294 if (foundAtCol)
00295 (*foundAtCol) = index;
00296
00297 if (matchLen)
00298 (*matchLen)=regexp.matchedLength();
00299 return true;
00300 }
00301
00302 return false;
00303 }
00304
00305 void KateTextLine::addAttribute (int start, int length, int attribute)
00306 {
00307
00308
00309
00310 if ((m_attributesList.size() > 2) && (m_attributesList[m_attributesList.size()-1] == attribute)
00311 && (m_attributesList[m_attributesList.size()-3]+m_attributesList[m_attributesList.size()-2]
00312 == start))
00313 {
00314 m_attributesList[m_attributesList.size()-2] += length;
00315 return;
00316 }
00317
00318 m_attributesList.resize (m_attributesList.size()+3);
00319 m_attributesList[m_attributesList.size()-3] = start;
00320 m_attributesList[m_attributesList.size()-2] = length;
00321 m_attributesList[m_attributesList.size()-1] = attribute;
00322 }
00323
00324