Changeset View
Changeset View
Standalone View
Standalone View
src/script/katescriptdocument.cpp
Context not available. | |||||
26 | #include "katehighlight.h" | 26 | #include "katehighlight.h" | ||
---|---|---|---|---|---|
27 | #include "katescript.h" | 27 | #include "katescript.h" | ||
28 | #include "katepartdebug.h" | 28 | #include "katepartdebug.h" | ||
29 | #include "scriptcursor.h" | ||||
30 | #include "scriptrange.h" | ||||
29 | 31 | | |||
30 | #include <ktexteditor/documentcursor.h> | 32 | #include <ktexteditor/documentcursor.h> | ||
33 | #include <QJSEngine> | ||||
31 | 34 | | |||
32 | #include <QScriptEngine> | 35 | KateScriptDocument::KateScriptDocument(QJSEngine *engine, QObject *parent) | ||
33 | 36 | : QObject(parent), m_document(nullptr), m_engine(engine) | |||
34 | KateScriptDocument::KateScriptDocument(QObject *parent) | | |||
35 | : QObject(parent), m_document(nullptr) | | |||
36 | { | 37 | { | ||
37 | } | 38 | } | ||
38 | 39 | | |||
Context not available. | |||||
67 | return isCode(cursor.line(), cursor.column()); | 68 | return isCode(cursor.line(), cursor.column()); | ||
68 | } | 69 | } | ||
69 | 70 | | |||
71 | bool KateScriptDocument::isCode(const QJSValue &jscursor) | ||||
72 | { | ||||
73 | KTextEditor::Cursor cursor; | ||||
74 | cursorFromScriptValue(jscursor, cursor); | ||||
75 | return isCode(cursor); | ||||
76 | } | ||||
77 | | ||||
70 | bool KateScriptDocument::isComment(int line, int column) | 78 | bool KateScriptDocument::isComment(int line, int column) | ||
71 | { | 79 | { | ||
72 | return m_document->isComment(line, column); | 80 | return m_document->isComment(line, column); | ||
Context not available. | |||||
77 | return isComment(cursor.line(), cursor.column()); | 85 | return isComment(cursor.line(), cursor.column()); | ||
78 | } | 86 | } | ||
79 | 87 | | |||
88 | bool KateScriptDocument::isComment(const QJSValue &jscursor) | ||||
89 | { | ||||
90 | KTextEditor::Cursor cursor; | ||||
91 | cursorFromScriptValue(jscursor, cursor); | ||||
92 | return isComment(cursor); | ||||
93 | } | ||||
94 | | ||||
80 | bool KateScriptDocument::isString(int line, int column) | 95 | bool KateScriptDocument::isString(int line, int column) | ||
81 | { | 96 | { | ||
82 | const int defaultStyle = defStyleNum(line, column); | 97 | const int defaultStyle = defStyleNum(line, column); | ||
Context not available. | |||||
88 | return isString(cursor.line(), cursor.column()); | 103 | return isString(cursor.line(), cursor.column()); | ||
89 | } | 104 | } | ||
90 | 105 | | |||
106 | bool KateScriptDocument::isString(const QJSValue &jscursor) | ||||
107 | { | ||||
108 | KTextEditor::Cursor cursor; | ||||
109 | cursorFromScriptValue(jscursor, cursor); | ||||
110 | return isString(cursor); | ||||
111 | } | ||||
112 | | ||||
91 | bool KateScriptDocument::isRegionMarker(int line, int column) | 113 | bool KateScriptDocument::isRegionMarker(int line, int column) | ||
92 | { | 114 | { | ||
93 | const int defaultStyle = defStyleNum(line, column); | 115 | const int defaultStyle = defStyleNum(line, column); | ||
Context not available. | |||||
99 | return isRegionMarker(cursor.line(), cursor.column()); | 121 | return isRegionMarker(cursor.line(), cursor.column()); | ||
100 | } | 122 | } | ||
101 | 123 | | |||
124 | bool KateScriptDocument::isRegionMarker(const QJSValue &jscursor) | ||||
125 | { | ||||
126 | KTextEditor::Cursor cursor; | ||||
127 | cursorFromScriptValue(jscursor, cursor); | ||||
128 | return isRegionMarker(cursor); | ||||
129 | } | ||||
130 | | ||||
102 | bool KateScriptDocument::isChar(int line, int column) | 131 | bool KateScriptDocument::isChar(int line, int column) | ||
103 | { | 132 | { | ||
104 | const int defaultStyle = defStyleNum(line, column); | 133 | const int defaultStyle = defStyleNum(line, column); | ||
Context not available. | |||||
110 | return isChar(cursor.line(), cursor.column()); | 139 | return isChar(cursor.line(), cursor.column()); | ||
111 | } | 140 | } | ||
112 | 141 | | |||
142 | bool KateScriptDocument::isChar(const QJSValue &jscursor) | ||||
143 | { | ||||
144 | KTextEditor::Cursor cursor; | ||||
145 | cursorFromScriptValue(jscursor, cursor); | ||||
146 | return isChar(cursor); | ||||
147 | } | ||||
148 | | ||||
113 | bool KateScriptDocument::isOthers(int line, int column) | 149 | bool KateScriptDocument::isOthers(int line, int column) | ||
114 | { | 150 | { | ||
115 | const int defaultStyle = defStyleNum(line, column); | 151 | const int defaultStyle = defStyleNum(line, column); | ||
Context not available. | |||||
121 | return isOthers(cursor.line(), cursor.column()); | 157 | return isOthers(cursor.line(), cursor.column()); | ||
122 | } | 158 | } | ||
123 | 159 | | |||
160 | bool KateScriptDocument::isOthers(const QJSValue &jscursor) | ||||
161 | { | ||||
162 | KTextEditor::Cursor cursor; | ||||
163 | cursorFromScriptValue(jscursor, cursor); | ||||
164 | return isOthers(cursor); | ||||
165 | } | ||||
166 | | ||||
124 | int KateScriptDocument::firstVirtualColumn(int line) | 167 | int KateScriptDocument::firstVirtualColumn(int line) | ||
125 | { | 168 | { | ||
126 | const int tabWidth = m_document->config()->tabWidth(); | 169 | const int tabWidth = m_document->config()->tabWidth(); | ||
Context not available. | |||||
158 | return toVirtualColumn(cursor.line(), cursor.column()); | 201 | return toVirtualColumn(cursor.line(), cursor.column()); | ||
159 | } | 202 | } | ||
160 | 203 | | |||
204 | int KateScriptDocument::toVirtualColumn(const QJSValue &jscursor) | ||||
205 | { | ||||
206 | KTextEditor::Cursor cursor; | ||||
207 | cursorFromScriptValue(jscursor, cursor); | ||||
208 | return toVirtualColumn(cursor); | ||||
209 | } | ||||
210 | | ||||
161 | KTextEditor::Cursor KateScriptDocument::toVirtualCursor(const KTextEditor::Cursor &cursor) | 211 | KTextEditor::Cursor KateScriptDocument::toVirtualCursor(const KTextEditor::Cursor &cursor) | ||
162 | { | 212 | { | ||
163 | return KTextEditor::Cursor(cursor.line(), | 213 | return KTextEditor::Cursor(cursor.line(), | ||
Context not available. | |||||
185 | fromVirtualColumn(virtualCursor.line(), virtualCursor.column())); | 235 | fromVirtualColumn(virtualCursor.line(), virtualCursor.column())); | ||
186 | } | 236 | } | ||
187 | 237 | | |||
188 | KTextEditor::Cursor KateScriptDocument::rfind(int line, int column, const QString &text, int attribute) | 238 | KTextEditor::Cursor KateScriptDocument::rfindInternal(int line, int column, const QString &text, int attribute) | ||
189 | { | 239 | { | ||
190 | KTextEditor::DocumentCursor cursor(document(), line, column); | 240 | KTextEditor::DocumentCursor cursor(document(), line, column); | ||
191 | const int start = cursor.line(); | 241 | const int start = cursor.line(); | ||
Context not available. | |||||
223 | 273 | | |||
224 | KTextEditor::Cursor KateScriptDocument::rfind(const KTextEditor::Cursor &cursor, const QString &text, int attribute) | 274 | KTextEditor::Cursor KateScriptDocument::rfind(const KTextEditor::Cursor &cursor, const QString &text, int attribute) | ||
225 | { | 275 | { | ||
226 | return rfind(cursor.line(), cursor.column(), text, attribute); | 276 | return rfindInternal(cursor.line(), cursor.column(), text, attribute); | ||
277 | } | ||||
278 | | ||||
279 | QJSValue KateScriptDocument::rfind(int line, int column, const QString &text, int attribute) | ||||
280 | { | ||||
281 | return cursorToScriptValue(m_engine, rfindInternal(line, column, text, attribute)); | ||||
227 | } | 282 | } | ||
228 | 283 | | |||
229 | KTextEditor::Cursor KateScriptDocument::anchor(int line, int column, QChar character) | 284 | QJSValue KateScriptDocument::rfind(const QJSValue &jscursor, const QString &text, int attribute) | ||
285 | { | ||||
286 | KTextEditor::Cursor cursor; | ||||
287 | cursorFromScriptValue(jscursor, cursor); | ||||
288 | return cursorToScriptValue(m_engine, rfind(cursor, text, attribute)); | ||||
289 | } | ||||
290 | | ||||
291 | KTextEditor::Cursor KateScriptDocument::anchorInternal(int line, int column, QChar character) | ||||
230 | { | 292 | { | ||
231 | QChar lc; | 293 | QChar lc; | ||
232 | QChar rc; | 294 | QChar rc; | ||
Context not available. | |||||
284 | 346 | | |||
285 | KTextEditor::Cursor KateScriptDocument::anchor(const KTextEditor::Cursor &cursor, QChar character) | 347 | KTextEditor::Cursor KateScriptDocument::anchor(const KTextEditor::Cursor &cursor, QChar character) | ||
286 | { | 348 | { | ||
349 | return anchorInternal(cursor.line(), cursor.column(), character); | ||||
350 | } | ||||
351 | | ||||
352 | QJSValue KateScriptDocument::anchor(int line, int column, QChar character) | ||||
353 | { | ||||
354 | return cursorToScriptValue(m_engine, anchorInternal(line, column, character)); | ||||
355 | } | ||||
356 | | ||||
357 | QJSValue KateScriptDocument::anchor(const QJSValue &jscursor, QChar character) | ||||
358 | { | ||||
359 | KTextEditor::Cursor cursor; | ||||
360 | cursorFromScriptValue(jscursor, cursor); | ||||
287 | return anchor(cursor.line(), cursor.column(), character); | 361 | return anchor(cursor.line(), cursor.column(), character); | ||
288 | } | 362 | } | ||
289 | 363 | | |||
Context not available. | |||||
349 | return m_document->embeddedHighlightingModes(); | 423 | return m_document->embeddedHighlightingModes(); | ||
350 | } | 424 | } | ||
351 | 425 | | |||
352 | QString KateScriptDocument::highlightingModeAt(const KTextEditor::Cursor &pos) | 426 | QString KateScriptDocument::highlightingModeAt(const QJSValue &jspos) | ||
353 | { | 427 | { | ||
428 | KTextEditor::Cursor pos; | ||||
429 | cursorFromScriptValue(jspos, pos); | ||||
354 | return m_document->highlightingModeAt(pos); | 430 | return m_document->highlightingModeAt(pos); | ||
355 | } | 431 | } | ||
356 | 432 | | |||
Context not available. | |||||
379 | return m_document->text(range); | 455 | return m_document->text(range); | ||
380 | } | 456 | } | ||
381 | 457 | | |||
458 | QString KateScriptDocument::text(const QJSValue &jsrange) | ||||
459 | { | ||||
460 | KTextEditor::Range range; | ||||
461 | rangeFromScriptValue(jsrange, range); | ||||
462 | return text(range); | ||||
463 | } | ||||
464 | | ||||
382 | QString KateScriptDocument::line(int line) | 465 | QString KateScriptDocument::line(int line) | ||
383 | { | 466 | { | ||
384 | return m_document->line(line); | 467 | return m_document->line(line); | ||
Context not available. | |||||
386 | 469 | | |||
387 | QString KateScriptDocument::wordAt(int line, int column) | 470 | QString KateScriptDocument::wordAt(int line, int column) | ||
388 | { | 471 | { | ||
389 | return m_document->wordAt(KTextEditor::Cursor(line, column)); | 472 | return wordAt(KTextEditor::Cursor(line, column)); | ||
390 | } | 473 | } | ||
391 | 474 | | |||
392 | QString KateScriptDocument::wordAt(const KTextEditor::Cursor &cursor) | 475 | QString KateScriptDocument::wordAt(const KTextEditor::Cursor &cursor) | ||
Context not available. | |||||
394 | return m_document->wordAt(cursor); | 477 | return m_document->wordAt(cursor); | ||
395 | } | 478 | } | ||
396 | 479 | | |||
397 | KTextEditor::Range KateScriptDocument::wordRangeAt(int line, int column) | 480 | QJSValue KateScriptDocument::wordRangeAt(int line, int column) | ||
398 | { | 481 | { | ||
399 | return wordRangeAt(KTextEditor::Cursor(line, column)); | 482 | return wordRangeAt(KTextEditor::Cursor(line, column)); | ||
400 | } | 483 | } | ||
401 | 484 | | |||
402 | KTextEditor::Range KateScriptDocument::wordRangeAt(const KTextEditor::Cursor &cursor) | 485 | QJSValue KateScriptDocument::wordRangeAt(const KTextEditor::Cursor &cursor) | ||
403 | { | 486 | { | ||
404 | return m_document->wordRangeAt(cursor); | 487 | return rangeToScriptValue(m_engine, m_document->wordRangeAt(cursor)); | ||
405 | } | 488 | } | ||
406 | 489 | | |||
407 | QString KateScriptDocument::charAt(int line, int column) | 490 | QString KateScriptDocument::charAt(int line, int column) | ||
Context not available. | |||||
415 | return c.isNull() ? QString() : QString(c); | 498 | return c.isNull() ? QString() : QString(c); | ||
416 | } | 499 | } | ||
417 | 500 | | |||
501 | QString KateScriptDocument::charAt(const QJSValue &jscursor) | ||||
502 | { | ||||
503 | KTextEditor::Cursor cursor; | ||||
504 | cursorFromScriptValue(jscursor, cursor); | ||||
505 | return charAt(cursor); | ||||
506 | } | ||||
507 | | ||||
418 | QString KateScriptDocument::firstChar(int line) | 508 | QString KateScriptDocument::firstChar(int line) | ||
419 | { | 509 | { | ||
420 | Kate::TextLine textLine = m_document->plainKateTextLine(line); | 510 | Kate::TextLine textLine = m_document->plainKateTextLine(line); | ||
Context not available. | |||||
447 | return m_document->characterAt(cursor).isSpace(); | 537 | return m_document->characterAt(cursor).isSpace(); | ||
448 | } | 538 | } | ||
449 | 539 | | |||
540 | bool KateScriptDocument::isSpace(const QJSValue &jscursor) | ||||
541 | { | ||||
542 | KTextEditor::Cursor cursor; | ||||
543 | cursorFromScriptValue(jscursor, cursor); | ||||
544 | return isSpace(cursor); | ||||
545 | } | ||||
546 | | ||||
450 | bool KateScriptDocument::matchesAt(int line, int column, const QString &s) | 547 | bool KateScriptDocument::matchesAt(int line, int column, const QString &s) | ||
451 | { | 548 | { | ||
452 | Kate::TextLine textLine = m_document->plainKateTextLine(line); | 549 | Kate::TextLine textLine = m_document->plainKateTextLine(line); | ||
Context not available. | |||||
455 | 552 | | |||
456 | bool KateScriptDocument::matchesAt(const KTextEditor::Cursor &cursor, const QString &s) | 553 | bool KateScriptDocument::matchesAt(const KTextEditor::Cursor &cursor, const QString &s) | ||
457 | { | 554 | { | ||
555 | | ||||
458 | return matchesAt(cursor.line(), cursor.column(), s); | 556 | return matchesAt(cursor.line(), cursor.column(), s); | ||
459 | } | 557 | } | ||
460 | 558 | | |||
559 | bool KateScriptDocument::matchesAt(const QJSValue &jscursor, const QString &s) | ||||
560 | { | ||||
561 | | ||||
562 | KTextEditor::Cursor cursor; | ||||
563 | cursorFromScriptValue(jscursor, cursor); | ||||
564 | return matchesAt(cursor, s); | ||||
565 | } | ||||
566 | | ||||
461 | bool KateScriptDocument::setText(const QString &s) | 567 | bool KateScriptDocument::setText(const QString &s) | ||
462 | { | 568 | { | ||
463 | return m_document->setText(s); | 569 | return m_document->setText(s); | ||
Context not available. | |||||
494 | return m_document->insertText(cursor, s); | 600 | return m_document->insertText(cursor, s); | ||
495 | } | 601 | } | ||
496 | 602 | | |||
603 | bool KateScriptDocument::insertText(const QJSValue &jscursor, const QString &s) | ||||
604 | { | ||||
605 | KTextEditor::Cursor cursor; | ||||
606 | cursorFromScriptValue(jscursor, cursor); | ||||
607 | return insertText(cursor, s); | ||||
608 | } | ||||
609 | | ||||
497 | bool KateScriptDocument::removeText(int fromLine, int fromColumn, int toLine, int toColumn) | 610 | bool KateScriptDocument::removeText(int fromLine, int fromColumn, int toLine, int toColumn) | ||
498 | { | 611 | { | ||
499 | return removeText(KTextEditor::Range(fromLine, fromColumn, toLine, toColumn)); | 612 | return removeText(KTextEditor::Range(fromLine, fromColumn, toLine, toColumn)); | ||
Context not available. | |||||
509 | return m_document->removeText(range); | 622 | return m_document->removeText(range); | ||
510 | } | 623 | } | ||
511 | 624 | | |||
625 | bool KateScriptDocument::removeText(const QJSValue &jsrange) | ||||
626 | { | ||||
627 | KTextEditor::Range range; | ||||
628 | rangeFromScriptValue(jsrange, range); | ||||
629 | return removeText(range); | ||||
630 | } | ||||
631 | | ||||
512 | bool KateScriptDocument::insertLine(int line, const QString &s) | 632 | bool KateScriptDocument::insertLine(int line, const QString &s) | ||
513 | { | 633 | { | ||
514 | return m_document->insertLine(line, s); | 634 | return m_document->insertLine(line, s); | ||
Context not available. | |||||
529 | return wrapLine(cursor.line(), cursor.column()); | 649 | return wrapLine(cursor.line(), cursor.column()); | ||
530 | } | 650 | } | ||
531 | 651 | | |||
652 | bool KateScriptDocument::wrapLine(const QJSValue &jscursor) | ||||
653 | { | ||||
654 | KTextEditor::Cursor cursor; | ||||
655 | cursorFromScriptValue(jscursor, cursor); | ||||
656 | return wrapLine(cursor); | ||||
657 | } | ||||
658 | | ||||
532 | void KateScriptDocument::joinLines(int startLine, int endLine) | 659 | void KateScriptDocument::joinLines(int startLine, int endLine) | ||
533 | { | 660 | { | ||
534 | m_document->joinLines(startLine, endLine); | 661 | m_document->joinLines(startLine, endLine); | ||
Context not available. | |||||
689 | return m_document->highlight()->getCommentEnd(attribute); | 816 | return m_document->highlight()->getCommentEnd(attribute); | ||
690 | } | 817 | } | ||
691 | 818 | | |||
692 | KTextEditor::Range KateScriptDocument::documentRange() | 819 | QJSValue KateScriptDocument::documentRange() | ||
693 | { | 820 | { | ||
694 | return m_document->documentRange(); | 821 | return rangeToScriptValue(m_engine, m_document->documentRange()); | ||
695 | } | 822 | } | ||
696 | 823 | | |||
697 | KTextEditor::Cursor KateScriptDocument::documentEnd() | 824 | QJSValue KateScriptDocument::documentEnd() | ||
698 | { | 825 | { | ||
699 | return m_document->documentEnd(); | 826 | return cursorToScriptValue(m_engine, m_document->documentEnd()); | ||
700 | } | 827 | } | ||
701 | 828 | | |||
702 | int KateScriptDocument::attribute(int line, int column) | 829 | int KateScriptDocument::attribute(int line, int column) | ||
Context not available. | |||||
736 | return attributeName(cursor.line(), cursor.column()); | 863 | return attributeName(cursor.line(), cursor.column()); | ||
737 | } | 864 | } | ||
738 | 865 | | |||
866 | QString KateScriptDocument::attributeName(const QJSValue &jscursor) | ||||
867 | { | ||||
868 | KTextEditor::Cursor cursor; | ||||
869 | cursorFromScriptValue(jscursor, cursor); | ||||
870 | return attributeName(cursor); | ||||
871 | } | ||||
872 | | ||||
739 | bool KateScriptDocument::isAttributeName(int line, int column, const QString &name) | 873 | bool KateScriptDocument::isAttributeName(int line, int column, const QString &name) | ||
740 | { | 874 | { | ||
741 | return name == attributeName(line, column); | 875 | return name == attributeName(line, column); | ||
Context not available. | |||||
746 | return isAttributeName(cursor.line(), cursor.column(), name); | 880 | return isAttributeName(cursor.line(), cursor.column(), name); | ||
747 | } | 881 | } | ||
748 | 882 | | |||
883 | bool KateScriptDocument::isAttributeName(const QJSValue &jscursor, const QString &name) | ||||
884 | { | ||||
885 | KTextEditor::Cursor cursor; | ||||
886 | cursorFromScriptValue(jscursor, cursor); | ||||
887 | return isAttributeName(cursor, name); | ||||
888 | } | ||||
889 | | ||||
749 | QString KateScriptDocument::variable(const QString &s) | 890 | QString KateScriptDocument::variable(const QString &s) | ||
750 | { | 891 | { | ||
751 | return m_document->variable(s); | 892 | return m_document->variable(s); | ||
Context not available. | |||||
771 | { | 912 | { | ||
772 | m_document->indent(range, change); | 913 | m_document->indent(range, change); | ||
773 | } | 914 | } | ||
915 | | ||||
916 | void KateScriptDocument::indent(const QJSValue &jsrange, int change) | ||||
917 | { | ||||
918 | KTextEditor::Range range; | ||||
919 | rangeFromScriptValue(jsrange, range); | ||||
920 | indent(range, change); | ||||
921 | } | ||||
Context not available. |