Kate
katetextline.cpp
Go to the documentation of this file.
00001 /* This file is part of the Kate project. 00002 * 00003 * Copyright (C) 2010 Christoph Cullmann <cullmann@kde.org> 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Library General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Library General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Library General Public License 00016 * along with this library; see the file COPYING.LIB. If not, write to 00017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 * Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include "katetextline.h" 00022 00023 namespace Kate { 00024 00025 TextLineData::TextLineData () 00026 : m_flags (0) 00027 { 00028 } 00029 00030 TextLineData::TextLineData (const QString &text) 00031 : m_text (text) 00032 , m_flags (0) 00033 { 00034 } 00035 00036 TextLineData::~TextLineData () 00037 { 00038 } 00039 00040 int TextLineData::firstChar() const 00041 { 00042 return nextNonSpaceChar(0); 00043 } 00044 00045 int TextLineData::lastChar() const 00046 { 00047 return previousNonSpaceChar(m_text.length() - 1); 00048 } 00049 00050 int TextLineData::nextNonSpaceChar (int pos) const 00051 { 00052 Q_ASSERT (pos >= 0); 00053 00054 for(int i = pos; i < m_text.length(); i++) 00055 if (!m_text[i].isSpace()) 00056 return i; 00057 00058 return -1; 00059 } 00060 00061 int TextLineData::previousNonSpaceChar (int pos) const 00062 { 00063 if (pos >= m_text.length()) 00064 pos = m_text.length() - 1; 00065 00066 for(int i = pos; i >= 0; i--) 00067 if (!m_text[i].isSpace()) 00068 return i; 00069 00070 return -1; 00071 } 00072 00073 QString TextLineData::leadingWhitespace() const 00074 { 00075 if (firstChar() < 0) 00076 return string(0, length()); 00077 00078 return string(0, firstChar()); 00079 } 00080 00081 int TextLineData::indentDepth (int tabWidth) const 00082 { 00083 int d = 0; 00084 const int len = m_text.length(); 00085 const QChar *unicode = m_text.unicode(); 00086 00087 for(int i = 0; i < len; ++i) 00088 { 00089 if(unicode[i].isSpace()) 00090 { 00091 if (unicode[i] == QLatin1Char('\t')) 00092 d += tabWidth - (d % tabWidth); 00093 else 00094 d++; 00095 } 00096 else 00097 return d; 00098 } 00099 00100 return d; 00101 } 00102 00103 bool TextLineData::matchesAt(int column, const QString& match) const 00104 { 00105 if (column < 0) 00106 return false; 00107 00108 const int len = m_text.length(); 00109 const int matchlen = match.length(); 00110 00111 if ((column + matchlen) > len) 00112 return false; 00113 00114 const QChar *unicode = m_text.unicode(); 00115 const QChar *matchUnicode = match.unicode(); 00116 00117 for (int i=0; i < matchlen; ++i) 00118 if (unicode[i+column] != matchUnicode[i]) 00119 return false; 00120 00121 return true; 00122 } 00123 00124 int TextLineData::toVirtualColumn (int column, int tabWidth) const 00125 { 00126 if (column < 0) 00127 return 0; 00128 00129 int x = 0; 00130 const int zmax = qMin(column, m_text.length()); 00131 const QChar *unicode = m_text.unicode(); 00132 00133 for ( int z = 0; z < zmax; ++z) 00134 { 00135 if (unicode[z] == QLatin1Char('\t')) 00136 x += tabWidth - (x % tabWidth); 00137 else 00138 x++; 00139 } 00140 00141 return x + column - zmax; 00142 } 00143 00144 int TextLineData::fromVirtualColumn (int column, int tabWidth) const 00145 { 00146 if (column < 0) 00147 return 0; 00148 00149 const int zmax = qMin(m_text.length(), column); 00150 const QChar *unicode = m_text.unicode(); 00151 00152 int x = 0; 00153 int z = 0; 00154 for (; z < zmax; ++z) 00155 { 00156 int diff = 1; 00157 if (unicode[z] == QLatin1Char('\t')) 00158 diff = tabWidth - (x % tabWidth); 00159 00160 if (x + diff > column) 00161 break; 00162 x += diff; 00163 } 00164 00165 return z; 00166 } 00167 00168 int TextLineData::virtualLength (int tabWidth) const 00169 { 00170 int x = 0; 00171 const int len = m_text.length(); 00172 const QChar *unicode = m_text.unicode(); 00173 00174 for ( int z = 0; z < len; ++z) 00175 { 00176 if (unicode[z] == QLatin1Char('\t')) 00177 x += tabWidth - (x % tabWidth); 00178 else 00179 x++; 00180 } 00181 00182 return x; 00183 } 00184 00185 void TextLineData::addAttribute (int start, int length, int attribute) 00186 { 00187 // try to append to previous range 00188 if ((m_attributesList.size() > 2) && (m_attributesList[m_attributesList.size()-1] == attribute) 00189 && (m_attributesList[m_attributesList.size()-3]+m_attributesList[m_attributesList.size()-2] 00190 == start)) 00191 { 00192 m_attributesList[m_attributesList.size()-2] += length; 00193 return; 00194 } 00195 00196 m_attributesList.resize (m_attributesList.size()+3); 00197 m_attributesList[m_attributesList.size()-3] = start; 00198 m_attributesList[m_attributesList.size()-2] = length; 00199 m_attributesList[m_attributesList.size()-1] = attribute; 00200 } 00201 00202 }
KDE 4.6 API Reference