Changeset View
Changeset View
Standalone View
Standalone View
autotests/src/testutils.cpp
Context not available. | |||||
30 | #include "kateconfig.h" | 30 | #include "kateconfig.h" | ||
---|---|---|---|---|---|
31 | #include "katedocument.h" | 31 | #include "katedocument.h" | ||
32 | #include "katescripthelpers.h" | 32 | #include "katescripthelpers.h" | ||
33 | #include "ktexteditor/cursor.h" | ||||
34 | #include "ktexteditor/range.h" | ||||
33 | 35 | | |||
34 | #include <QtScript/QScriptEngine> | 36 | #include <QJSEngine> | ||
37 | #include <QObject> | ||||
38 | #include <QQmlEngine> | ||||
35 | #include <QTest> | 39 | #include <QTest> | ||
36 | 40 | | |||
37 | //END Includes | 41 | //END Includes | ||
38 | 42 | | |||
39 | //BEGIN TestScriptEnv | 43 | //BEGIN TestScriptEnv | ||
40 | 44 | | |||
41 | //BEGIN conversion functions for Cursors and Ranges | | |||
42 | /** Converstion function from KTextEditor::Cursor to QtScript cursor */ | | |||
43 | static QScriptValue cursorToScriptValue(QScriptEngine *engine, const KTextEditor::Cursor &cursor) | | |||
44 | { | | |||
45 | QString code = QStringLiteral("new Cursor(%1, %2);").arg(cursor.line()) | | |||
46 | .arg(cursor.column()); | | |||
47 | return engine->evaluate(code); | | |||
48 | } | | |||
49 | | ||||
50 | /** Converstion function from QtScript cursor to KTextEditor::Cursor */ | | |||
51 | static void cursorFromScriptValue(const QScriptValue &obj, KTextEditor::Cursor &cursor) | | |||
52 | { | | |||
53 | cursor.setPosition(obj.property(QStringLiteral("line")).toInt32(), | | |||
54 | obj.property(QStringLiteral("column")).toInt32()); | | |||
55 | } | | |||
56 | | ||||
57 | /** Converstion function from QtScript range to KTextEditor::Range */ | | |||
58 | static QScriptValue rangeToScriptValue(QScriptEngine *engine, const KTextEditor::Range &range) | | |||
59 | { | | |||
60 | QString code = QStringLiteral("new Range(%1, %2, %3, %4);").arg(range.start().line()) | | |||
61 | .arg(range.start().column()) | | |||
62 | .arg(range.end().line()) | | |||
63 | .arg(range.end().column()); | | |||
64 | return engine->evaluate(code); | | |||
65 | } | | |||
66 | | ||||
67 | /** Converstion function from QtScript range to KTextEditor::Range */ | | |||
68 | static void rangeFromScriptValue(const QScriptValue &obj, KTextEditor::Range &range) | | |||
69 | { | | |||
70 | range.setStart(KTextEditor::Cursor( | | |||
71 | obj.property(QStringLiteral("start")).property(QStringLiteral("line")).toInt32(), | | |||
72 | obj.property(QStringLiteral("start")).property(QStringLiteral("column")).toInt32() | | |||
73 | )); | | |||
74 | range.setEnd(KTextEditor::Cursor( | | |||
75 | obj.property(QStringLiteral("end")).property(QStringLiteral("line")).toInt32(), | | |||
76 | obj.property(QStringLiteral("end")).property(QLatin1String("column")).toInt32() | | |||
77 | )); | | |||
78 | } | | |||
79 | //END | | |||
80 | | ||||
81 | TestScriptEnv::TestScriptEnv(KTextEditor::DocumentPrivate *part, bool &cflag) | 45 | TestScriptEnv::TestScriptEnv(KTextEditor::DocumentPrivate *part, bool &cflag) | ||
82 | : m_engine(nullptr), m_viewObj(nullptr), m_docObj(nullptr), m_output(nullptr) | 46 | : m_engine(nullptr), m_viewObj(nullptr), m_docObj(nullptr), m_output(nullptr) | ||
83 | { | 47 | { | ||
84 | m_engine = new QScriptEngine(this); | 48 | m_engine = new QJSEngine(this); | ||
85 | | ||||
86 | qScriptRegisterMetaType(m_engine, cursorToScriptValue, cursorFromScriptValue); | | |||
87 | qScriptRegisterMetaType(m_engine, rangeToScriptValue, rangeFromScriptValue); | | |||
88 | 49 | | |||
89 | // export read & require function and add the require guard object | 50 | // export read & require function and add the require guard object | ||
90 | m_engine->globalObject().setProperty(QStringLiteral("read"), m_engine->newFunction(Kate::Script::read)); | 51 | QJSValue functions = m_engine->newQObject(new Kate::ScriptHelper(m_engine)); | ||
91 | m_engine->globalObject().setProperty(QStringLiteral("require"), m_engine->newFunction(Kate::Script::require)); | 52 | m_engine->globalObject().setProperty(QStringLiteral("functions"), functions); | ||
53 | m_engine->globalObject().setProperty(QStringLiteral("read"), functions.property(QStringLiteral("read"))); | ||||
54 | m_engine->globalObject().setProperty(QStringLiteral("require"), functions.property(QStringLiteral("require"))); | ||||
92 | m_engine->globalObject().setProperty(QStringLiteral("require_guard"), m_engine->newObject()); | 55 | m_engine->globalObject().setProperty(QStringLiteral("require_guard"), m_engine->newObject()); | ||
93 | 56 | | |||
94 | // export debug function | 57 | // export debug function | ||
95 | m_engine->globalObject().setProperty(QStringLiteral("debug"), m_engine->newFunction(Kate::Script::debug)); | 58 | m_engine->globalObject().setProperty(QStringLiteral("debug"), functions.property(QStringLiteral("debug"))); | ||
96 | 59 | | |||
97 | // export translation functions | 60 | // export translation functions | ||
98 | m_engine->globalObject().setProperty(QStringLiteral("i18n"), m_engine->newFunction(Kate::Script::i18n)); | 61 | m_engine->globalObject().setProperty(QStringLiteral("i18n"), functions.property(QStringLiteral("_i18n"))); | ||
99 | m_engine->globalObject().setProperty(QStringLiteral("i18nc"), m_engine->newFunction(Kate::Script::i18nc)); | 62 | m_engine->globalObject().setProperty(QStringLiteral("i18nc"), functions.property(QStringLiteral("_i18nc"))); | ||
100 | m_engine->globalObject().setProperty(QStringLiteral("i18ncp"), m_engine->newFunction(Kate::Script::i18ncp)); | 63 | m_engine->globalObject().setProperty(QStringLiteral("i18np"), functions.property(QStringLiteral("_i18np"))); | ||
101 | m_engine->globalObject().setProperty(QStringLiteral("i18np"), m_engine->newFunction(Kate::Script::i18np)); | 64 | m_engine->globalObject().setProperty(QStringLiteral("i18ncp"), functions.property(QStringLiteral("_i18ncp"))); | ||
102 | 65 | | |||
103 | KTextEditor::ViewPrivate *view = qobject_cast<KTextEditor::ViewPrivate *>(part->widget()); | 66 | KTextEditor::ViewPrivate *view = qobject_cast<KTextEditor::ViewPrivate *>(part->widget()); | ||
104 | 67 | | |||
105 | m_viewObj = new KateViewObject(view); | 68 | m_viewObj = new KateViewObject(m_engine, view); | ||
106 | QScriptValue sv = m_engine->newQObject(m_viewObj); | 69 | QJSValue sv = m_engine->newQObject(m_viewObj); | ||
107 | 70 | | |||
108 | m_engine->globalObject().setProperty(QStringLiteral("view"), sv); | 71 | m_engine->globalObject().setProperty(QStringLiteral("view"), sv); | ||
109 | m_engine->globalObject().setProperty(QStringLiteral("v"), sv); | 72 | m_engine->globalObject().setProperty(QStringLiteral("v"), sv); | ||
110 | 73 | | |||
111 | m_docObj = new KateDocumentObject(view->doc()); | 74 | m_docObj = new KateDocumentObject(m_engine, view->doc()); | ||
112 | QScriptValue sd = m_engine->newQObject(m_docObj); | 75 | QJSValue sd = m_engine->newQObject(m_docObj); | ||
113 | 76 | | |||
114 | m_engine->globalObject().setProperty(QStringLiteral("document"), sd); | 77 | m_engine->globalObject().setProperty(QStringLiteral("document"), sd); | ||
115 | m_engine->globalObject().setProperty(QStringLiteral("d"), sd); | 78 | m_engine->globalObject().setProperty(QStringLiteral("d"), sd); | ||
116 | 79 | | |||
117 | m_output = new OutputObject(view, cflag); | 80 | m_output = new OutputObject(view, cflag); | ||
118 | QScriptValue so = m_engine->newQObject(m_output); | 81 | QJSValue so = m_engine->newQObject(m_output); | ||
119 | 82 | | |||
120 | m_engine->globalObject().setProperty(QStringLiteral("output"), so); | 83 | m_engine->globalObject().setProperty(QStringLiteral("output"), so); | ||
121 | m_engine->globalObject().setProperty(QStringLiteral("out"), so); | 84 | m_engine->globalObject().setProperty(QStringLiteral("out"), so); | ||
Context not available. | |||||
139 | 102 | | |||
140 | //BEGIN KateViewObject | 103 | //BEGIN KateViewObject | ||
141 | 104 | | |||
142 | KateViewObject::KateViewObject(KTextEditor::ViewPrivate *view) | 105 | KateViewObject::KateViewObject(QJSEngine *engine, KTextEditor::ViewPrivate *view) | ||
143 | : KateScriptView() | 106 | : KateScriptView(engine) | ||
144 | { | 107 | { | ||
145 | setView(view); | 108 | setView(view); | ||
146 | } | 109 | } | ||
Context not available. | |||||
234 | 197 | | |||
235 | //BEGIN KateDocumentObject | 198 | //BEGIN KateDocumentObject | ||
236 | 199 | | |||
237 | KateDocumentObject::KateDocumentObject(KTextEditor::DocumentPrivate *doc) | 200 | KateDocumentObject::KateDocumentObject(QJSEngine *engine, KTextEditor::DocumentPrivate *doc) | ||
238 | : KateScriptDocument() | 201 | : KateScriptDocument(engine) | ||
239 | { | 202 | { | ||
240 | setDocument(doc); | 203 | setDocument(doc); | ||
241 | } | 204 | } | ||
Context not available. | |||||
247 | //END KateDocumentObject | 210 | //END KateDocumentObject | ||
248 | 211 | | |||
249 | //BEGIN OutputObject | 212 | //BEGIN OutputObject | ||
250 | | ||||
251 | OutputObject::OutputObject(KTextEditor::ViewPrivate *v, bool &cflag) | 213 | OutputObject::OutputObject(KTextEditor::ViewPrivate *v, bool &cflag) | ||
252 | : view(v), cflag(cflag) | 214 | : view(v), cflag(cflag) | ||
253 | { | 215 | { | ||
Context not available. | |||||
261 | void OutputObject::output(bool cp, bool ln) | 223 | void OutputObject::output(bool cp, bool ln) | ||
262 | { | 224 | { | ||
263 | QString str; | 225 | QString str; | ||
264 | for (int i = 0; i < context()->argumentCount(); ++i) { | 226 | // FIXME: This is not available with QtQml, but not sure if we need it | ||
265 | QScriptValue arg = context()->argument(i); | 227 | // for (int i = 0; i < context()->argumentCount(); ++i) { | ||
266 | str += arg.toString(); | 228 | // QJSValue arg = context()->argument(i); | ||
267 | } | 229 | // str += arg.toString(); | ||
230 | // } | ||||
268 | 231 | | |||
269 | if (cp) { | 232 | if (cp) { | ||
270 | KTextEditor::Cursor c = view->cursorPosition(); | 233 | KTextEditor::Cursor c = view->cursorPosition(); | ||
Context not available. | |||||
349 | { | 312 | { | ||
350 | output(true, true); | 313 | output(true, true); | ||
351 | } | 314 | } | ||
352 | | ||||
353 | //END OutputObject | 315 | //END OutputObject | ||
Context not available. |