KTextEditor

kateargumenthintmodel.cpp
1/*
2 SPDX-FileCopyrightText: 2007 David Nolden <david.nolden.kdevelop@art-master.de>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "kateargumenthintmodel.h"
8#include "katecompletionmodel.h"
9#include "katepartdebug.h"
10#include <ktexteditor/codecompletionmodel.h>
11
12#include <QTextFormat>
13
14using namespace KTextEditor;
15
16void KateArgumentHintModel::clear()
17{
18 m_rows.clear();
19}
20
21QModelIndex KateArgumentHintModel::mapToSource(const QModelIndex &index) const
22{
23 if (size_t(index.row()) >= m_rows.size()) {
24 return QModelIndex();
25 }
26
27 if (m_rows[index.row()] < 0 || m_rows[index.row()] >= (int)group()->filtered.size()) {
28 return QModelIndex();
29 }
30
31 KateCompletionModel::ModelRow source = group()->filtered[m_rows[index.row()]].sourceRow();
32 if (!source.first) {
33 qCDebug(LOG_KTE) << "KateArgumentHintModel::data: Row does not exist in source";
34 return QModelIndex();
35 }
36
37 QModelIndex sourceIndex = source.second.sibling(source.second.row(), index.column());
38
39 return sourceIndex;
40}
41
42void KateArgumentHintModel::parentModelReset()
43{
44 clear();
45 buildRows();
46}
47
48void KateArgumentHintModel::buildRows()
49{
51
52 m_rows.clear();
53 std::map<int, std::vector<int>> m_depths; // Map each hint-depth to a list of functions of that depth
54 for (int a = 0; a < (int)group()->filtered.size(); a++) {
55 KateCompletionModel::ModelRow source = group()->filtered[a].sourceRow();
56 QModelIndex sourceIndex = source.second.sibling(source.second.row(), 0);
58 if (v.userType() == QMetaType::Int) {
59 std::vector<int> &lst(m_depths[v.toInt()]);
60 lst.push_back(a);
61 }
62 }
63
64 for (const auto &[key, value] : m_depths) {
65 for (int row : value) {
66 m_rows.insert(m_rows.begin(), row); // Insert filtered in reversed order
67 }
68 }
69
71
72 Q_EMIT contentStateChanged(!m_rows.empty());
73}
74
75KateArgumentHintModel::KateArgumentHintModel(KateCompletionModel *parent)
76 : QAbstractListModel(parent)
77 , m_parent(parent)
78{
79 connect(parent, &KateCompletionModel::modelReset, this, &KateArgumentHintModel::parentModelReset);
80 connect(parent, &KateCompletionModel::argumentHintsChanged, this, &KateArgumentHintModel::parentModelReset);
81}
82
83QVariant KateArgumentHintModel::data(const QModelIndex &index, int role) const
84{
85 if (size_t(index.row()) >= m_rows.size()) {
86 // qCDebug(LOG_KTE) << "KateArgumentHintModel::data: index out of bound: " << index.row() << " total filtered: " << m_rows.count();
87 return QVariant();
88 }
89
90 if (m_rows[index.row()] < 0 || m_rows[index.row()] >= (int)group()->filtered.size()) {
91 qCDebug(LOG_KTE) << "KateArgumentHintModel::data: index out of bound: " << m_rows[index.row()] << " total filtered: " << (int)group()->filtered.size();
92 return QVariant();
93 }
94
95 KateCompletionModel::ModelRow source = group()->filtered[m_rows[index.row()]].sourceRow();
96 if (!source.first) {
97 qCDebug(LOG_KTE) << "KateArgumentHintModel::data: Row does not exist in source";
98 return QVariant();
99 }
100
101 QModelIndex sourceIndex = source.second.sibling(source.second.row(), index.column());
102
103 if (!sourceIndex.isValid()) {
104 qCDebug(LOG_KTE) << "KateArgumentHintModel::data: Source-index is not valid";
105 return QVariant();
106 }
107
108 switch (role) {
109 case Qt::DisplayRole: {
110 // Construct the text
111 QString totalText;
112 for (int a = CodeCompletionModel::Prefix; a <= CodeCompletionModel::Postfix; a++) {
113 if (a != CodeCompletionModel::Scope) { // Skip the scope
114 totalText += source.second.sibling(source.second.row(), a).data(Qt::DisplayRole).toString() + QLatin1Char(' ');
115 }
116 }
117
118 return QVariant(totalText);
119 }
121 // Return that we are doing custom-highlighting of one of the sub-strings does it
122 for (int a = CodeCompletionModel::Prefix; a <= CodeCompletionModel::Postfix; a++) {
123 QVariant method = source.second.sibling(source.second.row(), a).data(CodeCompletionModel::HighlightingMethod);
124 if (method.userType() == QMetaType::Int && method.toInt() == CodeCompletionModel::CustomHighlighting) {
125 return QVariant(CodeCompletionModel::CustomHighlighting);
126 }
127 }
128
129 return QVariant();
130 }
132 QStringList strings;
133
134 // Collect strings
135 for (int a = CodeCompletionModel::Prefix; a <= CodeCompletionModel::Postfix; a++) {
136 strings << source.second.sibling(source.second.row(), a).data(Qt::DisplayRole).toString();
137 }
138
139 QList<QVariantList> highlights;
140
141 // Collect custom-highlightings
142 for (int a = CodeCompletionModel::Prefix; a <= CodeCompletionModel::Postfix; a++) {
143 highlights << source.second.sibling(source.second.row(), a).data(CodeCompletionModel::CustomHighlight).toList();
144 }
145
146 return mergeCustomHighlighting(strings, highlights, 1);
147 }
148 }
149
150 return {};
151}
152
153int KateArgumentHintModel::rowCount(const QModelIndex &) const
154{
155 return m_rows.size();
156}
157
158KateCompletionModel::Group *KateArgumentHintModel::group() const
159{
160 return m_parent->m_argumentHints;
161}
162
163void KateArgumentHintModel::emitDataChanged(const QModelIndex &start, const QModelIndex &end)
164{
166}
167
168#include "moc_kateargumenthintmodel.cpp"
@ HighlightingMethod
Define which highlighting method will be used:
@ ArgumentHintDepth
Is this completion-item an argument-hint? The model should return an integral positive number if the ...
@ CustomHighlight
Allows an item to provide custom highlighting.
This class has the responsibility for filtering, sorting, and manipulating code completion data provi...
Q_SCRIPTABLE Q_NOREPLY void start()
The KTextEditor namespace contains all the public API that is required to use the KTextEditor compone...
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
int column() const const
QVariant data(int role) const const
bool isValid() const const
int row() const const
Q_EMITQ_EMIT
DisplayRole
QFuture< typename qValueType< Iterator >::value_type > filtered(Iterator begin, Iterator end, KeepFunctor &&filterFunction)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
int toInt(bool *ok) const const
int userType() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:15:43 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.