Changeset View
Changeset View
Standalone View
Standalone View
src/script/katescriptdocument.h
Show All 16 Lines | |||||
17 | // the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 17 | // the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
18 | // Boston, MA 02110-1301, USA. | 18 | // Boston, MA 02110-1301, USA. | ||
19 | 19 | | |||
20 | #ifndef KATE_SCRIPT_DOCUMENT_H | 20 | #ifndef KATE_SCRIPT_DOCUMENT_H | ||
21 | #define KATE_SCRIPT_DOCUMENT_H | 21 | #define KATE_SCRIPT_DOCUMENT_H | ||
22 | 22 | | |||
23 | #include <QObject> | 23 | #include <QObject> | ||
24 | #include <QStringList> | 24 | #include <QStringList> | ||
25 | #include <QScriptable> | | |||
26 | 25 | | |||
27 | #include <ktexteditor_export.h> | 26 | #include <ktexteditor_export.h> | ||
28 | 27 | | |||
29 | #include <QScriptValue> | 28 | #include <QtQml/QJSValue> | ||
30 | 29 | | |||
31 | #include <ktexteditor/cursor.h> | 30 | #include <ktexteditor/cursor.h> | ||
32 | #include <ktexteditor/range.h> | 31 | #include <ktexteditor/range.h> | ||
33 | 32 | | |||
34 | namespace KTextEditor { class DocumentPrivate; } | 33 | namespace KTextEditor { class DocumentPrivate; } | ||
35 | 34 | | |||
36 | /** | 35 | /** | ||
37 | * Thinish wrapping around KTextEditor::DocumentPrivate, exposing the methods we want exposed | 36 | * Thinish wrapping around KTextEditor::DocumentPrivate, exposing the methods we want exposed | ||
38 | * and adding some helper methods. | 37 | * and adding some helper methods. | ||
39 | * | 38 | * | ||
40 | * We inherit from QScriptable to have more thight access to the scripting | | |||
41 | * engine. | | |||
42 | * | | |||
43 | * setDocument _must_ be called before using any other method. This is not checked | 39 | * setDocument _must_ be called before using any other method. This is not checked | ||
44 | * for the sake of speed. | 40 | * for the sake of speed. | ||
45 | */ | 41 | */ | ||
46 | class KTEXTEDITOR_EXPORT KateScriptDocument : public QObject, protected QScriptable | 42 | class KTEXTEDITOR_EXPORT KateScriptDocument : public QObject | ||
47 | { | 43 | { | ||
48 | Q_OBJECT | 44 | Q_OBJECT | ||
49 | // Note: we have no Q_PROPERTIES due to consistency: everything is a function. | 45 | // Note: we have no Q_PROPERTIES due to consistency: everything is a function. | ||
50 | 46 | | |||
51 | public: | 47 | public: | ||
52 | KateScriptDocument(QObject *parent = nullptr); | 48 | KateScriptDocument(QJSEngine *, QObject *parent = nullptr); | ||
53 | void setDocument(KTextEditor::DocumentPrivate *document); | 49 | void setDocument(KTextEditor::DocumentPrivate *document); | ||
54 | KTextEditor::DocumentPrivate *document(); | 50 | KTextEditor::DocumentPrivate *document(); | ||
55 | 51 | | |||
56 | //BEGIN | 52 | //BEGIN | ||
57 | Q_INVOKABLE QString fileName(); | 53 | Q_INVOKABLE QString fileName(); | ||
58 | Q_INVOKABLE QString url(); | 54 | Q_INVOKABLE QString url(); | ||
59 | Q_INVOKABLE QString mimeType(); | 55 | Q_INVOKABLE QString mimeType(); | ||
60 | Q_INVOKABLE QString encoding(); | 56 | Q_INVOKABLE QString encoding(); | ||
61 | Q_INVOKABLE QString highlightingMode(); | 57 | Q_INVOKABLE QString highlightingMode(); | ||
62 | Q_INVOKABLE QStringList embeddedHighlightingModes(); | 58 | Q_INVOKABLE QStringList embeddedHighlightingModes(); | ||
63 | Q_INVOKABLE QString highlightingModeAt(const KTextEditor::Cursor &pos); | 59 | Q_INVOKABLE QString highlightingModeAt(const QJSValue &pos); | ||
64 | Q_INVOKABLE bool isModified(); | 60 | Q_INVOKABLE bool isModified(); | ||
65 | Q_INVOKABLE QString text(); | 61 | Q_INVOKABLE QString text(); | ||
66 | Q_INVOKABLE QString text(int fromLine, int fromColumn, int toLine, int toColumn); | 62 | Q_INVOKABLE QString text(int fromLine, int fromColumn, int toLine, int toColumn); | ||
67 | Q_INVOKABLE QString text(const KTextEditor::Cursor &from, const KTextEditor::Cursor &to); | 63 | QString text(const KTextEditor::Cursor &from, const KTextEditor::Cursor &to); | ||
68 | Q_INVOKABLE QString text(const KTextEditor::Range &range); | 64 | QString text(const KTextEditor::Range &range); | ||
65 | Q_INVOKABLE QString text(const QJSValue &jsrange); | ||||
dhaumann: If I understand correctly, you are adding an overload for all functions that have KTextEditor… | |||||
69 | Q_INVOKABLE QString line(int line); | 66 | Q_INVOKABLE QString line(int line); | ||
70 | Q_INVOKABLE QString wordAt(int line, int column); | 67 | Q_INVOKABLE QString wordAt(int line, int column); | ||
71 | Q_INVOKABLE QString wordAt(const KTextEditor::Cursor &cursor); | 68 | QString wordAt(const KTextEditor::Cursor &cursor); | ||
72 | Q_INVOKABLE KTextEditor::Range wordRangeAt(int line, int column); | 69 | Q_INVOKABLE QJSValue wordRangeAt(int line, int column); | ||
73 | Q_INVOKABLE KTextEditor::Range wordRangeAt(const KTextEditor::Cursor &cursor); | 70 | QJSValue wordRangeAt(const KTextEditor::Cursor &cursor); | ||
74 | Q_INVOKABLE QString charAt(int line, int column); | 71 | Q_INVOKABLE QString charAt(int line, int column); | ||
75 | Q_INVOKABLE QString charAt(const KTextEditor::Cursor &cursor); | 72 | QString charAt(const KTextEditor::Cursor &cursor); | ||
73 | Q_INVOKABLE QString charAt(const QJSValue &cursor); | ||||
76 | Q_INVOKABLE QString firstChar(int line); | 74 | Q_INVOKABLE QString firstChar(int line); | ||
77 | Q_INVOKABLE QString lastChar(int line); | 75 | Q_INVOKABLE QString lastChar(int line); | ||
78 | Q_INVOKABLE bool isSpace(int line, int column); | 76 | Q_INVOKABLE bool isSpace(int line, int column); | ||
79 | Q_INVOKABLE bool isSpace(const KTextEditor::Cursor &cursor); | 77 | bool isSpace(const KTextEditor::Cursor &cursor); | ||
78 | Q_INVOKABLE bool isSpace(const QJSValue &jscursor); | ||||
80 | Q_INVOKABLE bool matchesAt(int line, int column, const QString &s); | 79 | Q_INVOKABLE bool matchesAt(int line, int column, const QString &s); | ||
81 | Q_INVOKABLE bool matchesAt(const KTextEditor::Cursor &cursor, const QString &s); | 80 | bool matchesAt(const KTextEditor::Cursor &cursor, const QString &s); | ||
81 | Q_INVOKABLE bool matchesAt(const QJSValue &cursor, const QString &s); | ||||
82 | Q_INVOKABLE bool setText(const QString &s); | 82 | Q_INVOKABLE bool setText(const QString &s); | ||
83 | Q_INVOKABLE bool clear(); | 83 | Q_INVOKABLE bool clear(); | ||
84 | Q_INVOKABLE bool truncate(int line, int column); | 84 | Q_INVOKABLE bool truncate(int line, int column); | ||
85 | Q_INVOKABLE bool truncate(const KTextEditor::Cursor &cursor); | 85 | bool truncate(const KTextEditor::Cursor &cursor); | ||
86 | Q_INVOKABLE bool truncate(const QJSValue &cursor); | ||||
86 | Q_INVOKABLE bool insertText(int line, int column, const QString &s); | 87 | Q_INVOKABLE bool insertText(int line, int column, const QString &s); | ||
87 | Q_INVOKABLE bool insertText(const KTextEditor::Cursor &cursor, const QString &s); | 88 | bool insertText(const KTextEditor::Cursor &cursor, const QString &s); | ||
89 | Q_INVOKABLE bool insertText(const QJSValue &jscursor, const QString &s); | ||||
88 | Q_INVOKABLE bool removeText(int fromLine, int fromColumn, int toLine, int toColumn); | 90 | Q_INVOKABLE bool removeText(int fromLine, int fromColumn, int toLine, int toColumn); | ||
89 | Q_INVOKABLE bool removeText(const KTextEditor::Cursor &from, const KTextEditor::Cursor &to); | 91 | bool removeText(const KTextEditor::Cursor &from, const KTextEditor::Cursor &to); | ||
90 | Q_INVOKABLE bool removeText(const KTextEditor::Range &range); | 92 | bool removeText(const KTextEditor::Range &range); | ||
93 | Q_INVOKABLE bool removeText(const QJSValue &range); | ||||
91 | Q_INVOKABLE bool insertLine(int line, const QString &s); | 94 | Q_INVOKABLE bool insertLine(int line, const QString &s); | ||
92 | Q_INVOKABLE bool removeLine(int line); | 95 | Q_INVOKABLE bool removeLine(int line); | ||
93 | Q_INVOKABLE bool wrapLine(int line, int column); | 96 | Q_INVOKABLE bool wrapLine(int line, int column); | ||
94 | Q_INVOKABLE bool wrapLine(const KTextEditor::Cursor &cursor); | 97 | bool wrapLine(const KTextEditor::Cursor &cursor); | ||
98 | Q_INVOKABLE bool wrapLine(const QJSValue &cursor); | ||||
95 | Q_INVOKABLE void joinLines(int startLine, int endLine); | 99 | Q_INVOKABLE void joinLines(int startLine, int endLine); | ||
96 | Q_INVOKABLE int lines(); | 100 | Q_INVOKABLE int lines(); | ||
97 | Q_INVOKABLE bool isLineModified(int line); | 101 | Q_INVOKABLE bool isLineModified(int line); | ||
98 | Q_INVOKABLE bool isLineSaved(int line); | 102 | Q_INVOKABLE bool isLineSaved(int line); | ||
99 | Q_INVOKABLE bool isLineTouched(int line); | 103 | Q_INVOKABLE bool isLineTouched(int line); | ||
100 | Q_INVOKABLE int findTouchedLine(int startLine, bool down); | 104 | Q_INVOKABLE int findTouchedLine(int startLine, bool down); | ||
101 | Q_INVOKABLE int length(); | 105 | Q_INVOKABLE int length(); | ||
102 | Q_INVOKABLE int lineLength(int line); | 106 | Q_INVOKABLE int lineLength(int line); | ||
103 | Q_INVOKABLE void editBegin(); | 107 | Q_INVOKABLE void editBegin(); | ||
104 | Q_INVOKABLE void editEnd(); | 108 | Q_INVOKABLE void editEnd(); | ||
105 | Q_INVOKABLE int firstColumn(int line); | 109 | Q_INVOKABLE int firstColumn(int line); | ||
106 | Q_INVOKABLE int lastColumn(int line); | 110 | Q_INVOKABLE int lastColumn(int line); | ||
107 | Q_INVOKABLE int prevNonSpaceColumn(int line, int column); | 111 | Q_INVOKABLE int prevNonSpaceColumn(int line, int column); | ||
108 | Q_INVOKABLE int prevNonSpaceColumn(const KTextEditor::Cursor &cursor); | 112 | int prevNonSpaceColumn(const KTextEditor::Cursor &cursor); | ||
113 | Q_INVOKABLE int prevNonSpaceColumn(const QJSValue &cursor); | ||||
109 | Q_INVOKABLE int nextNonSpaceColumn(int line, int column); | 114 | Q_INVOKABLE int nextNonSpaceColumn(int line, int column); | ||
110 | Q_INVOKABLE int nextNonSpaceColumn(const KTextEditor::Cursor &cursor); | 115 | int nextNonSpaceColumn(const KTextEditor::Cursor &cursor); | ||
116 | Q_INVOKABLE int nextNonSpaceColumn(const QJSValue &cursor); | ||||
111 | Q_INVOKABLE int prevNonEmptyLine(int line); | 117 | Q_INVOKABLE int prevNonEmptyLine(int line); | ||
112 | Q_INVOKABLE int nextNonEmptyLine(int line); | 118 | Q_INVOKABLE int nextNonEmptyLine(int line); | ||
113 | Q_INVOKABLE bool isInWord(const QString &character, int attribute); | 119 | Q_INVOKABLE bool isInWord(const QString &character, int attribute); | ||
114 | Q_INVOKABLE bool canBreakAt(const QString &character, int attribute); | 120 | Q_INVOKABLE bool canBreakAt(const QString &character, int attribute); | ||
115 | Q_INVOKABLE bool canComment(int startAttribute, int endAttribute); | 121 | Q_INVOKABLE bool canComment(int startAttribute, int endAttribute); | ||
116 | Q_INVOKABLE QString commentMarker(int attribute); | 122 | Q_INVOKABLE QString commentMarker(int attribute); | ||
117 | Q_INVOKABLE QString commentStart(int attribute); | 123 | Q_INVOKABLE QString commentStart(int attribute); | ||
118 | Q_INVOKABLE QString commentEnd(int attribute); | 124 | Q_INVOKABLE QString commentEnd(int attribute); | ||
119 | 125 | | |||
120 | Q_INVOKABLE KTextEditor::Range documentRange(); | 126 | Q_INVOKABLE QJSValue documentRange(); | ||
121 | Q_INVOKABLE KTextEditor::Cursor documentEnd(); | 127 | Q_INVOKABLE QJSValue documentEnd(); | ||
122 | Q_INVOKABLE bool isValidTextPosition(int line, int column); | 128 | Q_INVOKABLE bool isValidTextPosition(int line, int column); | ||
123 | Q_INVOKABLE bool isValidTextPosition(const KTextEditor::Cursor& cursor); | 129 | bool isValidTextPosition(const KTextEditor::Cursor& cursor); | ||
130 | Q_INVOKABLE bool isValidTextPosition(const QJSValue& cursor); | ||||
124 | 131 | | |||
125 | /** | 132 | /** | ||
126 | * Get the syntax highlighting attribute at a given position in the document. | 133 | * Get the syntax highlighting attribute at a given position in the document. | ||
127 | */ | 134 | */ | ||
128 | Q_INVOKABLE int attribute(int line, int column); | 135 | Q_INVOKABLE int attribute(int line, int column); | ||
129 | Q_INVOKABLE int attribute(const KTextEditor::Cursor &cursor); | 136 | int attribute(const KTextEditor::Cursor &cursor); | ||
130 | 137 | | |||
131 | /** | 138 | /** | ||
132 | * Return true if the highlight attribute equals @p attr. | 139 | * Return true if the highlight attribute equals @p attr. | ||
133 | */ | 140 | */ | ||
134 | Q_INVOKABLE bool isAttribute(int line, int column, int attr); | 141 | Q_INVOKABLE bool isAttribute(int line, int column, int attr); | ||
135 | Q_INVOKABLE bool isAttribute(const KTextEditor::Cursor &cursor, int attr); | 142 | bool isAttribute(const KTextEditor::Cursor &cursor, int attr); | ||
136 | 143 | | |||
137 | /** | 144 | /** | ||
138 | * Get the name of the syntax highlighting attribute at the given position. | 145 | * Get the name of the syntax highlighting attribute at the given position. | ||
139 | */ | 146 | */ | ||
140 | Q_INVOKABLE QString attributeName(int line, int column); | 147 | Q_INVOKABLE QString attributeName(int line, int column); | ||
141 | Q_INVOKABLE QString attributeName(const KTextEditor::Cursor &cursor); | 148 | QString attributeName(const KTextEditor::Cursor &cursor); | ||
149 | Q_INVOKABLE QString attributeName(const QJSValue &jscursor); | ||||
142 | 150 | | |||
143 | /** | 151 | /** | ||
144 | * Return true is the name of the syntax attribute equals @p name. | 152 | * Return true is the name of the syntax attribute equals @p name. | ||
145 | */ | 153 | */ | ||
146 | Q_INVOKABLE bool isAttributeName(int line, int column, const QString &name); | 154 | Q_INVOKABLE bool isAttributeName(int line, int column, const QString &name); | ||
147 | Q_INVOKABLE bool isAttributeName(const KTextEditor::Cursor &cursor, const QString &name); | 155 | bool isAttributeName(const KTextEditor::Cursor &cursor, const QString &name); | ||
156 | Q_INVOKABLE bool isAttributeName(const QJSValue &cursor, const QString &name); | ||||
148 | 157 | | |||
149 | Q_INVOKABLE QString variable(const QString &s); | 158 | Q_INVOKABLE QString variable(const QString &s); | ||
150 | Q_INVOKABLE void setVariable(const QString &s, const QString &v); | 159 | Q_INVOKABLE void setVariable(const QString &s, const QString &v); | ||
151 | //END | 160 | //END | ||
152 | 161 | | |||
153 | Q_INVOKABLE int firstVirtualColumn(int line); | 162 | Q_INVOKABLE int firstVirtualColumn(int line); | ||
154 | Q_INVOKABLE int lastVirtualColumn(int line); | 163 | Q_INVOKABLE int lastVirtualColumn(int line); | ||
155 | Q_INVOKABLE int toVirtualColumn(int line, int column); | 164 | Q_INVOKABLE int toVirtualColumn(int line, int column); | ||
156 | Q_INVOKABLE int toVirtualColumn(const KTextEditor::Cursor &cursor); | 165 | int toVirtualColumn(const KTextEditor::Cursor &cursor); | ||
157 | Q_INVOKABLE KTextEditor::Cursor toVirtualCursor(const KTextEditor::Cursor &cursor); | 166 | Q_INVOKABLE int toVirtualColumn(const QJSValue &cursor); | ||
167 | KTextEditor::Cursor toVirtualCursor(const KTextEditor::Cursor &cursor); | ||||
158 | Q_INVOKABLE int fromVirtualColumn(int line, int virtualColumn); | 168 | Q_INVOKABLE int fromVirtualColumn(int line, int virtualColumn); | ||
159 | Q_INVOKABLE int fromVirtualColumn(const KTextEditor::Cursor &virtualCursor); | 169 | int fromVirtualColumn(const KTextEditor::Cursor &virtualCursor); | ||
160 | Q_INVOKABLE KTextEditor::Cursor fromVirtualCursor(const KTextEditor::Cursor &virtualCursor); | 170 | KTextEditor::Cursor fromVirtualCursor(const KTextEditor::Cursor &virtualCursor); | ||
161 | 171 | | |||
162 | Q_INVOKABLE KTextEditor::Cursor anchor(int line, int column, QChar character); | 172 | KTextEditor::Cursor anchorInternal(int line, int column, QChar character); | ||
163 | Q_INVOKABLE KTextEditor::Cursor anchor(const KTextEditor::Cursor &cursor, QChar character); | 173 | KTextEditor::Cursor anchor(const KTextEditor::Cursor &cursor, QChar character); | ||
164 | Q_INVOKABLE KTextEditor::Cursor rfind(int line, int column, const QString &text, int attribute = -1); | 174 | Q_INVOKABLE QJSValue anchor(int line, int column, QChar character); | ||
165 | Q_INVOKABLE KTextEditor::Cursor rfind(const KTextEditor::Cursor &cursor, const QString &text, int attribute = -1); | 175 | Q_INVOKABLE QJSValue anchor(const QJSValue &cursor, QChar character); | ||
176 | KTextEditor::Cursor rfindInternal(int line, int column, const QString &text, int attribute = -1); | ||||
177 | KTextEditor::Cursor rfind(const KTextEditor::Cursor &cursor, const QString &text, int attribute = -1); | ||||
178 | Q_INVOKABLE QJSValue rfind(int line, int column, const QString &text, int attribute = -1); | ||||
179 | Q_INVOKABLE QJSValue rfind(const QJSValue &cursor, const QString &text, int attribute = -1); | ||||
166 | 180 | | |||
167 | Q_INVOKABLE int defStyleNum(int line, int column); | 181 | Q_INVOKABLE int defStyleNum(int line, int column); | ||
168 | Q_INVOKABLE int defStyleNum(const KTextEditor::Cursor &cursor); | 182 | int defStyleNum(const KTextEditor::Cursor &cursor); | ||
183 | Q_INVOKABLE int defStyleNum(const QJSValue &cursor); | ||||
169 | Q_INVOKABLE bool isCode(int line, int column); | 184 | Q_INVOKABLE bool isCode(int line, int column); | ||
170 | Q_INVOKABLE bool isCode(const KTextEditor::Cursor &cursor); | 185 | bool isCode(const KTextEditor::Cursor &cursor); | ||
186 | Q_INVOKABLE bool isCode(const QJSValue &cursor); | ||||
171 | Q_INVOKABLE bool isComment(int line, int column); | 187 | Q_INVOKABLE bool isComment(int line, int column); | ||
172 | Q_INVOKABLE bool isComment(const KTextEditor::Cursor &cursor); | 188 | bool isComment(const KTextEditor::Cursor &cursor); | ||
189 | Q_INVOKABLE bool isComment(const QJSValue &cursor); | ||||
173 | Q_INVOKABLE bool isString(int line, int column); | 190 | Q_INVOKABLE bool isString(int line, int column); | ||
174 | Q_INVOKABLE bool isString(const KTextEditor::Cursor &cursor); | 191 | bool isString(const KTextEditor::Cursor &cursor); | ||
192 | Q_INVOKABLE bool isString(const QJSValue &cursor); | ||||
175 | Q_INVOKABLE bool isRegionMarker(int line, int column); | 193 | Q_INVOKABLE bool isRegionMarker(int line, int column); | ||
176 | Q_INVOKABLE bool isRegionMarker(const KTextEditor::Cursor &cursor); | 194 | bool isRegionMarker(const KTextEditor::Cursor &cursor); | ||
195 | Q_INVOKABLE bool isRegionMarker(const QJSValue &cursor); | ||||
177 | Q_INVOKABLE bool isChar(int line, int column); | 196 | Q_INVOKABLE bool isChar(int line, int column); | ||
178 | Q_INVOKABLE bool isChar(const KTextEditor::Cursor &cursor); | 197 | bool isChar(const KTextEditor::Cursor &cursor); | ||
198 | Q_INVOKABLE bool isChar(const QJSValue &cursor); | ||||
179 | Q_INVOKABLE bool isOthers(int line, int column); | 199 | Q_INVOKABLE bool isOthers(int line, int column); | ||
180 | Q_INVOKABLE bool isOthers(const KTextEditor::Cursor &cursor); | 200 | bool isOthers(const KTextEditor::Cursor &cursor); | ||
201 | Q_INVOKABLE bool isOthers(const QJSValue &cursor); | ||||
181 | 202 | | |||
182 | Q_INVOKABLE bool startsWith(int line, const QString &pattern, bool skipWhiteSpaces); | 203 | Q_INVOKABLE bool startsWith(int line, const QString &pattern, bool skipWhiteSpaces); | ||
183 | Q_INVOKABLE bool endsWith(int line, const QString &pattern, bool skipWhiteSpaces); | 204 | Q_INVOKABLE bool endsWith(int line, const QString &pattern, bool skipWhiteSpaces); | ||
184 | 205 | | |||
185 | Q_INVOKABLE void indent(KTextEditor::Range range, int change); | 206 | void indent(KTextEditor::Range range, int change); | ||
207 | Q_INVOKABLE void indent(const QJSValue &jsrange, int change); | ||||
186 | 208 | | |||
187 | private: | 209 | private: | ||
188 | bool _isCode(int defaultStyle); | 210 | bool _isCode(int defaultStyle); | ||
189 | 211 | | |||
190 | KTextEditor::DocumentPrivate *m_document; | 212 | KTextEditor::DocumentPrivate *m_document; | ||
213 | QJSEngine *m_engine; | ||||
191 | }; | 214 | }; | ||
192 | 215 | | |||
193 | #endif | 216 | #endif | ||
194 | 217 | |
If I understand correctly, you are adding an overload for all functions that have KTextEditor::Cursor or Range in the parameter.
Does that imply we could remove the Q_INVOKABLE part of the overloads that use the complex types such as Cursor and Range?
If so, I would be in favor of doing this, just to make this explicit.