From 665ae04605cfa4033201b42194580b32739c9df9 Mon Sep 17 00:00:00 2001 From: Marcus Tillmanns Date: Mon, 6 Mar 2023 12:25:26 +0100 Subject: [PATCH] Terminal: Open Link with line/column info Change-Id: I3e70a7c33a935b7bd3e12fb903148bcd60ff55aa Reviewed-by: hjk Reviewed-by: Cristian Adam --- src/libs/utils/link.h | 6 +++++- src/plugins/terminal/terminalwidget.cpp | 23 +++++++++++++---------- src/plugins/terminal/terminalwidget.h | 5 +++-- src/plugins/terminal/tests/filenames | 22 ++++++++++++++++++++++ 4 files changed, 43 insertions(+), 13 deletions(-) create mode 100755 src/plugins/terminal/tests/filenames diff --git a/src/libs/utils/link.h b/src/libs/utils/link.h index 655957e9acc..00194654c97 100644 --- a/src/libs/utils/link.h +++ b/src/libs/utils/link.h @@ -27,7 +27,11 @@ public: static Link fromString(const QString &filePathWithNumbers, bool canContainLineNumber = false); bool hasValidTarget() const - { return !targetFilePath.isEmpty(); } + { + if (!targetFilePath.isEmpty()) + return true; + return !targetFilePath.scheme().isEmpty() || !targetFilePath.host().isEmpty(); + } bool hasValidLinkText() const { return linkTextStart != linkTextEnd; } diff --git a/src/plugins/terminal/terminalwidget.cpp b/src/plugins/terminal/terminalwidget.cpp index f2af06d5774..8df6e3c02aa 100644 --- a/src/plugins/terminal/terminalwidget.cpp +++ b/src/plugins/terminal/terminalwidget.cpp @@ -3,7 +3,6 @@ #include "terminalwidget.h" #include "glyphcache.h" -#include "keys.h" #include "terminalsettings.h" #include "terminalsurface.h" #include "terminaltr.h" @@ -993,11 +992,12 @@ void TerminalWidget::mousePressEvent(QMouseEvent *event) if (event->button() == Qt::LeftButton && event->modifiers() == Qt::ControlModifier) { if (m_linkSelection) { - if (m_linkSelection->filePath.scheme().toString().startsWith("http")) { - QDesktopServices::openUrl(m_linkSelection->filePath.toUrl()); + if (m_linkSelection->link.targetFilePath.scheme().toString().startsWith("http")) { + QDesktopServices::openUrl(m_linkSelection->link.targetFilePath.toUrl()); return; } - Core::EditorManager::openEditorAt(Utils::Link(m_linkSelection->filePath)); + + Core::EditorManager::openEditorAt(m_linkSelection->link); } return; } @@ -1062,17 +1062,20 @@ void TerminalWidget::checkLinkAt(const QPoint &pos) const TextAndOffsets hit = textAt(pos); if (hit.text.size() > 0) { - QString t = QString::fromUcs4(hit.text.c_str(), hit.text.size()); + QString t + = Utils::chopIfEndsWith(QString::fromUcs4(hit.text.c_str(), hit.text.size()).trimmed(), + ':'); if (t.startsWith("~/")) { t = QDir::homePath() + t.mid(1); } - // Todo: Windows path support - const FilePath p = FilePath::fromString(t.trimmed()); + Utils::Link link = Utils::Link::fromString(t, true); - if (!p.isEmpty() && (p.scheme().toString().startsWith("http") || p.exists())) { - const LinkSelection newSelection = LinkSelection{{hit.start, hit.end}, p}; - if (*m_linkSelection != newSelection) { + if (link.hasValidTarget() + && (link.targetFilePath.scheme().toString().startsWith("http") + || link.targetFilePath.exists())) { + const LinkSelection newSelection = LinkSelection{{hit.start, hit.end}, link}; + if (!m_linkSelection || *m_linkSelection != newSelection) { m_linkSelection = newSelection; updateViewport(); } diff --git a/src/plugins/terminal/terminalwidget.h b/src/plugins/terminal/terminalwidget.h index 07355b95bc2..6238fac52c7 100644 --- a/src/plugins/terminal/terminalwidget.h +++ b/src/plugins/terminal/terminalwidget.h @@ -5,6 +5,7 @@ #include "terminalsurface.h" +#include #include #include @@ -59,11 +60,11 @@ public: struct LinkSelection : public Selection { - Utils::FilePath filePath; + Utils::Link link; bool operator!=(const LinkSelection &other) const { - return filePath != other.filePath || Selection::operator!=(other); + return link != other.link || Selection::operator!=(other); } }; diff --git a/src/plugins/terminal/tests/filenames b/src/plugins/terminal/tests/filenames new file mode 100755 index 00000000000..6a4e33e3ede --- /dev/null +++ b/src/plugins/terminal/tests/filenames @@ -0,0 +1,22 @@ +#!/usr/bin/env bash + +echo "Home:" +echo "~/" + +FULLPATH=$(readlink -f "$0") + +echo "This file:" +echo "$FULLPATH" + +echo "This file, this line:" +echo "$FULLPATH:11" + +echo "This file, this line, this word:" +echo "$FULLPATH:14:34" + +echo "This file, with an error message:" +echo "$FULLPATH:18:23: error: C++ requires a type specifier for all declarations" + +echo "A link: http://google.com" +echo "Another link: https://www.qt.io" +echo "Another one: https://codereview.qt-project.org/c/qt-creator/qt-creator/+/464740"