Commit 602386d3 authored by Waqar Ahmed's avatar Waqar Ahmed
Browse files

Only match last section of pattern when matching for path

Since filenames are very unlikely to contain a '/', take the last portion
of the pattern to match filename because otherwise we just waste cpu
trying to match a pattern that will never match a path.

Also, when just matching filename, if the file path == file name,
give it extra score. Such files are files that are present in project
root dir and matching them directly becomes hard if there are many
files with the same name in the project. Consider:

CMakeLists.txt
src/CMakeLists.txt
...

On typing "cmake"

the first one will appear at top. If one really wants some other cmake
file, one can jump to that easily using path matching: s/cmake
parent c2105380
Pipeline #132818 passed with stage
in 5 minutes and 3 seconds
......@@ -72,14 +72,29 @@ protected:
return false;
}
QStringView fileNameMatchPattern = pattern;
// When matching path, we want to match the last section of the pattern
// with filenames. /path/to/file => pattern: file
if (matchPath) {
int lastSlash = pattern.lastIndexOf(QLatin1Char('/'));
if (lastSlash != -1) {
fileNameMatchPattern = fileNameMatchPattern.mid(lastSlash + 1);
}
}
const QString &name = sm->idxToFileName(sourceRow);
int score = 0;
bool res;
// dont use the QStringView(QString) ctor
bool res = filterByName(QStringView(name.data(), name.size()), QStringView(pattern.data(), pattern.size()), score);
if (fileNameMatchPattern.isEmpty()) {
res = true;
} else {
res = filterByName(QStringView(name.data(), name.size()), fileNameMatchPattern, score);
}
// only match file path if filename got a match
if (matchPath) {
// only match file path if needed
if (matchPath && res) {
int scorep = 0;
QStringView path{sm->idxToFilePath(sourceRow)};
bool resp = filterByPath(path, QStringView(pattern.data(), pattern.size()), scorep);
......@@ -90,6 +105,15 @@ protected:
if (res) {
// +1 point for opened files
score += (sm->isOpened(sourceRow));
// extra points if file exists in project root
// This gives priority to the files at the root
// of the project over others. This is important
// because otherwise getting to root files may
// not be that easy
if (!matchPath) {
score += (sm->idxToFilePath(sourceRow) == name) + name.size();
}
}
sm->setScoreForIndex(sourceRow, score);
......
Supports Markdown
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