KTextEditor

katecommandlinescript.cpp
1 /*
2  SPDX-FileCopyrightText: 2009-2018 Dominik Haumann <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "katecommandlinescript.h"
8 
9 #include <QJSEngine>
10 #include <QJSValue>
11 
12 #include <KLocalizedString>
13 #include <KShell>
14 
15 #include "katecmd.h"
16 #include "katedocument.h"
17 #include "katepartdebug.h"
18 #include "kateview.h"
19 
20 KateCommandLineScript::KateCommandLineScript(const QString &url, const KateCommandLineScriptHeader &header)
21  : KateScript(url)
22  , KTextEditor::Command(header.functions())
23  , m_commandHeader(header)
24 {
25 }
26 
27 KateCommandLineScript::~KateCommandLineScript()
28 {
29 }
30 
31 const KateCommandLineScriptHeader &KateCommandLineScript::commandHeader()
32 {
33  return m_commandHeader;
34 }
35 
36 bool KateCommandLineScript::callFunction(const QString &cmd, const QStringList &args, QString &errorMessage)
37 {
38  clearExceptions();
39  QJSValue command = function(cmd);
40  if (!command.isCallable()) {
41  errorMessage = i18n("Function '%1' not found in script: %2", cmd, url());
42  return false;
43  }
44 
45  // add the arguments that we are going to pass to the function
46  QJSValueList arguments;
47  for (const QString &arg : args) {
48  arguments << QJSValue(arg);
49  }
50 
51  QJSValue result = command.call(arguments);
52  // error during the calling?
53  if (result.isError()) {
54  errorMessage = backtrace(result, i18n("Error calling %1", cmd));
55  return false;
56  }
57 
58  return true;
59 }
60 
62 {
63  if (range.isValid())
64  view->setSelection(range);
65 
66  KShell::Errors errorCode;
67  QStringList args(KShell::splitArgs(cmd, KShell::NoOptions, &errorCode));
68 
69  if (errorCode != KShell::NoError) {
70  msg = i18n("Bad quoting in call: %1. Please escape single quotes with a backslash.", cmd);
71  return false;
72  }
73 
74  QString _cmd(args.first());
75  args.removeFirst();
76 
77  if (!view) {
78  msg = i18n("Could not access view");
79  return false;
80  }
81 
82  if (setView(qobject_cast<KTextEditor::ViewPrivate *>(view))) {
83  // setView fails if the script cannot be loaded
84  // balance edit stack in any case!
85  qobject_cast<KTextEditor::ViewPrivate *>(view)->doc()->pushEditState();
86  bool success = callFunction(_cmd, args, msg);
87  qobject_cast<KTextEditor::ViewPrivate *>(view)->doc()->popEditState();
88  return success;
89  }
90 
91  return false;
92 }
93 
95 {
96  return true;
97 }
98 
100 {
101  if (!setView(qobject_cast<KTextEditor::ViewPrivate *>(view))) {
102  // setView fails, if the script cannot be loaded
103  return false;
104  }
105 
106  clearExceptions();
107  QJSValue helpFunction = function(QStringLiteral("help"));
108  if (!helpFunction.isCallable()) {
109  return false;
110  }
111 
112  // add the arguments that we are going to pass to the function
113  QJSValueList arguments;
114  arguments << QJSValue(cmd);
115 
116  QJSValue result = helpFunction.call(arguments);
117 
118  // error during the calling?
119  if (result.isError()) {
120  msg = backtrace(result, i18n("Error calling 'help %1'", cmd));
121  return false;
122  }
123 
124  if (result.isUndefined() || !result.isString()) {
125  qCDebug(LOG_KTE) << i18n("No help specified for command '%1' in script %2", cmd, url());
126  return false;
127  }
128  msg = result.toString();
129 
130  return !msg.isEmpty();
131 }
virtual bool setSelection(const Range &range)=0
Set the view&#39;s selection to the range selection.
KateScript objects represent a script that can be executed and inspected.
Definition: katescript.h:106
void removeFirst()
QString toString() const const
QJSValue call(const QJSValueList &args)
constexpr bool isValid() const Q_DECL_NOEXCEPT
Validity check.
bool isError() const const
bool supportsRange(const QString &cmd) override
Find out if a given command can act on a range.
bool isString() const const
bool isEmpty() const const
T & first()
KCOREADDONS_EXPORT QStringList splitArgs(const QString &cmd, Options flags=NoOptions, Errors *err=nullptr)
The KTextEditor namespace contains all the public API that is required to use the KTextEditor compone...
bool isCallable() const const
An object representing a section of text, from one Cursor to another.
QString i18n(const char *text, const TYPE &arg...)
bool exec(KTextEditor::View *view, const QString &cmd, QString &msg, const KTextEditor::Range &range=KTextEditor::Range::invalid()) override
Execute the command for the given view and cmd string.
bool isUndefined() const const
A text widget with KXMLGUIClient that represents a Document.
Definition: view.h:143
bool help(KTextEditor::View *view, const QString &cmd, QString &msg) override
Shows help for the given view and cmd string.
T qobject_cast(QObject *object)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Thu Sep 17 2020 22:57:33 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.