Utils: fix Text::Range length and remove mid

Those functions are based on the assumption that the passed text starts
at the begin position, which was good enough for search results, but if
used in other parts of the codebase it might give unwanted results.
Calculate the length of the range now as expected and subtract the
beginning lines.
In order to still got the correct results for the text result texts
modify the result range to always start at the first line before
calculating the length of the range.
Also add tests for the modified functionality

Change-Id: I7ccd75b642dda6dd4f738877cbe3543d46c03652
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
This commit is contained in:
David Schulz
2023-05-11 09:34:53 +02:00
parent 70609cdec6
commit 1a98dda5c4
8 changed files with 101 additions and 15 deletions

View File

@@ -58,22 +58,30 @@ Position Position::fromPositionInDocument(const QTextDocument *document, int pos
int Range::length(const QString &text) const
{
if (end.line < begin.line)
return -1;
if (begin.line == end.line)
return end.column - begin.column;
const int lineCount = end.line - begin.line;
int index = text.indexOf(QChar::LineFeed);
int index = 0;
int currentLine = 1;
while (index > 0 && currentLine < lineCount) {
++index;
while (currentLine < begin.line) {
index = text.indexOf(QChar::LineFeed, index);
if (index < 0)
return -1;
++index;
++currentLine;
}
if (index < 0)
return 0;
return index - begin.column + end.column;
const int beginIndex = index + begin.column;
while (currentLine < end.line) {
index = text.indexOf(QChar::LineFeed, index);
if (index < 0)
return -1;
++index;
++currentLine;
}
return index + end.column - beginIndex;
}
bool Range::operator==(const Range &other) const