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

Search: Utilize context lengths more efficiently

Sometimes we have a lot of extra space available that we can use because
pre or post context length is small. In such cases, if one is smaller
increase the other.

BUG: 438094
parent 700d088e
Pipeline #121685 passed with stage
in 3 minutes and 13 seconds
......@@ -67,6 +67,34 @@ public:
typedef KateSearchMatch Match;
/// Utility function that is used to figure out how much context text we want to show
/// before and after the match
/// @p lineLength length of the line we are in
/// @return a pair, first = preContextStart i.e., the start position in line
/// second = postContextLen i.e., how much of post text we will show
static constexpr std::pair<int, int> contextLengths(int lineLength, int matchStart, int matchEnd)
{
int preContextStart = 0;
const int requiredPostLen = lineLength - matchEnd;
if (requiredPostLen < MatchModel::PostContextLen) {
int extra = MatchModel::PostContextLen - requiredPostLen;
preContextStart = qMax(0, matchStart - (MatchModel::PreContextLen + extra));
} else {
preContextStart = qMax(0, matchStart - MatchModel::PreContextLen);
}
int postContextLen = 0;
const int requiredPreLen = matchStart;
if (matchStart < MatchModel::PreContextLen) {
int extra = MatchModel::PreContextLen - requiredPreLen;
postContextLen = MatchModel::PostContextLen + extra;
} else {
postContextLen = MatchModel::PostContextLen;
}
return {preContextStart, postContextLen};
}
private:
struct MatchFile {
QUrl fileUrl;
......@@ -202,6 +230,17 @@ private:
friend class Results;
};
// tests
// output = {start, postContextLen}
// precontext starts from 0 instead of at a higher place as post context is small
static_assert(MatchModel::contextLengths(140, 110, 114) == std::pair<int, int>{0, 100});
static_assert(MatchModel::contextLengths(140, 90, 114) == std::pair<int, int>{0, 100});
// post context length increases from hundered as precontext is small here
static_assert(MatchModel::contextLengths(140, 40, 50) == std::pair<int, int>{0, 140});
static_assert(MatchModel::contextLengths(140, 20, 50) == std::pair<int, int>{0, 160});
Q_DECLARE_METATYPE(KateSearchMatch)
#endif
......@@ -95,9 +95,9 @@ QVector<KateSearchMatch> SearchDiskFiles::searchSingleLineRegExp(QFile &file)
// remember match
const int endColumn = column + match.capturedLength();
const int preContextStart = qMax(0, column - MatchModel::PreContextLen);
const auto [preContextStart, postContextLen] = MatchModel::contextLengths(line.size(), column, endColumn);
const QString preContext = line.mid(preContextStart, column - preContextStart);
const QString postContext = line.mid(endColumn, MatchModel::PostContextLen);
const QString postContext = line.mid(endColumn, postContextLen);
matches.push_back(KateSearchMatch{preContext,
match.captured(),
postContext,
......
......@@ -113,10 +113,10 @@ int SearchOpenFiles::searchSingleLineRegExp(KTextEditor::Document *doc, const QR
while (column != -1 && !match.captured().isEmpty()) {
int endColumn = column + match.capturedLength();
int preContextStart = qMax(0, column - MatchModel::PreContextLen);
const QString &lineStr = doc->line(line);
const auto [preContextStart, postContextLen] = MatchModel::contextLengths(lineStr.size(), column, endColumn);
QString preContext = lineStr.mid(preContextStart, column - preContextStart);
QString postContext = lineStr.mid(endColumn, MatchModel::PostContextLen);
QString postContext = lineStr.mid(endColumn, postContextLen);
matches.push_back(KateSearchMatch{preContext,
match.captured(),
......
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