From f939f968eae18c25565681c58a3a84ef3b4680fc Mon Sep 17 00:00:00 2001 From: hjk Date: Fri, 13 Jan 2023 11:36:24 +0100 Subject: [PATCH] Utils: Consolidate Link::from{FilePath,String} The input is more unparsed-string-ish in nature, so FilePath is not appropriate. Change-Id: I85efb5813b8f5fbbc4127be1c936d5487637b75c Reviewed-by: Christian Stenger --- src/libs/utils/link.cpp | 39 ++++++++++--------------- src/libs/utils/link.h | 5 +--- src/plugins/coreplugin/mainwindow.cpp | 2 +- src/plugins/qmljseditor/qmljseditor.cpp | 2 +- 4 files changed, 19 insertions(+), 29 deletions(-) diff --git a/src/libs/utils/link.cpp b/src/libs/utils/link.cpp index 36798ed6095..528295c19f4 100644 --- a/src/libs/utils/link.cpp +++ b/src/libs/utils/link.cpp @@ -8,37 +8,30 @@ namespace Utils { /*! - Returns the Link to \a fileName. + Returns the Link to \a filePath. 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 the \a filePath's \c path() and the found \a postfix is set. 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)}. */ -Link Link::fromString(const QString &fileName, bool canContainLineNumber, QString *postfix) -{ - if (!canContainLineNumber) - return {FilePath::fromString(fileName)}; - int postfixPos = -1; - const LineColumn lineColumn = LineColumn::extractFromFileName(fileName, postfixPos); - if (postfix && postfixPos >= 0) - *postfix = fileName.mid(postfixPos); - return {FilePath::fromString(fileName.left(postfixPos)), - lineColumn.line, - lineColumn.column}; -} -Link Link::fromFilePath(const FilePath &filePath, bool canContainLineNumber, QString *postfix) +Link Link::fromString(const QString &filePathWithNumbers, bool canContainLineNumber, QString *postfix) { - if (!canContainLineNumber) - return {filePath}; - int postfixPos = -1; - QString fileName = filePath.path(); - const LineColumn lineColumn = LineColumn::extractFromFileName(fileName, postfixPos); - if (postfix && postfixPos >= 0) - *postfix = fileName.mid(postfixPos); - return Link{filePath.withNewPath(fileName.left(postfixPos)), lineColumn.line, lineColumn.column}; + Link link; + if (!canContainLineNumber) { + link.targetFilePath = FilePath::fromUserInput(filePathWithNumbers); + } else { + int postfixPos = -1; + const LineColumn lineColumn = LineColumn::extractFromFileName(filePathWithNumbers, postfixPos); + if (postfix && postfixPos >= 0) + *postfix = filePathWithNumbers.mid(postfixPos); + link.targetFilePath = FilePath::fromUserInput(filePathWithNumbers.left(postfixPos)); + link.targetLine = lineColumn.line; + link.targetColumn = lineColumn.column; + } + return link; } } // namespace Utils diff --git a/src/libs/utils/link.h b/src/libs/utils/link.h index 83eadeb4b69..500863f7ef4 100644 --- a/src/libs/utils/link.h +++ b/src/libs/utils/link.h @@ -23,12 +23,9 @@ public: , targetColumn(column) {} - static Link fromString(const QString &fileName, + static Link fromString(const QString &filePathWithNumbers, bool canContainLineNumber = false, QString *postfix = nullptr); - static Link fromFilePath(const FilePath &filePath, - bool canContainLineNumber = false, - QString *postfix = nullptr); bool hasValidTarget() const { return !targetFilePath.isEmpty(); } diff --git a/src/plugins/coreplugin/mainwindow.cpp b/src/plugins/coreplugin/mainwindow.cpp index ed905dee028..4e113816589 100644 --- a/src/plugins/coreplugin/mainwindow.cpp +++ b/src/plugins/coreplugin/mainwindow.cpp @@ -1000,7 +1000,7 @@ IDocument *MainWindow::openFiles(const FilePaths &filePaths, emFlags |= EditorManager::SwitchSplitIfAlreadyVisible; IEditor *editor = nullptr; if (flags & ICore::CanContainLineAndColumnNumbers) { - const Link &link = Link::fromFilePath(absoluteFilePath, true); + const Link &link = Link::fromString(absoluteFilePath.toString(), true); editor = EditorManager::openEditorAt(link, {}, emFlags); } else { editor = EditorManager::openEditor(absoluteFilePath, {}, emFlags); diff --git a/src/plugins/qmljseditor/qmljseditor.cpp b/src/plugins/qmljseditor/qmljseditor.cpp index f7ccc3dc09a..8a515d97228 100644 --- a/src/plugins/qmljseditor/qmljseditor.cpp +++ b/src/plugins/qmljseditor/qmljseditor.cpp @@ -791,7 +791,7 @@ void QmlJSEditorWidget::findLinkAt(const QTextCursor &cursor, } if (text.startsWith("https:/") || text.startsWith("http:/")) { - Link link = Link::fromFilePath(FilePath::fromPathPart(text)); + Link link = Link::fromString(text); link.linkTextStart = literal->literalToken.begin(); link.linkTextEnd = literal->literalToken.end(); processLinkCallback(link);