KTextEditor

kateindentscript.cpp
1 /* SPDX-License-Identifier: LGPL-2.0-or-later
2 
3  Copyright (C) 2008 Paul Giannaros <[email protected]>
4  Copyright (C) 2009-2018 Dominik Haumann <[email protected]>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
22 #include "kateindentscript.h"
23 
24 #include <QJSEngine>
25 #include <QJSValue>
26 
27 #include "katedocument.h"
28 #include "kateview.h"
29 
30 KateIndentScript::KateIndentScript(const QString &url, const KateIndentScriptHeader &header)
31  : KateScript(url)
32  , m_indentHeader(header)
33 {
34 }
35 
36 const KateIndentScriptHeader &KateIndentScript::indentHeader() const
37 {
38  return m_indentHeader;
39 }
40 
41 const QString &KateIndentScript::triggerCharacters()
42 {
43  // already set, perfect, just return...
44  if (m_triggerCharactersSet) {
45  return m_triggerCharacters;
46  }
47 
48  m_triggerCharactersSet = true;
49 
50  auto triggerCharacters = global(QStringLiteral("triggerCharacters"));
51  if (!triggerCharacters.isUndefined()) {
52  m_triggerCharacters = triggerCharacters.toString();
53  }
54 
55  // qCDebug(LOG_KTE) << "trigger chars: '" << m_triggerCharacters << "'";
56 
57  return m_triggerCharacters;
58 }
59 
60 QPair<int, int> KateIndentScript::indent(KTextEditor::ViewPrivate *view, const KTextEditor::Cursor &position, QChar typedCharacter, int indentWidth)
61 {
62  // if it hasn't loaded or we can't load, return
63  if (!setView(view)) {
64  return qMakePair(-2, -2);
65  }
66 
67  clearExceptions();
68  QJSValue indentFunction = function(QStringLiteral("indent"));
69  if (!indentFunction.isCallable()) {
70  return qMakePair(-2, -2);
71  }
72  // add the arguments that we are going to pass to the function
73  QJSValueList arguments;
74  arguments << QJSValue(position.line());
75  arguments << QJSValue(indentWidth);
76  arguments << (typedCharacter.isNull() ? QJSValue(QString()) : QJSValue(QString(typedCharacter)));
77  // get the required indent
78  QJSValue result = indentFunction.call(arguments);
79  // error during the calling?
80  if (result.isError()) {
81  displayBacktrace(result, QStringLiteral("Error calling indent()"));
82  return qMakePair(-2, -2);
83  }
84  int indentAmount = -2;
85  int alignAmount = -2;
86  if (result.isArray()) {
87  indentAmount = result.property(0).toInt();
88  alignAmount = result.property(1).toInt();
89  } else {
90  indentAmount = result.toInt();
91  }
92 
93  return qMakePair(indentAmount, alignAmount);
94 }
KateScript objects represent a script that can be executed and inspected.
Definition: katescript.h:120
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:85
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:199
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed Jul 8 2020 22:56:16 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.