File indexing completed on 2024-04-21 03:57:39

0001 /*
0002     SPDX-FileCopyrightText: 2008 Paul Giannaros <paul@giannaros.org>
0003     SPDX-FileCopyrightText: 2008 Christoph Cullmann <cullmann@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef KATE_SCRIPT_VIEW_H
0009 #define KATE_SCRIPT_VIEW_H
0010 
0011 #include <QJSValue>
0012 #include <QObject>
0013 
0014 #include <ktexteditor_export.h>
0015 
0016 #include <ktexteditor/cursor.h>
0017 #include <ktexteditor/range.h>
0018 
0019 namespace KTextEditor
0020 {
0021 class ViewPrivate;
0022 }
0023 class QJSEngine;
0024 /**
0025  * Thinish wrapping around KTextEditor::ViewPrivate, exposing the methods we want exposed
0026  * and adding some helper methods.
0027  *
0028  * setView _must_ be called before using any other method. This is not checked
0029  * for the sake of speed.
0030  */
0031 class KTEXTEDITOR_EXPORT KateScriptView : public QObject
0032 {
0033     /// Properties are accessible with a nicer syntax from JavaScript
0034     Q_OBJECT
0035 
0036 public:
0037     explicit KateScriptView(QJSEngine *, QObject *parent = nullptr);
0038     void setView(KTextEditor::ViewPrivate *view);
0039     KTextEditor::ViewPrivate *view();
0040 
0041     Q_INVOKABLE void copy();
0042     Q_INVOKABLE void cut();
0043     Q_INVOKABLE void paste();
0044 
0045     Q_INVOKABLE QJSValue cursorPosition();
0046     Q_INVOKABLE QJSValue cursorPositions();
0047 
0048     /**
0049      * Set the cursor position in the view.
0050      * @since 4.4
0051      */
0052     Q_INVOKABLE void setCursorPosition(int line, int column);
0053     Q_INVOKABLE void setCursorPosition(const QJSValue &cursor);
0054     Q_INVOKABLE void setCursorPositions(const QJSValue &cursors);
0055 
0056     Q_INVOKABLE QJSValue virtualCursorPosition();
0057     Q_INVOKABLE void setVirtualCursorPosition(int line, int column);
0058     Q_INVOKABLE void setVirtualCursorPosition(const QJSValue &cursor);
0059 
0060     Q_INVOKABLE QString selectedText();
0061     Q_INVOKABLE bool hasSelection();
0062     Q_INVOKABLE QJSValue selection();
0063     Q_INVOKABLE QJSValue selections();
0064     Q_INVOKABLE void setSelection(const QJSValue &range);
0065     Q_INVOKABLE void setSelections(const QJSValue &ranges);
0066     Q_INVOKABLE void removeSelectedText();
0067     Q_INVOKABLE void selectAll();
0068     Q_INVOKABLE void clearSelection();
0069 
0070     Q_INVOKABLE void setBlockSelection(bool on);
0071     Q_INVOKABLE bool blockSelection();
0072 
0073     Q_INVOKABLE void align(const QJSValue &range);
0074     Q_INVOKABLE void alignOn(const QJSValue &jsrange, const QJSValue &pattern = QJSValue(QStringLiteral("")));
0075 
0076     Q_INVOKABLE QJSValue searchText(const QJSValue &range, const QString &pattern, bool backwards = false);
0077 
0078     Q_INVOKABLE QJSValue executeCommand(const QString &command, const QString &args = QString(), const QJSValue &jsrange = QJSValue());
0079 
0080 private:
0081     KTextEditor::ViewPrivate *m_view;
0082     QJSEngine *m_engine;
0083 };
0084 
0085 #endif