Utils: move line column parsing again

This will allow us to just parse the postfix without fileName
extraction.

Change-Id: Ied6b80f25078473e2b91ced68913205a17f9068e
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
David Schulz
2021-05-27 09:44:22 +02:00
parent 6caa8cc289
commit 84ea908b2d
6 changed files with 87 additions and 32 deletions

View File

@@ -77,7 +77,7 @@ add_qtc_library(Utils
json.cpp json.h json.cpp json.h
jsontreeitem.cpp jsontreeitem.h jsontreeitem.cpp jsontreeitem.h
layoutbuilder.cpp layoutbuilder.h layoutbuilder.cpp layoutbuilder.h
linecolumn.h linecolumn.cpp linecolumn.h
link.cpp link.h link.cpp link.h
listmodel.h listmodel.h
listutils.h listutils.h

View File

@@ -0,0 +1,68 @@
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "linecolumn.h"
#include <QRegularExpression>
namespace Utils {
/*!
Returns the line and column 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)}.
*/
LineColumn LineColumn::extractFromFileName(const QString &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);
QString filePath = fileName;
LineColumn lineColumn;
if (match.hasMatch()) {
postfixPos = match.capturedStart(0);
filePath = fileName.left(match.capturedStart(0));
lineColumn.line = 0; // for the case that there's only a : at the end
if (match.lastCapturedIndex() > 0) {
lineColumn.line = match.captured(1).toInt();
if (match.lastCapturedIndex() > 2) // index 2 includes the + or : for the column number
lineColumn.column = match.captured(3).toInt() - 1; //column is 0 based, despite line being 1 based
}
} else {
const QRegularExpressionMatch vsMatch = vsRegexp.match(fileName);
postfixPos = match.capturedStart(0);
filePath = fileName.left(vsMatch.capturedStart(0));
if (vsMatch.lastCapturedIndex() > 1) // index 1 includes closing )
lineColumn.line = vsMatch.captured(2).toInt();
}
return lineColumn;
}
} // namespace Utils

View File

@@ -25,22 +25,25 @@
#pragma once #pragma once
#include "utils_global.h"
#include "optional.h" #include "optional.h"
#include <QMetaType> #include <QMetaType>
namespace Utils { namespace Utils {
class LineColumn class QTCREATOR_UTILS_EXPORT LineColumn
{ {
public: public:
constexpr LineColumn() = default; constexpr LineColumn() = default;
constexpr constexpr
LineColumn(int line, int column) LineColumn(int line, int column)
: line(line), : line(line),
column(column) column(column)
{} {}
bool isValid() const bool isValid() const
{ {
return line >= 0 && column >= 0; return line >= 0 && column >= 0;
@@ -56,6 +59,8 @@ public:
return !(first == second); return !(first == second);
} }
static LineColumn extractFromFileName(const QString &fileName, int &postfixPos);
public: public:
int line = -1; int line = -1;
int column = -1; int column = -1;

View File

@@ -25,14 +25,14 @@
#include "link.h" #include "link.h"
#include <QRegularExpression> #include "linecolumn.h"
namespace Utils { namespace Utils {
/*! /*!
Returns the Link to \a fileName. Returns the Link to \a fileName.
If \a canContainLineNumber is true the line number, and column number components If \a canContainLineNumber is true the line number, and column number components
are extracted from \a fileName and the found \a postFix is set. are extracted from \a fileName and the found \a postfix is set.
The following patterns are supported: \c {filepath.txt:19}, The following patterns are supported: \c {filepath.txt:19},
\c{filepath.txt:19:12}, \c {filepath.txt+19}, \c{filepath.txt:19:12}, \c {filepath.txt+19},
@@ -42,33 +42,13 @@ Link Link::fromString(const QString &fileName, bool canContainLineNumber, QStrin
{ {
if (!canContainLineNumber) if (!canContainLineNumber)
return {Utils::FilePath::fromString(fileName)}; return {Utils::FilePath::fromString(fileName)};
// :10:2 GCC/Clang-style int postfixPos = -1;
static const auto regexp = QRegularExpression("[:+](\\d+)?([:+](\\d+)?)?$"); const LineColumn lineColumn = LineColumn::extractFromFileName(fileName, postfixPos);
// (10) MSVC-style if (postfix && postfixPos >= 0)
static const auto vsRegexp = QRegularExpression("[(]((\\d+)[)]?)?$"); *postfix = fileName.mid(postfixPos);
const QRegularExpressionMatch match = regexp.match(fileName); return {Utils::FilePath::fromString(fileName.left(postfixPos - 1)),
QString filePath = fileName; lineColumn.line,
int line = -1; lineColumn.column};
int column = -1;
if (match.hasMatch()) {
if (postfix)
*postfix = match.captured(0);
filePath = fileName.left(match.capturedStart(0));
line = 0; // for the case that there's only a : at the end
if (match.lastCapturedIndex() > 0) {
line = match.captured(1).toInt();
if (match.lastCapturedIndex() > 2) // index 2 includes the + or : for the column number
column = match.captured(3).toInt() - 1; //column is 0 based, despite line being 1 based
}
} else {
const QRegularExpressionMatch vsMatch = vsRegexp.match(fileName);
if (postfix)
*postfix = vsMatch.captured(0);
filePath = fileName.left(vsMatch.capturedStart(0));
if (vsMatch.lastCapturedIndex() > 1) // index 1 includes closing )
line = vsMatch.captured(2).toInt();
}
return {Utils::FilePath::fromString(filePath), line, column};
} }
} // namespace Utils } // namespace Utils

View File

@@ -144,6 +144,7 @@ SOURCES += \
$$PWD/futuresynchronizer.cpp \ $$PWD/futuresynchronizer.cpp \
$$PWD/qtcsettings.cpp \ $$PWD/qtcsettings.cpp \
$$PWD/link.cpp \ $$PWD/link.cpp \
$$PWD/linecolumn.cpp \
HEADERS += \ HEADERS += \
$$PWD/environmentfwd.h \ $$PWD/environmentfwd.h \

View File

@@ -161,6 +161,7 @@ Project {
"jsontreeitem.h", "jsontreeitem.h",
"layoutbuilder.cpp", "layoutbuilder.cpp",
"layoutbuilder.h", "layoutbuilder.h",
"linecolumn.cpp",
"linecolumn.h", "linecolumn.h",
"link.cpp", "link.cpp",
"link.h", "link.h",