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

0001 /*
0002     SPDX-FileCopyrightText: 2005-2006 Hamish Rodda <rodda@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "codecompletionmodel.h"
0008 
0009 #include "document.h"
0010 #include "view.h"
0011 
0012 using namespace KTextEditor;
0013 
0014 class KTextEditor::CodeCompletionModelPrivate
0015 {
0016 public:
0017     CodeCompletionModelPrivate()
0018     {
0019     }
0020 
0021     int rowCount = 0;
0022     bool hasGroups = false;
0023 };
0024 
0025 CodeCompletionModel::CodeCompletionModel(QObject *parent)
0026     : QAbstractItemModel(parent)
0027     , d(new CodeCompletionModelPrivate)
0028 {
0029 }
0030 
0031 CodeCompletionModel::~CodeCompletionModel()
0032 {
0033     delete d;
0034 }
0035 
0036 int CodeCompletionModel::columnCount(const QModelIndex &) const
0037 {
0038     return ColumnCount;
0039 }
0040 
0041 QModelIndex CodeCompletionModel::index(int row, int column, const QModelIndex &parent) const
0042 {
0043     if (row < 0 || row >= d->rowCount || column < 0 || column >= ColumnCount || parent.isValid()) {
0044         return QModelIndex();
0045     }
0046 
0047     return createIndex(row, column, (void *)nullptr);
0048 }
0049 
0050 QMap<int, QVariant> CodeCompletionModel::itemData(const QModelIndex &index) const
0051 {
0052     QMap<int, QVariant> ret = QAbstractItemModel::itemData(index);
0053 
0054     for (int i = CompletionRole; i <= AccessibilityAccept; ++i) {
0055         QVariant v = data(index, i);
0056         if (v.isValid()) {
0057             ret.insert(i, v);
0058         }
0059     }
0060 
0061     return ret;
0062 }
0063 
0064 QModelIndex CodeCompletionModel::parent(const QModelIndex &) const
0065 {
0066     return QModelIndex();
0067 }
0068 
0069 void CodeCompletionModel::setRowCount(int rowCount)
0070 {
0071     d->rowCount = rowCount;
0072 }
0073 
0074 int CodeCompletionModel::rowCount(const QModelIndex &parent) const
0075 {
0076     if (parent.isValid()) {
0077         return 0;
0078     }
0079 
0080     return d->rowCount;
0081 }
0082 
0083 void CodeCompletionModel::completionInvoked(KTextEditor::View *view, const Range &range, InvocationType invocationType)
0084 {
0085     Q_UNUSED(view)
0086     Q_UNUSED(range)
0087     Q_UNUSED(invocationType)
0088 }
0089 
0090 void CodeCompletionModel::executeCompletionItem(KTextEditor::View *view, const Range &word, const QModelIndex &index) const
0091 {
0092     view->document()->replaceText(word, data(index.sibling(index.row(), Name)).toString());
0093 }
0094 
0095 bool CodeCompletionModel::hasGroups() const
0096 {
0097     return d->hasGroups;
0098 }
0099 
0100 void CodeCompletionModel::setHasGroups(bool hasGroups)
0101 {
0102     if (d->hasGroups != hasGroups) {
0103         d->hasGroups = hasGroups;
0104         Q_EMIT hasGroupsChanged(this, hasGroups);
0105     }
0106 }