Commit 47cff294 authored by Waqar Ahmed's avatar Waqar Ahmed Committed by Christoph Cullmann
Browse files

Use fuzzy filter in kate lspclientplugin symbol view

parent 515f7b3f
......@@ -45,6 +45,12 @@ target_sources(
${UI_SOURCES}
)
target_include_directories(
lspclientplugin
PUBLIC
${CMAKE_SOURCE_DIR}/shared
)
kcoreaddons_desktop_to_json(lspclientplugin lspclientplugin.desktop)
install(TARGETS lspclientplugin DESTINATION ${PLUGIN_INSTALL_DIR}/ktexteditor)
......
......@@ -26,6 +26,8 @@
#include <memory>
#include <utility>
#include <kfts_fuzzy_match.h>
class LSPClientViewTrackerImpl : public LSPClientViewTracker
{
Q_OBJECT
......@@ -103,6 +105,46 @@ LSPClientViewTracker *LSPClientViewTracker::new_(LSPClientPlugin *plugin, KTextE
return new LSPClientViewTrackerImpl(plugin, mainWin, change_ms, motion_ms);
}
class LSPClientSymbolViewFilterProxyModel : public QSortFilterProxyModel
{
public:
LSPClientSymbolViewFilterProxyModel(QObject *parent = nullptr)
: QSortFilterProxyModel(parent)
{
}
void setFilterString(const QString &string)
{
m_pattern = string;
invalidateFilter();
}
protected:
bool lessThan(const QModelIndex &sourceLeft, const QModelIndex &sourceRight) const override
{
const int l = sourceLeft.data(WeightRole).toInt();
const int r = sourceRight.data(WeightRole).toInt();
return l < r;
}
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override
{
if (m_pattern.isEmpty())
return true;
int score = 0;
const auto idx = sourceModel()->index(sourceRow, 0, sourceParent);
const QString symbol = idx.data().toString();
const bool res = kfts::fuzzy_match(m_pattern, symbol, score);
sourceModel()->setData(idx, score, WeightRole);
return res;
}
private:
QString m_pattern;
static constexpr int WeightRole = Qt::UserRole + 1;
};
/*
* Instantiates and manages the symbol outline toolview.
*/
......@@ -143,7 +185,7 @@ class LSPClientSymbolViewImpl : public QObject, public LSPClientSymbolView
// last outline model we constructed
std::shared_ptr<QStandardItemModel> m_outline;
// filter model, setup once
QSortFilterProxyModel m_filterModel;
LSPClientSymbolViewFilterProxyModel m_filterModel;
// cached icons for model
const QIcon m_icon_pkg = QIcon::fromTheme(QStringLiteral("code-block"));
......@@ -347,7 +389,7 @@ public:
newModel->invisibleRootItem()->setData(details);
// fixup headers
QStringList headers {i18n("Symbols")};
QStringList headers{i18n("Symbols")};
newModel->setHorizontalHeaderLabels(headers);
setModel(newModel);
......@@ -527,7 +569,7 @@ private Q_SLOTS:
/**
* filter
*/
m_filterModel.setFilterFixedString(filterText);
m_filterModel.setFilterString(filterText);
/**
* expand
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment