Git: Allow clicking references in VcsOutputWindow

Fixes: QTCREATORBUG-16477
Change-Id: If1f36bec0826a3116e5261a270cd63a1536e13f5
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
This commit is contained in:
Andre Hartmann
2020-02-16 14:03:47 +01:00
committed by André Hartmann
parent d0a11322f4
commit 4cbb24906f
5 changed files with 47 additions and 8 deletions

View File

@@ -919,6 +919,14 @@ GitPluginPrivate::GitPluginPrivate()
m_gerritPlugin->initialize(remoteRepositoryMenu);
m_gerritPlugin->updateActions(currentState());
m_gerritPlugin->addToLocator(m_commandLocator);
connect(VcsOutputWindow::instance(), &VcsOutputWindow::referenceClicked,
this, [this](const QString &name) {
const VcsBasePluginState state = currentState();
QTC_ASSERT(state.hasTopLevel(), return);
m_gitClient.show(state.topLevel(), name);
});
}
void GitPluginPrivate::diffCurrentFile()

View File

@@ -30,17 +30,24 @@
namespace VcsBase {
VcsOutputFormatter::VcsOutputFormatter() : m_urlRegexp("https?://\\S*") {}
VcsOutputFormatter::VcsOutputFormatter() :
m_urlRegexp("https?://\\S*"),
m_referenceRegexp("(v[0-9]+\\.[0-9]+\\.[0-9]+[\\-A-Za-z0-9]*)" // v0.1.2-beta3
"|([0-9a-f]{6,}(?:\\.\\.[0-9a-f]{6,})?)") // 789acf or 123abc..456cde
{
}
void VcsOutputFormatter::appendMessage(const QString &text, Utils::OutputFormat format)
{
QString out = text;
const QRegularExpressionMatch match = m_urlRegexp.match(text);
if (match.hasMatch()) {
const QRegularExpressionMatch urlMatch = m_urlRegexp.match(text);
const QRegularExpressionMatch referenceMatch = m_referenceRegexp.match(text);
auto append = [this](const QRegularExpressionMatch &match,
QString text, Utils::OutputFormat format) {
const QTextCharFormat normalFormat = charFormat(format);
OutputFormatter::appendMessage(text.left(match.capturedStart()), format);
QTextCursor tc = plainTextEdit()->textCursor();
QStringRef url = match.capturedRef();
QStringView url = match.capturedView();
int end = match.capturedEnd();
while (url.rbegin()->isPunct()) {
url.chop(1);
@@ -50,14 +57,22 @@ void VcsOutputFormatter::appendMessage(const QString &text, Utils::OutputFormat
tc.insertText(url.toString(), linkFormat(normalFormat, url.toString()));
tc.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
OutputFormatter::appendMessage(text.mid(end), format);
} else {
};
if (urlMatch.hasMatch())
append(urlMatch, text, format);
else if (referenceMatch.hasMatch())
append(referenceMatch, text, format);
else
OutputFormatter::appendMessage(text, format);
}
}
void VcsOutputFormatter::handleLink(const QString &href)
{
QDesktopServices::openUrl(QUrl(href));
if (href.startsWith("http://") || href.startsWith("https://"))
QDesktopServices::openUrl(QUrl(href));
else if (!href.isEmpty())
emit referenceClicked(href);
}
}

View File

@@ -31,14 +31,19 @@ namespace VcsBase {
class VcsOutputFormatter : public Utils::OutputFormatter
{
Q_OBJECT
public:
VcsOutputFormatter();
~VcsOutputFormatter() override = default;
void appendMessage(const QString &text, Utils::OutputFormat format) override;
void handleLink(const QString &href) override;
signals:
void referenceClicked(const QString &reference);
private:
const QRegularExpression m_urlRegexp;
const QRegularExpression m_referenceRegexp;
};
}

View File

@@ -102,6 +102,7 @@ public:
void appendLines(const QString &s, const QString &repository = QString());
void appendLinesWithStyle(const QString &s, VcsOutputWindow::MessageStyle style,
const QString &repository = QString());
VcsOutputFormatter *formatter();
protected:
void contextMenuEvent(QContextMenuEvent *event) override;
@@ -247,6 +248,11 @@ void OutputWindowPlainTextEdit::appendLinesWithStyle(const QString &s,
}
}
VcsOutputFormatter *OutputWindowPlainTextEdit::formatter()
{
return m_formatter;
}
void OutputWindowPlainTextEdit::setFormat(VcsOutputWindow::MessageStyle style)
{
m_formatter->setBoldFontEnabled(style == VcsOutputWindow::Command);
@@ -305,6 +311,8 @@ VcsOutputWindow::VcsOutputWindow()
connect(this, &IOutputPane::resetZoom, &d->widget, &Core::OutputWindow::resetZoom);
connect(TextEditor::TextEditorSettings::instance(), &TextEditor::TextEditorSettings::behaviorSettingsChanged,
this, updateBehaviorSettings);
connect(d->widget.formatter(), &VcsOutputFormatter::referenceClicked,
VcsOutputWindow::instance(), &VcsOutputWindow::referenceClicked);
}
static QString filterPasswordFromUrls(const QString &input)

View File

@@ -76,6 +76,9 @@ public:
Message, // A blue message text (e.g. "command has finished successfully")
};
signals:
void referenceClicked(const QString &reference);
public slots:
static void setRepository(const QString &);
static void clearRepository();