KTextEditor

kateindentscript.cpp
1/*
2 SPDX-FileCopyrightText: 2008 Paul Giannaros <paul@giannaros.org>
3 SPDX-FileCopyrightText: 2009-2018 Dominik Haumann <dhaumann@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "kateindentscript.h"
9
10#include <QJSEngine>
11#include <QJSValue>
12
13KateIndentScript::KateIndentScript(const QString &url, const KateIndentScriptHeader &header)
14 : KateScript(url)
15 , m_indentHeader(header)
16{
17}
18
19const KateIndentScriptHeader &KateIndentScript::indentHeader() const
20{
21 return m_indentHeader;
22}
23
24const QString &KateIndentScript::triggerCharacters()
25{
26 // already set, perfect, just return...
27 if (m_triggerCharactersSet) {
28 return m_triggerCharacters;
29 }
30
31 m_triggerCharactersSet = true;
32
33 auto triggerCharacters = global(QStringLiteral("triggerCharacters"));
34 if (!triggerCharacters.isUndefined()) {
35 m_triggerCharacters = triggerCharacters.toString();
36 }
37
38 // qCDebug(LOG_KTE) << "trigger chars: '" << m_triggerCharacters << "'";
39
40 return m_triggerCharacters;
41}
42
43QPair<int, int> KateIndentScript::indent(KTextEditor::ViewPrivate *view, const KTextEditor::Cursor position, QChar typedCharacter, int indentWidth)
44{
45 // if it hasn't loaded or we can't load, return
46 if (!setView(view)) {
47 return qMakePair(-2, -2);
48 }
49
51 QJSValue indentFunction = function(QStringLiteral("indent"));
52 if (!indentFunction.isCallable()) {
53 return qMakePair(-2, -2);
54 }
55 // add the arguments that we are going to pass to the function
56 QJSValueList arguments;
57 arguments << QJSValue(position.line());
58 arguments << QJSValue(indentWidth);
59 arguments << (typedCharacter.isNull() ? QJSValue(QString()) : QJSValue(QString(typedCharacter)));
60 // get the required indent
61 QJSValue result = indentFunction.call(arguments);
62 // error during the calling?
63 if (result.isError()) {
64 displayBacktrace(result, QStringLiteral("Error calling indent()"));
65 return qMakePair(-2, -2);
66 }
67 int indentAmount = -2;
68 int alignAmount = -2;
69 if (result.isArray()) {
70 indentAmount = result.property(0).toInt();
71 alignAmount = result.property(1).toInt();
72 } else {
73 indentAmount = result.toInt();
74 }
75
76 return qMakePair(indentAmount, alignAmount);
77}
The Cursor represents a position in a Document.
Definition cursor.h:75
constexpr int line() const noexcept
Retrieve the line on which this cursor is situated.
Definition cursor.h:174
QPair< int, int > indent(KTextEditor::ViewPrivate *view, const KTextEditor::Cursor position, QChar typedCharacter, int indentWidth)
Returns a pair where the first value is the indent amount, and the second value is the alignment.
KateScript objects represent a script that can be executed and inspected.
Definition katescript.h:107
QJSValue global(const QString &name)
Get a QJSValue for a global item in the script given its name, or an invalid QJSValue if no such glob...
void displayBacktrace(const QJSValue &error, const QString &header=QString())
Displays the backtrace when a script has errored out.
bool setView(KTextEditor::ViewPrivate *view)
set view for this script for the execution will trigger load!
void clearExceptions()
Clears any uncaught exceptions in the script engine.
QJSValue function(const QString &name)
Return a function in the script of the given name, or an invalid QJSValue if no such function exists.
bool isNull() const const
QJSValue call(const QJSValueList &args) const const
bool isArray() const const
bool isCallable() const const
bool isError() const const
QJSValue property(const QString &name) const const
qint32 toInt() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 6 2024 12:05:18 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.