1 | /* |
2 | SPDX-FileCopyrightText: 2001 Christoph Cullmann <cullmann@kde.org> |
3 | |
4 | SPDX-License-Identifier: LGPL-2.0-or-later |
5 | */ |
6 | |
7 | #include "kateview.h" |
8 | |
9 | #include "cursor.h" |
10 | |
11 | #include "configpage.h" |
12 | |
13 | #include "editor.h" |
14 | |
15 | #include "document.h" |
16 | |
17 | #include "view.h" |
18 | |
19 | #include "plugin.h" |
20 | |
21 | #include "command.h" |
22 | #include "inlinenote.h" |
23 | #include "inlinenotedata.h" |
24 | #include "inlinenoteprovider.h" |
25 | #include "katerenderer.h" |
26 | #include "katevariableexpansionmanager.h" |
27 | #include "sessionconfiginterface.h" |
28 | #include "texthintinterface.h" |
29 | #include "variable.h" |
30 | |
31 | #include "abstractannotationitemdelegate.h" |
32 | #include "annotationinterface.h" |
33 | |
34 | #include "katecmd.h" |
35 | #include "kateconfig.h" |
36 | #include "kateglobal.h" |
37 | #include "katesyntaxmanager.h" |
38 | |
39 | using namespace KTextEditor; |
40 | |
41 | Cursor Cursor::fromString(QStringView str) noexcept |
42 | { |
43 | // parse format "(line, column)" |
44 | const int startIndex = str.indexOf(c: QLatin1Char('(')); |
45 | const int endIndex = str.indexOf(c: QLatin1Char(')')); |
46 | const int commaIndex = str.indexOf(c: QLatin1Char(',')); |
47 | |
48 | if (startIndex < 0 || endIndex < 0 || commaIndex < 0 || commaIndex < startIndex || endIndex < commaIndex || endIndex < startIndex) { |
49 | return invalid(); |
50 | } |
51 | |
52 | bool ok1 = false; |
53 | bool ok2 = false; |
54 | |
55 | const int line = str.mid(pos: startIndex + 1, n: commaIndex - startIndex - 1).toInt(ok: &ok1); |
56 | const int column = str.mid(pos: commaIndex + 1, n: endIndex - commaIndex - 1).toInt(ok: &ok2); |
57 | |
58 | if (!ok1 || !ok2) { |
59 | return invalid(); |
60 | } |
61 | |
62 | return {line, column}; |
63 | } |
64 | |
65 | QString Cursor::toString() const |
66 | { |
67 | return QStringLiteral("(%1, %2)" ).arg(a: m_line).arg(a: m_column); |
68 | } |
69 | |
70 | QDebug operator<<(QDebug s, KTextEditor::Cursor cursor) |
71 | { |
72 | s.nospace() << "(" << cursor.line() << ", " << cursor.column() << ")" ; |
73 | return s.space(); |
74 | } |
75 | |
76 | size_t KTextEditor::qHash(KTextEditor::Cursor cursor, size_t seed) noexcept |
77 | { |
78 | return qHashMulti(seed, args: cursor.line(), args: cursor.column()); |
79 | } |
80 | |
81 | Editor::Editor(EditorPrivate *impl) |
82 | : QObject() |
83 | , d(impl) |
84 | { |
85 | } |
86 | |
87 | Editor::~Editor() = default; |
88 | |
89 | Editor *KTextEditor::Editor::instance() |
90 | { |
91 | // Just use internal KTextEditor::EditorPrivate::self() |
92 | return KTextEditor::EditorPrivate::self(); |
93 | } |
94 | |
95 | QString Editor::defaultEncoding() const |
96 | { |
97 | // return default encoding in global config object |
98 | return d->documentConfig()->encoding(); |
99 | } |
100 | |
101 | bool Editor::registerVariableMatch(const QString &name, const QString &description, ExpandFunction expansionFunc) |
102 | { |
103 | const auto var = Variable(name, description, expansionFunc, false); |
104 | return d->variableExpansionManager()->addVariable(variable: var); |
105 | } |
106 | |
107 | bool Editor::registerVariablePrefix(const QString &prefix, const QString &description, ExpandFunction expansionFunc) |
108 | { |
109 | const auto var = Variable(prefix, description, expansionFunc, true); |
110 | return d->variableExpansionManager()->addVariable(variable: var); |
111 | } |
112 | |
113 | bool Editor::unregisterVariable(const QString &variable) |
114 | { |
115 | return d->variableExpansionManager()->removeVariable(name: variable); |
116 | } |
117 | |
118 | bool Editor::expandVariable(const QString &variable, KTextEditor::View *view, QString &output) const |
119 | { |
120 | return d->variableExpansionManager()->expandVariable(variable, view, output); |
121 | } |
122 | |
123 | QString Editor::expandText(const QString &text, KTextEditor::View *view) const |
124 | { |
125 | return d->variableExpansionManager()->expandText(text, view); |
126 | } |
127 | |
128 | void Editor::addVariableExpansion(const QList<QWidget *> &widgets, const QStringList &variables) const |
129 | { |
130 | d->variableExpansionManager()->showDialog(widgets, names: variables); |
131 | } |
132 | |
133 | QFont Editor::font() const |
134 | { |
135 | return d->rendererConfig()->baseFont(); |
136 | } |
137 | |
138 | KSyntaxHighlighting::Theme Editor::theme() const |
139 | { |
140 | return KateHlManager::self()->repository().theme(themeName: d->rendererConfig()->schema()); |
141 | } |
142 | |
143 | const KSyntaxHighlighting::Repository &Editor::repository() const |
144 | { |
145 | return KateHlManager::self()->repository(); |
146 | } |
147 | |
148 | bool View::insertText(const QString &text) |
149 | { |
150 | KTextEditor::Document *doc = document(); |
151 | if (!doc) { |
152 | return false; |
153 | } |
154 | return doc->insertText(position: cursorPosition(), text, block: blockSelection()); |
155 | } |
156 | |
157 | bool View::isStatusBarEnabled() const |
158 | { |
159 | // is the status bar around? |
160 | return !!d->statusBar(); |
161 | } |
162 | |
163 | void View::setStatusBarEnabled(bool enable) |
164 | { |
165 | // no state change, do nothing |
166 | if (enable == !!d->statusBar()) { |
167 | return; |
168 | } |
169 | |
170 | // else toggle it |
171 | d->toggleStatusBar(); |
172 | } |
173 | |
174 | bool View::insertTemplate(KTextEditor::Cursor insertPosition, const QString &templateString, const QString &script) |
175 | { |
176 | return d->insertTemplateInternal(insertPosition, templateString, script); |
177 | } |
178 | |
179 | KSyntaxHighlighting::Theme View::theme() const |
180 | { |
181 | return KateHlManager::self()->repository().theme(themeName: d->rendererConfig()->schema()); |
182 | } |
183 | |
184 | void View::setCursorPositions(const QList<KTextEditor::Cursor> &positions) |
185 | { |
186 | d->setCursors(positions); |
187 | } |
188 | |
189 | QList<KTextEditor::Cursor> View::cursorPositions() const |
190 | { |
191 | return d->cursors(); |
192 | } |
193 | |
194 | void View::setSelections(const QList<KTextEditor::Range> &ranges) |
195 | { |
196 | d->setSelections(ranges); |
197 | } |
198 | |
199 | QList<KTextEditor::Range> View::selectionRanges() const |
200 | { |
201 | return d->selectionRanges(); |
202 | } |
203 | |
204 | ConfigPage::ConfigPage(QWidget *parent) |
205 | : QWidget(parent) |
206 | , d(nullptr) |
207 | { |
208 | } |
209 | |
210 | ConfigPage::~ConfigPage() = default; |
211 | |
212 | QString ConfigPage::fullName() const |
213 | { |
214 | return name(); |
215 | } |
216 | |
217 | QIcon ConfigPage::icon() const |
218 | { |
219 | return QIcon::fromTheme(QStringLiteral("document-properties" )); |
220 | } |
221 | |
222 | View::View(ViewPrivate *impl, QWidget *parent) |
223 | : QWidget(parent) |
224 | , KXMLGUIClient() |
225 | , d(impl) |
226 | { |
227 | } |
228 | |
229 | View::~View() = default; |
230 | |
231 | Plugin::Plugin(QObject *parent) |
232 | : QObject(parent) |
233 | , d(nullptr) |
234 | { |
235 | } |
236 | |
237 | Plugin::~Plugin() = default; |
238 | |
239 | int Plugin::configPages() const |
240 | { |
241 | return 0; |
242 | } |
243 | |
244 | ConfigPage *Plugin::configPage(int, QWidget *) |
245 | { |
246 | return nullptr; |
247 | } |
248 | |
249 | SessionConfigInterface::SessionConfigInterface() = default; |
250 | |
251 | SessionConfigInterface::~SessionConfigInterface() = default; |
252 | |
253 | TextHintProvider::TextHintProvider() = default; |
254 | |
255 | TextHintProvider::~TextHintProvider() = default; |
256 | |
257 | InlineNoteProvider::InlineNoteProvider() = default; |
258 | |
259 | InlineNoteProvider::~InlineNoteProvider() = default; |
260 | |
261 | KateInlineNoteData::KateInlineNoteData(KTextEditor::InlineNoteProvider *provider, |
262 | const KTextEditor::View *view, |
263 | const KTextEditor::Cursor position, |
264 | int index, |
265 | bool underMouse, |
266 | const QFont &font, |
267 | int lineHeight) |
268 | : m_provider(provider) |
269 | , m_view(view) |
270 | , m_position(position) |
271 | , m_index(index) |
272 | , m_underMouse(underMouse) |
273 | , m_font(font) |
274 | , m_lineHeight(lineHeight) |
275 | { |
276 | } |
277 | |
278 | InlineNote::InlineNote(const KateInlineNoteData &data) |
279 | : d(data) |
280 | { |
281 | } |
282 | |
283 | qreal InlineNote::width() const |
284 | { |
285 | return d.m_provider->inlineNoteSize(note: *this).width(); |
286 | } |
287 | |
288 | bool KTextEditor::InlineNote::underMouse() const |
289 | { |
290 | return d.m_underMouse; |
291 | } |
292 | |
293 | void KTextEditor::InlineNoteProvider::inlineNoteActivated(const InlineNote ¬e, Qt::MouseButtons buttons, const QPoint &globalPos) |
294 | { |
295 | Q_UNUSED(note); |
296 | Q_UNUSED(buttons); |
297 | Q_UNUSED(globalPos); |
298 | } |
299 | |
300 | void KTextEditor::InlineNoteProvider::inlineNoteFocusInEvent(const KTextEditor::InlineNote ¬e, const QPoint &globalPos) |
301 | { |
302 | Q_UNUSED(note); |
303 | Q_UNUSED(globalPos); |
304 | } |
305 | |
306 | void KTextEditor::InlineNoteProvider::inlineNoteFocusOutEvent(const KTextEditor::InlineNote ¬e) |
307 | { |
308 | Q_UNUSED(note); |
309 | } |
310 | |
311 | void KTextEditor::InlineNoteProvider::inlineNoteMouseMoveEvent(const KTextEditor::InlineNote ¬e, const QPoint &globalPos) |
312 | { |
313 | Q_UNUSED(note); |
314 | Q_UNUSED(globalPos); |
315 | } |
316 | |
317 | KTextEditor::InlineNoteProvider *InlineNote::provider() const |
318 | { |
319 | return d.m_provider; |
320 | } |
321 | |
322 | const KTextEditor::View *InlineNote::view() const |
323 | { |
324 | return d.m_view; |
325 | } |
326 | |
327 | QFont InlineNote::font() const |
328 | { |
329 | return d.m_font; |
330 | } |
331 | |
332 | int InlineNote::index() const |
333 | { |
334 | return d.m_index; |
335 | } |
336 | |
337 | int InlineNote::lineHeight() const |
338 | { |
339 | return d.m_lineHeight; |
340 | } |
341 | |
342 | KTextEditor::Cursor InlineNote::position() const |
343 | { |
344 | return d.m_position; |
345 | } |
346 | |
347 | Command::Command(const QStringList &cmds, QObject *parent) |
348 | : QObject(parent) |
349 | , m_cmds(cmds) |
350 | , d(nullptr) |
351 | { |
352 | // register this command |
353 | static_cast<KTextEditor::EditorPrivate *>(KTextEditor::Editor::instance())->cmdManager()->registerCommand(cmd: this); |
354 | } |
355 | |
356 | Command::~Command() |
357 | { |
358 | // unregister this command, if instance is still there! |
359 | if (KTextEditor::Editor::instance()) { |
360 | static_cast<KTextEditor::EditorPrivate *>(KTextEditor::Editor::instance())->cmdManager()->unregisterCommand(cmd: this); |
361 | } |
362 | } |
363 | |
364 | bool Command::supportsRange(const QString &) |
365 | { |
366 | return false; |
367 | } |
368 | |
369 | KCompletion *Command::completionObject(KTextEditor::View *, const QString &) |
370 | { |
371 | return nullptr; |
372 | } |
373 | |
374 | bool Command::wantsToProcessText(const QString &) |
375 | { |
376 | return false; |
377 | } |
378 | |
379 | void Command::processText(KTextEditor::View *, const QString &) |
380 | { |
381 | } |
382 | |
383 | void View::setScrollPosition(KTextEditor::Cursor cursor) |
384 | { |
385 | d->setScrollPositionInternal(cursor); |
386 | } |
387 | |
388 | void View::setHorizontalScrollPosition(int x) |
389 | { |
390 | d->setHorizontalScrollPositionInternal(x); |
391 | } |
392 | |
393 | KTextEditor::Cursor View::maxScrollPosition() const |
394 | { |
395 | return d->maxScrollPositionInternal(); |
396 | } |
397 | |
398 | int View::firstDisplayedLine(LineType lineType) const |
399 | { |
400 | return d->firstDisplayedLineInternal(lineType); |
401 | } |
402 | |
403 | int View::lastDisplayedLine(LineType lineType) const |
404 | { |
405 | return d->lastDisplayedLineInternal(lineType); |
406 | } |
407 | |
408 | QRect View::textAreaRect() const |
409 | { |
410 | return d->textAreaRectInternal(); |
411 | } |
412 | |
413 | StyleOptionAnnotationItem::StyleOptionAnnotationItem() |
414 | : contentFontMetrics(QFont()) |
415 | { |
416 | } |
417 | |
418 | StyleOptionAnnotationItem::StyleOptionAnnotationItem(const StyleOptionAnnotationItem &other) |
419 | : QStyleOption(Version, Type) |
420 | , contentFontMetrics(QFont()) |
421 | { |
422 | *this = other; |
423 | } |
424 | |
425 | StyleOptionAnnotationItem::StyleOptionAnnotationItem(int version) |
426 | : QStyleOption(version, Type) |
427 | , contentFontMetrics(QFont()) |
428 | { |
429 | } |
430 | |
431 | AbstractAnnotationItemDelegate::AbstractAnnotationItemDelegate(QObject *parent) |
432 | : QObject(parent) |
433 | { |
434 | } |
435 | |
436 | AbstractAnnotationItemDelegate::~AbstractAnnotationItemDelegate() = default; |
437 | |
438 | AnnotationModel::~AnnotationModel() = default; |
439 | |
440 | #include "moc_abstractannotationitemdelegate.cpp" |
441 | #include "moc_annotationinterface.cpp" |
442 | #include "moc_application.cpp" |
443 | #include "moc_codecompletionmodel.cpp" |
444 | #include "moc_command.cpp" |
445 | #include "moc_configpage.cpp" |
446 | #include "moc_document.cpp" |
447 | #include "moc_editor.cpp" |
448 | #include "moc_inlinenoteprovider.cpp" |
449 | #include "moc_mainwindow.cpp" |
450 | #include "moc_message.cpp" |
451 | #include "moc_plugin.cpp" |
452 | #include "moc_view.cpp" |
453 | |