• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

Kate

  • kde-4.14
  • applications
  • kate
  • part
  • script
kateindentscript.cpp
Go to the documentation of this file.
1 // This file is part of the KDE libraries
2 // Copyright (C) 2008 Paul Giannaros <paul@giannaros.org>
3 // Copyright (C) 2009 Dominik Haumann <dhaumann kde org>
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Library General Public
7 // License as published by the Free Software Foundation; either
8 // version 2 of the License, or (at your option) version 3.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
14 //
15 // You should have received a copy of the GNU Library General Public License
16 // along with this library; see the file COPYING.LIB. If not, write to
17 // the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 // Boston, MA 02110-1301, USA.
19 
20 #include "kateindentscript.h"
21 
22 #include <QScriptValue>
23 #include <QScriptEngine>
24 
25 #include "katedocument.h"
26 #include "kateview.h"
27 
28 KateIndentScript::KateIndentScript(const QString &url, const KateIndentScriptHeader &header)
29  : KateScript(url)
30  , m_triggerCharactersSet (false)
31  , m_indentHeader(header)
32 {
33 }
34 
35 const KateIndentScriptHeader& KateIndentScript::indentHeader() const
36 {
37  return m_indentHeader;
38 }
39 
40 const QString &KateIndentScript::triggerCharacters()
41 {
42  // already set, perfect, just return...
43  if (m_triggerCharactersSet)
44  return m_triggerCharacters;
45 
46  m_triggerCharactersSet = true;
47 
48  m_triggerCharacters = global("triggerCharacters").toString();
49 
50  //kDebug( 13050 ) << "trigger chars: '" << m_triggerCharacters << "'";
51 
52  return m_triggerCharacters;
53 }
54 
55 QPair<int, int> KateIndentScript::indent(KateView* view, const KTextEditor::Cursor& position,
56  QChar typedCharacter, int indentWidth)
57 {
58  // if it hasn't loaded or we can't load, return
59  if(!setView(view))
60  return qMakePair(-2,-2);
61 
62  clearExceptions();
63  QScriptValue indentFunction = function("indent");
64  if(!indentFunction.isValid()) {
65  return qMakePair(-2,-2);
66  }
67  // add the arguments that we are going to pass to the function
68  QScriptValueList arguments;
69  arguments << QScriptValue(m_engine, position.line());
70  arguments << QScriptValue(m_engine, indentWidth);
71  arguments << QScriptValue(m_engine, typedCharacter.isNull() ? QString("") : QString(typedCharacter));
72  // get the required indent
73  QScriptValue result = indentFunction.call(QScriptValue(), arguments);
74  // error during the calling?
75  if(m_engine->hasUncaughtException()) {
76  displayBacktrace(result, "Error calling indent()");
77  return qMakePair(-2,-2);
78  }
79  int indentAmount = -2;
80  int alignAmount = -2;
81  if (result.isArray()) {
82  indentAmount = result.property(0).toInt32();
83  alignAmount = result.property(1).toInt32();
84  } else {
85  indentAmount = result.toInt32();
86  }
87  if(m_engine->hasUncaughtException()) {
88  displayBacktrace(QScriptValue(), "Bad return type (must be integer)");
89  return qMakePair(-2,-2);
90  }
91  return qMakePair(indentAmount, alignAmount);
92 }
93 
94 // kate: space-indent on; indent-width 2; replace-tabs on;
kateview.h
KateScript
KateScript objects represent a script that can be executed and inspected.
Definition: katescript.h:106
QChar
QScriptValue
katedocument.h
KateScript::global
QScriptValue global(const QString &name)
Get a QScriptValue for a global item in the script given its name, or an invalid QScriptValue if no s...
Definition: katescript.cpp:135
QScriptEngine::hasUncaughtException
bool hasUncaughtException() const
kateindentscript.h
QScriptValue::call
QScriptValue call(const QScriptValue &thisObject, const QScriptValueList &args)
QScriptValue::toString
QString toString() const
KateIndentScript::indent
QPair< int, int > indent(KateView *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...
Definition: kateindentscript.cpp:55
QString
KateScript::clearExceptions
void clearExceptions()
Clears any uncaught exceptions in the script engine.
Definition: katescript.cpp:128
QPair
KateView
Definition: kateview.h:77
QChar::isNull
bool isNull() const
KateScript::m_engine
QScriptEngine * m_engine
The Qt interpreter for this script.
Definition: katescript.h:188
KateIndentScript::triggerCharacters
const QString & triggerCharacters()
Definition: kateindentscript.cpp:40
KateScript::setView
bool setView(KateView *view)
set view for this script for the execution will trigger load!
Definition: katescript.cpp:219
KateScript::displayBacktrace
void displayBacktrace(const QScriptValue &error, const QString &header=QString())
Displays the backtrace when a script has errored out.
Definition: katescript.cpp:119
QScriptValue::isValid
bool isValid() const
KateIndentScript::KateIndentScript
KateIndentScript(const QString &url, const KateIndentScriptHeader &header)
Definition: kateindentscript.cpp:28
KateIndentScriptHeader
Definition: kateindentscript.h:30
KateIndentScript::indentHeader
const KateIndentScriptHeader & indentHeader() const
Definition: kateindentscript.cpp:35
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:58 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Kate

Skip menu "Kate"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Konsole

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal