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 | |
21 | KateScriptView::KateScriptView(QJSEngine *engine, QObject *parent) |
22 | : QObject(parent) |
23 | , m_view(nullptr) |
24 | , m_engine(engine) |
25 | { |
26 | } |
27 | |
28 | void KateScriptView::setView(KTextEditor::ViewPrivate *view) |
29 | { |
30 | m_view = view; |
31 | } |
32 | |
33 | KTextEditor::ViewPrivate *KateScriptView::view() |
34 | { |
35 | return m_view; |
36 | } |
37 | |
38 | void KateScriptView::copy() |
39 | { |
40 | m_view->copy(); |
41 | } |
42 | |
43 | void KateScriptView::cut() |
44 | { |
45 | m_view->cut(); |
46 | } |
47 | |
48 | void KateScriptView::paste() |
49 | { |
50 | m_view->paste(); |
51 | } |
52 | |
53 | QJSValue KateScriptView::cursorPosition() |
54 | { |
55 | return cursorToScriptValue(engine: m_engine, cursor: m_view->cursorPosition()); |
56 | } |
57 | |
58 | QJSValue KateScriptView::cursorPositions() |
59 | { |
60 | // pack our stuff into js array |
61 | const auto cursors = m_view->cursorPositions(); |
62 | QJSValue jsArray = m_engine->newArray(length: cursors.size()); |
63 | for (int i = 0; i < cursors.size(); ++i) { |
64 | jsArray.setProperty(arrayIndex: i, value: cursorToScriptValue(engine: m_engine, cursor: cursors[i])); |
65 | } |
66 | return jsArray; |
67 | } |
68 | |
69 | void KateScriptView::setCursorPosition(int line, int column) |
70 | { |
71 | const KTextEditor::Cursor cursor(line, column); |
72 | m_view->setCursorPosition(cursor); |
73 | } |
74 | |
75 | void KateScriptView::setCursorPosition(const QJSValue &jscursor) |
76 | { |
77 | const auto cursor = cursorFromScriptValue(obj: jscursor); |
78 | m_view->setCursorPosition(cursor); |
79 | } |
80 | |
81 | void 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(t: cursorFromScriptValue(obj: cursors.property(arrayIndex: i))); |
88 | } |
89 | m_view->setCursorPositions(unboxedCursors); |
90 | } |
91 | |
92 | QJSValue KateScriptView::virtualCursorPosition() |
93 | { |
94 | return cursorToScriptValue(engine: m_engine, cursor: m_view->cursorPositionVirtual()); |
95 | } |
96 | |
97 | void KateScriptView::setVirtualCursorPosition(int line, int column) |
98 | { |
99 | const KTextEditor::Cursor cursor(line, column); |
100 | m_view->setCursorPositionVisual(cursor); |
101 | } |
102 | |
103 | void KateScriptView::setVirtualCursorPosition(const QJSValue &jscursor) |
104 | { |
105 | const auto cursor = cursorFromScriptValue(obj: jscursor); |
106 | setVirtualCursorPosition(line: cursor.line(), column: cursor.column()); |
107 | } |
108 | |
109 | QString KateScriptView::selectedText() |
110 | { |
111 | return m_view->selectionText(); |
112 | } |
113 | |
114 | bool KateScriptView::hasSelection() |
115 | { |
116 | return m_view->selection(); |
117 | } |
118 | |
119 | QJSValue KateScriptView::selection() |
120 | { |
121 | return rangeToScriptValue(engine: m_engine, range: m_view->selectionRange()); |
122 | } |
123 | |
124 | QJSValue KateScriptView::selections() |
125 | { |
126 | // pack our stuff into js array |
127 | const auto ranges = m_view->selectionRanges(); |
128 | QJSValue jsArray = m_engine->newArray(length: ranges.size()); |
129 | for (int i = 0; i < ranges.size(); ++i) { |
130 | jsArray.setProperty(arrayIndex: i, value: rangeToScriptValue(engine: m_engine, range: ranges[i])); |
131 | } |
132 | return jsArray; |
133 | } |
134 | |
135 | void KateScriptView::setSelection(const QJSValue &jsrange) |
136 | { |
137 | m_view->setSelection(rangeFromScriptValue(obj: jsrange)); |
138 | } |
139 | |
140 | void 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(t: rangeFromScriptValue(obj: ranges.property(arrayIndex: i))); |
147 | } |
148 | m_view->setSelections(unboxedRanges); |
149 | } |
150 | |
151 | void KateScriptView::removeSelectedText() |
152 | { |
153 | m_view->removeSelectedText(); |
154 | } |
155 | |
156 | void KateScriptView::selectAll() |
157 | { |
158 | m_view->selectAll(); |
159 | } |
160 | |
161 | void KateScriptView::clearSelection() |
162 | { |
163 | m_view->clearSelection(); |
164 | } |
165 | |
166 | void KateScriptView::setBlockSelection(bool on) |
167 | { |
168 | m_view->setBlockSelection(on); |
169 | } |
170 | |
171 | bool KateScriptView::blockSelection() |
172 | { |
173 | return m_view->blockSelection(); |
174 | } |
175 | |
176 | void KateScriptView::align(const QJSValue &jsrange) |
177 | { |
178 | const auto range = rangeFromScriptValue(obj: jsrange); |
179 | m_view->doc()->align(view: m_view, range); |
180 | } |
181 | |
182 | void KateScriptView::alignOn(const QJSValue &jsrange, const QJSValue &pattern) |
183 | { |
184 | if (!pattern.isString()) { |
185 | return; |
186 | } |
187 | const auto range = rangeFromScriptValue(obj: jsrange); |
188 | m_view->doc()->alignOn(range, pattern: pattern.toString(), blockwise: m_view->blockSelection()); |
189 | } |
190 | |
191 | QJSValue KateScriptView::searchText(const QJSValue &range, const QString &pattern, bool backwards) |
192 | { |
193 | KTextEditor::SearchOptions option = backwards ? KTextEditor::Backwards : KTextEditor::Default; |
194 | KTextEditor::Range match = m_view->doc()->searchText(range: rangeFromScriptValue(obj: range), pattern, options: option).first(); |
195 | return rangeToScriptValue(engine: m_engine, range: match); |
196 | } |
197 | |
198 | QJSValue 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(obj: jsrange); |
204 | const auto cmd = KTextEditor::EditorPrivate::self()->queryCommand(cmd: 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(view: m_view, cmd: cmdLine, msg&: message, range); |
211 | } |
212 | |
213 | QJSValue object; |
214 | object.setProperty(QStringLiteral("ok" ), value: ok); |
215 | object.setProperty(QStringLiteral("status" ), value: message); |
216 | return object; |
217 | } |
218 | |
219 | #include "moc_katescriptview.cpp" |
220 | |