File indexing completed on 2024-04-28 07:46:34
0001 /* 0002 SPDX-FileCopyrightText: 2008 Paul Giannaros <paul@giannaros.org> 0003 SPDX-FileCopyrightText: 2009-2018 Dominik Haumann <dhaumann@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #include "kateindentscript.h" 0009 0010 #include <QJSEngine> 0011 #include <QJSValue> 0012 0013 KateIndentScript::KateIndentScript(const QString &url, const KateIndentScriptHeader &header) 0014 : KateScript(url) 0015 , m_indentHeader(header) 0016 { 0017 } 0018 0019 const KateIndentScriptHeader &KateIndentScript::indentHeader() const 0020 { 0021 return m_indentHeader; 0022 } 0023 0024 const QString &KateIndentScript::triggerCharacters() 0025 { 0026 // already set, perfect, just return... 0027 if (m_triggerCharactersSet) { 0028 return m_triggerCharacters; 0029 } 0030 0031 m_triggerCharactersSet = true; 0032 0033 auto triggerCharacters = global(QStringLiteral("triggerCharacters")); 0034 if (!triggerCharacters.isUndefined()) { 0035 m_triggerCharacters = triggerCharacters.toString(); 0036 } 0037 0038 // qCDebug(LOG_KTE) << "trigger chars: '" << m_triggerCharacters << "'"; 0039 0040 return m_triggerCharacters; 0041 } 0042 0043 QPair<int, int> KateIndentScript::indent(KTextEditor::ViewPrivate *view, const KTextEditor::Cursor position, QChar typedCharacter, int indentWidth) 0044 { 0045 // if it hasn't loaded or we can't load, return 0046 if (!setView(view)) { 0047 return qMakePair(-2, -2); 0048 } 0049 0050 clearExceptions(); 0051 QJSValue indentFunction = function(QStringLiteral("indent")); 0052 if (!indentFunction.isCallable()) { 0053 return qMakePair(-2, -2); 0054 } 0055 // add the arguments that we are going to pass to the function 0056 QJSValueList arguments; 0057 arguments << QJSValue(position.line()); 0058 arguments << QJSValue(indentWidth); 0059 arguments << (typedCharacter.isNull() ? QJSValue(QString()) : QJSValue(QString(typedCharacter))); 0060 // get the required indent 0061 QJSValue result = indentFunction.call(arguments); 0062 // error during the calling? 0063 if (result.isError()) { 0064 displayBacktrace(result, QStringLiteral("Error calling indent()")); 0065 return qMakePair(-2, -2); 0066 } 0067 int indentAmount = -2; 0068 int alignAmount = -2; 0069 if (result.isArray()) { 0070 indentAmount = result.property(0).toInt(); 0071 alignAmount = result.property(1).toInt(); 0072 } else { 0073 indentAmount = result.toInt(); 0074 } 0075 0076 return qMakePair(indentAmount, alignAmount); 0077 }