File indexing completed on 2024-04-28 11:44:44

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