8#include "katescriptdocument.h"
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"
20#include <ktexteditor/documentcursor.h>
31 m_document = document;
39int KateScriptDocument::defStyleNum(
int line,
int column)
44int KateScriptDocument::defStyleNum(
const QJSValue &jscursor)
46 const auto cursor = cursorFromScriptValue(jscursor);
47 return defStyleNum(cursor.line(), cursor.column());
50bool KateScriptDocument::isCode(
int line,
int column)
52 const int defaultStyle = defStyleNum(line, column);
53 return _isCode(defaultStyle);
56bool KateScriptDocument::isCode(
const QJSValue &jscursor)
58 const auto cursor = cursorFromScriptValue(jscursor);
59 return isCode(cursor.line(), cursor.column());
62bool KateScriptDocument::isComment(
int line,
int column)
64 return m_document->isComment(line, column);
67bool KateScriptDocument::isComment(
const QJSValue &jscursor)
69 const auto cursor = cursorFromScriptValue(jscursor);
70 return isComment(cursor.line(), cursor.column());
73bool KateScriptDocument::isString(
int line,
int column)
75 const int defaultStyle = defStyleNum(line, column);
76 return defaultStyle == KSyntaxHighlighting::Theme::TextStyle::String;
79bool KateScriptDocument::isString(
const QJSValue &jscursor)
81 const auto cursor = cursorFromScriptValue(jscursor);
82 return isString(cursor.line(), cursor.column());
85bool KateScriptDocument::isRegionMarker(
int line,
int column)
87 const int defaultStyle = defStyleNum(line, column);
88 return defaultStyle == KSyntaxHighlighting::Theme::TextStyle::RegionMarker;
91bool KateScriptDocument::isRegionMarker(
const QJSValue &jscursor)
93 const auto cursor = cursorFromScriptValue(jscursor);
94 return isRegionMarker(cursor.line(), cursor.column());
97bool KateScriptDocument::isChar(
int line,
int column)
99 const int defaultStyle = defStyleNum(line, column);
100 return defaultStyle == KSyntaxHighlighting::Theme::TextStyle::Char;
103bool KateScriptDocument::isChar(
const QJSValue &jscursor)
105 const auto cursor = cursorFromScriptValue(jscursor);
106 return isChar(cursor.line(), cursor.column());
109bool KateScriptDocument::isOthers(
int line,
int column)
111 const int defaultStyle = defStyleNum(line, column);
112 return defaultStyle == KSyntaxHighlighting::Theme::TextStyle::Others;
115bool KateScriptDocument::isOthers(
const QJSValue &jscursor)
117 const auto cursor = cursorFromScriptValue(jscursor);
118 return isOthers(cursor.line(), cursor.column());
121int KateScriptDocument::firstVirtualColumn(
int line)
123 const int tabWidth = m_document->
config()->tabWidth();
125 if (textLine.firstChar() == -1) {
131int KateScriptDocument::lastVirtualColumn(
int line)
133 const int tabWidth = m_document->
config()->tabWidth();
135 const auto lastPos = textLine.
lastChar();
139 return textLine.toVirtualColumn(lastPos, tabWidth);
142int KateScriptDocument::toVirtualColumn(
int line,
int column)
144 const int tabWidth = m_document->
config()->tabWidth();
146 if (column < 0 || column > textLine.length()) {
152int KateScriptDocument::toVirtualColumn(
const QJSValue &jscursor)
154 const auto cursor = cursorFromScriptValue(jscursor);
155 return toVirtualColumn(cursor.line(), cursor.column());
158QJSValue KateScriptDocument::toVirtualCursor(
int line,
int column)
161 return cursorToScriptValue(m_engine, cursor);
166 const auto cursor = cursorFromScriptValue(jscursor);
167 return toVirtualCursor(cursor.line(), cursor.column());
170int KateScriptDocument::fromVirtualColumn(
int line,
int virtualColumn)
172 const int tabWidth = m_document->
config()->tabWidth();
174 if (virtualColumn < 0 || virtualColumn > textLine.virtualLength(tabWidth)) {
180int KateScriptDocument::fromVirtualColumn(
const QJSValue &jscursor)
182 const auto cursor = cursorFromScriptValue(jscursor);
183 return fromVirtualColumn(cursor.line(), cursor.column());
186QJSValue KateScriptDocument::fromVirtualCursor(
int line,
int column)
189 return cursorToScriptValue(m_engine, cursor);
194 const auto cursor = cursorFromScriptValue(jscursor);
195 return fromVirtualCursor(cursor.line(), cursor.column());
201 const int start = cursor.line();
206 if (cursor.line() !=
start) {
207 cursor.setColumn(textLine.length());
208 }
else if (column >= textLine.length()) {
209 cursor.setColumn(qMax(textLine.length(), 0));
214 bool hasStyle =
true;
223 cursor.setColumn(foundAt);
226 }
while (cursor.gotoPreviousLine());
236QJSValue KateScriptDocument::rfind(
int line,
int column,
const QString &text,
int attribute)
238 return cursorToScriptValue(m_engine, rfindInternal(line, column, text,
attribute));
244 return cursorToScriptValue(m_engine, rfind(cursor, text,
attribute));
261 qCDebug(LOG_KTE) <<
"invalid anchor character:" << character <<
" allowed are: (){}[]";
265 auto *highlighter = m_document->highlight();
266 auto isCodePos = [highlighter](
const Kate::TextLine ¤tLine,
int i) {
273 for (
int l = line; l >= 0; --l) {
280 column = lineText.
length();
282 for (
int i = column - 1; i >= 0; --i) {
283 const QChar ch = lineText[i];
284 if (ch == lc && isCodePos(currentLine, i)) {
286 }
else if (ch == rc && isCodePos(currentLine, i)) {
301 return anchorInternal(cursor.
line(), cursor.
column(), character);
304QJSValue KateScriptDocument::anchor(
int line,
int column,
QChar character)
306 return cursorToScriptValue(m_engine, anchorInternal(line, column, character));
312 return anchor(cursor.
line(), cursor.
column(), character);
315bool KateScriptDocument::startsWith(
int line,
const QString &pattern,
bool skipWhiteSpaces)
319 if (skipWhiteSpaces) {
326bool KateScriptDocument::endsWith(
int line,
const QString &pattern,
bool skipWhiteSpaces)
330 if (skipWhiteSpaces) {
337QString KateScriptDocument::fileName()
342QString KateScriptDocument::url()
347QString KateScriptDocument::mimeType()
352QString KateScriptDocument::encoding()
357QString KateScriptDocument::highlightingMode()
362QStringList KateScriptDocument::embeddedHighlightingModes()
372bool KateScriptDocument::isModified()
377QString KateScriptDocument::text()
379 return m_document->
text();
382QString KateScriptDocument::text(
int fromLine,
int fromColumn,
int toLine,
int toColumn)
385 return m_document->
text(range);
397 const auto range = rangeFromScriptValue(jsrange);
398 return text(range.start().line(), range.start().column(), range.end().line(), range.end().column());
401QString KateScriptDocument::line(
int line)
403 return m_document->
line(line);
406QString KateScriptDocument::wordAt(
int line,
int column)
409 return m_document->
wordAt(cursor);
414 const auto cursor = cursorFromScriptValue(jscursor);
415 return wordAt(cursor.
line(), cursor.
column());
418QJSValue KateScriptDocument::wordRangeAt(
int line,
int column)
421 return rangeToScriptValue(m_engine, m_document->
wordRangeAt(cursor));
426 const auto cursor = cursorFromScriptValue(jscursor);
427 return wordRangeAt(cursor.
line(), cursor.
column());
430QString KateScriptDocument::charAt(
int line,
int column)
439 const auto cursor = cursorFromScriptValue(jscursor);
440 return charAt(cursor.
line(), cursor.
column());
443QString KateScriptDocument::firstChar(
int line)
451QString KateScriptDocument::lastChar(
int line)
459bool KateScriptDocument::isSpace(
int line,
int column)
465bool KateScriptDocument::isSpace(
const QJSValue &jscursor)
467 const auto cursor = cursorFromScriptValue(jscursor);
468 return isSpace(cursor.
line(), cursor.
column());
471bool KateScriptDocument::matchesAt(
int line,
int column,
const QString &s)
477bool KateScriptDocument::matchesAt(
const QJSValue &jscursor,
const QString &s)
479 const auto cursor = cursorFromScriptValue(jscursor);
480 return matchesAt(cursor.
line(), cursor.
column(), s);
483bool KateScriptDocument::setText(
const QString &s)
485 return m_document->setText(s);
488bool KateScriptDocument::clear()
490 return m_document->clear();
493bool KateScriptDocument::truncate(
int line,
int column)
496 if (textLine.
text().
size() < column) {
499 return removeText(line, column, line, textLine.
text().
size() - column);
502bool KateScriptDocument::truncate(
const QJSValue &jscursor)
504 const auto cursor = cursorFromScriptValue(jscursor);
505 return truncate(cursor.
line(), cursor.
column());
508bool KateScriptDocument::insertText(
int line,
int column,
const QString &s)
511 return m_document->insertText(cursor, s);
514bool KateScriptDocument::insertText(
const QJSValue &jscursor,
const QString &s)
516 const auto cursor = cursorFromScriptValue(jscursor);
517 return insertText(cursor.
line(), cursor.
column(), s);
520bool KateScriptDocument::removeText(
int fromLine,
int fromColumn,
int toLine,
int toColumn)
523 return m_document->removeText(range);
526bool KateScriptDocument::removeText(
const QJSValue &jsfrom,
const QJSValue &jsto)
533bool KateScriptDocument::removeText(
const QJSValue &jsrange)
535 const auto range = rangeFromScriptValue(jsrange);
536 return removeText(range.start().line(), range.start().column(), range.end().line(), range.end().column());
539bool KateScriptDocument::insertLine(
int line,
const QString &s)
541 return m_document->insertLine(line, s);
544bool KateScriptDocument::removeLine(
int line)
546 return m_document->removeLine(line);
549bool KateScriptDocument::wrapLine(
int line,
int column)
554bool KateScriptDocument::wrapLine(
const QJSValue &jscursor)
556 const auto cursor = cursorFromScriptValue(jscursor);
557 return wrapLine(cursor.
line(), cursor.
column());
560void KateScriptDocument::joinLines(
int startLine,
int endLine)
562 m_document->
joinLines(startLine, endLine);
565int KateScriptDocument::lines()
567 return m_document->
lines();
570bool KateScriptDocument::isLineModified(
int line)
575bool KateScriptDocument::isLineSaved(
int line)
580bool KateScriptDocument::isLineTouched(
int line)
585int KateScriptDocument::findTouchedLine(
int startLine,
bool down)
590int KateScriptDocument::length()
595int KateScriptDocument::lineLength(
int line)
600void KateScriptDocument::editBegin()
605void KateScriptDocument::editEnd()
610bool KateScriptDocument::isValidTextPosition(
int line,
int column)
615bool KateScriptDocument::isValidTextPosition(
const QJSValue &cursor)
620int KateScriptDocument::firstColumn(
int line)
626int KateScriptDocument::lastColumn(
int line)
632int KateScriptDocument::prevNonSpaceColumn(
int line,
int column)
638int KateScriptDocument::prevNonSpaceColumn(
const QJSValue &jscursor)
640 const auto cursor = cursorFromScriptValue(jscursor);
641 return prevNonSpaceColumn(cursor.line(), cursor.column());
644int KateScriptDocument::nextNonSpaceColumn(
int line,
int column)
650int KateScriptDocument::nextNonSpaceColumn(
const QJSValue &jscursor)
652 const auto cursor = cursorFromScriptValue(jscursor);
653 return nextNonSpaceColumn(cursor.line(), cursor.column());
656int KateScriptDocument::prevNonEmptyLine(
int line)
658 const int startLine = line;
659 for (
int currentLine = startLine; currentLine >= 0; --currentLine) {
668int KateScriptDocument::nextNonEmptyLine(
int line)
670 const int startLine = line;
671 for (
int currentLine = startLine; currentLine < m_document->
lines(); ++currentLine) {
680bool KateScriptDocument::isInWord(
const QString &character,
int attribute)
682 return m_document->highlight()->isInWord(character.
at(0),
attribute);
685bool KateScriptDocument::canBreakAt(
const QString &character,
int attribute)
687 return m_document->highlight()->canBreakAt(character.
at(0),
attribute);
690bool KateScriptDocument::canComment(
int startAttribute,
int endAttribute)
692 return m_document->highlight()->canComment(startAttribute, endAttribute);
695QString KateScriptDocument::commentMarker(
int attribute)
697 return m_document->highlight()->getCommentSingleLineStart(
attribute);
700QString KateScriptDocument::commentStart(
int attribute)
702 return m_document->highlight()->getCommentStart(
attribute);
705QString KateScriptDocument::commentEnd(
int attribute)
707 return m_document->highlight()->getCommentEnd(
attribute);
710QJSValue KateScriptDocument::documentRange()
712 return rangeToScriptValue(m_engine, m_document->
documentRange());
715QJSValue KateScriptDocument::documentEnd()
717 return cursorToScriptValue(m_engine, m_document->
documentEnd());
728 const auto cursor = cursorFromScriptValue(jscursor);
729 return attribute(cursor.line(), cursor.column());
739 const auto cursor = cursorFromScriptValue(jscursor);
740 return isAttribute(cursor.line(), cursor.column(), attr);
745 return m_document->highlight()->nameForAttrib(document()->plainKateTextLine(line).
attribute(column));
750 const auto cursor = cursorFromScriptValue(jscursor);
761 const auto cursor = cursorFromScriptValue(jscursor);
770void KateScriptDocument::setVariable(
const QString &s,
const QString &v)
775bool KateScriptDocument::_isCode(
int defaultStyle)
778 return (defaultStyle != S::Comment && defaultStyle != S::Alert && defaultStyle != S::String && defaultStyle != S::RegionMarker && defaultStyle != S::Char
779 && defaultStyle != S::Others);
782void KateScriptDocument::indent(
const QJSValue &jsrange,
int change)
784 const auto range = rangeFromScriptValue(jsrange);
785 m_document->indent(range, change);
788#include "moc_katescriptdocument.cpp"
The Cursor represents a position in a Document.
constexpr int column() const noexcept
Retrieve the column on which this cursor is situated.
constexpr int line() const noexcept
Retrieve the line on which this cursor is situated.
static constexpr Cursor invalid() noexcept
Returns an invalid cursor.
A Cursor which is bound to a specific Document.
Backend of KTextEditor::Document related public KTextEditor interfaces.
KTextEditor::Cursor documentEnd() const override
End position of the document.
void joinLines(uint first, uint last)
Unwrap a range of lines.
virtual QString variable(const QString &name) const
Returns the value for the variable name.
QString text(KTextEditor::Range range, bool blockwise=false) const override
Get the document content within the given range.
bool isLineModified(int line) const override
Check whether line currently contains unsaved data.
qsizetype totalCharacters() const override
Get the count of characters in the document.
QString highlightingMode() const override
Return the name of the currently used mode.
QString line(int line) const override
Get a single text line.
QString mimeType() override
Tries to detect mime-type based on file name and content of buffer.
QChar characterAt(KTextEditor::Cursor position) const override
Get the character at text position cursor.
QString documentName() const override
Get this document's name.
bool isLineSaved(int line) const override
Check whether line currently contains only saved text.
KateBuffer & buffer()
Get access to buffer of this document.
bool isLineTouched(int line) const override
Check whether line was touched since the file was opened.
int lines() const override
Get the count of lines of the document.
bool editStart()
Enclose editor actions with editStart() and editEnd() to group them.
KSyntaxHighlighting::Theme::TextStyle defStyleNum(int line, int column)
KateDocumentConfig * config()
Configuration.
bool editWrapLine(int line, int col, bool newLine=true, bool *newLineAdded=nullptr, bool notify=true)
Wrap line.
QString highlightingModeAt(KTextEditor::Cursor position) override
Get the highlight mode used at a given position in the document.
Kate::TextLine plainKateTextLine(int i)
Return line lineno.
bool isValidTextPosition(KTextEditor::Cursor cursor) const override
Get whether cursor is a valid text position.
QStringList embeddedHighlightingModes() const override
Get all available highlighting modes for the current document.
Kate::TextLine kateTextLine(int i)
Same as plainKateTextLine(), except that it is made sure the line is highlighted.
QString encoding() const override
Get the current chosen encoding.
bool editEnd()
End a editor operation.
virtual void setVariable(const QString &name, const QString &value)
Set the variable name to value.
int findTouchedLine(int startLine, bool down)
Find the next modified/saved line, starting at startLine.
QString wordAt(KTextEditor::Cursor cursor) const override
Get the word at the text position cursor.
int lineLength(int line) const override
Get the length of a given line in characters.
KTextEditor::Range wordRangeAt(KTextEditor::Cursor cursor) const override
Get the text range for the word located under the text position cursor.
Range documentRange() const
A Range which encompasses the whole document.
An object representing a section of text, from one Cursor to another.
Kate::TextLine plainLine(int lineno)
Return line lineno.
Q_INVOKABLE bool isAttributeName(int line, int column, const QString &name)
Return true is the name of the syntax attribute equals name.
Q_INVOKABLE bool isAttribute(int line, int column, int attr)
Return true if the highlight attribute equals attr.
Q_INVOKABLE int attribute(int line, int column)
Get the syntax highlighting attribute at a given position in the document.
Q_INVOKABLE QString attributeName(int line, int column)
Get the name of the syntax highlighting attribute at the given position.
Class representing a single text line.
int attribute(int pos) const
Gets the attribute at the given position use KRenderer::attributes to get the KTextAttribute for this...
const QString & text() const
Accessor to the text contained in this line.
bool endsWith(const QString &match) const
Returns true, if the line ends with match, otherwise returns false.
int indentDepth(int tabWidth) const
Returns the indentation depth with each tab expanded into tabWidth characters.
int previousNonSpaceChar(int pos) const
Find the position of the previous char that is not a space.
bool startsWith(const QString &match) const
Returns true, if the line starts with match, otherwise returns false.
int lastChar() const
Returns the position of the last non-whitespace character.
QChar at(int column) const
Returns the character at the given column.
int firstChar() const
Returns the position of the first non-whitespace character.
int toVirtualColumn(int column, int tabWidth) const
Returns the column with each tab expanded into tabWidth characters.
bool matchesAt(int column, const QString &match) const
Returns true, if match equals to the text at position column, otherwise returns false.
int nextNonSpaceChar(int pos) const
Find the position of the next char that is not a space.
int fromVirtualColumn(int column, int tabWidth) const
Returns the "real" column where each tab only counts one character.
Q_SCRIPTABLE Q_NOREPLY void start()
bool isNull() const const
bool isSpace(char32_t ucs4)
const QChar at(qsizetype position) const const
qsizetype length() const const
qsizetype size() const const
QStringView left(qsizetype length) const const
qsizetype lastIndexOf(QChar c, Qt::CaseSensitivity cs) const const
QString toString(FormattingOptions options) const const