Changeset View
Changeset View
Standalone View
Standalone View
src/script/katecommandlinescript.cpp
Show All 14 Lines | |||||
15 | * You should have received a copy of the GNU Library General Public License | 15 | * You should have received a copy of the GNU Library General Public License | ||
16 | * along with this library; see the file COPYING.LIB. If not, write to | 16 | * along with this library; see the file COPYING.LIB. If not, write to | ||
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 | 20 | | |||
21 | #include "katecommandlinescript.h" | 21 | #include "katecommandlinescript.h" | ||
22 | 22 | | |||
23 | #include <QScriptValue> | 23 | #include <QJSValue> | ||
24 | #include <QScriptEngine> | 24 | #include <QJSEngine> | ||
25 | 25 | | |||
26 | #include <KLocalizedString> | 26 | #include <KLocalizedString> | ||
27 | #include <KShell> | 27 | #include <KShell> | ||
28 | 28 | | |||
29 | #include "katedocument.h" | 29 | #include "katedocument.h" | ||
30 | #include "kateview.h" | 30 | #include "kateview.h" | ||
31 | #include "katecmd.h" | 31 | #include "katecmd.h" | ||
32 | #include "katepartdebug.h" | 32 | #include "katepartdebug.h" | ||
Show All 12 Lines | |||||
45 | const KateCommandLineScriptHeader &KateCommandLineScript::commandHeader() | 45 | const KateCommandLineScriptHeader &KateCommandLineScript::commandHeader() | ||
46 | { | 46 | { | ||
47 | return m_commandHeader; | 47 | return m_commandHeader; | ||
48 | } | 48 | } | ||
49 | 49 | | |||
50 | bool KateCommandLineScript::callFunction(const QString &cmd, const QStringList args, QString &errorMessage) | 50 | bool KateCommandLineScript::callFunction(const QString &cmd, const QStringList args, QString &errorMessage) | ||
51 | { | 51 | { | ||
52 | clearExceptions(); | 52 | clearExceptions(); | ||
53 | QScriptValue command = function(cmd); | 53 | QJSValue command = function(cmd); | ||
54 | if (!command.isValid()) { | 54 | if (!command.isCallable()) { | ||
55 | errorMessage = i18n("Function '%1' not found in script: %2", cmd, url()); | 55 | errorMessage = i18n("Function '%1' not found in script: %2", cmd, url()); | ||
56 | return false; | 56 | return false; | ||
57 | } | 57 | } | ||
58 | 58 | | |||
59 | // add the arguments that we are going to pass to the function | 59 | // add the arguments that we are going to pass to the function | ||
60 | QScriptValueList arguments; | 60 | QJSValueList arguments; | ||
61 | foreach (const QString &arg, args) { | 61 | for (const QString &arg : args) { | ||
62 | arguments << QScriptValue(m_engine, arg); | 62 | arguments << QJSValue(arg); | ||
63 | } | 63 | } | ||
64 | 64 | | |||
65 | QScriptValue result = command.call(QScriptValue(), arguments); | 65 | QJSValue result = command.call(arguments); | ||
66 | // error during the calling? | 66 | // error during the calling? | ||
67 | if (m_engine->hasUncaughtException()) { | 67 | if (result.isError()) { | ||
68 | errorMessage = backtrace(result, i18n("Error calling %1", cmd)); | 68 | errorMessage = backtrace(result, i18n("Error calling %1", cmd)); | ||
69 | return false; | 69 | return false; | ||
70 | } | 70 | } | ||
71 | 71 | | |||
72 | return true; | 72 | return true; | ||
73 | } | 73 | } | ||
74 | 74 | | |||
75 | bool KateCommandLineScript::exec(KTextEditor::View *view, const QString &cmd, QString &msg, | 75 | bool KateCommandLineScript::exec(KTextEditor::View *view, const QString &cmd, QString &msg, | ||
Show All 38 Lines | |||||
114 | bool KateCommandLineScript::help(KTextEditor::View *view, const QString &cmd, QString &msg) | 114 | bool KateCommandLineScript::help(KTextEditor::View *view, const QString &cmd, QString &msg) | ||
115 | { | 115 | { | ||
116 | if (!setView(qobject_cast<KTextEditor::ViewPrivate *>(view))) { | 116 | if (!setView(qobject_cast<KTextEditor::ViewPrivate *>(view))) { | ||
117 | // setView fails, if the script cannot be loaded | 117 | // setView fails, if the script cannot be loaded | ||
118 | return false; | 118 | return false; | ||
119 | } | 119 | } | ||
120 | 120 | | |||
121 | clearExceptions(); | 121 | clearExceptions(); | ||
122 | QScriptValue helpFunction = function(QStringLiteral("help")); | 122 | QJSValue helpFunction = function(QStringLiteral("help")); | ||
123 | if (!helpFunction.isValid()) { | 123 | if (!helpFunction.isCallable()) { | ||
124 | return false; | 124 | return false; | ||
125 | } | 125 | } | ||
126 | 126 | | |||
127 | // add the arguments that we are going to pass to the function | 127 | // add the arguments that we are going to pass to the function | ||
128 | QScriptValueList arguments; | 128 | QJSValueList arguments; | ||
129 | arguments << QScriptValue(m_engine, cmd); | 129 | arguments << QJSValue(cmd); | ||
130 | 130 | | |||
131 | QScriptValue result = helpFunction.call(QScriptValue(), arguments); | 131 | QJSValue result = helpFunction.call(arguments); | ||
132 | 132 | | |||
133 | // error during the calling? | 133 | // error during the calling? | ||
134 | if (m_engine->hasUncaughtException()) { | 134 | if (result.isError()) { | ||
135 | msg = backtrace(result, i18n("Error calling 'help %1'", cmd)); | 135 | msg = backtrace(result, i18n("Error calling 'help %1'", cmd)); | ||
136 | return false; | 136 | return false; | ||
137 | } | 137 | } | ||
138 | 138 | | |||
139 | if (result.isUndefined() || !result.isString()) { | 139 | if (result.isUndefined() || !result.isString()) { | ||
140 | qCDebug(LOG_KTE) << i18n("No help specified for command '%1' in script %2", cmd, url()); | 140 | qCDebug(LOG_KTE) << i18n("No help specified for command '%1' in script %2", cmd, url()); | ||
141 | return false; | 141 | return false; | ||
142 | } | 142 | } | ||
143 | msg = result.toString(); | 143 | msg = result.toString(); | ||
144 | 144 | | |||
145 | return !msg.isEmpty(); | 145 | return !msg.isEmpty(); | ||
146 | } | 146 | } | ||
147 | 147 | |