KTextEditor

katescriptview.cpp
1/*
2 SPDX-FileCopyrightText: 2008 Paul Giannaros <paul@giannaros.org>
3 SPDX-FileCopyrightText: 2008 Christoph Cullmann <cullmann@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "katescriptview.h"
9
10#include "katedocument.h"
11#include "kateglobal.h"
12#include "kateview.h"
13#include "scriptcursor.h"
14#include "scriptrange.h"
15
16#include <KLocalizedString>
17#include <KTextEditor/Command>
18
19#include <QJSEngine>
20
22 : QObject(parent)
23 , m_view(nullptr)
24 , m_engine(engine)
25{
26}
27
28void KateScriptView::setView(KTextEditor::ViewPrivate *view)
29{
30 m_view = view;
31}
32
33KTextEditor::ViewPrivate *KateScriptView::view()
34{
35 return m_view;
36}
37
38void KateScriptView::copy()
39{
40 m_view->copy();
41}
42
43void KateScriptView::cut()
44{
45 m_view->cut();
46}
47
48void KateScriptView::paste()
49{
50 m_view->paste();
51}
52
53QJSValue KateScriptView::cursorPosition()
54{
55 return cursorToScriptValue(m_engine, m_view->cursorPosition());
56}
57
58QJSValue KateScriptView::cursorPositions()
59{
60 // pack our stuff into js array
61 const auto cursors = m_view->cursorPositions();
62 QJSValue jsArray = m_engine->newArray(cursors.size());
63 for (int i = 0; i < cursors.size(); ++i) {
64 jsArray.setProperty(i, cursorToScriptValue(m_engine, cursors[i]));
65 }
66 return jsArray;
67}
68
69void KateScriptView::setCursorPosition(int line, int column)
70{
71 const KTextEditor::Cursor cursor(line, column);
72 m_view->setCursorPosition(cursor);
73}
74
76{
77 const auto cursor = cursorFromScriptValue(jscursor);
78 m_view->setCursorPosition(cursor);
79}
80
81void KateScriptView::setCursorPositions(const QJSValue &cursors)
82{
83 // unpack the array of cursors
84 QList<KTextEditor::Cursor> unboxedCursors;
85 const int length = cursors.property(QStringLiteral("length")).toInt();
86 for (int i = 0; i < length; ++i) {
87 unboxedCursors.append(cursorFromScriptValue(cursors.property(i)));
88 }
89 m_view->setCursorPositions(unboxedCursors);
90}
91
92QJSValue KateScriptView::virtualCursorPosition()
93{
94 return cursorToScriptValue(m_engine, m_view->cursorPositionVirtual());
95}
96
97void KateScriptView::setVirtualCursorPosition(int line, int column)
98{
99 const KTextEditor::Cursor cursor(line, column);
100 m_view->setCursorPositionVisual(cursor);
101}
102
103void KateScriptView::setVirtualCursorPosition(const QJSValue &jscursor)
104{
105 const auto cursor = cursorFromScriptValue(jscursor);
106 setVirtualCursorPosition(cursor.line(), cursor.column());
107}
108
109QString KateScriptView::selectedText()
110{
111 return m_view->selectionText();
112}
113
114bool KateScriptView::hasSelection()
115{
116 return m_view->selection();
117}
118
119QJSValue KateScriptView::selection()
120{
121 return rangeToScriptValue(m_engine, m_view->selectionRange());
122}
123
124QJSValue KateScriptView::selections()
125{
126 // pack our stuff into js array
127 const auto ranges = m_view->selectionRanges();
128 QJSValue jsArray = m_engine->newArray(ranges.size());
129 for (int i = 0; i < ranges.size(); ++i) {
130 jsArray.setProperty(i, rangeToScriptValue(m_engine, ranges[i]));
131 }
132 return jsArray;
133}
134
135void KateScriptView::setSelection(const QJSValue &jsrange)
136{
137 m_view->setSelection(rangeFromScriptValue(jsrange));
138}
139
140void KateScriptView::setSelections(const QJSValue &ranges)
141{
142 // unpack the array of ranges
143 QList<KTextEditor::Range> unboxedRanges;
144 const int length = ranges.property(QStringLiteral("length")).toInt();
145 for (int i = 0; i < length; ++i) {
146 unboxedRanges.append(rangeFromScriptValue(ranges.property(i)));
147 }
148 m_view->setSelections(unboxedRanges);
149}
150
151void KateScriptView::removeSelectedText()
152{
153 m_view->removeSelectedText();
154}
155
156void KateScriptView::selectAll()
157{
158 m_view->selectAll();
159}
160
161void KateScriptView::clearSelection()
162{
163 m_view->clearSelection();
164}
165
166void KateScriptView::setBlockSelection(bool on)
167{
168 m_view->setBlockSelection(on);
169}
170
171bool KateScriptView::blockSelection()
172{
173 return m_view->blockSelection();
174}
175
176void KateScriptView::align(const QJSValue &jsrange)
177{
178 const auto range = rangeFromScriptValue(jsrange);
179 m_view->doc()->align(m_view, range);
180}
181
182void KateScriptView::alignOn(const QJSValue &jsrange, const QJSValue &pattern)
183{
184 if (!pattern.isString()) {
185 return;
186 }
187 const auto range = rangeFromScriptValue(jsrange);
188 m_view->doc()->alignOn(range, pattern.toString(), m_view->blockSelection());
189}
190
191QJSValue KateScriptView::searchText(const QJSValue &range, const QString &pattern, bool backwards)
192{
194 KTextEditor::Range match = m_view->doc()->searchText(rangeFromScriptValue(range), pattern, option).first();
195 return rangeToScriptValue(m_engine, match);
196}
197
198QJSValue KateScriptView::executeCommand(const QString &command, const QString &args, const QJSValue &jsrange)
199{
200 QString message;
201 bool ok = false;
202
203 const auto range = rangeFromScriptValue(jsrange);
204 const auto cmd = KTextEditor::EditorPrivate::self()->queryCommand(command);
205 if (!cmd) {
206 ok = false;
207 message = i18n("Command not found: %1", command);
208 } else {
209 const auto cmdLine = args.isEmpty() ? (command) : (command + QLatin1Char(' ') + args);
210 ok = cmd->exec(m_view, cmdLine, message, range);
211 }
212
213 QJSValue object = m_engine->newObject();
214 object.setProperty(QStringLiteral("ok"), ok);
215 object.setProperty(QStringLiteral("status"), message);
216 return object;
217}
218
219#include "moc_katescriptview.cpp"
The Cursor represents a position in a Document.
Definition cursor.h:75
KTextEditor::Command * queryCommand(const QString &cmd) const override
query for command
static KTextEditor::EditorPrivate * self()
Kate Part Internal stuff ;)
An object representing a section of text, from one Cursor to another.
void setCursorPositions(const QList< KTextEditor::Cursor > &positions)
Set the view's new cursors to positions.
QList< KTextEditor::Cursor > cursorPositions() const
Get the view's current cursor positions.
KateScriptView(QJSEngine *, QObject *parent=nullptr)
Properties are accessible with a nicer syntax from JavaScript.
Q_INVOKABLE void setCursorPosition(int line, int column)
Set the cursor position in the view.
QString i18n(const char *text, const TYPE &arg...)
KCOREADDONS_EXPORT Result match(QStringView pattern, QStringView str)
@ Default
Default settings.
Definition document.h:48
@ Backwards
Searches in backward direction.
Definition document.h:55
QJSValue newArray(uint length)
QJSValue newObject()
bool isString() const const
QJSValue property(const QString &name) const const
void setProperty(const QString &name, const QJSValue &value)
qint32 toInt() const const
QString toString() const const
void append(QList< T > &&value)
T & first()
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 11:56:00 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.