Changeset View
Changeset View
Standalone View
Standalone View
src/script/kateindentscript.cpp
Show All 13 Lines | |||||
14 | // | 14 | // | ||
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 | #include "kateindentscript.h" | 20 | #include "kateindentscript.h" | ||
21 | 21 | | |||
22 | #include <QScriptValue> | 22 | #include <QJSValue> | ||
23 | #include <QScriptEngine> | 23 | #include <QJSEngine> | ||
24 | 24 | | |||
25 | #include "katedocument.h" | 25 | #include "katedocument.h" | ||
26 | #include "kateview.h" | 26 | #include "kateview.h" | ||
27 | 27 | | |||
28 | KateIndentScript::KateIndentScript(const QString &url, const KateIndentScriptHeader &header) | 28 | KateIndentScript::KateIndentScript(const QString &url, const KateIndentScriptHeader &header) | ||
29 | : KateScript(url) | 29 | : KateScript(url) | ||
30 | , m_triggerCharactersSet(false) | 30 | , m_triggerCharactersSet(false) | ||
31 | , m_indentHeader(header) | 31 | , m_indentHeader(header) | ||
Show All 25 Lines | 56 | QPair<int, int> KateIndentScript::indent(KTextEditor::ViewPrivate *view, const KTextEditor::Cursor &position, | |||
57 | QChar typedCharacter, int indentWidth) | 57 | QChar typedCharacter, int indentWidth) | ||
58 | { | 58 | { | ||
59 | // if it hasn't loaded or we can't load, return | 59 | // if it hasn't loaded or we can't load, return | ||
60 | if (!setView(view)) { | 60 | if (!setView(view)) { | ||
61 | return qMakePair(-2, -2); | 61 | return qMakePair(-2, -2); | ||
62 | } | 62 | } | ||
63 | 63 | | |||
64 | clearExceptions(); | 64 | clearExceptions(); | ||
65 | QScriptValue indentFunction = function(QStringLiteral("indent")); | 65 | QJSValue indentFunction = function(QStringLiteral("indent")); | ||
66 | if (!indentFunction.isValid()) { | 66 | if (!indentFunction.isCallable()) { | ||
67 | return qMakePair(-2, -2); | 67 | return qMakePair(-2, -2); | ||
68 | } | 68 | } | ||
69 | // add the arguments that we are going to pass to the function | 69 | // add the arguments that we are going to pass to the function | ||
70 | QScriptValueList arguments; | 70 | QJSValueList arguments; | ||
71 | arguments << QScriptValue(m_engine, position.line()); | 71 | arguments << QJSValue(position.line()); | ||
72 | arguments << QScriptValue(m_engine, indentWidth); | 72 | arguments << QJSValue(indentWidth); | ||
73 | arguments << QScriptValue(m_engine, typedCharacter.isNull() ? QString() : QString(typedCharacter)); | 73 | arguments << (typedCharacter.isNull() ? QJSValue(QString()) : QJSValue(QString(typedCharacter))); | ||
74 | // get the required indent | 74 | // get the required indent | ||
75 | QScriptValue result = indentFunction.call(QScriptValue(), arguments); | 75 | QJSValue result = indentFunction.call(arguments); | ||
76 | // error during the calling? | 76 | // error during the calling? | ||
77 | if (m_engine->hasUncaughtException()) { | 77 | if (result.isError()) { | ||
78 | displayBacktrace(result, QStringLiteral("Error calling indent()")); | 78 | displayBacktrace(result, QStringLiteral("Error calling indent()")); | ||
79 | return qMakePair(-2, -2); | 79 | return qMakePair(-2, -2); | ||
80 | } | 80 | } | ||
81 | int indentAmount = -2; | 81 | int indentAmount = -2; | ||
82 | int alignAmount = -2; | 82 | int alignAmount = -2; | ||
83 | if (result.isArray()) { | 83 | if (result.isArray()) { | ||
84 | indentAmount = result.property(0).toInt32(); | 84 | indentAmount = result.property(0).toInt(); | ||
85 | alignAmount = result.property(1).toInt32(); | 85 | alignAmount = result.property(1).toInt(); | ||
86 | } else { | 86 | } else { | ||
87 | indentAmount = result.toInt32(); | 87 | indentAmount = result.toInt(); | ||
88 | } | | |||
89 | if (m_engine->hasUncaughtException()) { | | |||
90 | displayBacktrace(QScriptValue(), QStringLiteral("Bad return type (must be integer)")); | | |||
91 | return qMakePair(-2, -2); | | |||
92 | } | 88 | } | ||
89 | | ||||
93 | return qMakePair(indentAmount, alignAmount); | 90 | return qMakePair(indentAmount, alignAmount); | ||
94 | } | 91 | } | ||
95 | 92 | |