KTextEditor

kateindentscript.cpp
1 /*
2  SPDX-FileCopyrightText: 2008 Paul Giannaros <[email protected]>
3  SPDX-FileCopyrightText: 2009-2018 Dominik Haumann <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "kateindentscript.h"
9 
10 #include <QJSEngine>
11 #include <QJSValue>
12 
13 #include "katedocument.h"
14 
15 KateIndentScript::KateIndentScript(const QString &url, const KateIndentScriptHeader &header)
16  : KateScript(url)
17  , m_indentHeader(header)
18 {
19 }
20 
21 const KateIndentScriptHeader &KateIndentScript::indentHeader() const
22 {
23  return m_indentHeader;
24 }
25 
26 const QString &KateIndentScript::triggerCharacters()
27 {
28  // already set, perfect, just return...
29  if (m_triggerCharactersSet) {
30  return m_triggerCharacters;
31  }
32 
33  m_triggerCharactersSet = true;
34 
35  auto triggerCharacters = global(QStringLiteral("triggerCharacters"));
36  if (!triggerCharacters.isUndefined()) {
37  m_triggerCharacters = triggerCharacters.toString();
38  }
39 
40  // qCDebug(LOG_KTE) << "trigger chars: '" << m_triggerCharacters << "'";
41 
42  return m_triggerCharacters;
43 }
44 
45 QPair<int, int> KateIndentScript::indent(KTextEditor::ViewPrivate *view, const KTextEditor::Cursor &position, QChar typedCharacter, int indentWidth)
46 {
47  // if it hasn't loaded or we can't load, return
48  if (!setView(view)) {
49  return qMakePair(-2, -2);
50  }
51 
52  clearExceptions();
53  QJSValue indentFunction = function(QStringLiteral("indent"));
54  if (!indentFunction.isCallable()) {
55  return qMakePair(-2, -2);
56  }
57  // add the arguments that we are going to pass to the function
58  QJSValueList arguments;
59  arguments << QJSValue(position.line());
60  arguments << QJSValue(indentWidth);
61  arguments << (typedCharacter.isNull() ? QJSValue(QString()) : QJSValue(QString(typedCharacter)));
62  // get the required indent
63  QJSValue result = indentFunction.call(arguments);
64  // error during the calling?
65  if (result.isError()) {
66  displayBacktrace(result, QStringLiteral("Error calling indent()"));
67  return qMakePair(-2, -2);
68  }
69  int indentAmount = -2;
70  int alignAmount = -2;
71  if (result.isArray()) {
72  indentAmount = result.property(0).toInt();
73  alignAmount = result.property(1).toInt();
74  } else {
75  indentAmount = result.toInt();
76  }
77 
78  return qMakePair(indentAmount, alignAmount);
79 }
KateScript objects represent a script that can be executed and inspected.
Definition: katescript.h:106
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...
QJSValue call(const QJSValueList &args)
The Cursor represents a position in a Document.
Definition: cursor.h:71
bool isCallable() const const
bool isNull() const const
constexpr int line() const Q_DECL_NOEXCEPT
Retrieve the line on which this cursor is situated.
Definition: cursor.h:195
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Mar 2 2021 01:49:08 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.