FileSearch: Get rid of FileSearchResult

Use SearchResultItem instead.
This change should reduce the remaining freeze described in
a9eb732ce6 even more.

Change-Id: I102b82ed5677360ccd9e425dd0bdd941d87116f0
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
Jarek Kobus
2023-05-05 14:05:10 +02:00
parent 1d4228dfda
commit 936086745a
18 changed files with 289 additions and 294 deletions

View File

@@ -7,6 +7,7 @@
#include "filepath.h"
#include "mapreduce.h"
#include "qtcassert.h"
#include "searchresultitem.h"
#include "stringutils.h"
#include "utilstr.h"
#include "utiltypes.h"
@@ -69,7 +70,7 @@ public:
FileSearch(const QString &searchTerm,
QTextDocument::FindFlags flags,
const QMap<FilePath, QString> &fileToContentsMap);
void operator()(QFutureInterface<FileSearchResultList> &futureInterface,
void operator()(QFutureInterface<SearchResultItems> &futureInterface,
const FileIterator::Item &item) const;
private:
@@ -91,7 +92,7 @@ public:
QTextDocument::FindFlags flags,
const QMap<FilePath, QString> &fileToContentsMap);
FileSearchRegExp(const FileSearchRegExp &other);
void operator()(QFutureInterface<FileSearchResultList> &futureInterface,
void operator()(QFutureInterface<SearchResultItems> &futureInterface,
const FileIterator::Item &item) const;
private:
@@ -117,7 +118,7 @@ FileSearch::FileSearch(const QString &searchTerm,
termDataUpper = searchTermUpper.constData();
}
void FileSearch::operator()(QFutureInterface<FileSearchResultList> &futureInterface,
void FileSearch::operator()(QFutureInterface<SearchResultItems> &futureInterface,
const FileIterator::Item &item) const
{
if (futureInterface.isCanceled())
@@ -125,7 +126,7 @@ void FileSearch::operator()(QFutureInterface<FileSearchResultList> &futureInterf
qCDebug(searchLog) << "Searching in" << item.filePath;
futureInterface.setProgressRange(0, 1);
futureInterface.setProgressValue(0);
FileSearchResultList results;
SearchResultItems results;
QString tempString;
if (!getFileContent(item.filePath, item.encoding, &tempString, fileToContentsMap)) {
qCDebug(searchLog) << "- failed to get content for" << item.filePath;
@@ -190,13 +191,13 @@ void FileSearch::operator()(QFutureInterface<FileSearchResultList> &futureInterf
}
}
if (equal) {
const QString resultItemText = clippedText(chunk, MAX_LINE_SIZE);
results << FileSearchResult(item.filePath,
lineNr,
resultItemText,
regionPtr - chunkPtr,
termMaxIndex + 1,
QStringList());
SearchResultItem result;
result.setFilePath(item.filePath);
result.setMainRange(lineNr, regionPtr - chunkPtr, termMaxIndex + 1);
result.setDisplayText(clippedText(chunk, MAX_LINE_SIZE));
result.setUserData(QStringList());
result.setUseTextEditorFont(true);
results << result;
regionPtr += termMaxIndex; // another +1 done by for-loop
}
}
@@ -238,7 +239,7 @@ QRegularExpressionMatch FileSearchRegExp::doGuardedMatch(const QString &line, in
return expression.match(line, offset);
}
void FileSearchRegExp::operator()(QFutureInterface<FileSearchResultList> &futureInterface,
void FileSearchRegExp::operator()(QFutureInterface<SearchResultItems> &futureInterface,
const FileIterator::Item &item) const
{
if (!expression.isValid()) {
@@ -250,7 +251,7 @@ void FileSearchRegExp::operator()(QFutureInterface<FileSearchResultList> &future
qCDebug(searchLog) << "Searching in" << item.filePath;
futureInterface.setProgressRange(0, 1);
futureInterface.setProgressValue(0);
FileSearchResultList results;
SearchResultItems results;
QString tempString;
if (!getFileContent(item.filePath, item.encoding, &tempString, fileToContentsMap)) {
qCDebug(searchLog) << "- failed to get content for" << item.filePath;
@@ -270,12 +271,13 @@ void FileSearchRegExp::operator()(QFutureInterface<FileSearchResultList> &future
int pos = 0;
while ((match = doGuardedMatch(line, pos)).hasMatch()) {
pos = match.capturedStart();
results << FileSearchResult(item.filePath,
lineNr,
resultItemText,
pos,
match.capturedLength(),
match.capturedTexts());
SearchResultItem result;
result.setFilePath(item.filePath);
result.setMainRange(lineNr, pos, match.capturedLength());
result.setDisplayText(resultItemText);
result.setUserData(match.capturedTexts());
result.setUseTextEditorFont(true);
results << result;
if (match.capturedLength() == 0)
break;
pos += match.capturedLength();
@@ -299,12 +301,12 @@ struct SearchState
SearchState(const QString &term, FileIterator *iterator) : searchTerm(term), files(iterator) {}
QString searchTerm;
FileIterator *files = nullptr;
FileSearchResultList cachedResults;
SearchResultItems cachedResults;
int numFilesSearched = 0;
int numMatches = 0;
};
SearchState initFileSearch(QFutureInterface<FileSearchResultList> &futureInterface,
SearchState initFileSearch(QFutureInterface<SearchResultItems> &futureInterface,
const QString &searchTerm, FileIterator *files)
{
futureInterface.setProgressRange(0, files->maxProgress());
@@ -312,9 +314,9 @@ SearchState initFileSearch(QFutureInterface<FileSearchResultList> &futureInterfa
return SearchState(searchTerm, files);
}
void collectSearchResults(QFutureInterface<FileSearchResultList> &futureInterface,
void collectSearchResults(QFutureInterface<SearchResultItems> &futureInterface,
SearchState &state,
const FileSearchResultList &results)
const SearchResultItems &results)
{
state.numMatches += results.size();
state.cachedResults << results;
@@ -333,7 +335,7 @@ void collectSearchResults(QFutureInterface<FileSearchResultList> &futureInterfac
}
}
void cleanUpFileSearch(QFutureInterface<FileSearchResultList> &futureInterface,
void cleanUpFileSearch(QFutureInterface<SearchResultItems> &futureInterface,
SearchState &state)
{
if (!state.cachedResults.isEmpty()) {
@@ -356,13 +358,13 @@ void cleanUpFileSearch(QFutureInterface<FileSearchResultList> &futureInterface,
} // namespace
QFuture<FileSearchResultList> Utils::findInFiles(const QString &searchTerm,
FileIterator *files,
QTextDocument::FindFlags flags,
const QMap<FilePath, QString> &fileToContentsMap)
QFuture<SearchResultItems> Utils::findInFiles(const QString &searchTerm,
FileIterator *files,
QTextDocument::FindFlags flags,
const QMap<FilePath, QString> &fileToContentsMap)
{
return mapReduce(files->begin(), files->end(),
[searchTerm, files](QFutureInterface<FileSearchResultList> &futureInterface) {
[searchTerm, files](QFutureInterface<SearchResultItems> &futureInterface) {
return initFileSearch(futureInterface, searchTerm, files);
},
FileSearch(searchTerm, flags, fileToContentsMap),
@@ -370,14 +372,14 @@ QFuture<FileSearchResultList> Utils::findInFiles(const QString &searchTerm,
&cleanUpFileSearch);
}
QFuture<FileSearchResultList> Utils::findInFilesRegExp(
QFuture<SearchResultItems> Utils::findInFilesRegExp(
const QString &searchTerm,
FileIterator *files,
QTextDocument::FindFlags flags,
const QMap<FilePath, QString> &fileToContentsMap)
{
return mapReduce(files->begin(), files->end(),
[searchTerm, files](QFutureInterface<FileSearchResultList> &futureInterface) {
[searchTerm, files](QFutureInterface<SearchResultItems> &futureInterface) {
return initFileSearch(futureInterface, searchTerm, files);
},
FileSearchRegExp(searchTerm, flags, fileToContentsMap),