forked from qt-creator/qt-creator
VcsBase: Add Qt Jira and Gerrit URLs support
This way one can simply click on the url in git log. Change-Id: I91abda71a48f079e554a48a70a0f05e8417731ed Reviewed-by: Orgad Shaneh <orgads@gmail.com>
This commit is contained in:
@@ -371,16 +371,22 @@ private:
|
|||||||
public:
|
public:
|
||||||
int startColumn;
|
int startColumn;
|
||||||
QString url;
|
QString url;
|
||||||
|
qsizetype urlLength;
|
||||||
};
|
};
|
||||||
|
|
||||||
UrlData m_urlData;
|
UrlData m_urlData;
|
||||||
QRegularExpression m_pattern;
|
QRegularExpression m_pattern;
|
||||||
|
QRegularExpression m_jiraPattern;
|
||||||
|
QRegularExpression m_gerritPattern;
|
||||||
};
|
};
|
||||||
|
|
||||||
UrlTextCursorHandler::UrlTextCursorHandler(VcsBaseEditorWidget *editorWidget)
|
UrlTextCursorHandler::UrlTextCursorHandler(VcsBaseEditorWidget *editorWidget)
|
||||||
: AbstractTextCursorHandler(editorWidget)
|
: AbstractTextCursorHandler(editorWidget)
|
||||||
{
|
{
|
||||||
setUrlPattern(QLatin1String("https?\\://[^\\s]+"));
|
setUrlPattern(QLatin1String("https?\\://[^\\s]+"));
|
||||||
|
|
||||||
|
m_jiraPattern = QRegularExpression("(Fixes|Task-number): ([A-Z]+-[0-9]+)");
|
||||||
|
m_gerritPattern = QRegularExpression("Change-Id: (I[a-f0-9]{40})");
|
||||||
}
|
}
|
||||||
|
|
||||||
bool UrlTextCursorHandler::findContentsUnderCursor(const QTextCursor &cursor)
|
bool UrlTextCursorHandler::findContentsUnderCursor(const QTextCursor &cursor)
|
||||||
@@ -389,23 +395,37 @@ bool UrlTextCursorHandler::findContentsUnderCursor(const QTextCursor &cursor)
|
|||||||
|
|
||||||
m_urlData.url.clear();
|
m_urlData.url.clear();
|
||||||
m_urlData.startColumn = -1;
|
m_urlData.startColumn = -1;
|
||||||
|
m_urlData.urlLength = 0;
|
||||||
|
|
||||||
QTextCursor cursorForUrl = cursor;
|
QTextCursor cursorForUrl = cursor;
|
||||||
cursorForUrl.select(QTextCursor::LineUnderCursor);
|
cursorForUrl.select(QTextCursor::LineUnderCursor);
|
||||||
if (cursorForUrl.hasSelection()) {
|
if (cursorForUrl.hasSelection()) {
|
||||||
const QString line = cursorForUrl.selectedText();
|
const QString line = cursorForUrl.selectedText();
|
||||||
const int cursorCol = cursor.columnNumber();
|
const int cursorCol = cursor.columnNumber();
|
||||||
QRegularExpressionMatchIterator i = m_pattern.globalMatch(line);
|
|
||||||
while (i.hasNext()) {
|
struct {
|
||||||
const QRegularExpressionMatch match = i.next();
|
QRegularExpression &pattern;
|
||||||
const int urlMatchIndex = match.capturedStart();
|
int matchNumber;
|
||||||
const QString url = match.captured(0);
|
QString urlPrefix;
|
||||||
if (urlMatchIndex <= cursorCol && cursorCol <= urlMatchIndex + url.length()) {
|
} RegexUrls[] = {
|
||||||
m_urlData.startColumn = urlMatchIndex;
|
{m_pattern, 0, ""},
|
||||||
m_urlData.url = url;
|
{m_jiraPattern, 2, "https://bugreports.qt.io/browse/"},
|
||||||
break;
|
{m_gerritPattern, 1, "https://codereview.qt-project.org/r/"},
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
for (const auto &r : RegexUrls) {
|
||||||
|
QRegularExpressionMatchIterator i = r.pattern.globalMatch(line);
|
||||||
|
while (i.hasNext()) {
|
||||||
|
const QRegularExpressionMatch match = i.next();
|
||||||
|
const int urlMatchIndex = match.capturedStart(r.matchNumber);
|
||||||
|
const QString url = match.captured(r.matchNumber);
|
||||||
|
if (urlMatchIndex <= cursorCol && cursorCol <= urlMatchIndex + url.length()) {
|
||||||
|
m_urlData.startColumn = urlMatchIndex;
|
||||||
|
m_urlData.url = r.urlPrefix + url;
|
||||||
|
m_urlData.urlLength = url.length();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return m_urlData.startColumn != -1;
|
return m_urlData.startColumn != -1;
|
||||||
@@ -418,7 +438,7 @@ void UrlTextCursorHandler::highlightCurrentContents()
|
|||||||
sel.cursor = currentCursor();
|
sel.cursor = currentCursor();
|
||||||
sel.cursor.setPosition(currentCursor().position()
|
sel.cursor.setPosition(currentCursor().position()
|
||||||
- (currentCursor().columnNumber() - m_urlData.startColumn));
|
- (currentCursor().columnNumber() - m_urlData.startColumn));
|
||||||
sel.cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, m_urlData.url.length());
|
sel.cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, m_urlData.urlLength);
|
||||||
sel.format.setFontUnderline(true);
|
sel.format.setFontUnderline(true);
|
||||||
sel.format.setForeground(linkColor);
|
sel.format.setForeground(linkColor);
|
||||||
sel.format.setUnderlineColor(linkColor);
|
sel.format.setUnderlineColor(linkColor);
|
||||||
|
|||||||
Reference in New Issue
Block a user