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 const KateCommandLineScriptHeader &KateCommandLineScript::commandHeader()
28 {
29  return m_commandHeader;
30 }
31 
32 bool KateCommandLineScript::callFunction(const QString &cmd, const QStringList &args, QString &errorMessage)
33 {
34  clearExceptions();
35  QJSValue command = function(cmd);
36  if (!command.isCallable()) {
37  errorMessage = i18n("Function '%1' not found in script: %2", cmd, url());
38  return false;
39  }
40 
41  // add the arguments that we are going to pass to the function
42  QJSValueList arguments;
43  arguments.reserve(args.size());
44  for (const QString &arg : args) {
45  arguments << QJSValue(arg);
46  }
47 
48  QJSValue result = command.call(arguments);
49  // error during the calling?
50  if (result.isError()) {
51  errorMessage = backtrace(result, i18n("Error calling %1", cmd));
52  return false;
53  }
54 
55  return true;
56 }
57 
59 {
60  if (range.isValid())
61  view->setSelection(range);
62 
63  KShell::Errors errorCode;
64  QStringList args(KShell::splitArgs(cmd, KShell::NoOptions, &errorCode));
65 
66  if (errorCode != KShell::NoError) {
67  msg = i18n("Bad quoting in call: %1. Please escape single quotes with a backslash.", cmd);
68  return false;
69  }
70 
71  QString _cmd(args.first());
72  args.removeFirst();
73 
74  if (!view) {
75  msg = i18n("Could not access view");
76  return false;
77  }
78 
79  if (setView(qobject_cast<KTextEditor::ViewPrivate *>(view))) {
80  // setView fails if the script cannot be loaded
81  // balance edit stack in any case!
82  qobject_cast<KTextEditor::ViewPrivate *>(view)->doc()->pushEditState();
83  bool success = callFunction(_cmd, args, msg);
84  qobject_cast<KTextEditor::ViewPrivate *>(view)->doc()->popEditState();
85  return success;
86  }
87 
88  return false;
89 }
90 
92 {
93  return true;
94 }
95 
97 {
98  if (!setView(qobject_cast<KTextEditor::ViewPrivate *>(view))) {
99  // setView fails, if the script cannot be loaded
100  return false;
101  }
102 
103  clearExceptions();
104  QJSValue helpFunction = function(QStringLiteral("help"));
105  if (!helpFunction.isCallable()) {
106  return false;
107  }
108 
109  // add the arguments that we are going to pass to the function
110  QJSValueList arguments;
111  arguments << QJSValue(cmd);
112 
113  QJSValue result = helpFunction.call(arguments);
114 
115  // error during the calling?
116  if (result.isError()) {
117  msg = backtrace(result, i18n("Error calling 'help %1'", cmd));
118  return false;
119  }
120 
121  if (result.isUndefined() || !result.isString()) {
122  qCDebug(LOG_KTE) << i18n("No help specified for command '%1' in script %2", cmd, url());
123  return false;
124  }
125  msg = result.toString();
126 
127  return !msg.isEmpty();
128 }
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.
int size() const const
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...
Definition: katetextblock.h:20
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:146
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-2021 The KDE developers.
Generated on Fri Jul 30 2021 22:58:07 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.