00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "katescriptdocument.h"
00020
00021 #include "katedocument.h"
00022 #include "kateview.h"
00023 #include "katerenderer.h"
00024 #include "katehighlight.h"
00025 #include "katescript.h"
00026
00027 KateScriptDocument::KateScriptDocument(QObject *parent)
00028 : QObject(parent), m_document(0)
00029 {
00030 }
00031
00032 void KateScriptDocument::setDocument(KateDocument *document)
00033 {
00034 m_document = document;
00035 }
00036
00037 KateDocument *KateScriptDocument::document()
00038 {
00039 return m_document;
00040 }
00041
00042 int KateScriptDocument::defStyleNum(int line, int column)
00043 {
00044 KateDocCursor cursor(line, column, m_document);
00045 QList<KTextEditor::Attribute::Ptr> attributes = m_document->highlight()->attributes(((KateView*) m_document->activeView())->renderer()->config()->schema());
00046 KTextEditor::Attribute::Ptr a = attributes[cursor.currentAttrib()];
00047 return a->property(KateExtendedAttribute::AttributeDefaultStyleIndex).toInt();
00048 }
00049
00050 bool KateScriptDocument::isCode(int line, int column)
00051 {
00052 const int defaultStyle = defStyleNum(line, column);
00053 return _isCode(defaultStyle);
00054 }
00055
00056 bool KateScriptDocument::isComment(int line, int column)
00057 {
00058 const int defaultStyle = defStyleNum(line, column);
00059 return defaultStyle == KateExtendedAttribute::dsComment;
00060 }
00061
00062 bool KateScriptDocument::isString(int line, int column)
00063 {
00064 const int defaultStyle = defStyleNum(line, column);
00065 return defaultStyle == KateExtendedAttribute::dsString;
00066 }
00067
00068 bool KateScriptDocument::isRegionMarker(int line, int column)
00069 {
00070 const int defaultStyle = defStyleNum(line, column);
00071 return defaultStyle == KateExtendedAttribute::dsRegionMarker;
00072 }
00073
00074 bool KateScriptDocument::isChar(int line, int column)
00075 {
00076 const int defaultStyle = defStyleNum(line, column);
00077 return defaultStyle == KateExtendedAttribute::dsChar;
00078 }
00079
00080 bool KateScriptDocument::isOthers(int line, int column)
00081 {
00082 const int defaultStyle = defStyleNum(line, column);
00083 return defaultStyle == KateExtendedAttribute::dsOthers;
00084 }
00085
00086 int KateScriptDocument::firstVirtualColumn(int line)
00087 {
00088 const int tabWidth = m_document->config()->tabWidth();
00089 KateTextLine::Ptr textLine = m_document->plainKateTextLine(line);
00090 const int firstPos = textLine ? textLine->firstChar() : -1;
00091 if(!textLine || firstPos == -1)
00092 return -1;
00093 return textLine->indentDepth(tabWidth);
00094 }
00095
00096 int KateScriptDocument::lastVirtualColumn(int line)
00097 {
00098 const int tabWidth = m_document->config()->tabWidth();
00099 KateTextLine::Ptr textLine = m_document->plainKateTextLine(line);
00100 const int lastPos = textLine ? textLine->lastChar() : -1;
00101 if(!textLine || lastPos == -1)
00102 return -1;
00103 return textLine->toVirtualColumn(lastPos, tabWidth);
00104 }
00105
00106 int KateScriptDocument::toVirtualColumn(int line, int column)
00107 {
00108 const int tabWidth = m_document->config()->tabWidth();
00109 KateTextLine::Ptr textLine = m_document->plainKateTextLine(line);
00110 if (!textLine || column < 0 || column > textLine->length()) return -1;
00111 return textLine->toVirtualColumn(column, tabWidth);
00112 }
00113
00114 int KateScriptDocument::fromVirtualColumn(int line, int virtualColumn)
00115 {
00116 const int tabWidth = m_document->config()->tabWidth();
00117 KateTextLine::Ptr textLine = m_document->plainKateTextLine(line);
00118 if(!textLine || virtualColumn < 0 || virtualColumn > textLine->virtualLength(tabWidth))
00119 return -1;
00120 return textLine->fromVirtualColumn(virtualColumn, tabWidth);
00121 }
00122
00123 KTextEditor::Cursor KateScriptDocument::anchor(int line, int column, QChar character)
00124 {
00125 KateDocCursor cursor(line, column, m_document);
00126 QList<KTextEditor::Attribute::Ptr> attributes =
00127 m_document->highlight()->attributes(((KateView*) m_document->activeView())->renderer()->config()->schema());
00128 int count = 1;
00129 QChar lc = character;
00130 QChar rc;
00131 if (lc == '(') rc = ')';
00132 else if (lc == '{') rc = '}';
00133 else if (lc == '[') rc = ']';
00134 else return KTextEditor::Cursor::invalid ();
00135
00136
00137 while (cursor.moveBackward(1)) {
00138 QChar ch = cursor.currentChar();
00139 if (ch == lc) {
00140 KTextEditor::Attribute::Ptr a = attributes[cursor.currentAttrib()];
00141 const int ds = a->property(KateExtendedAttribute::AttributeDefaultStyleIndex).toInt();
00142 if (_isCode(ds)) {
00143 --count;
00144 }
00145 }
00146 else if (ch == rc) {
00147 KTextEditor::Attribute::Ptr a = attributes[cursor.currentAttrib()];
00148 const int ds = a->property(KateExtendedAttribute::AttributeDefaultStyleIndex).toInt();
00149 if (_isCode(ds)) {
00150 ++count;
00151 }
00152 }
00153
00154 if (count == 0) {
00155 return cursor;
00156 }
00157 }
00158 return KTextEditor::Cursor::invalid ();
00159 }
00160
00161 bool KateScriptDocument::startsWith (int line, const QString &pattern, bool skipWhiteSpaces)
00162 {
00163 KateTextLine::Ptr textLine = m_document->plainKateTextLine(line);
00164
00165 if (!textLine)
00166 return false;
00167
00168 if (skipWhiteSpaces)
00169 return textLine->matchesAt(textLine->firstChar(), pattern);
00170
00171 return textLine->startsWith (pattern);
00172 }
00173
00174 bool KateScriptDocument::endsWith (int line, const QString &pattern, bool skipWhiteSpaces)
00175 {
00176 KateTextLine::Ptr textLine = m_document->plainKateTextLine(line);
00177
00178 if (!textLine)
00179 return false;
00180
00181 if (skipWhiteSpaces)
00182 return textLine->matchesAt(textLine->lastChar() - pattern.length() + 1, pattern);
00183
00184 return textLine->endsWith (pattern);
00185 }
00186
00187
00188
00189 QString KateScriptDocument::fileName()
00190 {
00191 return m_document->documentName();
00192 }
00193
00194 QString KateScriptDocument::url()
00195 {
00196 return m_document->url().prettyUrl();
00197 }
00198
00199 QString KateScriptDocument::mimeType()
00200 {
00201 return m_document->mimeType();
00202 }
00203
00204 QString KateScriptDocument::encoding()
00205 {
00206 return m_document->encoding();
00207 }
00208
00209 bool KateScriptDocument::isModified()
00210 {
00211 return m_document->isModified();
00212 }
00213
00214 QString KateScriptDocument::text()
00215 {
00216 return m_document->text();
00217 }
00218
00219 QString KateScriptDocument::textRange(int i, int j, int k, int l)
00220 {
00221 return m_document->text(KTextEditor::Range(i, j, k, l));
00222 }
00223
00224 QString KateScriptDocument::line(int i)
00225 {
00226 return m_document->line (i);
00227 }
00228
00229 QString KateScriptDocument::wordAt(int i, int j)
00230 {
00231 return m_document->getWord(KTextEditor::Cursor(i, j));
00232 }
00233
00234 QString KateScriptDocument::charAt(int i, int j)
00235 {
00236 const QChar c = m_document->character (KTextEditor::Cursor(i, j));
00237 return c.isNull() ? "" : QString(c);
00238 }
00239
00240 QString KateScriptDocument::firstChar(int i)
00241 {
00242 KateTextLine::Ptr textLine = m_document->plainKateTextLine(i);
00243 if(!textLine) return "";
00244
00245 const QChar c = textLine->at(textLine->firstChar());
00246 return c.isNull() ? "" : QString(c);
00247 }
00248
00249 QString KateScriptDocument::lastChar(int i)
00250 {
00251 KateTextLine::Ptr textLine = m_document->plainKateTextLine(i);
00252 if(!textLine) return "";
00253
00254 const QChar c = textLine->at(textLine->lastChar());
00255 return c.isNull() ? "" : QString(c);
00256 }
00257
00258 bool KateScriptDocument::isSpace(int i, int j)
00259 {
00260 return m_document->character (KTextEditor::Cursor(i, j)).isSpace();
00261 }
00262
00263 bool KateScriptDocument::matchesAt(int i, int j, const QString &s)
00264 {
00265 KateTextLine::Ptr textLine = m_document->plainKateTextLine(i);
00266 return textLine ? textLine->matchesAt(j, s) : false;
00267 }
00268
00269 bool KateScriptDocument::setText(const QString &s)
00270 {
00271 return m_document->setText(s);
00272 }
00273
00274 bool KateScriptDocument::clear()
00275 {
00276 return m_document->clear();
00277 }
00278
00279 bool KateScriptDocument::truncate(int i, int j)
00280 {
00281 KateTextLine::Ptr textLine = m_document->plainKateTextLine(i);
00282 if(textLine) textLine->truncate(j);
00283 return static_cast<bool>(textLine);
00284 }
00285
00286 bool KateScriptDocument::insertText(int i, int j, const QString &s)
00287 {
00288 return m_document->insertText (KTextEditor::Cursor(i, j), s);
00289 }
00290
00291 bool KateScriptDocument::removeText(int i, int j, int k, int l)
00292 {
00293 return m_document->removeText(KTextEditor::Range(i, j, k, l));
00294 }
00295
00296 bool KateScriptDocument::insertLine(int i, const QString &s)
00297 {
00298 return m_document->insertLine (i, s);
00299 }
00300
00301 bool KateScriptDocument::removeLine(int i)
00302 {
00303 return m_document->removeLine (i);
00304 }
00305
00306 void KateScriptDocument::joinLines(int i, int j)
00307 {
00308 m_document->joinLines (i, j);
00309 return;
00310 }
00311
00312 int KateScriptDocument::lines()
00313 {
00314 return m_document->lines();
00315 }
00316
00317 int KateScriptDocument::length()
00318 {
00319 return m_document->totalCharacters();
00320 }
00321
00322 int KateScriptDocument::lineLength(int i)
00323 {
00324 return m_document->lineLength(i);
00325 }
00326
00327 void KateScriptDocument::editBegin()
00328 {
00329 m_document->editBegin();
00330 return;
00331 }
00332
00333 void KateScriptDocument::editEnd()
00334 {
00335 m_document->editEnd ();
00336 return;
00337 }
00338
00339 int KateScriptDocument::firstColumn(int i)
00340 {
00341 KateTextLine::Ptr textLine = m_document->plainKateTextLine(i);
00342 return textLine ? textLine->firstChar() : -1;
00343 }
00344
00345 int KateScriptDocument::lastColumn(int i)
00346 {
00347 KateTextLine::Ptr textLine = m_document->plainKateTextLine(i);
00348 return textLine ? textLine->lastChar() : -1;
00349 }
00350
00351 int KateScriptDocument::prevNonSpaceColumn(int i, int j)
00352 {
00353 KateTextLine::Ptr textLine = m_document->plainKateTextLine(i);
00354 if(!textLine) return -1;
00355 return textLine->previousNonSpaceChar(j);
00356 }
00357
00358 int KateScriptDocument::nextNonSpaceColumn(int i, int j)
00359 {
00360 KateTextLine::Ptr textLine = m_document->plainKateTextLine(i);
00361 if(!textLine) return -1;
00362 return textLine->nextNonSpaceChar(j);
00363 }
00364
00365 int KateScriptDocument::prevNonEmptyLine(int i)
00366 {
00367 const int startLine = i;
00368 for (int currentLine = startLine; currentLine >= 0; --currentLine) {
00369 KateTextLine::Ptr textLine = m_document->plainKateTextLine(currentLine);
00370 if(!textLine)
00371 return -1;
00372 if(textLine->firstChar() != -1)
00373 return currentLine;
00374 }
00375 return -1;
00376 }
00377
00378 int KateScriptDocument::nextNonEmptyLine(int i)
00379 {
00380 const int startLine = i;
00381 for (int currentLine = startLine; currentLine < m_document->lines(); ++currentLine) {
00382 KateTextLine::Ptr textLine = m_document->plainKateTextLine(currentLine);
00383 if(!textLine)
00384 return -1;
00385 if(textLine->firstChar() != -1)
00386 return currentLine;
00387 }
00388 return -1;
00389 }
00390
00391 bool KateScriptDocument::isInWord(const QString &s, int i)
00392 {
00393 return m_document->highlight()->isInWord(s.at(0), i);
00394 }
00395
00396 bool KateScriptDocument::canBreakAt(const QString &s, int i)
00397 {
00398 return m_document->highlight()->canBreakAt(s.at(0), i);
00399 }
00400
00401 bool KateScriptDocument::canComment(int i, int j)
00402 {
00403 return m_document->highlight()->canComment(i, j);
00404 }
00405
00406 QString KateScriptDocument::commentMarker(int i)
00407 {
00408 return m_document->highlight()->getCommentSingleLineStart(i);
00409 }
00410
00411 QString KateScriptDocument::commentStart(int i)
00412 {
00413 return m_document->highlight()->getCommentStart(i);
00414 }
00415
00416 QString KateScriptDocument::commentEnd(int i)
00417 {
00418 return m_document->highlight()->getCommentEnd(i);
00419 }
00420
00421 int KateScriptDocument::attribute(int i, int j)
00422 {
00423 KateTextLine::Ptr textLine = m_document->kateTextLine(i);
00424 if(!textLine) return 0;
00425 return textLine->attribute(j);
00426 }
00427
00428 QString KateScriptDocument::variable(const QString &s)
00429 {
00430 return m_document->variable(s);
00431 }
00432
00433
00434
00435 bool KateScriptDocument::_isCode(int defaultStyle)
00436 {
00437 return (defaultStyle != KateExtendedAttribute::dsComment
00438 && defaultStyle != KateExtendedAttribute::dsString
00439 && defaultStyle != KateExtendedAttribute::dsRegionMarker
00440 && defaultStyle != KateExtendedAttribute::dsChar
00441 && defaultStyle != KateExtendedAttribute::dsOthers);
00442 }
00443
00444
00445