diff options
author | Nicolas Werner <nicolas.werner@hotmail.de> | 2022-01-31 21:36:22 +0100 |
---|---|---|
committer | Nicolas Werner <nicolas.werner@hotmail.de> | 2022-01-31 21:36:22 +0100 |
commit | eae43782a378f93ee1b3495ee761612b4bfc001e (patch) | |
tree | 17289e4cc6b4ad6746c3ce83a1b8f3695ab773f5 /src/CompletionProxyModel.cpp | |
parent | Remove unused funtion (diff) | |
download | nheko-eae43782a378f93ee1b3495ee761612b4bfc001e.tar.xz |
Do word splitting in completer
Diffstat (limited to '')
-rw-r--r-- | src/CompletionProxyModel.cpp | 47 |
1 files changed, 26 insertions, 21 deletions
diff --git a/src/CompletionProxyModel.cpp b/src/CompletionProxyModel.cpp index fdc6fd57..b7c2ca79 100644 --- a/src/CompletionProxyModel.cpp +++ b/src/CompletionProxyModel.cpp @@ -6,6 +6,7 @@ #include "CompletionProxyModel.h" #include <QRegularExpression> +#include <QTextBoundaryFinder> #include "CompletionModelRoles.h" #include "Logging.h" @@ -44,27 +45,31 @@ CompletionProxyModel::CompletionProxyModel(QAbstractItemModel *model, // insert the partial matches for (int i = 0; i < sourceModel()->rowCount(); i++) { - auto string1 = sourceModel() - ->data(sourceModel()->index(i, 0), CompletionModel::SearchRole) - .toString() - .toLower(); - - auto split1 = QStringView(string1).split(splitPoints, Qt::SkipEmptyParts); - for (const auto &e : qAsConst(split1)) { - trie_.insert(e.toUcs4(), i); - } - - auto string2 = sourceModel() - ->data(sourceModel()->index(i, 0), CompletionModel::SearchRole2) - .toString() - .toLower(); - - if (!string2.isEmpty()) { - auto split2 = QStringView(string2).split(splitPoints, Qt::SkipEmptyParts); - for (const auto &e : qAsConst(split2)) { - trie_.insert(e.toUcs4(), i); - } - } + auto insertParts = [i, this](const QString &str) { + if (str.isEmpty()) + return; + + QTextBoundaryFinder finder(QTextBoundaryFinder::BoundaryType::Word, str); + finder.toStart(); + do { + auto start = finder.position(); + finder.toNextBoundary(); + auto end = finder.position(); + + auto ref = str.midRef(start, end - start).trimmed(); + if (!ref.isEmpty()) + trie_.insert(ref.toUcs4(), i); + } while (finder.position() < str.size()); + }; + + insertParts(sourceModel() + ->data(sourceModel()->index(i, 0), CompletionModel::SearchRole) + .toString() + .toLower()); + insertParts(sourceModel() + ->data(sourceModel()->index(i, 0), CompletionModel::SearchRole2) + .toString() + .toLower()); } connect( |