Commit e9b9280f authored by Alexander Neundorf's avatar Alexander Neundorf Committed by Kåre Särs
Browse files

Search&Replace: update info line correctly depending on current filter

The first line of the results shows how many matches were found
and how many of them ae checked. With this patch, these numbers are now
updated to show only the matches items which are actually visible,
i.e. which match the current filter.
parent 5c6eaca4
......@@ -484,6 +484,29 @@ void MatchModel::cancelReplace()
m_cancelReplace = true;
}
void MatchModel::setFilterText(const QString &text)
{
m_filterText = text;
}
bool MatchModel::matchesFilter(const QModelIndex &index)
{
bool matches = true;
if (!m_filterText.isEmpty()) {
const QString text = index.data(MatchModel::PlainTextRole).toString();
matches = text.contains(m_filterText, Qt::CaseInsensitive);
}
int fileRow = index.internalId() == InfoItemId ? -1 : index.internalId() == FileItemId ? index.row() : (int)index.internalId();
int matchRow = index.internalId() == InfoItemId || index.internalId() == FileItemId ? -1 : index.row();
if ((fileRow >= 0) && (fileRow < m_matchFiles.size()) && (matchRow >= 0) && (matchRow < m_matchFiles[fileRow].matches.size())) {
m_matchFiles[fileRow].matches[matchRow].matchesFilter = matches;
}
return matches;
}
static QString nbsFormated(int number, int width)
{
QString str = QString::number(number);
......@@ -505,10 +528,14 @@ QString MatchModel::infoHtmlString() const
int matchesTotal = 0;
int checkedTotal = 0;
for (const auto &matchFile : qAsConst(m_matchFiles)) {
matchesTotal += matchFile.matches.size();
checkedTotal += std::count_if(matchFile.matches.begin(), matchFile.matches.end(), [](const KateSearchMatch &match) {
return match.checked;
});
for (const auto &match : qAsConst(matchFile.matches)) {
if (match.matchesFilter) {
matchesTotal++;
if (match.checked) {
checkedTotal++;
}
}
}
}
if (m_searchState == Preparing) {
......
......@@ -33,6 +33,7 @@ public:
QString replaceText;
KTextEditor::Range range;
bool checked;
bool matchesFilter;
};
class MatchModel : public QAbstractItemModel
......@@ -178,6 +179,9 @@ public:
/** This function is used to replace a single match */
bool replaceSingleMatch(KTextEditor::Document *doc, const QModelIndex &matchIndex, const QRegularExpression &regExp, const QString &replaceString);
void setFilterText(const QString &text);
bool matchesFilter(const QModelIndex &index);
// Model-View model functions
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
......@@ -227,6 +231,7 @@ private:
QUrl m_lastMatchUrl;
QString m_lastSearchPath;
QTimer m_infoUpdateTimer;
QString m_filterText;
// Replacing related objects
KTextEditor::Application *m_docManager = nullptr;
......
......@@ -6,6 +6,14 @@
#include "MatchProxyModel.h"
#include "MatchModel.h"
void MatchProxyModel::setFilterText(const QString &text)
{
beginResetModel();
auto *matchModel = dynamic_cast<MatchModel *>(sourceModel());
matchModel->setFilterText(text);
endResetModel();
}
bool MatchProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &parent) const
{
// root item always visible
......@@ -13,20 +21,16 @@ bool MatchProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &parent)
return true;
}
// nothing to filter
if (m_text.isEmpty()) {
return true;
}
const auto index = sourceModel()->index(sourceRow, 0, parent);
if (!index.isValid()) {
return false;
}
const QString text = index.data(MatchModel::PlainTextRole).toString();
// match text;
if (text.contains(m_text, Qt::CaseInsensitive)) {
auto *matchModel = dynamic_cast<MatchModel *>(sourceModel());
bool matches = matchModel->matchesFilter(index);
if (matches) {
return true;
}
......
......@@ -16,19 +16,12 @@ public:
bool filterAcceptsRow(int sourceRow, const QModelIndex &parent) const override;
Q_SLOT void setFilterText(const QString &text)
{
beginResetModel();
m_text = text;
endResetModel();
}
Q_SLOT void setFilterText(const QString &text);
static bool isMatchItem(const QModelIndex &index);
private:
bool parentAcceptsRow(const QModelIndex &source_parent) const;
QString m_text;
};
#endif
......@@ -103,6 +103,7 @@ QVector<KateSearchMatch> SearchDiskFiles::searchSingleLineRegExp(QFile &file)
postContext,
QString(),
KTextEditor::Range{currentLineNumber, column, currentLineNumber, column + match.capturedLength()},
true,
true});
// advance match column
......@@ -185,7 +186,7 @@ QVector<KateSearchMatch> SearchDiskFiles::searchMultiLineRegExp(QFile &file)
QString postContext = fullDoc.mid(column + match.captured().length(), MatchModel::PostContextLen);
matches.push_back(
KateSearchMatch{preContext, match.captured(), postContext, QString(), KTextEditor::Range{line, startColumn, endLine, endColumn}, true});
KateSearchMatch{preContext, match.captured(), postContext, QString(), KTextEditor::Range{line, startColumn, endLine, endColumn}, true, true});
match = tmpRegExp.match(fullDoc, column + match.capturedLength());
column = match.capturedStart();
......
......@@ -123,6 +123,7 @@ int SearchOpenFiles::searchSingleLineRegExp(KTextEditor::Document *doc, const QR
postContext,
QString(),
KTextEditor::Range{line, column, line, column + match.capturedLength()},
true,
true});
match = regExp.match(doc->line(line), column + match.capturedLength());
column = match.capturedStart();
......@@ -201,7 +202,7 @@ int SearchOpenFiles::searchMultiLineRegExp(KTextEditor::Document *doc, const QRe
QString postContext = doc->line(endLine).mid(endColumn, MatchModel::PostContextLen);
matches.push_back(
KateSearchMatch{preContext, match.captured(), postContext, QString(), KTextEditor::Range{startLine, startColumn, endLine, endColumn}, true});
KateSearchMatch{preContext, match.captured(), postContext, QString(), KTextEditor::Range{startLine, startColumn, endLine, endColumn}, true, true});
match = tmpRegExp.match(m_fullDoc, column + match.capturedLength());
column = match.capturedStart();
......
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