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

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 2005 Hamish Rodda <rodda@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "codecompletiontestmodel.h"
0009 
0010 #include <kateglobal.h>
0011 #include <katewordcompletion.h>
0012 #include <ktexteditor/document.h>
0013 #include <ktexteditor/view.h>
0014 
0015 CodeCompletionTestModel::CodeCompletionTestModel(KTextEditor::View *parent, const QString &startText)
0016     : KTextEditor::CodeCompletionModel(parent)
0017     , m_startText(startText)
0018     , m_autoStartText(m_startText.isEmpty())
0019 {
0020     setRowCount(40);
0021 
0022     parent->setAutomaticInvocationEnabled(true);
0023     parent->unregisterCompletionModel(KTextEditor::EditorPrivate::self()->wordCompletionModel()); // would add additional items, we don't want that in tests
0024     parent->registerCompletionModel(this);
0025 }
0026 
0027 // Fake a series of completions
0028 QVariant CodeCompletionTestModel::data(const QModelIndex &index, int role) const
0029 {
0030     switch (role) {
0031     case Qt::DisplayRole:
0032         if (index.row() < rowCount() / 2) {
0033             switch (index.column()) {
0034             case Prefix:
0035                 switch (index.row() % 3) {
0036                 default:
0037                     return QStringLiteral("void ");
0038                 case 1:
0039                     return QStringLiteral("const QString& ");
0040                 case 2:
0041                     if (index.row() % 6) {
0042                         return QStringLiteral("inline virtual bool ");
0043                     }
0044                     return QStringLiteral("virtual bool ");
0045                 }
0046 
0047             case Scope:
0048                 switch (index.row() % 4) {
0049                 default:
0050                     return QString();
0051                 case 1:
0052                     return QStringLiteral("KTextEditor::");
0053                 case 2:
0054                     return QStringLiteral("::");
0055                 case 3:
0056                     return QStringLiteral("std::");
0057                 }
0058 
0059             case Name:
0060                 return QString(m_startText + QStringLiteral("%1%2%3").arg(QChar('a' + (index.row() % 3))).arg(QChar('a' + index.row())).arg(index.row()));
0061 
0062             case Arguments:
0063                 switch (index.row() % 5) {
0064                 default:
0065                     return QStringLiteral("()");
0066                 case 1:
0067                     return QStringLiteral("(bool trigger)");
0068                 case 4:
0069                     return QStringLiteral("(const QString& name, Qt::CaseSensitivity cs)");
0070                 case 5:
0071                     return QStringLiteral("(int count)");
0072                 }
0073 
0074             case Postfix:
0075                 switch (index.row() % 3) {
0076                 default:
0077                     return QStringLiteral(" const");
0078                 case 1:
0079                     return QStringLiteral(" KDE_DEPRECATED");
0080                 case 2:
0081                     return QLatin1String("");
0082                 }
0083             }
0084         } else {
0085             switch (index.column()) {
0086             case Prefix:
0087                 switch (index.row() % 3) {
0088                 default:
0089                     return QStringLiteral("void ");
0090                 case 1:
0091                     return QStringLiteral("const QString ");
0092                 case 2:
0093                     return QStringLiteral("bool ");
0094                 }
0095 
0096             case Scope:
0097                 switch (index.row() % 4) {
0098                 default:
0099                     return QString();
0100                 case 1:
0101                     return QStringLiteral("KTextEditor::");
0102                 case 2:
0103                     return QStringLiteral("::");
0104                 case 3:
0105                     return QStringLiteral("std::");
0106                 }
0107 
0108             case Name:
0109                 return QString(m_startText + QStringLiteral("%1%2%3").arg(QChar('a' + (index.row() % 3))).arg(QChar('a' + index.row())).arg(index.row()));
0110 
0111             default:
0112                 return QLatin1String("");
0113             }
0114         }
0115         break;
0116 
0117     case Qt::DecorationRole:
0118         break;
0119 
0120     case CompletionRole: {
0121         CompletionProperties p;
0122         if (index.row() < rowCount() / 2) {
0123             p |= Function;
0124         } else {
0125             p |= Variable;
0126         }
0127         switch (index.row() % 3) {
0128         case 0:
0129             p |= Const | Public;
0130             break;
0131         case 1:
0132             p |= Protected;
0133             break;
0134         case 2:
0135             p |= Private;
0136             break;
0137         }
0138         return (int)p;
0139     }
0140 
0141     case ScopeIndex:
0142         return (index.row() % 4) - 1;
0143     }
0144 
0145     return QVariant();
0146 }
0147 
0148 KTextEditor::View *CodeCompletionTestModel::view() const
0149 {
0150     return static_cast<KTextEditor::View *>(const_cast<QObject *>(QObject::parent()));
0151 }
0152 
0153 void CodeCompletionTestModel::completionInvoked(KTextEditor::View *view, const KTextEditor::Range &range, InvocationType invocationType)
0154 {
0155     Q_UNUSED(invocationType)
0156 
0157     if (m_autoStartText) {
0158         m_startText = view->document()->text(KTextEditor::Range(range.start(), view->cursorPosition()));
0159     }
0160     qDebug() << m_startText;
0161 }
0162 
0163 AbbreviationCodeCompletionTestModel::AbbreviationCodeCompletionTestModel(KTextEditor::View *parent, const QString &startText)
0164     : CodeCompletionTestModel(parent, startText)
0165 {
0166     m_items << QStringLiteral("SomeCoolAbbreviation") << QStringLiteral("someCoolAbbreviation") << QStringLiteral("sca") << QStringLiteral("SCA");
0167     m_items << QStringLiteral("some_cool_abbreviation") << QStringLiteral("Some_Cool_Abbreviation");
0168     m_items << QStringLiteral("thisContainsSomeWord") << QStringLiteral("this_contains_some_word") << QStringLiteral("thiscontainssomeword");
0169     m_items << QStringLiteral("notmatchedbecausemissingcaps") << QStringLiteral("not_m_atch_ed_because_underscores");
0170     setRowCount(m_items.size());
0171 }
0172 
0173 QVariant AbbreviationCodeCompletionTestModel::data(const QModelIndex &index, int role) const
0174 {
0175     if (index.column() == Name && role == Qt::DisplayRole) {
0176         return m_items[index.row()];
0177     }
0178     return QVariant();
0179 }
0180 
0181 AsyncCodeCompletionTestModel::AsyncCodeCompletionTestModel(KTextEditor::View *parent, const QString &startText)
0182     : CodeCompletionTestModel(parent, startText)
0183 {
0184     setRowCount(0);
0185 }
0186 
0187 QVariant AsyncCodeCompletionTestModel::data(const QModelIndex &index, int role) const
0188 {
0189     if (index.column() == Name && role == Qt::DisplayRole) {
0190         return m_items[index.row()];
0191     }
0192     return QVariant();
0193 }
0194 
0195 void AsyncCodeCompletionTestModel::setItems(const QStringList &items)
0196 {
0197     beginResetModel();
0198     m_items = items;
0199     setRowCount(m_items.size());
0200     endResetModel();
0201 }
0202 
0203 void AsyncCodeCompletionTestModel::completionInvoked(KTextEditor::View *view, const KTextEditor::Range &range, InvocationType invocationType)
0204 {
0205     Q_UNUSED(invocationType)
0206 
0207     Q_EMIT waitForReset();
0208     CodeCompletionTestModel::completionInvoked(view, range, invocationType);
0209 }
0210 
0211 #include "moc_codecompletiontestmodel.cpp"