diff options
author | David Schulz <[email protected]> | 2023-05-09 14:24:24 +0200 |
---|---|---|
committer | David Schulz <[email protected]> | 2023-05-11 10:45:49 +0000 |
commit | 5a0f2e6d15123559d0d489b8b466888af28136ef (patch) | |
tree | fa91090401dedbcef1f6ae4f08398dc29eda2d71 /src/libs/utils/textutils.cpp | |
parent | 7d87233c9c70eed9bf10d36ff1d4da115c071db7 (diff) |
Utils: move extractFromFileName from LineColumn to Text::Position
Change-Id: Ibb2465e66c280d4201377921f69741a050d94bc1
Reviewed-by: Jarek Kobus <[email protected]>
Reviewed-by: Qt CI Bot <[email protected]>
Diffstat (limited to 'src/libs/utils/textutils.cpp')
-rw-r--r-- | src/libs/utils/textutils.cpp | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/src/libs/utils/textutils.cpp b/src/libs/utils/textutils.cpp index 55e8a736d05..5c9846a24d6 100644 --- a/src/libs/utils/textutils.cpp +++ b/src/libs/utils/textutils.cpp @@ -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) |