Kate
kateindentscript.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "kateindentscript.h"
00020
00021 #include <QScriptValue>
00022 #include <QScriptEngine>
00023
00024 #include "katedocument.h"
00025 #include "kateview.h"
00026
00027 KateIndentScript::KateIndentScript(const QString &url, const KateScriptInformation &information)
00028 : KateScript(url, information), m_triggerCharactersSet (false)
00029 {
00030 }
00031
00032 const QString &KateIndentScript::triggerCharacters()
00033 {
00034
00035 if (m_triggerCharactersSet)
00036 return m_triggerCharacters;
00037
00038 m_triggerCharactersSet = true;
00039
00040 m_triggerCharacters = global("triggerCharacters").toString();
00041
00042 kDebug( 13050 ) << "trigger chars: '" << m_triggerCharacters << "'";
00043
00044 return m_triggerCharacters;
00045 }
00046
00047 QPair<int, int> KateIndentScript::indent(KateView* view, const KTextEditor::Cursor& position,
00048 QChar typedCharacter, int indentWidth)
00049 {
00050
00051 if(!setView(view))
00052 return qMakePair(-2,-2);
00053
00054 clearExceptions();
00055 QScriptValue indentFunction = function("indent");
00056 if(!indentFunction.isValid()) {
00057 return qMakePair(-2,-2);
00058 }
00059
00060 QScriptValueList arguments;
00061 arguments << QScriptValue(m_engine, position.line());
00062 arguments << QScriptValue(m_engine, indentWidth);
00063 arguments << QScriptValue(m_engine, typedCharacter.isNull() ? QString("") : QString(typedCharacter));
00064
00065 QScriptValue result = indentFunction.call(QScriptValue(), arguments);
00066
00067 if(m_engine->hasUncaughtException()) {
00068 displayBacktrace(result, "Error calling indent()");
00069 return qMakePair(-2,-2);
00070 }
00071 int indentAmount = -2;
00072 int alignAmount = -2;
00073 if (result.isArray()) {
00074 indentAmount = result.property(0).toInt32();
00075 alignAmount = result.property(1).toInt32();
00076 } else {
00077 indentAmount = result.toInt32();
00078 }
00079 if(m_engine->hasUncaughtException()) {
00080 displayBacktrace(QScriptValue(), "Bad return type (must be integer)");
00081 return qMakePair(-2,-2);
00082 }
00083 return qMakePair(indentAmount, alignAmount);
00084 }
00085
00086