KTextEditor

katescriptview.cpp
1 /* SPDX-License-Identifier: LGPL-2.0-or-later
2 
3  Copyright (C) 2008 Paul Giannaros <[email protected]>
4  Copyright (C) 2008 Christoph Cullmann <[email protected]>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
22 #include "katescriptview.h"
23 
24 #include "katedocument.h"
25 #include "kateglobal.h"
26 #include "katerenderer.h"
27 #include "katescript.h"
28 #include "kateview.h"
29 #include "scriptcursor.h"
30 #include "scriptrange.h"
31 
32 #include <KLocalizedString>
33 #include <KTextEditor/Command>
34 
35 #include <QJSEngine>
36 
38  : QObject(parent)
39  , m_view(nullptr)
40  , m_engine(engine)
41 {
42 }
43 
44 void KateScriptView::setView(KTextEditor::ViewPrivate *view)
45 {
46  m_view = view;
47 }
48 
49 KTextEditor::ViewPrivate *KateScriptView::view()
50 {
51  return m_view;
52 }
53 
54 QJSValue KateScriptView::cursorPosition()
55 {
56  return cursorToScriptValue(m_engine, m_view->cursorPosition());
57 }
58 
59 void KateScriptView::setCursorPosition(int line, int column)
60 {
61  const KTextEditor::Cursor cursor(line, column);
62  m_view->setCursorPosition(cursor);
63 }
64 
65 void KateScriptView::setCursorPosition(const QJSValue &jscursor)
66 {
67  const auto cursor = cursorFromScriptValue(jscursor);
68  m_view->setCursorPosition(cursor);
69 }
70 
71 QJSValue KateScriptView::virtualCursorPosition()
72 {
73  return cursorToScriptValue(m_engine, m_view->cursorPositionVirtual());
74 }
75 
76 void KateScriptView::setVirtualCursorPosition(int line, int column)
77 {
78  const KTextEditor::Cursor cursor(line, column);
79  m_view->setCursorPositionVisual(cursor);
80 }
81 
82 void KateScriptView::setVirtualCursorPosition(const QJSValue &jscursor)
83 {
84  const auto cursor = cursorFromScriptValue(jscursor);
85  setVirtualCursorPosition(cursor.line(), cursor.column());
86 }
87 
88 QString KateScriptView::selectedText()
89 {
90  return m_view->selectionText();
91 }
92 
93 bool KateScriptView::hasSelection()
94 {
95  return m_view->selection();
96 }
97 
98 QJSValue KateScriptView::selection()
99 {
100  return rangeToScriptValue(m_engine, m_view->selectionRange());
101 }
102 
103 void KateScriptView::setSelection(const QJSValue &jsrange)
104 {
105  m_view->setSelection(rangeFromScriptValue(jsrange));
106 }
107 
108 void KateScriptView::removeSelectedText()
109 {
110  m_view->removeSelectedText();
111 }
112 
113 void KateScriptView::selectAll()
114 {
115  m_view->selectAll();
116 }
117 
118 void KateScriptView::clearSelection()
119 {
120  m_view->clearSelection();
121 }
122 
123 void KateScriptView::align(const QJSValue &jsrange)
124 {
125  const auto range = rangeFromScriptValue(jsrange);
126  m_view->doc()->align(m_view, range);
127 }
128 
129 QJSValue KateScriptView::executeCommand(const QString &command, const QString &args, const QJSValue &jsrange)
130 {
131  QString message;
132  bool ok = false;
133 
134  const auto range = rangeFromScriptValue(jsrange);
135  const auto cmd = KTextEditor::EditorPrivate::self()->queryCommand(command);
136  if (!cmd) {
137  ok = false;
138  message = i18n("Command not found: %1", command);
139  } else {
140  const auto cmdLine = args.isEmpty() ? (command) : (command + QLatin1Char(' ') + args);
141  ok = cmd->exec(m_view, cmdLine, message, range);
142  }
143 
144  QJSValue object;
145  object.setProperty(QStringLiteral("ok"), ok);
146  object.setProperty(QStringLiteral("status"), message);
147  return object;
148 }
The Cursor represents a position in a Document.
Definition: cursor.h:85
void setProperty(const QString &name, const QJSValue &value)
bool isEmpty() const const
QString i18n(const char *text, const TYPE &arg...)
KTextEditor::Command * queryCommand(const QString &cmd) const override
query for command
Definition: kateglobal.cpp:434
Q_INVOKABLE void setCursorPosition(int line, int column)
Set the cursor position in the view.
static KTextEditor::EditorPrivate * self()
Kate Part Internal stuff ;)
Definition: kateglobal.cpp:373
KateScriptView(QJSEngine *, QObject *parent=nullptr)
Properties are accessible with a nicer syntax from JavaScript.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed Jul 8 2020 22:56:16 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.