File indexing completed on 2024-04-28 07:46:48

0001 /*
0002     SPDX-FileCopyrightText: 2001 Christoph Cullmann <cullmann@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "kateview.h"
0008 
0009 #include "cursor.h"
0010 
0011 #include "configpage.h"
0012 
0013 #include "editor.h"
0014 
0015 #include "document.h"
0016 
0017 #include "view.h"
0018 
0019 #include "plugin.h"
0020 
0021 #include "command.h"
0022 #include "inlinenote.h"
0023 #include "inlinenotedata.h"
0024 #include "inlinenoteprovider.h"
0025 #include "katerenderer.h"
0026 #include "katevariableexpansionmanager.h"
0027 #include "sessionconfiginterface.h"
0028 #include "texthintinterface.h"
0029 #include "variable.h"
0030 
0031 #include "abstractannotationitemdelegate.h"
0032 #include "annotationinterface.h"
0033 
0034 #include "katecmd.h"
0035 #include "kateconfig.h"
0036 #include "kateglobal.h"
0037 #include "katesyntaxmanager.h"
0038 
0039 using namespace KTextEditor;
0040 
0041 Cursor Cursor::fromString(QStringView str) noexcept
0042 {
0043     // parse format "(line, column)"
0044     const int startIndex = str.indexOf(QLatin1Char('('));
0045     const int endIndex = str.indexOf(QLatin1Char(')'));
0046     const int commaIndex = str.indexOf(QLatin1Char(','));
0047 
0048     if (startIndex < 0 || endIndex < 0 || commaIndex < 0 || commaIndex < startIndex || endIndex < commaIndex || endIndex < startIndex) {
0049         return invalid();
0050     }
0051 
0052     bool ok1 = false;
0053     bool ok2 = false;
0054 
0055     const int line = str.mid(startIndex + 1, commaIndex - startIndex - 1).toInt(&ok1);
0056     const int column = str.mid(commaIndex + 1, endIndex - commaIndex - 1).toInt(&ok2);
0057 
0058     if (!ok1 || !ok2) {
0059         return invalid();
0060     }
0061 
0062     return {line, column};
0063 }
0064 
0065 QString Cursor::toString() const
0066 {
0067     return QStringLiteral("(%1, %2)").arg(m_line).arg(m_column);
0068 }
0069 
0070 QDebug operator<<(QDebug s, KTextEditor::Cursor cursor)
0071 {
0072     s.nospace() << "(" << cursor.line() << ", " << cursor.column() << ")";
0073     return s.space();
0074 }
0075 
0076 size_t KTextEditor::qHash(KTextEditor::Cursor cursor, size_t seed) noexcept
0077 {
0078     return qHashMulti(seed, cursor.line(), cursor.column());
0079 }
0080 
0081 Editor::Editor(EditorPrivate *impl)
0082     : QObject()
0083     , d(impl)
0084 {
0085 }
0086 
0087 Editor::~Editor() = default;
0088 
0089 Editor *KTextEditor::Editor::instance()
0090 {
0091     // Just use internal KTextEditor::EditorPrivate::self()
0092     return KTextEditor::EditorPrivate::self();
0093 }
0094 
0095 QString Editor::defaultEncoding() const
0096 {
0097     // return default encoding in global config object
0098     return d->documentConfig()->encoding();
0099 }
0100 
0101 bool Editor::registerVariableMatch(const QString &name, const QString &description, ExpandFunction expansionFunc)
0102 {
0103     const auto var = Variable(name, description, expansionFunc, false);
0104     return d->variableExpansionManager()->addVariable(var);
0105 }
0106 
0107 bool Editor::registerVariablePrefix(const QString &prefix, const QString &description, ExpandFunction expansionFunc)
0108 {
0109     const auto var = Variable(prefix, description, expansionFunc, true);
0110     return d->variableExpansionManager()->addVariable(var);
0111 }
0112 
0113 bool Editor::unregisterVariable(const QString &variable)
0114 {
0115     return d->variableExpansionManager()->removeVariable(variable);
0116 }
0117 
0118 bool Editor::expandVariable(const QString &variable, KTextEditor::View *view, QString &output) const
0119 {
0120     return d->variableExpansionManager()->expandVariable(variable, view, output);
0121 }
0122 
0123 QString Editor::expandText(const QString &text, KTextEditor::View *view) const
0124 {
0125     return d->variableExpansionManager()->expandText(text, view);
0126 }
0127 
0128 void Editor::addVariableExpansion(const QList<QWidget *> &widgets, const QStringList &variables) const
0129 {
0130     d->variableExpansionManager()->showDialog(widgets, variables);
0131 }
0132 
0133 QFont Editor::font() const
0134 {
0135     return d->rendererConfig()->baseFont();
0136 }
0137 
0138 KSyntaxHighlighting::Theme Editor::theme() const
0139 {
0140     return KateHlManager::self()->repository().theme(d->rendererConfig()->schema());
0141 }
0142 
0143 const KSyntaxHighlighting::Repository &Editor::repository() const
0144 {
0145     return KateHlManager::self()->repository();
0146 }
0147 
0148 bool View::insertText(const QString &text)
0149 {
0150     KTextEditor::Document *doc = document();
0151     if (!doc) {
0152         return false;
0153     }
0154     return doc->insertText(cursorPosition(), text, blockSelection());
0155 }
0156 
0157 bool View::isStatusBarEnabled() const
0158 {
0159     // is the status bar around?
0160     return !!d->statusBar();
0161 }
0162 
0163 void View::setStatusBarEnabled(bool enable)
0164 {
0165     // no state change, do nothing
0166     if (enable == !!d->statusBar()) {
0167         return;
0168     }
0169 
0170     // else toggle it
0171     d->toggleStatusBar();
0172 }
0173 
0174 bool View::insertTemplate(KTextEditor::Cursor insertPosition, const QString &templateString, const QString &script)
0175 {
0176     return d->insertTemplateInternal(insertPosition, templateString, script);
0177 }
0178 
0179 KSyntaxHighlighting::Theme View::theme() const
0180 {
0181     return KateHlManager::self()->repository().theme(d->rendererConfig()->schema());
0182 }
0183 
0184 void View::setCursorPositions(const QList<KTextEditor::Cursor> &positions)
0185 {
0186     d->setCursors(positions);
0187 }
0188 
0189 QList<KTextEditor::Cursor> View::cursorPositions() const
0190 {
0191     return d->cursors();
0192 }
0193 
0194 void View::setSelections(const QList<KTextEditor::Range> &ranges)
0195 {
0196     d->setSelections(ranges);
0197 }
0198 
0199 QList<KTextEditor::Range> View::selectionRanges() const
0200 {
0201     return d->selectionRanges();
0202 }
0203 
0204 ConfigPage::ConfigPage(QWidget *parent)
0205     : QWidget(parent)
0206     , d(nullptr)
0207 {
0208 }
0209 
0210 ConfigPage::~ConfigPage() = default;
0211 
0212 QString ConfigPage::fullName() const
0213 {
0214     return name();
0215 }
0216 
0217 QIcon ConfigPage::icon() const
0218 {
0219     return QIcon::fromTheme(QStringLiteral("document-properties"));
0220 }
0221 
0222 View::View(ViewPrivate *impl, QWidget *parent)
0223     : QWidget(parent)
0224     , KXMLGUIClient()
0225     , d(impl)
0226 {
0227 }
0228 
0229 View::~View() = default;
0230 
0231 Plugin::Plugin(QObject *parent)
0232     : QObject(parent)
0233     , d(nullptr)
0234 {
0235 }
0236 
0237 Plugin::~Plugin() = default;
0238 
0239 int Plugin::configPages() const
0240 {
0241     return 0;
0242 }
0243 
0244 ConfigPage *Plugin::configPage(int, QWidget *)
0245 {
0246     return nullptr;
0247 }
0248 
0249 SessionConfigInterface::SessionConfigInterface() = default;
0250 
0251 SessionConfigInterface::~SessionConfigInterface() = default;
0252 
0253 TextHintProvider::TextHintProvider() = default;
0254 
0255 TextHintProvider::~TextHintProvider() = default;
0256 
0257 InlineNoteProvider::InlineNoteProvider() = default;
0258 
0259 InlineNoteProvider::~InlineNoteProvider() = default;
0260 
0261 KateInlineNoteData::KateInlineNoteData(KTextEditor::InlineNoteProvider *provider,
0262                                        const KTextEditor::View *view,
0263                                        const KTextEditor::Cursor position,
0264                                        int index,
0265                                        bool underMouse,
0266                                        const QFont &font,
0267                                        int lineHeight)
0268     : m_provider(provider)
0269     , m_view(view)
0270     , m_position(position)
0271     , m_index(index)
0272     , m_underMouse(underMouse)
0273     , m_font(font)
0274     , m_lineHeight(lineHeight)
0275 {
0276 }
0277 
0278 InlineNote::InlineNote(const KateInlineNoteData &data)
0279     : d(data)
0280 {
0281 }
0282 
0283 qreal InlineNote::width() const
0284 {
0285     return d.m_provider->inlineNoteSize(*this).width();
0286 }
0287 
0288 bool KTextEditor::InlineNote::underMouse() const
0289 {
0290     return d.m_underMouse;
0291 }
0292 
0293 void KTextEditor::InlineNoteProvider::inlineNoteActivated(const InlineNote &note, Qt::MouseButtons buttons, const QPoint &globalPos)
0294 {
0295     Q_UNUSED(note);
0296     Q_UNUSED(buttons);
0297     Q_UNUSED(globalPos);
0298 }
0299 
0300 void KTextEditor::InlineNoteProvider::inlineNoteFocusInEvent(const KTextEditor::InlineNote &note, const QPoint &globalPos)
0301 {
0302     Q_UNUSED(note);
0303     Q_UNUSED(globalPos);
0304 }
0305 
0306 void KTextEditor::InlineNoteProvider::inlineNoteFocusOutEvent(const KTextEditor::InlineNote &note)
0307 {
0308     Q_UNUSED(note);
0309 }
0310 
0311 void KTextEditor::InlineNoteProvider::inlineNoteMouseMoveEvent(const KTextEditor::InlineNote &note, const QPoint &globalPos)
0312 {
0313     Q_UNUSED(note);
0314     Q_UNUSED(globalPos);
0315 }
0316 
0317 KTextEditor::InlineNoteProvider *InlineNote::provider() const
0318 {
0319     return d.m_provider;
0320 }
0321 
0322 const KTextEditor::View *InlineNote::view() const
0323 {
0324     return d.m_view;
0325 }
0326 
0327 QFont InlineNote::font() const
0328 {
0329     return d.m_font;
0330 }
0331 
0332 int InlineNote::index() const
0333 {
0334     return d.m_index;
0335 }
0336 
0337 int InlineNote::lineHeight() const
0338 {
0339     return d.m_lineHeight;
0340 }
0341 
0342 KTextEditor::Cursor InlineNote::position() const
0343 {
0344     return d.m_position;
0345 }
0346 
0347 Command::Command(const QStringList &cmds, QObject *parent)
0348     : QObject(parent)
0349     , m_cmds(cmds)
0350     , d(nullptr)
0351 {
0352     // register this command
0353     static_cast<KTextEditor::EditorPrivate *>(KTextEditor::Editor::instance())->cmdManager()->registerCommand(this);
0354 }
0355 
0356 Command::~Command()
0357 {
0358     // unregister this command, if instance is still there!
0359     if (KTextEditor::Editor::instance()) {
0360         static_cast<KTextEditor::EditorPrivate *>(KTextEditor::Editor::instance())->cmdManager()->unregisterCommand(this);
0361     }
0362 }
0363 
0364 bool Command::supportsRange(const QString &)
0365 {
0366     return false;
0367 }
0368 
0369 KCompletion *Command::completionObject(KTextEditor::View *, const QString &)
0370 {
0371     return nullptr;
0372 }
0373 
0374 bool Command::wantsToProcessText(const QString &)
0375 {
0376     return false;
0377 }
0378 
0379 void Command::processText(KTextEditor::View *, const QString &)
0380 {
0381 }
0382 
0383 void View::setScrollPosition(KTextEditor::Cursor cursor)
0384 {
0385     d->setScrollPositionInternal(cursor);
0386 }
0387 
0388 void View::setHorizontalScrollPosition(int x)
0389 {
0390     d->setHorizontalScrollPositionInternal(x);
0391 }
0392 
0393 KTextEditor::Cursor View::maxScrollPosition() const
0394 {
0395     return d->maxScrollPositionInternal();
0396 }
0397 
0398 int View::firstDisplayedLine(LineType lineType) const
0399 {
0400     return d->firstDisplayedLineInternal(lineType);
0401 }
0402 
0403 int View::lastDisplayedLine(LineType lineType) const
0404 {
0405     return d->lastDisplayedLineInternal(lineType);
0406 }
0407 
0408 QRect View::textAreaRect() const
0409 {
0410     return d->textAreaRectInternal();
0411 }
0412 
0413 StyleOptionAnnotationItem::StyleOptionAnnotationItem()
0414     : contentFontMetrics(QFont())
0415 {
0416 }
0417 
0418 StyleOptionAnnotationItem::StyleOptionAnnotationItem(const StyleOptionAnnotationItem &other)
0419     : QStyleOption(Version, Type)
0420     , contentFontMetrics(QFont())
0421 {
0422     *this = other;
0423 }
0424 
0425 StyleOptionAnnotationItem::StyleOptionAnnotationItem(int version)
0426     : QStyleOption(version, Type)
0427     , contentFontMetrics(QFont())
0428 {
0429 }
0430 
0431 AbstractAnnotationItemDelegate::AbstractAnnotationItemDelegate(QObject *parent)
0432     : QObject(parent)
0433 {
0434 }
0435 
0436 AbstractAnnotationItemDelegate::~AbstractAnnotationItemDelegate() = default;
0437 
0438 AnnotationModel::~AnnotationModel() = default;
0439 
0440 #include "moc_abstractannotationitemdelegate.cpp"
0441 #include "moc_annotationinterface.cpp"
0442 #include "moc_application.cpp"
0443 #include "moc_codecompletionmodel.cpp"
0444 #include "moc_command.cpp"
0445 #include "moc_configpage.cpp"
0446 #include "moc_document.cpp"
0447 #include "moc_editor.cpp"
0448 #include "moc_inlinenoteprovider.cpp"
0449 #include "moc_mainwindow.cpp"
0450 #include "moc_message.cpp"
0451 #include "moc_plugin.cpp"
0452 #include "moc_view.cpp"