| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2008 Paul Giannaros <paul@giannaros.org> |
| 3 | SPDX-FileCopyrightText: 2009-2018 Dominik Haumann <dhaumann@kde.org> |
| 4 | |
| 5 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #include "katescriptdocument.h" |
| 9 | |
| 10 | #include "katebuffer.h" |
| 11 | #include "kateconfig.h" |
| 12 | #include "katedocument.h" |
| 13 | #include "katehighlight.h" |
| 14 | #include "katepartdebug.h" |
| 15 | #include "katescript.h" |
| 16 | #include "scriptcursor.h" |
| 17 | #include "scriptrange.h" |
| 18 | |
| 19 | #include <QJSEngine> |
| 20 | #include <ktexteditor/documentcursor.h> |
| 21 | |
| 22 | KateScriptDocument::KateScriptDocument(QJSEngine *engine, QObject *parent) |
| 23 | : QObject(parent) |
| 24 | , m_document(nullptr) |
| 25 | , m_engine(engine) |
| 26 | { |
| 27 | } |
| 28 | |
| 29 | void KateScriptDocument::setDocument(KTextEditor::DocumentPrivate *document) |
| 30 | { |
| 31 | m_document = document; |
| 32 | } |
| 33 | |
| 34 | KTextEditor::DocumentPrivate *KateScriptDocument::document() |
| 35 | { |
| 36 | return m_document; |
| 37 | } |
| 38 | |
| 39 | int KateScriptDocument::defStyleNum(int line, int column) |
| 40 | { |
| 41 | return m_document->defStyleNum(line, column); |
| 42 | } |
| 43 | |
| 44 | int KateScriptDocument::defStyleNum(const QJSValue &jscursor) |
| 45 | { |
| 46 | const auto cursor = cursorFromScriptValue(obj: jscursor); |
| 47 | return defStyleNum(line: cursor.line(), column: cursor.column()); |
| 48 | } |
| 49 | |
| 50 | bool KateScriptDocument::isCode(int line, int column) |
| 51 | { |
| 52 | const int defaultStyle = defStyleNum(line, column); |
| 53 | return _isCode(defaultStyle); |
| 54 | } |
| 55 | |
| 56 | bool KateScriptDocument::isCode(const QJSValue &jscursor) |
| 57 | { |
| 58 | const auto cursor = cursorFromScriptValue(obj: jscursor); |
| 59 | return isCode(line: cursor.line(), column: cursor.column()); |
| 60 | } |
| 61 | |
| 62 | bool KateScriptDocument::(int line, int column) |
| 63 | { |
| 64 | return m_document->isComment(line, column); |
| 65 | } |
| 66 | |
| 67 | bool KateScriptDocument::(const QJSValue &jscursor) |
| 68 | { |
| 69 | const auto cursor = cursorFromScriptValue(obj: jscursor); |
| 70 | return isComment(line: cursor.line(), column: cursor.column()); |
| 71 | } |
| 72 | |
| 73 | bool KateScriptDocument::isString(int line, int column) |
| 74 | { |
| 75 | const int defaultStyle = defStyleNum(line, column); |
| 76 | return defaultStyle == KSyntaxHighlighting::Theme::TextStyle::String; |
| 77 | } |
| 78 | |
| 79 | bool KateScriptDocument::isString(const QJSValue &jscursor) |
| 80 | { |
| 81 | const auto cursor = cursorFromScriptValue(obj: jscursor); |
| 82 | return isString(line: cursor.line(), column: cursor.column()); |
| 83 | } |
| 84 | |
| 85 | bool KateScriptDocument::isRegionMarker(int line, int column) |
| 86 | { |
| 87 | const int defaultStyle = defStyleNum(line, column); |
| 88 | return defaultStyle == KSyntaxHighlighting::Theme::TextStyle::RegionMarker; |
| 89 | } |
| 90 | |
| 91 | bool KateScriptDocument::isRegionMarker(const QJSValue &jscursor) |
| 92 | { |
| 93 | const auto cursor = cursorFromScriptValue(obj: jscursor); |
| 94 | return isRegionMarker(line: cursor.line(), column: cursor.column()); |
| 95 | } |
| 96 | |
| 97 | bool KateScriptDocument::isChar(int line, int column) |
| 98 | { |
| 99 | const int defaultStyle = defStyleNum(line, column); |
| 100 | return defaultStyle == KSyntaxHighlighting::Theme::TextStyle::Char; |
| 101 | } |
| 102 | |
| 103 | bool KateScriptDocument::isChar(const QJSValue &jscursor) |
| 104 | { |
| 105 | const auto cursor = cursorFromScriptValue(obj: jscursor); |
| 106 | return isChar(line: cursor.line(), column: cursor.column()); |
| 107 | } |
| 108 | |
| 109 | bool KateScriptDocument::isOthers(int line, int column) |
| 110 | { |
| 111 | const int defaultStyle = defStyleNum(line, column); |
| 112 | return defaultStyle == KSyntaxHighlighting::Theme::TextStyle::Others; |
| 113 | } |
| 114 | |
| 115 | bool KateScriptDocument::isOthers(const QJSValue &jscursor) |
| 116 | { |
| 117 | const auto cursor = cursorFromScriptValue(obj: jscursor); |
| 118 | return isOthers(line: cursor.line(), column: cursor.column()); |
| 119 | } |
| 120 | |
| 121 | int KateScriptDocument::firstVirtualColumn(int line) |
| 122 | { |
| 123 | const int tabWidth = m_document->config()->tabWidth(); |
| 124 | const auto textLine = m_document->plainKateTextLine(i: line); |
| 125 | if (textLine.firstChar() == -1) { |
| 126 | return -1; |
| 127 | } |
| 128 | return textLine.indentDepth(tabWidth); |
| 129 | } |
| 130 | |
| 131 | int KateScriptDocument::lastVirtualColumn(int line) |
| 132 | { |
| 133 | const int tabWidth = m_document->config()->tabWidth(); |
| 134 | const auto textLine = m_document->plainKateTextLine(i: line); |
| 135 | const auto lastPos = textLine.lastChar(); |
| 136 | if (lastPos == -1) { |
| 137 | return -1; |
| 138 | } |
| 139 | return textLine.toVirtualColumn(column: lastPos, tabWidth); |
| 140 | } |
| 141 | |
| 142 | int KateScriptDocument::toVirtualColumn(int line, int column) |
| 143 | { |
| 144 | const int tabWidth = m_document->config()->tabWidth(); |
| 145 | const auto textLine = m_document->plainKateTextLine(i: line); |
| 146 | if (column < 0 || column > textLine.length()) { |
| 147 | return -1; |
| 148 | } |
| 149 | return textLine.toVirtualColumn(column, tabWidth); |
| 150 | } |
| 151 | |
| 152 | int KateScriptDocument::toVirtualColumn(const QJSValue &jscursor) |
| 153 | { |
| 154 | const auto cursor = cursorFromScriptValue(obj: jscursor); |
| 155 | return toVirtualColumn(line: cursor.line(), column: cursor.column()); |
| 156 | } |
| 157 | |
| 158 | QJSValue KateScriptDocument::toVirtualCursor(int line, int column) |
| 159 | { |
| 160 | const KTextEditor::Cursor cursor(line, toVirtualColumn(line, column)); |
| 161 | return cursorToScriptValue(engine: m_engine, cursor); |
| 162 | } |
| 163 | |
| 164 | QJSValue KateScriptDocument::toVirtualCursor(const QJSValue &jscursor) |
| 165 | { |
| 166 | const auto cursor = cursorFromScriptValue(obj: jscursor); |
| 167 | return toVirtualCursor(line: cursor.line(), column: cursor.column()); |
| 168 | } |
| 169 | |
| 170 | int KateScriptDocument::fromVirtualColumn(int line, int virtualColumn) |
| 171 | { |
| 172 | const int tabWidth = m_document->config()->tabWidth(); |
| 173 | const auto textLine = m_document->plainKateTextLine(i: line); |
| 174 | if (virtualColumn < 0 || virtualColumn > textLine.virtualLength(tabWidth)) { |
| 175 | return -1; |
| 176 | } |
| 177 | return textLine.fromVirtualColumn(column: virtualColumn, tabWidth); |
| 178 | } |
| 179 | |
| 180 | int KateScriptDocument::fromVirtualColumn(const QJSValue &jscursor) |
| 181 | { |
| 182 | const auto cursor = cursorFromScriptValue(obj: jscursor); |
| 183 | return fromVirtualColumn(line: cursor.line(), virtualColumn: cursor.column()); |
| 184 | } |
| 185 | |
| 186 | QJSValue KateScriptDocument::fromVirtualCursor(int line, int column) |
| 187 | { |
| 188 | const KTextEditor::Cursor cursor(line, fromVirtualColumn(line, virtualColumn: column)); |
| 189 | return cursorToScriptValue(engine: m_engine, cursor); |
| 190 | } |
| 191 | |
| 192 | QJSValue KateScriptDocument::fromVirtualCursor(const QJSValue &jscursor) |
| 193 | { |
| 194 | const auto cursor = cursorFromScriptValue(obj: jscursor); |
| 195 | return fromVirtualCursor(line: cursor.line(), column: cursor.column()); |
| 196 | } |
| 197 | |
| 198 | KTextEditor::Cursor KateScriptDocument::rfindInternal(int line, int column, const QString &text, int attribute) |
| 199 | { |
| 200 | KTextEditor::DocumentCursor cursor(document(), line, column); |
| 201 | const int start = cursor.line(); |
| 202 | |
| 203 | do { |
| 204 | const auto textLine = m_document->plainKateTextLine(i: cursor.line()); |
| 205 | |
| 206 | if (cursor.line() != start) { |
| 207 | cursor.setColumn(textLine.length()); |
| 208 | } else if (column >= textLine.length()) { |
| 209 | cursor.setColumn(qMax(a: textLine.length(), b: 0)); |
| 210 | } |
| 211 | |
| 212 | int foundAt; |
| 213 | while ((foundAt = QStringView(textLine.text()).left(n: cursor.column()).lastIndexOf(s: text)) >= 0) { |
| 214 | bool hasStyle = true; |
| 215 | if (attribute != -1) { |
| 216 | const KSyntaxHighlighting::Theme::TextStyle ds = m_document->highlight()->defaultStyleForAttribute(attr: textLine.attribute(pos: foundAt)); |
| 217 | hasStyle = (ds == attribute); |
| 218 | } |
| 219 | |
| 220 | if (hasStyle) { |
| 221 | return KTextEditor::Cursor(cursor.line(), foundAt); |
| 222 | } else { |
| 223 | cursor.setColumn(foundAt); |
| 224 | } |
| 225 | } |
| 226 | } while (cursor.gotoPreviousLine()); |
| 227 | |
| 228 | return KTextEditor::Cursor::invalid(); |
| 229 | } |
| 230 | |
| 231 | KTextEditor::Cursor KateScriptDocument::rfind(KTextEditor::Cursor cursor, const QString &text, int attribute) |
| 232 | { |
| 233 | return rfindInternal(line: cursor.line(), column: cursor.column(), text, attribute); |
| 234 | } |
| 235 | |
| 236 | QJSValue KateScriptDocument::rfind(int line, int column, const QString &text, int attribute) |
| 237 | { |
| 238 | return cursorToScriptValue(engine: m_engine, cursor: rfindInternal(line, column, text, attribute)); |
| 239 | } |
| 240 | |
| 241 | QJSValue KateScriptDocument::rfind(const QJSValue &jscursor, const QString &text, int attribute) |
| 242 | { |
| 243 | KTextEditor::Cursor cursor = cursorFromScriptValue(obj: jscursor); |
| 244 | return cursorToScriptValue(engine: m_engine, cursor: rfind(cursor, text, attribute)); |
| 245 | } |
| 246 | |
| 247 | KTextEditor::Cursor KateScriptDocument::anchorInternal(int line, int column, QChar character) |
| 248 | { |
| 249 | QChar lc; |
| 250 | QChar rc; |
| 251 | if (character == QLatin1Char('(') || character == QLatin1Char(')')) { |
| 252 | lc = QLatin1Char('('); |
| 253 | rc = QLatin1Char(')'); |
| 254 | } else if (character == QLatin1Char('{') || character == QLatin1Char('}')) { |
| 255 | lc = QLatin1Char('{'); |
| 256 | rc = QLatin1Char('}'); |
| 257 | } else if (character == QLatin1Char('[') || character == QLatin1Char(']')) { |
| 258 | lc = QLatin1Char('['); |
| 259 | rc = QLatin1Char(']'); |
| 260 | } else { |
| 261 | qCDebug(LOG_KTE) << "invalid anchor character:" << character << " allowed are: (){}[]" ; |
| 262 | return KTextEditor::Cursor::invalid(); |
| 263 | } |
| 264 | |
| 265 | auto *highlighter = m_document->highlight(); |
| 266 | auto isCodePos = [highlighter](const Kate::TextLine ¤tLine, int i) { |
| 267 | const KSyntaxHighlighting::Theme::TextStyle ds = highlighter->defaultStyleForAttribute(attr: currentLine.attribute(pos: i)); |
| 268 | return _isCode(defaultStyle: ds); |
| 269 | }; |
| 270 | |
| 271 | // Move backwards char by char and find the opening character |
| 272 | int count = 1; |
| 273 | for (int l = line; l >= 0; --l) { |
| 274 | const Kate::TextLine currentLine = document()->buffer().plainLine(lineno: l); |
| 275 | const QString &lineText = currentLine.text(); |
| 276 | if (l < line) { |
| 277 | // If the line is first line, we use the column |
| 278 | // specified by the caller of this function |
| 279 | // otherwise we start at line length |
| 280 | column = lineText.length(); |
| 281 | } |
| 282 | for (int i = column - 1; i >= 0; --i) { |
| 283 | const QChar ch = lineText[i]; |
| 284 | if (ch == lc && isCodePos(currentLine, i)) { |
| 285 | --count; |
| 286 | } else if (ch == rc && isCodePos(currentLine, i)) { |
| 287 | ++count; |
| 288 | } |
| 289 | |
| 290 | if (count == 0) { |
| 291 | return KTextEditor::Cursor(l, i); |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | return KTextEditor::Cursor::invalid(); |
| 297 | } |
| 298 | |
| 299 | KTextEditor::Cursor KateScriptDocument::anchor(KTextEditor::Cursor cursor, QChar character) |
| 300 | { |
| 301 | return anchorInternal(line: cursor.line(), column: cursor.column(), character); |
| 302 | } |
| 303 | |
| 304 | QJSValue KateScriptDocument::anchor(int line, int column, QChar character) |
| 305 | { |
| 306 | return cursorToScriptValue(engine: m_engine, cursor: anchorInternal(line, column, character)); |
| 307 | } |
| 308 | |
| 309 | QJSValue KateScriptDocument::anchor(const QJSValue &jscursor, QChar character) |
| 310 | { |
| 311 | KTextEditor::Cursor cursor = cursorFromScriptValue(obj: jscursor); |
| 312 | return anchor(line: cursor.line(), column: cursor.column(), character); |
| 313 | } |
| 314 | |
| 315 | bool KateScriptDocument::startsWith(int line, const QString &pattern, bool skipWhiteSpaces) |
| 316 | { |
| 317 | Kate::TextLine textLine = m_document->plainKateTextLine(i: line); |
| 318 | |
| 319 | if (skipWhiteSpaces) { |
| 320 | return textLine.matchesAt(column: textLine.firstChar(), match: pattern); |
| 321 | } |
| 322 | |
| 323 | return textLine.startsWith(match: pattern); |
| 324 | } |
| 325 | |
| 326 | bool KateScriptDocument::endsWith(int line, const QString &pattern, bool skipWhiteSpaces) |
| 327 | { |
| 328 | Kate::TextLine textLine = m_document->plainKateTextLine(i: line); |
| 329 | |
| 330 | if (skipWhiteSpaces) { |
| 331 | return textLine.matchesAt(column: textLine.lastChar() - pattern.length() + 1, match: pattern); |
| 332 | } |
| 333 | |
| 334 | return textLine.endsWith(match: pattern); |
| 335 | } |
| 336 | |
| 337 | QString KateScriptDocument::fileName() |
| 338 | { |
| 339 | return m_document->documentName(); |
| 340 | } |
| 341 | |
| 342 | QString KateScriptDocument::url() |
| 343 | { |
| 344 | return m_document->url().toString(); |
| 345 | } |
| 346 | |
| 347 | QString KateScriptDocument::mimeType() |
| 348 | { |
| 349 | return m_document->mimeType(); |
| 350 | } |
| 351 | |
| 352 | QString KateScriptDocument::encoding() |
| 353 | { |
| 354 | return m_document->encoding(); |
| 355 | } |
| 356 | |
| 357 | QString KateScriptDocument::highlightingMode() |
| 358 | { |
| 359 | return m_document->highlightingMode(); |
| 360 | } |
| 361 | |
| 362 | QStringList KateScriptDocument::embeddedHighlightingModes() |
| 363 | { |
| 364 | return m_document->embeddedHighlightingModes(); |
| 365 | } |
| 366 | |
| 367 | QString KateScriptDocument::highlightingModeAt(const QJSValue &jspos) |
| 368 | { |
| 369 | return m_document->highlightingModeAt(position: cursorFromScriptValue(obj: jspos)); |
| 370 | } |
| 371 | |
| 372 | bool KateScriptDocument::isModified() |
| 373 | { |
| 374 | return m_document->isModified(); |
| 375 | } |
| 376 | |
| 377 | QString KateScriptDocument::text() |
| 378 | { |
| 379 | return m_document->text(); |
| 380 | } |
| 381 | |
| 382 | QString KateScriptDocument::text(int fromLine, int fromColumn, int toLine, int toColumn) |
| 383 | { |
| 384 | const KTextEditor::Range range(fromLine, fromColumn, toLine, toColumn); |
| 385 | return m_document->text(range); |
| 386 | } |
| 387 | |
| 388 | QString KateScriptDocument::text(const QJSValue &jsfrom, const QJSValue &jsto) |
| 389 | { |
| 390 | const KTextEditor::Cursor from = cursorFromScriptValue(obj: jsfrom); |
| 391 | const KTextEditor::Cursor to = cursorFromScriptValue(obj: jsto); |
| 392 | return text(fromLine: from.line(), fromColumn: from.column(), toLine: to.line(), toColumn: to.column()); |
| 393 | } |
| 394 | |
| 395 | QString KateScriptDocument::text(const QJSValue &jsrange) |
| 396 | { |
| 397 | const auto range = rangeFromScriptValue(obj: jsrange); |
| 398 | return text(fromLine: range.start().line(), fromColumn: range.start().column(), toLine: range.end().line(), toColumn: range.end().column()); |
| 399 | } |
| 400 | |
| 401 | QString KateScriptDocument::line(int line) |
| 402 | { |
| 403 | return m_document->line(line); |
| 404 | } |
| 405 | |
| 406 | QString KateScriptDocument::wordAt(int line, int column) |
| 407 | { |
| 408 | const KTextEditor::Cursor cursor(line, column); |
| 409 | return m_document->wordAt(cursor); |
| 410 | } |
| 411 | |
| 412 | QString KateScriptDocument::wordAt(const QJSValue &jscursor) |
| 413 | { |
| 414 | const auto cursor = cursorFromScriptValue(obj: jscursor); |
| 415 | return wordAt(line: cursor.line(), column: cursor.column()); |
| 416 | } |
| 417 | |
| 418 | QJSValue KateScriptDocument::wordRangeAt(int line, int column) |
| 419 | { |
| 420 | const KTextEditor::Cursor cursor(line, column); |
| 421 | return rangeToScriptValue(engine: m_engine, range: m_document->wordRangeAt(cursor)); |
| 422 | } |
| 423 | |
| 424 | QJSValue KateScriptDocument::wordRangeAt(const QJSValue &jscursor) |
| 425 | { |
| 426 | const auto cursor = cursorFromScriptValue(obj: jscursor); |
| 427 | return wordRangeAt(line: cursor.line(), column: cursor.column()); |
| 428 | } |
| 429 | |
| 430 | QString KateScriptDocument::charAt(int line, int column) |
| 431 | { |
| 432 | const KTextEditor::Cursor cursor(line, column); |
| 433 | const QChar c = m_document->characterAt(position: cursor); |
| 434 | return c.isNull() ? QString() : QString(c); |
| 435 | } |
| 436 | |
| 437 | QString KateScriptDocument::charAt(const QJSValue &jscursor) |
| 438 | { |
| 439 | const auto cursor = cursorFromScriptValue(obj: jscursor); |
| 440 | return charAt(line: cursor.line(), column: cursor.column()); |
| 441 | } |
| 442 | |
| 443 | QString KateScriptDocument::firstChar(int line) |
| 444 | { |
| 445 | Kate::TextLine textLine = m_document->plainKateTextLine(i: line); |
| 446 | // check for isNull(), as the returned character then would be "\0" |
| 447 | const QChar c = textLine.at(column: textLine.firstChar()); |
| 448 | return c.isNull() ? QString() : QString(c); |
| 449 | } |
| 450 | |
| 451 | QString KateScriptDocument::lastChar(int line) |
| 452 | { |
| 453 | Kate::TextLine textLine = m_document->plainKateTextLine(i: line); |
| 454 | // check for isNull(), as the returned character then would be "\0" |
| 455 | const QChar c = textLine.at(column: textLine.lastChar()); |
| 456 | return c.isNull() ? QString() : QString(c); |
| 457 | } |
| 458 | |
| 459 | bool KateScriptDocument::isSpace(int line, int column) |
| 460 | { |
| 461 | const KTextEditor::Cursor cursor(line, column); |
| 462 | return m_document->characterAt(position: cursor).isSpace(); |
| 463 | } |
| 464 | |
| 465 | bool KateScriptDocument::isSpace(const QJSValue &jscursor) |
| 466 | { |
| 467 | const auto cursor = cursorFromScriptValue(obj: jscursor); |
| 468 | return isSpace(line: cursor.line(), column: cursor.column()); |
| 469 | } |
| 470 | |
| 471 | bool KateScriptDocument::matchesAt(int line, int column, const QString &s) |
| 472 | { |
| 473 | Kate::TextLine textLine = m_document->plainKateTextLine(i: line); |
| 474 | return textLine.matchesAt(column, match: s); |
| 475 | } |
| 476 | |
| 477 | bool KateScriptDocument::matchesAt(const QJSValue &jscursor, const QString &s) |
| 478 | { |
| 479 | const auto cursor = cursorFromScriptValue(obj: jscursor); |
| 480 | return matchesAt(line: cursor.line(), column: cursor.column(), s); |
| 481 | } |
| 482 | |
| 483 | bool KateScriptDocument::setText(const QString &s) |
| 484 | { |
| 485 | return m_document->setText(s); |
| 486 | } |
| 487 | |
| 488 | bool KateScriptDocument::clear() |
| 489 | { |
| 490 | return m_document->clear(); |
| 491 | } |
| 492 | |
| 493 | bool KateScriptDocument::truncate(int line, int column) |
| 494 | { |
| 495 | Kate::TextLine textLine = m_document->plainKateTextLine(i: line); |
| 496 | if (textLine.text().size() < column) { |
| 497 | return false; |
| 498 | } |
| 499 | return removeText(fromLine: line, fromColumn: column, toLine: line, toColumn: textLine.text().size() - column); |
| 500 | } |
| 501 | |
| 502 | bool KateScriptDocument::truncate(const QJSValue &jscursor) |
| 503 | { |
| 504 | const auto cursor = cursorFromScriptValue(obj: jscursor); |
| 505 | return truncate(line: cursor.line(), column: cursor.column()); |
| 506 | } |
| 507 | |
| 508 | bool KateScriptDocument::insertText(int line, int column, const QString &s) |
| 509 | { |
| 510 | KTextEditor::Cursor cursor(line, column); |
| 511 | return m_document->insertText(position: cursor, s); |
| 512 | } |
| 513 | |
| 514 | bool KateScriptDocument::insertText(const QJSValue &jscursor, const QString &s) |
| 515 | { |
| 516 | const auto cursor = cursorFromScriptValue(obj: jscursor); |
| 517 | return insertText(line: cursor.line(), column: cursor.column(), s); |
| 518 | } |
| 519 | |
| 520 | bool KateScriptDocument::removeText(int fromLine, int fromColumn, int toLine, int toColumn) |
| 521 | { |
| 522 | const KTextEditor::Range range(fromLine, fromColumn, toLine, toColumn); |
| 523 | return m_document->removeText(range); |
| 524 | } |
| 525 | |
| 526 | bool KateScriptDocument::removeText(const QJSValue &jsfrom, const QJSValue &jsto) |
| 527 | { |
| 528 | const KTextEditor::Cursor from = cursorFromScriptValue(obj: jsfrom); |
| 529 | const KTextEditor::Cursor to = cursorFromScriptValue(obj: jsto); |
| 530 | return removeText(fromLine: from.line(), fromColumn: from.column(), toLine: to.line(), toColumn: to.column()); |
| 531 | } |
| 532 | |
| 533 | bool KateScriptDocument::removeText(const QJSValue &jsrange) |
| 534 | { |
| 535 | const auto range = rangeFromScriptValue(obj: jsrange); |
| 536 | return removeText(fromLine: range.start().line(), fromColumn: range.start().column(), toLine: range.end().line(), toColumn: range.end().column()); |
| 537 | } |
| 538 | |
| 539 | bool KateScriptDocument::insertLine(int line, const QString &s) |
| 540 | { |
| 541 | return m_document->insertLine(line, s); |
| 542 | } |
| 543 | |
| 544 | bool KateScriptDocument::removeLine(int line) |
| 545 | { |
| 546 | return m_document->removeLine(line); |
| 547 | } |
| 548 | |
| 549 | bool KateScriptDocument::wrapLine(int line, int column) |
| 550 | { |
| 551 | return m_document->editWrapLine(line, col: column); |
| 552 | } |
| 553 | |
| 554 | bool KateScriptDocument::wrapLine(const QJSValue &jscursor) |
| 555 | { |
| 556 | const auto cursor = cursorFromScriptValue(obj: jscursor); |
| 557 | return wrapLine(line: cursor.line(), column: cursor.column()); |
| 558 | } |
| 559 | |
| 560 | void KateScriptDocument::joinLines(int startLine, int endLine) |
| 561 | { |
| 562 | m_document->joinLines(first: startLine, last: endLine); |
| 563 | } |
| 564 | |
| 565 | int KateScriptDocument::lines() |
| 566 | { |
| 567 | return m_document->lines(); |
| 568 | } |
| 569 | |
| 570 | bool KateScriptDocument::isLineModified(int line) |
| 571 | { |
| 572 | return m_document->isLineModified(line); |
| 573 | } |
| 574 | |
| 575 | bool KateScriptDocument::isLineSaved(int line) |
| 576 | { |
| 577 | return m_document->isLineSaved(line); |
| 578 | } |
| 579 | |
| 580 | bool KateScriptDocument::isLineTouched(int line) |
| 581 | { |
| 582 | return m_document->isLineTouched(line); |
| 583 | } |
| 584 | |
| 585 | int KateScriptDocument::findTouchedLine(int startLine, bool down) |
| 586 | { |
| 587 | return m_document->findTouchedLine(startLine, down); |
| 588 | } |
| 589 | |
| 590 | int KateScriptDocument::length() |
| 591 | { |
| 592 | return m_document->totalCharacters(); |
| 593 | } |
| 594 | |
| 595 | int KateScriptDocument::lineLength(int line) |
| 596 | { |
| 597 | return m_document->lineLength(line); |
| 598 | } |
| 599 | |
| 600 | void KateScriptDocument::editBegin() |
| 601 | { |
| 602 | m_document->editStart(); |
| 603 | } |
| 604 | |
| 605 | void KateScriptDocument::editEnd() |
| 606 | { |
| 607 | m_document->editEnd(); |
| 608 | } |
| 609 | |
| 610 | bool KateScriptDocument::isValidTextPosition(int line, int column) |
| 611 | { |
| 612 | return m_document->isValidTextPosition(cursor: KTextEditor::Cursor(line, column)); |
| 613 | } |
| 614 | |
| 615 | bool KateScriptDocument::isValidTextPosition(const QJSValue &cursor) |
| 616 | { |
| 617 | return m_document->isValidTextPosition(cursor: cursorFromScriptValue(obj: cursor)); |
| 618 | } |
| 619 | |
| 620 | int KateScriptDocument::firstColumn(int line) |
| 621 | { |
| 622 | Kate::TextLine textLine = m_document->plainKateTextLine(i: line); |
| 623 | return textLine.firstChar(); |
| 624 | } |
| 625 | |
| 626 | int KateScriptDocument::lastColumn(int line) |
| 627 | { |
| 628 | Kate::TextLine textLine = m_document->plainKateTextLine(i: line); |
| 629 | return textLine.lastChar(); |
| 630 | } |
| 631 | |
| 632 | int KateScriptDocument::prevNonSpaceColumn(int line, int column) |
| 633 | { |
| 634 | Kate::TextLine textLine = m_document->plainKateTextLine(i: line); |
| 635 | return textLine.previousNonSpaceChar(pos: column); |
| 636 | } |
| 637 | |
| 638 | int KateScriptDocument::prevNonSpaceColumn(const QJSValue &jscursor) |
| 639 | { |
| 640 | const auto cursor = cursorFromScriptValue(obj: jscursor); |
| 641 | return prevNonSpaceColumn(line: cursor.line(), column: cursor.column()); |
| 642 | } |
| 643 | |
| 644 | int KateScriptDocument::nextNonSpaceColumn(int line, int column) |
| 645 | { |
| 646 | Kate::TextLine textLine = m_document->plainKateTextLine(i: line); |
| 647 | return textLine.nextNonSpaceChar(pos: column); |
| 648 | } |
| 649 | |
| 650 | int KateScriptDocument::nextNonSpaceColumn(const QJSValue &jscursor) |
| 651 | { |
| 652 | const auto cursor = cursorFromScriptValue(obj: jscursor); |
| 653 | return nextNonSpaceColumn(line: cursor.line(), column: cursor.column()); |
| 654 | } |
| 655 | |
| 656 | int KateScriptDocument::prevNonEmptyLine(int line) |
| 657 | { |
| 658 | const int startLine = line; |
| 659 | for (int currentLine = startLine; currentLine >= 0; --currentLine) { |
| 660 | Kate::TextLine textLine = m_document->plainKateTextLine(i: currentLine); |
| 661 | if (textLine.firstChar() != -1) { |
| 662 | return currentLine; |
| 663 | } |
| 664 | } |
| 665 | return -1; |
| 666 | } |
| 667 | |
| 668 | int KateScriptDocument::nextNonEmptyLine(int line) |
| 669 | { |
| 670 | const int startLine = line; |
| 671 | for (int currentLine = startLine; currentLine < m_document->lines(); ++currentLine) { |
| 672 | Kate::TextLine textLine = m_document->plainKateTextLine(i: currentLine); |
| 673 | if (textLine.firstChar() != -1) { |
| 674 | return currentLine; |
| 675 | } |
| 676 | } |
| 677 | return -1; |
| 678 | } |
| 679 | |
| 680 | bool KateScriptDocument::isInWord(const QString &character, int attribute) |
| 681 | { |
| 682 | return m_document->highlight()->isInWord(c: character.at(i: 0), attrib: attribute); |
| 683 | } |
| 684 | |
| 685 | bool KateScriptDocument::canBreakAt(const QString &character, int attribute) |
| 686 | { |
| 687 | return m_document->highlight()->canBreakAt(c: character.at(i: 0), attrib: attribute); |
| 688 | } |
| 689 | |
| 690 | bool KateScriptDocument::(int startAttribute, int endAttribute) |
| 691 | { |
| 692 | return m_document->highlight()->canComment(startAttr: startAttribute, endAttr: endAttribute); |
| 693 | } |
| 694 | |
| 695 | QString KateScriptDocument::(int attribute) |
| 696 | { |
| 697 | return m_document->highlight()->getCommentSingleLineStart(attrib: attribute); |
| 698 | } |
| 699 | |
| 700 | QString KateScriptDocument::(int attribute) |
| 701 | { |
| 702 | return m_document->highlight()->getCommentStart(attrib: attribute); |
| 703 | } |
| 704 | |
| 705 | QString KateScriptDocument::(int attribute) |
| 706 | { |
| 707 | return m_document->highlight()->getCommentEnd(attrib: attribute); |
| 708 | } |
| 709 | |
| 710 | QJSValue KateScriptDocument::documentRange() |
| 711 | { |
| 712 | return rangeToScriptValue(engine: m_engine, range: m_document->documentRange()); |
| 713 | } |
| 714 | |
| 715 | QJSValue KateScriptDocument::documentEnd() |
| 716 | { |
| 717 | return cursorToScriptValue(engine: m_engine, cursor: m_document->documentEnd()); |
| 718 | } |
| 719 | |
| 720 | int KateScriptDocument::attribute(int line, int column) |
| 721 | { |
| 722 | Kate::TextLine textLine = m_document->kateTextLine(i: line); |
| 723 | return textLine.attribute(pos: column); |
| 724 | } |
| 725 | |
| 726 | int KateScriptDocument::attribute(const QJSValue &jscursor) |
| 727 | { |
| 728 | const auto cursor = cursorFromScriptValue(obj: jscursor); |
| 729 | return attribute(line: cursor.line(), column: cursor.column()); |
| 730 | } |
| 731 | |
| 732 | bool KateScriptDocument::isAttribute(int line, int column, int attr) |
| 733 | { |
| 734 | return attr == attribute(line, column); |
| 735 | } |
| 736 | |
| 737 | bool KateScriptDocument::isAttribute(const QJSValue &jscursor, int attr) |
| 738 | { |
| 739 | const auto cursor = cursorFromScriptValue(obj: jscursor); |
| 740 | return isAttribute(line: cursor.line(), column: cursor.column(), attr); |
| 741 | } |
| 742 | |
| 743 | QString KateScriptDocument::attributeName(int line, int column) |
| 744 | { |
| 745 | return m_document->highlight()->nameForAttrib(attrib: document()->plainKateTextLine(i: line).attribute(pos: column)); |
| 746 | } |
| 747 | |
| 748 | QString KateScriptDocument::attributeName(const QJSValue &jscursor) |
| 749 | { |
| 750 | const auto cursor = cursorFromScriptValue(obj: jscursor); |
| 751 | return attributeName(line: cursor.line(), column: cursor.column()); |
| 752 | } |
| 753 | |
| 754 | bool KateScriptDocument::isAttributeName(int line, int column, const QString &name) |
| 755 | { |
| 756 | return name == attributeName(line, column); |
| 757 | } |
| 758 | |
| 759 | bool KateScriptDocument::isAttributeName(const QJSValue &jscursor, const QString &name) |
| 760 | { |
| 761 | const auto cursor = cursorFromScriptValue(obj: jscursor); |
| 762 | return isAttributeName(line: cursor.line(), column: cursor.column(), name); |
| 763 | } |
| 764 | |
| 765 | QString KateScriptDocument::variable(const QString &s) |
| 766 | { |
| 767 | return m_document->variable(name: s); |
| 768 | } |
| 769 | |
| 770 | void KateScriptDocument::setVariable(const QString &s, const QString &v) |
| 771 | { |
| 772 | m_document->setVariable(name: s, value: v); |
| 773 | } |
| 774 | |
| 775 | bool KateScriptDocument::_isCode(int defaultStyle) |
| 776 | { |
| 777 | using S = KSyntaxHighlighting::Theme::TextStyle; |
| 778 | return (defaultStyle != S::Comment && defaultStyle != S::Alert && defaultStyle != S::String && defaultStyle != S::RegionMarker && defaultStyle != S::Char |
| 779 | && defaultStyle != S::Others); |
| 780 | } |
| 781 | |
| 782 | void KateScriptDocument::indent(const QJSValue &jsrange, int change) |
| 783 | { |
| 784 | const auto range = rangeFromScriptValue(obj: jsrange); |
| 785 | m_document->indent(range, change); |
| 786 | } |
| 787 | |
| 788 | #include "moc_katescriptdocument.cpp" |
| 789 | |