Warning, file /sdk/lokalize/src/completionstorage.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002   This file is part of Lokalize
0003 
0004   SPDX-FileCopyrightText: 2009-2014 Nick Shaforostoff <shafff@ukr.net>
0005   SPDX-FileCopyrightText: 2018-2019 Simon Depiets <sdepiets@gmail.com>
0006 
0007   SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0008 */
0009 
0010 #include "completionstorage.h"
0011 
0012 #include "lokalize_debug.h"
0013 
0014 #include "project.h"
0015 #include "prefs_lokalize.h"
0016 #include <QCoreApplication>
0017 #include <QElapsedTimer>
0018 
0019 CompletionStorage* CompletionStorage::_instance = nullptr;
0020 void CompletionStorage::cleanupCompletionStorage()
0021 {
0022     delete CompletionStorage::_instance; CompletionStorage::_instance = nullptr;
0023 }
0024 
0025 CompletionStorage* CompletionStorage::instance()
0026 {
0027     if (_instance == nullptr) {
0028         _instance = new CompletionStorage();
0029         qAddPostRoutine(CompletionStorage::cleanupCompletionStorage);
0030     }
0031     return _instance;
0032 }
0033 
0034 
0035 void CompletionStorage::scanCatalog(Catalog* catalog)
0036 {
0037     if (!catalog->numberOfEntries()) return;
0038     QElapsedTimer a; a.start();
0039 
0040     int wordCompletionLength = Settings::self()->wordCompletionLength();
0041     /* we can't skip the scanning because there might be explicit completion triggered
0042     if (wordCompletionLength<3 || !catalog->numberOfEntries())
0043         return;
0044     */
0045     wordCompletionLength += 3; //only long words
0046 
0047     QString accel = Project::instance()->accel();
0048 
0049     DocPosition pos(0);
0050     do {
0051         QString string = catalog->targetWithTags(pos).string;
0052         string.remove(accel);
0053 
0054         const QStringList words = string.toLower().split(rxSplit, Qt::SkipEmptyParts);
0055         for (const QString& word : words) {
0056             if (word.length() < wordCompletionLength)
0057                 continue;
0058             m_words[word]++;
0059         }
0060     } while (switchNext(catalog, pos));
0061 
0062     qCWarning(LOKALIZE_LOG) << "indexed" << catalog->url() << "for word completion in" << a.elapsed() << "msecs";
0063 }
0064 
0065 QStringList CompletionStorage::makeCompletion(const QString& word) const
0066 {
0067     //QTime a;a.start();
0068     if (word.isEmpty())
0069         return QStringList();
0070     QMultiMap<int, QString> hits; //we use the fact that qmap sorts it's items by keys
0071     QString cleanWord = word.toLower();
0072     QMap<QString, int>::const_iterator it = m_words.lowerBound(cleanWord);
0073     while (it != m_words.constEnd() && it.key().startsWith(cleanWord)) {
0074         hits.insert(-it.value(), it.key().mid(word.length()));
0075         ++it;
0076     }
0077     //qCDebug(LOKALIZE_LOG)<<"hits generated in"<<a.elapsed()<<"msecs";
0078     return hits.values();
0079 }
0080