diff options
Diffstat (limited to 'src/plugins/git/githighlighters.cpp')
-rw-r--r-- | src/plugins/git/githighlighters.cpp | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/plugins/git/githighlighters.cpp b/src/plugins/git/githighlighters.cpp index d223a5e5240..2f00c9f2db3 100644 --- a/src/plugins/git/githighlighters.cpp +++ b/src/plugins/git/githighlighters.cpp @@ -38,6 +38,8 @@ namespace Git { namespace Internal { +static const char CHANGE_PATTERN[] = "\\b[a-f0-9]{7,40}\\b"; + // Retrieve the comment char format from the text editor. static QTextCharFormat commentFormat() { @@ -104,5 +106,59 @@ void GitSubmitHighlighter::highlightBlock(const QString &text) } } +GitRebaseHighlighter::RebaseAction::RebaseAction(const QString ®exp, + const TextEditor::FontSettings &settings, + TextEditor::TextStyle category) + : exp(regexp) +{ + format = settings.toTextCharFormat(category); +} + +GitRebaseHighlighter::GitRebaseHighlighter(TextEditor::BaseTextDocument *parent) : + TextEditor::SyntaxHighlighter(parent), + m_hashChar(QLatin1Char('#')), + m_changeNumberPattern(QLatin1String(CHANGE_PATTERN)) +{ + const TextEditor::FontSettings settings = TextEditor::TextEditorSettings::instance()->fontSettings(); + m_commentFormat = settings.toTextCharFormat(TextEditor::C_COMMENT); + m_changeFormat = settings.toTextCharFormat(TextEditor::C_DOXYGEN_COMMENT); + m_descFormat = settings.toTextCharFormat(TextEditor::C_STRING); + m_actions << RebaseAction(QLatin1String("^(p|pick)\\b"), settings, TextEditor::C_KEYWORD); + m_actions << RebaseAction(QLatin1String("^(r|reword)\\b"), settings, TextEditor::C_FIELD); + m_actions << RebaseAction(QLatin1String("^(e|edit)\\b"), settings, TextEditor::C_TYPE); + m_actions << RebaseAction(QLatin1String("^(s|squash)\\b"), settings, TextEditor::C_ENUMERATION); + m_actions << RebaseAction(QLatin1String("^(f|fixup)\\b"), settings, TextEditor::C_NUMBER); + m_actions << RebaseAction(QLatin1String("^(x|exec)\\b"), settings, TextEditor::C_LABEL); +} + +void GitRebaseHighlighter::highlightBlock(const QString &text) +{ + if (text.startsWith(m_hashChar)) { + setFormat(0, text.size(), m_commentFormat); + int changeIndex = 0; + while ((changeIndex = m_changeNumberPattern.indexIn(text, changeIndex)) != -1) { + const int changeLen = m_changeNumberPattern.matchedLength(); + setFormat(changeIndex, changeLen, m_changeFormat); + changeIndex += changeLen; + } + return; + } + + foreach (const RebaseAction &action, m_actions) { + if (action.exp.indexIn(text) != -1) { + const int len = action.exp.matchedLength(); + setFormat(0, len, action.format); + const int changeIndex = m_changeNumberPattern.indexIn(text, len); + if (changeIndex != -1) { + const int changeLen = m_changeNumberPattern.matchedLength(); + const int descStart = changeIndex + changeLen + 1; + setFormat(changeIndex, changeLen, m_changeFormat); + setFormat(descStart, text.size() - descStart, m_descFormat); + } + break; + } + } +} + } // namespace Internal } // namespace Git |