Utils: move extractFromFileName from LineColumn to Text::Position

Change-Id: Ibb2465e66c280d4201377921f69741a050d94bc1
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
David Schulz
2023-05-09 14:24:24 +02:00
parent 7d87233c9c
commit 5a0f2e6d15
7 changed files with 42 additions and 55 deletions

View File

@@ -3,8 +3,9 @@
#include "textutils.h"
#include <QTextDocument>
#include <QRegularExpression>
#include <QTextBlock>
#include <QTextDocument>
namespace Utils::Text {
@@ -13,6 +14,39 @@ bool Position::operator==(const Position &other) const
return line == other.line && column == other.column;
}
/*!
Returns the text position of a \a fileName and sets the \a postfixPos if
it can find a positional postfix.
The following patterns are supported: \c {filepath.txt:19},
\c{filepath.txt:19:12}, \c {filepath.txt+19},
\c {filepath.txt+19+12}, and \c {filepath.txt(19)}.
*/
Position Position::fromFileName(QStringView fileName, int &postfixPos)
{
static const auto regexp = QRegularExpression("[:+](\\d+)?([:+](\\d+)?)?$");
// (10) MSVC-style
static const auto vsRegexp = QRegularExpression("[(]((\\d+)[)]?)?$");
const QRegularExpressionMatch match = regexp.match(fileName);
Position pos;
if (match.hasMatch()) {
postfixPos = match.capturedStart(0);
pos.line = 0; // for the case that there's only a : at the end
if (match.lastCapturedIndex() > 0) {
pos.line = match.captured(1).toInt();
if (match.lastCapturedIndex() > 2) // index 2 includes the + or : for the column number
pos.column = match.captured(3).toInt() - 1; //column is 0 based, despite line being 1 based
}
} else {
const QRegularExpressionMatch vsMatch = vsRegexp.match(fileName);
postfixPos = vsMatch.capturedStart(0);
if (vsMatch.lastCapturedIndex() > 1) // index 1 includes closing )
pos.line = vsMatch.captured(2).toInt();
}
return pos;
}
int Range::length(const QString &text) const
{
if (begin.line == end.line)