From 8922a4777683e08dc0cccfaaff37bb693bd642a6 Mon Sep 17 00:00:00 2001
From: Nicolas Werner <nicolas.werner@hotmail.de>
Date: Tue, 24 Nov 2020 02:56:14 +0100
Subject: [PATCH] Fix completer fuzzy match not applying after prefix match and
 insert being broken

---
 src/CompletionProxyModel.h | 28 +++++++++-------------------
 1 file changed, 9 insertions(+), 19 deletions(-)

diff --git a/src/CompletionProxyModel.h b/src/CompletionProxyModel.h
index ba28e84c6..91f937a4a 100644
--- a/src/CompletionProxyModel.h
+++ b/src/CompletionProxyModel.h
@@ -57,7 +57,7 @@ struct trie
                 return ret;
         }
 
-        std::vector<Value> search(const QVector<Key> &keys,
+        std::vector<Value> search(const QVector<Key> &keys, //< TODO(Nico): replace this with a span
                                   size_t limit,
                                   size_t max_distance = 2) const
         {
@@ -65,6 +65,9 @@ struct trie
                 if (!limit)
                         return ret;
 
+                if (keys.isEmpty())
+                        return valuesAndSubvalues(limit);
+
                 auto append = [&ret, limit](std::vector<Value> &&in) {
                         for (auto &&v : in) {
                                 if (ret.size() >= limit)
@@ -76,24 +79,11 @@ struct trie
                         }
                 };
 
-                {
-                        auto t = this;
-                        int i  = 0;
-                        for (; i < (int)keys.size(); i++) {
-                                if (auto e = t->next.find(keys[i]); e != t->next.end()) {
-                                        t = &e->second;
-                                } else {
-                                        t = nullptr;
-                                        break;
-                                }
-                        }
-
-                        if (t) {
-                                ret = t->valuesAndSubvalues(limit);
-                        }
+                if (auto e = this->next.find(keys[0]); e != this->next.end()) {
+                        append(e->second.search(keys.mid(1), limit, max_distance));
                 }
 
-                if (max_distance && keys.size() < static_cast<int>(limit) && keys.size() > 1) {
+                if (max_distance && ret.size() < limit) {
                         max_distance -= 1;
 
                         // swap chars case
@@ -123,13 +113,13 @@ struct trie
                                         break;
 
                                 // substitute
-                                append(this->search(keys.mid(1), limit - ret.size(), max_distance));
+                                append(t.search(keys.mid(1), limit - ret.size(), max_distance));
 
                                 if (ret.size() >= limit)
                                         break;
 
                                 // insert
-                                append(this->search(keys, limit - ret.size(), max_distance));
+                                append(t.search(keys, limit - ret.size(), max_distance));
                         }
                 }
 
-- 
GitLab