From 7b1223b03dc0a12c871fea53017a6257bb443c2c Mon Sep 17 00:00:00 2001 From: Oleg Shparber Date: Wed, 26 Apr 2017 00:34:26 -0400 Subject: [PATCH] core,registry,ui: Make fuzzy search optional --- src/libs/core/application.cpp | 1 + src/libs/core/settings.cpp | 9 +++++++ src/libs/core/settings.h | 3 +++ src/libs/registry/docset.cpp | 40 ++++++++++++++++++++++------ src/libs/registry/docset.h | 4 +++ src/libs/registry/docsetregistry.cpp | 24 +++++++++++++++-- src/libs/registry/docsetregistry.h | 4 +++ src/libs/ui/settingsdialog.cpp | 6 +++++ src/libs/ui/settingsdialog.ui | 36 +++++++++++++++++++++++++ 9 files changed, 117 insertions(+), 10 deletions(-) diff --git a/src/libs/core/application.cpp b/src/libs/core/application.cpp index f7cb5aa..4083599 100644 --- a/src/libs/core/application.cpp +++ b/src/libs/core/application.cpp @@ -192,6 +192,7 @@ void Application::checkForUpdates(bool quiet) void Application::applySettings() { m_docsetRegistry->setStoragePath(m_settings->docsetPath); + m_docsetRegistry->setFuzzySearchEnabled(m_settings->fuzzySearchEnabled); // HTTP Proxy Settings switch (m_settings->proxyType) { diff --git a/src/libs/core/settings.cpp b/src/libs/core/settings.cpp index 303e7f5..360d86b 100644 --- a/src/libs/core/settings.cpp +++ b/src/libs/core/settings.cpp @@ -35,6 +35,7 @@ namespace { const char GroupContent[] = "content"; const char GroupDocsets[] = "docsets"; const char GroupGlobalShortcuts[] = "global_shortcuts"; +const char GroupSearch[] = "search"; const char GroupTabs[] = "tabs"; const char GroupInternal[] = "internal"; const char GroupState[] = "state"; @@ -75,6 +76,10 @@ void Settings::load() openNewTabAfterActive = settings->value(QStringLiteral("open_new_tab_after_active"), false).toBool(); settings->endGroup(); + settings->beginGroup(GroupSearch); + fuzzySearchEnabled = settings->value(QStringLiteral("fuzzy_search_enabled"), false).toBool(); + settings->endGroup(); + settings->beginGroup(GroupContent); minimumFontSize = settings->value(QStringLiteral("minimum_font_size"), QWebSettings::globalSettings()->fontSize(QWebSettings::MinimumFontSize)).toInt(); @@ -142,6 +147,10 @@ void Settings::save() settings->setValue(QStringLiteral("open_new_tab_after_active"), openNewTabAfterActive); settings->endGroup(); + settings->beginGroup(GroupSearch); + settings->setValue(QStringLiteral("fuzzy_search_enabled"), fuzzySearchEnabled); + settings->endGroup(); + settings->beginGroup(GroupContent); settings->setValue(QStringLiteral("minimum_font_size"), minimumFontSize); settings->setValue(QStringLiteral("dark_mode"), darkModeEnabled); diff --git a/src/libs/core/settings.h b/src/libs/core/settings.h index 4d0ebbe..088cc13 100644 --- a/src/libs/core/settings.h +++ b/src/libs/core/settings.h @@ -56,6 +56,9 @@ public: // Tabs Behavior bool openNewTabAfterActive; + // Search + bool fuzzySearchEnabled; + // Content int minimumFontSize; bool darkModeEnabled; diff --git a/src/libs/registry/docset.cpp b/src/libs/registry/docset.cpp index cd67dd9..f6c78cb 100644 --- a/src/libs/registry/docset.cpp +++ b/src/libs/registry/docset.cpp @@ -260,15 +260,29 @@ QList Docset::search(const QString &query, const CancellationToken { QString sql; if (m_type == Docset::Type::Dash) { - sql = QStringLiteral("SELECT name, type, path, '', zealScore('%1', name) as score" - " FROM searchIndex" - " WHERE score > 0" - " ORDER BY score DESC"); + if (m_fuzzySearchEnabled) { + sql = QStringLiteral("SELECT name, type, path, '', zealScore('%1', name) as score" + " FROM searchIndex" + " WHERE score > 0" + " ORDER BY score DESC"); + } else { + sql = QStringLiteral("SELECT name, type, path, ''" + " FROM searchIndex" + " WHERE (name LIKE '%%1%' ESCAPE '\\')" + " ORDER BY name COLLATE NOCASE"); + } } else { - sql = QStringLiteral("SELECT name, type, path, fragment, zealScore('%1', name) as score" - " FROM searchIndex" - " WHERE score > 0" - " ORDER BY score DESC"); + if (m_fuzzySearchEnabled) { + sql = QStringLiteral("SELECT name, type, path, fragment, zealScore('%1', name) as score" + " FROM searchIndex" + " WHERE score > 0" + " ORDER BY score DESC"); + } else { + sql = QStringLiteral("SELECT name, type, path, fragment" + " FROM searchIndex" + " WHERE (name LIKE '%%1%' ESCAPE '\\')" + " ORDER BY name COLLATE NOCASE"); + } } // Limit for very short queries. @@ -643,6 +657,16 @@ QString Docset::parseSymbolType(const QString &str) return aliases.value(str, str); } +bool Docset::isFuzzySearchEnabled() const +{ + return m_fuzzySearchEnabled; +} + +void Docset::setFuzzySearchEnabled(bool enabled) +{ + m_fuzzySearchEnabled = enabled; +} + /** * \brief Returns score based on a substring position in a string. * \param str Original string. diff --git a/src/libs/registry/docset.h b/src/libs/registry/docset.h index 5493012..e599a64 100644 --- a/src/libs/registry/docset.h +++ b/src/libs/registry/docset.h @@ -75,6 +75,9 @@ public: // FIXME: This is an ugly workaround before we have a proper docset sources implementation bool hasUpdate = false; + bool isFuzzySearchEnabled() const; + void setFuzzySearchEnabled(bool enabled); + private: enum class Type { Invalid, @@ -107,6 +110,7 @@ private: QMap m_symbolCounts; mutable QMap> m_symbols; Util::SQLiteDatabase *m_db = nullptr; + bool m_fuzzySearchEnabled = false; }; } // namespace Registry diff --git a/src/libs/registry/docsetregistry.cpp b/src/libs/registry/docsetregistry.cpp index e653dfe..b61617a 100644 --- a/src/libs/registry/docsetregistry.cpp +++ b/src/libs/registry/docsetregistry.cpp @@ -80,6 +80,24 @@ void DocsetRegistry::setStoragePath(const QString &path) addDocsetsFromFolder(path); } +bool DocsetRegistry::isFuzzySearchEnabled() const +{ + return m_fuzzySearchEnabled; +} + +void DocsetRegistry::setFuzzySearchEnabled(bool enabled) +{ + if (enabled == m_fuzzySearchEnabled) { + return; + } + + m_fuzzySearchEnabled = enabled; + + for (Docset *docset : m_docsets) { + docset->setFuzzySearchEnabled(enabled); + } +} + int DocsetRegistry::count() const { return m_docsets.count(); @@ -134,10 +152,12 @@ void DocsetRegistry::addDocset(const QString &path) return; } - const QString name = docset->name(); + docset->setFuzzySearchEnabled(m_fuzzySearchEnabled); - if (m_docsets.contains(name)) + const QString name = docset->name(); + if (m_docsets.contains(name)) { remove(name); + } m_docsets[name] = docset; emit docsetAdded(name); diff --git a/src/libs/registry/docsetregistry.h b/src/libs/registry/docsetregistry.h index 76dec8e..462d7cc 100644 --- a/src/libs/registry/docsetregistry.h +++ b/src/libs/registry/docsetregistry.h @@ -47,6 +47,9 @@ public: QString storagePath() const; void setStoragePath(const QString &path); + bool isFuzzySearchEnabled() const; + void setFuzzySearchEnabled(bool enabled); + int count() const; bool contains(const QString &name) const; QStringList names() const; @@ -75,6 +78,7 @@ private: void addDocsetsFromFolder(const QString &path); QString m_storagePath; + bool m_fuzzySearchEnabled = false; QThread *m_thread = nullptr; QMap m_docsets; diff --git a/src/libs/ui/settingsdialog.cpp b/src/libs/ui/settingsdialog.cpp index a109450..002b697 100644 --- a/src/libs/ui/settingsdialog.cpp +++ b/src/libs/ui/settingsdialog.cpp @@ -102,6 +102,9 @@ void SettingsDialog::loadSettings() // Tabs Tab ui->openNewTabAfterActive->setChecked(settings->openNewTabAfterActive); + // Search Tab + ui->fuzzySearchCheckBox->setChecked(settings->fuzzySearchEnabled); + // Content Tab ui->minimumFontSizeSpinBox->setValue(settings->minimumFontSize); ui->darkModeCheckBox->setChecked(settings->darkModeEnabled); @@ -147,6 +150,9 @@ void SettingsDialog::saveSettings() // Tabs Tab settings->openNewTabAfterActive = ui->openNewTabAfterActive->isChecked(); + // Search Tab + settings->fuzzySearchEnabled = ui->fuzzySearchCheckBox->isChecked(); + // Content Tab settings->minimumFontSize = ui->minimumFontSizeSpinBox->text().toInt(); settings->darkModeEnabled = ui->darkModeCheckBox->isChecked(); diff --git a/src/libs/ui/settingsdialog.ui b/src/libs/ui/settingsdialog.ui index c81fc59..dadbe24 100644 --- a/src/libs/ui/settingsdialog.ui +++ b/src/libs/ui/settingsdialog.ui @@ -186,6 +186,42 @@ + + + Search + + + + + + Local search + + + + + + Use fuzzy search (experimental) + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + Content