Kate
kateindentscript.cpp
Go to the documentation of this file.
00001 // This file is part of the KDE libraries 00002 // Copyright (C) 2008 Paul Giannaros <paul@giannaros.org> 00003 // Copyright (C) 2009 Dominik Haumann <dhaumann kde org> 00004 // 00005 // This library is free software; you can redistribute it and/or 00006 // modify it under the terms of the GNU Library General Public 00007 // License as published by the Free Software Foundation; either 00008 // version 2 of the License, or (at your option) version 3. 00009 // 00010 // This library is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 // Library General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU Library General Public License 00016 // along with this library; see the file COPYING.LIB. If not, write to 00017 // the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 // Boston, MA 02110-1301, USA. 00019 00020 #include "kateindentscript.h" 00021 00022 #include <QScriptValue> 00023 #include <QScriptEngine> 00024 00025 #include "katedocument.h" 00026 #include "kateview.h" 00027 00028 KateIndentScript::KateIndentScript(const QString &url, const KateIndentScriptHeader &header) 00029 : KateScript(url) 00030 , m_triggerCharactersSet (false) 00031 , m_indentHeader(header) 00032 { 00033 } 00034 00035 const KateIndentScriptHeader& KateIndentScript::indentHeader() const 00036 { 00037 return m_indentHeader; 00038 } 00039 00040 const QString &KateIndentScript::triggerCharacters() 00041 { 00042 // already set, perfect, just return... 00043 if (m_triggerCharactersSet) 00044 return m_triggerCharacters; 00045 00046 m_triggerCharactersSet = true; 00047 00048 m_triggerCharacters = global("triggerCharacters").toString(); 00049 00050 //kDebug( 13050 ) << "trigger chars: '" << m_triggerCharacters << "'"; 00051 00052 return m_triggerCharacters; 00053 } 00054 00055 QPair<int, int> KateIndentScript::indent(KateView* view, const KTextEditor::Cursor& position, 00056 QChar typedCharacter, int indentWidth) 00057 { 00058 // if it hasn't loaded or we can't load, return 00059 if(!setView(view)) 00060 return qMakePair(-2,-2); 00061 00062 clearExceptions(); 00063 QScriptValue indentFunction = function("indent"); 00064 if(!indentFunction.isValid()) { 00065 return qMakePair(-2,-2); 00066 } 00067 // add the arguments that we are going to pass to the function 00068 QScriptValueList arguments; 00069 arguments << QScriptValue(m_engine, position.line()); 00070 arguments << QScriptValue(m_engine, indentWidth); 00071 arguments << QScriptValue(m_engine, typedCharacter.isNull() ? QString("") : QString(typedCharacter)); 00072 // get the required indent 00073 QScriptValue result = indentFunction.call(QScriptValue(), arguments); 00074 // error during the calling? 00075 if(m_engine->hasUncaughtException()) { 00076 displayBacktrace(result, "Error calling indent()"); 00077 return qMakePair(-2,-2); 00078 } 00079 int indentAmount = -2; 00080 int alignAmount = -2; 00081 if (result.isArray()) { 00082 indentAmount = result.property(0).toInt32(); 00083 alignAmount = result.property(1).toInt32(); 00084 } else { 00085 indentAmount = result.toInt32(); 00086 } 00087 if(m_engine->hasUncaughtException()) { 00088 displayBacktrace(QScriptValue(), "Bad return type (must be integer)"); 00089 return qMakePair(-2,-2); 00090 } 00091 return qMakePair(indentAmount, alignAmount); 00092 } 00093 00094 // kate: space-indent on; indent-width 2; replace-tabs on;
KDE 4.6 API Reference