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#ifndef KATE_SCRIPT_DOCUMENT_H
9#define KATE_SCRIPT_DOCUMENT_H
10
11#include <QObject>
12#include <QStringList>
13
14#include <ktexteditor_export.h>
15
16#include <QJSValue>
17
18#include <ktexteditor/cursor.h>
19#include <ktexteditor/range.h>
20
21namespace KTextEditor
22{
23class DocumentPrivate;
24}
25
26/**
27 * Thinish wrapping around KTextEditor::DocumentPrivate, exposing the methods we want exposed
28 * and adding some helper methods.
29 *
30 * setDocument _must_ be called before using any other method. This is not checked
31 * for the sake of speed.
32 */
33class KTEXTEDITOR_EXPORT KateScriptDocument : public QObject
34{
35 Q_OBJECT
36 // Note: we have no Q_PROPERTIES due to consistency: everything is a function.
37
38public:
39 explicit KateScriptDocument(QJSEngine *, QObject *parent = nullptr);
40 void setDocument(KTextEditor::DocumentPrivate *document);
41 KTextEditor::DocumentPrivate *document();
42
43 // BEGIN
44 Q_INVOKABLE QString fileName();
45 Q_INVOKABLE QString url();
46 Q_INVOKABLE QString mimeType();
47 Q_INVOKABLE QString encoding();
48 Q_INVOKABLE QString highlightingMode();
49 Q_INVOKABLE QStringList embeddedHighlightingModes();
50 Q_INVOKABLE QString highlightingModeAt(const QJSValue &pos);
51 Q_INVOKABLE bool isModified();
52 Q_INVOKABLE QString text();
53 Q_INVOKABLE QString text(int fromLine, int fromColumn, int toLine, int toColumn);
54 Q_INVOKABLE QString text(const QJSValue &jsfrom, const QJSValue &jsto);
55 Q_INVOKABLE QString text(const QJSValue &jsrange);
56 Q_INVOKABLE QString line(int line);
57 Q_INVOKABLE QString wordAt(int line, int column);
58 Q_INVOKABLE QString wordAt(const QJSValue &jscursor);
59 Q_INVOKABLE QJSValue wordRangeAt(int line, int column);
60 Q_INVOKABLE QJSValue wordRangeAt(const QJSValue &jscursor);
61 Q_INVOKABLE QString charAt(int line, int column);
62 Q_INVOKABLE QString charAt(const QJSValue &jscursor);
63 Q_INVOKABLE QString firstChar(int line);
64 Q_INVOKABLE QString lastChar(int line);
65 Q_INVOKABLE bool isSpace(int line, int column);
66 Q_INVOKABLE bool isSpace(const QJSValue &jscursor);
67 Q_INVOKABLE bool matchesAt(int line, int column, const QString &s);
68 Q_INVOKABLE bool matchesAt(const QJSValue &cursor, const QString &s);
69 Q_INVOKABLE bool setText(const QString &s);
70 Q_INVOKABLE bool clear();
71 Q_INVOKABLE bool truncate(int line, int column);
72 Q_INVOKABLE bool truncate(const QJSValue &jscursor);
73 Q_INVOKABLE bool insertText(int line, int column, const QString &s);
74 Q_INVOKABLE bool insertText(const QJSValue &jscursor, const QString &s);
75 Q_INVOKABLE bool removeText(int fromLine, int fromColumn, int toLine, int toColumn);
76 Q_INVOKABLE bool removeText(const QJSValue &range);
77 Q_INVOKABLE bool removeText(const QJSValue &jsfrom, const QJSValue &jsto);
78 Q_INVOKABLE bool insertLine(int line, const QString &s);
79 Q_INVOKABLE bool removeLine(int line);
80 Q_INVOKABLE bool wrapLine(int line, int column);
81 Q_INVOKABLE bool wrapLine(const QJSValue &cursor);
82 Q_INVOKABLE void joinLines(int startLine, int endLine);
83 Q_INVOKABLE int lines();
84 Q_INVOKABLE bool isLineModified(int line);
85 Q_INVOKABLE bool isLineSaved(int line);
86 Q_INVOKABLE bool isLineTouched(int line);
87 Q_INVOKABLE int findTouchedLine(int startLine, bool down);
88 Q_INVOKABLE int length();
89 Q_INVOKABLE int lineLength(int line);
90 Q_INVOKABLE void editBegin();
91 Q_INVOKABLE void editEnd();
92 Q_INVOKABLE int firstColumn(int line);
93 Q_INVOKABLE int lastColumn(int line);
94 Q_INVOKABLE int prevNonSpaceColumn(int line, int column);
95 Q_INVOKABLE int prevNonSpaceColumn(const QJSValue &jscursor);
96 Q_INVOKABLE int nextNonSpaceColumn(int line, int column);
97 Q_INVOKABLE int nextNonSpaceColumn(const QJSValue &jscursor);
98 Q_INVOKABLE int prevNonEmptyLine(int line);
99 Q_INVOKABLE int nextNonEmptyLine(int line);
100 Q_INVOKABLE bool isInWord(const QString &character, int attribute);
101 Q_INVOKABLE bool canBreakAt(const QString &character, int attribute);
102 Q_INVOKABLE bool canComment(int startAttribute, int endAttribute);
103 Q_INVOKABLE QString commentMarker(int attribute);
104 Q_INVOKABLE QString commentStart(int attribute);
105 Q_INVOKABLE QString commentEnd(int attribute);
106
107 Q_INVOKABLE QJSValue documentRange();
108 Q_INVOKABLE QJSValue documentEnd();
109 Q_INVOKABLE bool isValidTextPosition(int line, int column);
110 Q_INVOKABLE bool isValidTextPosition(const QJSValue &cursor);
111
112 /**
113 * Get the syntax highlighting attribute at a given position in the document.
114 */
115 Q_INVOKABLE int attribute(int line, int column);
116 Q_INVOKABLE int attribute(const QJSValue &jscursor);
117
118 /**
119 * Return true if the highlight attribute equals @p attr.
120 */
121 Q_INVOKABLE bool isAttribute(int line, int column, int attr);
122 Q_INVOKABLE bool isAttribute(const QJSValue &jscursor, int attr);
123
124 /**
125 * Get the name of the syntax highlighting attribute at the given position.
126 */
127 Q_INVOKABLE QString attributeName(int line, int column);
128 Q_INVOKABLE QString attributeName(const QJSValue &jscursor);
129
130 /**
131 * Return true is the name of the syntax attribute equals @p name.
132 */
133 Q_INVOKABLE bool isAttributeName(int line, int column, const QString &name);
134 Q_INVOKABLE bool isAttributeName(const QJSValue &cursor, const QString &name);
135
136 Q_INVOKABLE QString variable(const QString &s);
137 Q_INVOKABLE void setVariable(const QString &s, const QString &v);
138 // END
139
140 Q_INVOKABLE int firstVirtualColumn(int line);
141 Q_INVOKABLE int lastVirtualColumn(int line);
142 Q_INVOKABLE int toVirtualColumn(int line, int column);
143 Q_INVOKABLE int toVirtualColumn(const QJSValue &cursor);
144 Q_INVOKABLE QJSValue toVirtualCursor(int line, int column);
145 Q_INVOKABLE QJSValue toVirtualCursor(const QJSValue &jscursor);
146 Q_INVOKABLE int fromVirtualColumn(int line, int virtualColumn);
147 Q_INVOKABLE int fromVirtualColumn(const QJSValue &jscursor);
148 Q_INVOKABLE QJSValue fromVirtualCursor(int line, int column);
149 Q_INVOKABLE QJSValue fromVirtualCursor(const QJSValue &jscursor);
150
151 KTextEditor::Cursor anchorInternal(int line, int column, QChar character);
152 KTextEditor::Cursor anchor(KTextEditor::Cursor cursor, QChar character);
153 Q_INVOKABLE QJSValue anchor(int line, int column, QChar character);
154 Q_INVOKABLE QJSValue anchor(const QJSValue &cursor, QChar character);
155 KTextEditor::Cursor rfindInternal(int line, int column, const QString &text, int attribute = -1);
156 KTextEditor::Cursor rfind(KTextEditor::Cursor cursor, const QString &text, int attribute = -1);
157 Q_INVOKABLE QJSValue rfind(int line, int column, const QString &text, int attribute = -1);
158 Q_INVOKABLE QJSValue rfind(const QJSValue &cursor, const QString &text, int attribute = -1);
159
160 Q_INVOKABLE int defStyleNum(int line, int column);
161 Q_INVOKABLE int defStyleNum(const QJSValue &cursor);
162 Q_INVOKABLE bool isCode(int line, int column);
163 Q_INVOKABLE bool isCode(const QJSValue &cursor);
164 Q_INVOKABLE bool isComment(int line, int column);
165 Q_INVOKABLE bool isComment(const QJSValue &cursor);
166 Q_INVOKABLE bool isString(int line, int column);
167 Q_INVOKABLE bool isString(const QJSValue &cursor);
168 Q_INVOKABLE bool isRegionMarker(int line, int column);
169 Q_INVOKABLE bool isRegionMarker(const QJSValue &cursor);
170 Q_INVOKABLE bool isChar(int line, int column);
171 Q_INVOKABLE bool isChar(const QJSValue &cursor);
172 Q_INVOKABLE bool isOthers(int line, int column);
173 Q_INVOKABLE bool isOthers(const QJSValue &cursor);
174
175 Q_INVOKABLE bool startsWith(int line, const QString &pattern, bool skipWhiteSpaces);
176 Q_INVOKABLE bool endsWith(int line, const QString &pattern, bool skipWhiteSpaces);
177
178 Q_INVOKABLE void indent(const QJSValue &jsrange, int change);
179
180private:
181 KTEXTEDITOR_NO_EXPORT
182 static bool _isCode(int defaultStyle);
183
184 KTextEditor::DocumentPrivate *m_document;
185 QJSEngine *m_engine;
186};
187
188#endif
189

source code of ktexteditor/src/script/katescriptdocument.h