Debian Codegraph @

src/CompletionProxyModel.h

raw symbols SCIP
1 // SPDX-FileCopyrightText: Nheko Contributors
2 //
3 // SPDX-License-Identifier: GPL-3.0-or-later
4
5 #pragma once
6
7 // Class for showing a limited amount of completions at a time
8
9 #include <QAbstractProxyModel>
10
11 #include <algorithm>
12 #include <span>
13
14 enum class ElementRank
15 {
16 first,
17 second
18 };
19
20 template<typename Key, typename Value>
21 struct trie
22 {
23 std::vector<std::pair<ElementRank, Value>> values;
24 std::map<Key, trie> next;
25
26 template<ElementRank r>
27 void insert(const QVector<Key> &keys, const Value &v)
28 {
29 auto t = this;
30 for (const auto k : keys) {
31 t = &t->next[k];
32 }
33
34 if constexpr (r == ElementRank::first) {
35 auto it =
36 std::ranges::upper_bound(t->values, r, {}, &std::pair<ElementRank, Value>::first);
37 t->values.emplace(it, r, v);
38 } else if constexpr (r == ElementRank::second) {
39 t->values.emplace_back(r, v);
40 }
41 }
42
43 std::vector<Value> valuesAndSubvalues(size_t limit = -1) const
44 {
45 std::vector<Value> ret;
46 if (limit < 200)
47 ret.reserve(limit);
48
49 for (const auto &v : values) {
50 if (ret.size() >= limit)
51 return ret;
52 else
53 ret.push_back(v.second);
54 }
55
56 for (const auto &[k, t] : next) {
57 (void)k;
58 if (ret.size() >= limit)
59 return ret;
60 else {
61 auto temp = t.valuesAndSubvalues(limit - ret.size());
62 for (auto &&v : temp) {
63 if (ret.size() >= limit)
64 return ret;
65
66 if (std::find(ret.begin(), ret.end(), v) == ret.end()) {
67 ret.push_back(std::move(v));
68 }
69 }
70 }
71 }
72
73 return ret;
74 }
75
76 std::vector<Value> search(const std::span<Key> &keys,
77 size_t result_count_limit,
78 size_t max_edit_distance_ = 2) const
79 {
80 std::vector<Value> ret;
81 if (!result_count_limit)
82 return ret;
83
84 if (keys.empty())
85 return valuesAndSubvalues(result_count_limit);
86
87 auto append = [&ret, result_count_limit](std::vector<Value> &&in) {
88 for (auto &&v : in) {
89 if (ret.size() >= result_count_limit)
90 return;
91
92 if (std::find(ret.begin(), ret.end(), v) == ret.end()) {
93 ret.push_back(std::move(v));
94 }
95 }
96 };
97
98 auto limit = [&ret, result_count_limit] {
99 return std::min(result_count_limit, (result_count_limit - ret.size()) * 2);
100 };
101
102 // Try first exact matches, then with maximum errors
103 for (size_t max_edit_distance = 0;
104 max_edit_distance <= max_edit_distance_ && ret.size() < result_count_limit;
105 max_edit_distance += 1) {
106 if (max_edit_distance && ret.size() < result_count_limit) {
107 max_edit_distance -= 1;
108
109 // swap chars case
110 if (keys.size() >= 2) {
111 auto t = this;
112 for (int i = 1; i >= 0; i--) {
113 if (auto e = t->next.find(keys[i]); e != t->next.end()) {
114 t = &e->second;
115 } else {
116 t = nullptr;
117 break;
118 }
119 }
120
121 if (t) {
122 append(t->search(keys.subspan(2), limit(), max_edit_distance));
123 }
124 }
125
126 // insert case
127 for (const auto &[k, t] : this->next) {
128 if (k == keys[0])
129 continue;
130 if (ret.size() >= limit())
131 break;
132
133 // insert
134 append(t.search(keys, limit(), max_edit_distance));
135 }
136
137 // delete character case
138 append(this->search(keys.subspan(1), limit(), max_edit_distance));
139
140 // substitute case
141 for (const auto &[k, t] : this->next) {
142 if (k == keys[0])
143 continue;
144 if (ret.size() >= limit())
145 break;
146
147 // substitute
148 append(t.search(keys.subspan(1), limit(), max_edit_distance));
149 }
150
151 max_edit_distance += 1;
152 }
153
154 if (auto e = this->next.find(keys[0]); e != this->next.end()) {
155 append(e->second.search(keys.subspan(1), limit(), max_edit_distance));
156 }
157 }
158
159 return ret;
160 }
161 };
162
163 class CompletionProxyModel final : public QAbstractProxyModel
164 {
165 Q_OBJECT
166 Q_PROPERTY(QString searchString READ searchString WRITE setSearchString NOTIFY newSearchString)
167 public:
168 CompletionProxyModel(QAbstractItemModel *model,
169 int max_mistakes = 2,
170 size_t max_completions = 30,
171 QObject *parent = nullptr);
172
173 void invalidate();
174
175 QHash<int, QByteArray> roleNames() const override;
176 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
177 int columnCount(const QModelIndex &) const override;
178
179 QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override;
180 QModelIndex mapToSource(const QModelIndex &proxyIndex) const override;
181
182 QModelIndex
183 index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
184 QModelIndex parent(const QModelIndex &) const override;
185
186 public slots:
187 QVariant completionAt(int i) const;
188
189 void setSearchString(const QString &s);
190 QString searchString() const { return searchString_; }
191
192 signals:
193 void newSearchString(QString);
194
195 private:
196 QString searchString_;
197 trie<uint, int> trie_;
198 std::vector<int> mapping;
199 int maxMistakes_;
200 size_t max_completions_;
201 };