KTextEditor

katecommandlinescript.cpp
1/*
2 SPDX-FileCopyrightText: 2009-2018 Dominik Haumann <dhaumann@kde.org>
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
20KateCommandLineScript::KateCommandLineScript(const QString &url, const KateCommandLineScriptHeader &header)
21 : KateScript(url)
22 , KTextEditor::Command(header.functions())
23 , m_commandHeader(header)
24{
25}
26
27const KateCommandLineScriptHeader &KateCommandLineScript::commandHeader()
28{
29 return m_commandHeader;
30}
31
32bool KateCommandLineScript::callFunction(const QString &cmd, const QStringList &args, QString &errorMessage)
33{
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
64 KShell::Errors errorCode;
65 QStringList args(KShell::splitArgs(cmd, KShell::NoOptions, &errorCode));
66
67 if (errorCode != KShell::NoError) {
68 msg = i18n("Bad quoting in call: %1. Please escape single quotes with a backslash.", cmd);
69 return false;
70 }
71
72 QString _cmd(args.first());
73 args.removeFirst();
74
75 if (!view) {
76 msg = i18n("Could not access view");
77 return false;
78 }
79
81 // setView fails if the script cannot be loaded
82 // balance edit stack in any case!
83 qobject_cast<KTextEditor::ViewPrivate *>(view)->doc()->pushEditState();
84 bool success = callFunction(_cmd, args, msg);
85 qobject_cast<KTextEditor::ViewPrivate *>(view)->doc()->popEditState();
86 return success;
87 }
88
89 return false;
90}
91
93{
94 return true;
95}
96
98{
100 // setView fails, if the script cannot be loaded
101 return false;
102 }
103
105 QJSValue helpFunction = function(QStringLiteral("help"));
106 if (!helpFunction.isCallable()) {
107 return false;
108 }
109
110 // add the arguments that we are going to pass to the function
111 QJSValueList arguments;
112 arguments << QJSValue(cmd);
113
114 QJSValue result = helpFunction.call(arguments);
115
116 // error during the calling?
117 if (result.isError()) {
118 msg = backtrace(result, i18n("Error calling 'help %1'", cmd));
119 return false;
120 }
121
122 if (result.isUndefined() || !result.isString()) {
123 qCDebug(LOG_KTE) << i18n("No help specified for command '%1' in script %2", cmd, url());
124 return false;
125 }
126 msg = result.toString();
127
128 return !msg.isEmpty();
129}
An object representing a section of text, from one Cursor to another.
constexpr bool isValid() const noexcept
Validity check.
A text widget with KXMLGUIClient that represents a Document.
Definition view.h:244
virtual bool setSelection(Range range)=0
Set the view's selection to the range selection.
bool supportsRange(const QString &cmd) override
Find out if a given command can act on a range.
bool help(KTextEditor::View *view, const QString &cmd, QString &msg) override
Shows help for the given view and cmd string.
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.
KateScript objects represent a script that can be executed and inspected.
Definition katescript.h:107
bool setView(KTextEditor::ViewPrivate *view)
set view for this script for the execution will trigger load!
void clearExceptions()
Clears any uncaught exceptions in the script engine.
const QString & url()
The script's URL.
Definition katescript.h:122
static QString backtrace(const QJSValue &error, const QString &header=QString())
Returns the backtrace when a script has errored out.
const QString & errorMessage()
Return a context-specific error message.
Definition katescript.h:155
QJSValue function(const QString &name)
Return a function in the script of the given name, or an invalid QJSValue if no such function exists.
QString i18n(const char *text, const TYPE &arg...)
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...
QJSValue call(const QJSValueList &args) const const
bool isCallable() const const
bool isError() const const
bool isString() const const
bool isUndefined() const const
QString toString() const const
T & first()
void removeFirst()
qsizetype size() const const
T qobject_cast(QObject *object)
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 4 2024 12:03:01 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.