From 6b2ff42f0dde7eda81e93efd6ef7691f1e363658 Mon Sep 17 00:00:00 2001 From: Andre Hartmann Date: Wed, 10 Jan 2024 09:42:13 +0100 Subject: [PATCH] GitPlugin: Add color to diff description text MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I19d8bbaf77d0b6070b8300ca9fc0868579cab4e8 Reviewed-by: Jarek Kobus Reviewed-by: André Hartmann --- src/plugins/git/gitclient.cpp | 111 ++++++++++++++++++++++++++++++---- 1 file changed, 98 insertions(+), 13 deletions(-) diff --git a/src/plugins/git/gitclient.cpp b/src/plugins/git/gitclient.cpp index 10cf2dcecdc..b7f14ec910b 100644 --- a/src/plugins/git/gitclient.cpp +++ b/src/plugins/git/gitclient.cpp @@ -19,6 +19,12 @@ #include #include +#include + +#include +#include +#include + #include #include #include @@ -41,11 +47,6 @@ #include #include -#include - -#include -#include - #include #include #include @@ -77,11 +78,100 @@ const char showFormatC[] = using namespace Core; using namespace DiffEditor; using namespace Tasking; +using namespace TextEditor; using namespace Utils; using namespace VcsBase; namespace Git::Internal { +static const QRegularExpression s_commitPattern("commit ([a-f0-9]{7,40})(.*)"); +static const QRegularExpression s_authorPattern("(?:Author|Committer): (.*>), (.*)"); +static const QRegularExpression s_branchesPattern("(?:Branches|Precedes|Follows): (.*)"); +static const QRegularExpression s_keywordPattern("^[\\w-]+:"); +static const QRegularExpression s_changeNumberPattern("\\b[a-f0-9]{7,40}\\b"); + +static QColor colorForStyle(TextStyle style) +{ + const ColorScheme &scheme = TextEditorSettings::fontSettings().colorScheme(); + const QColor color = scheme.formatFor(style).foreground(); + if (color.isValid()) + return color; + return scheme.formatFor(C_TEXT).foreground(); +} + +class GitDescriptionHighlighter : public SyntaxHighlighter +{ +public: + void highlightBlock(const QString &text) final + { + const QRegularExpressionMatch commitMatch = s_commitPattern.match(text); + if (commitMatch.hasMatch()) { + m_state = Commit; + setStyle(commitMatch.capturedStart(1), commitMatch.capturedLength(1), C_LOG_COMMIT_HASH); + setStyle(commitMatch.capturedStart(2), commitMatch.capturedLength(2), C_LOG_DECORATION); + return; + } + + const QRegularExpressionMatch authorMatch = s_authorPattern.match(text); + if (authorMatch.hasMatch()) { + m_state = Author; + setStyle(authorMatch.capturedStart(1), authorMatch.capturedLength(1), C_LOG_AUTHOR_NAME); + setStyle(authorMatch.capturedStart(2), authorMatch.capturedLength(2), C_LOG_COMMIT_DATE); + return; + } + + const QRegularExpressionMatch branchesMatch = s_branchesPattern.match(text); + if (branchesMatch.hasMatch()) { + m_state = Branches; + setStyle(branchesMatch.capturedStart(1), branchesMatch.capturedLength(1), C_LOG_DECORATION); + return; + } + + if (m_state == Branches) { + if (text.isEmpty()) + m_state = Subject; + else + setStyle(0, text.length(), C_LOG_DECORATION); + return; + } + + if ((m_state == Subject) && !text.isEmpty()) { + setStyle(0, text.length(), C_LOG_COMMIT_SUBJECT); + m_state = Body; + return; + } + + setStyle(0, text.length(), C_TEXT); + + const QRegularExpressionMatch keywordMatch = s_keywordPattern.match(text); + if (keywordMatch.hasMatch() && keywordMatch.capturedStart() == 0) { + QTextCharFormat charFormat = format(0); + charFormat.setFontItalic(true); + setFormat(0, keywordMatch.capturedLength(), charFormat); + } + + QRegularExpressionMatchIterator it = s_changeNumberPattern.globalMatch(text); + while (it.hasNext()) { + const QRegularExpressionMatch match = it.next(); + setStyle(match.capturedStart(), match.capturedLength(), C_LOG_COMMIT_HASH); + } + } + +private: + void setStyle(int start, int size, TextStyle style) + { + setFormat(start, size, colorForStyle(style)); + } + + enum State { + Commit, + Author, + Branches, + Subject, + Body + } m_state = Commit; +}; + static QString branchesDisplay(const QString &prefix, QStringList *branches, bool *first) { const int limit = 12; @@ -258,6 +348,7 @@ GitDiffEditorController::GitDiffEditorController(IDocument *document, GitBaseDiffEditorController::GitBaseDiffEditorController(IDocument *document) : VcsBaseDiffEditorController(document) { + setDescriptionSyntaxHighlighterCreator([] { return new GitDescriptionHighlighter; }); setDisplayName("Git Diff"); } @@ -635,14 +726,8 @@ static bool gitHasRgbColors() static QString logColorName(TextEditor::TextStyle style) { - using namespace TextEditor; - - const ColorScheme &scheme = TextEditorSettings::fontSettings().colorScheme(); - QColor color = scheme.formatFor(style).foreground(); - if (!color.isValid()) - color = scheme.formatFor(C_TEXT).foreground(); - return color.name(); -}; + return colorForStyle(style).name(); +} class GitLogArgumentsWidget : public BaseGitLogArgumentsWidget {