Commit f3774d0a authored by Eike Ziller's avatar Eike Ziller

Limit the length of shown search result line.

This e.g. avoids "lines" of several MB if file search hits a binary
file.

Task-number: QTCREATORBUG-6116
Change-Id: Ibf89a332bc82ddbdb2aeb83c4b3886e784dab4b8
Reviewed-by: default avatarRalf Neubersch <neubersch@neubersch.de>
Reviewed-by: default avatarRobert Löhning <robert.loehning@nokia.com>
parent c4f6ad85
......@@ -70,6 +70,15 @@ static inline QString msgFound(const QString &searchTerm, int numMatches, int nu
namespace {
const int MAX_LINE_SIZE = 400;
QString clippedText(const QString &text, int maxLength)
{
if (text.length() > maxLength)
return text.left(maxLength) + QChar(0x2026); // '...'
return text;
}
void runFileSearch(QFutureInterface<FileSearchResultList> &future,
QString searchTerm,
FileIterator *files,
......@@ -123,6 +132,7 @@ void runFileSearch(QFutureInterface<FileSearchResultList> &future,
while (!stream.atEnd()) {
++lineNr;
const QString chunk = stream.readLine();
const QString resultItemText = clippedText(chunk, MAX_LINE_SIZE);
int chunkLength = chunk.length();
const QChar *chunkPtr = chunk.constData();
const QChar *chunkEnd = chunkPtr + chunkLength - 1;
......@@ -166,7 +176,7 @@ void runFileSearch(QFutureInterface<FileSearchResultList> &future,
}
}
if (equal) {
results << FileSearchResult(s, lineNr, chunk,
results << FileSearchResult(s, lineNr, resultItemText,
regionPtr - chunkPtr, termLength,
QStringList());
++numMatches;
......@@ -244,10 +254,11 @@ void runFileSearchRegExp(QFutureInterface<FileSearchResultList> &future,
QString line;
while (!stream.atEnd()) {
line = stream.readLine();
const QString resultItemText = clippedText(line, MAX_LINE_SIZE);
int lengthOfLine = line.size();
int pos = 0;
while ((pos = expression.indexIn(line, pos)) != -1) {
results << FileSearchResult(s, lineNr, line,
results << FileSearchResult(s, lineNr, resultItemText,
pos, expression.matchedLength(),
expression.capturedTexts());
++numMatches;
......
......@@ -162,8 +162,10 @@ void SearchResultTreeItemDelegate::drawMarker(QPainter *painter, const QModelInd
{
int searchTermStart = index.model()->data(index, ItemDataRoles::SearchTermStartRole).toInt();
int searchTermLength = index.model()->data(index, ItemDataRoles::SearchTermLengthRole).toInt();
if (searchTermStart < 0 || searchTermLength < 1)
if (searchTermStart < 0 || searchTermStart >= text.length() || searchTermLength < 1)
return;
// clip searchTermLength to end of line
searchTermLength = qMin(searchTermLength, text.length() - searchTermStart);
const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
int searchTermStartPixels = painter->fontMetrics().width(text.left(searchTermStart));
int searchTermLengthPixels = painter->fontMetrics().width(text.mid(searchTermStart, searchTermLength));
......
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