Commit 3f1901cb authored by Orgad Shaneh's avatar Orgad Shaneh Committed by Orgad Shaneh

Git: Provide a real highlighter for interactive rebase

* Kate highlighter doesn't have enough colors
* It works out of the box only on Windows
* New editor support links

Change-Id: I09bbaef08574660e535ccb86c2c460d5976fc2e3
Reviewed-by: default avatarTobias Hunger <tobias.hunger@digia.com>
parent 3e022beb
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE language SYSTEM "language.dtd">
<language name="Git Rebase" version="1.00" kateversion="2.4" section="Other" extensions="git-rebase-todo" license="LGPL" mimetype="text/vnd.git.rebase" author="Orgad Shaneh (orgads@gmail.com)">
<highlighting>
<list name="pick">
<item>p</item>
<item>pick</item>
</list>
<list name="reword">
<item>r</item>
<item>reword</item>
</list>
<list name="edit">
<item>e</item>
<item>edit</item>
</list>
<list name="squash">
<item>s</item>
<item>squash</item>
</list>
<list name="fixup">
<item>f</item>
<item>fixup</item>
</list>
<list name="exec">
<item>x</item>
<item>exec</item>
</list>
<contexts>
<context attribute="Normal Text" lineEndContext="#pop" name="Normal">
<DetectChar char="#" attribute="Comment" context="Comment" column="0"/>
<keyword attribute="Pick" context="Commit" String="pick" />
<keyword attribute="Reword" context="Commit" String="reword" />
<keyword attribute="Edit" context="Commit" String="edit" />
<keyword attribute="Squash" context="Commit" String="squash" />
<keyword attribute="Fixup" context="Commit" String="fixup" />
<keyword attribute="Exec" context="Commit" String="exec" />
</context>
<context attribute="Commit" lineEndContext="#pop" name="Commit">
<RegExpr attribute="Commit" context="Summary" String="[0-9a-f]{7,40}" />
</context>
<context attribute="Summary" lineEndContext="#pop" name="Summary" />
<context attribute="Comment" lineEndContext="#pop" name="Comment">
<RegExpr attribute="Commit" context="#stay" String="[0-9a-f]{7,40}" />
</context>
</contexts>
<itemDatas>
<itemData name="Normal Text" defStyleNum="dsNormal"/>
<itemData name="Pick" defStyleNum="dsKeyword"/>
<itemData name="Reword" defStyleNum="dsDecVal"/>
<itemData name="Edit" defStyleNum="dsDataType"/>
<itemData name="Squash" defStyleNum="dsChar"/>
<itemData name="Fixup" defStyleNum="dsBaseN"/>
<itemData name="Exec" defStyleNum="dsOthers"/>
<itemData name="Commit" defStyleNum="dsFunction"/>
<itemData name="Summary" defStyleNum="dsString"/>
<itemData name="Comment" defStyleNum="dsComment"/>
</itemDatas>
</highlighting>
<general>
<comments>
<comment name="singleLine" start="#"/>
</comments>
</general>
</language>
......@@ -6,4 +6,9 @@
<glob pattern="COMMIT_MSG"/>
<glob pattern="COMMIT_EDITMSG"/>
</mime-type>
<mime-type type="text/vnd.qtcreator.git.rebase">
<sub-class-of type="text/plain"/>
<comment>Git Commit File</comment>
<glob pattern="git-rebase-todo"/>
</mime-type>
</mime-info>
......@@ -50,6 +50,9 @@ const char C_GIT_DIFF_EDITOR[] = "Git Diff Editor";
const char GIT_COMMIT_TEXT_EDITOR_ID[] = "Git Commit Editor";
const char GIT_COMMIT_TEXT_EDITOR_DISPLAY_NAME[] = QT_TRANSLATE_NOOP("VCS", "Git Commit Editor");
const char C_GIT_COMMIT_TEXT_EDITOR[] = "Git Commit Editor";
const char GIT_REBASE_EDITOR_ID[] = "Git Rebase Editor";
const char GIT_REBASE_EDITOR_DISPLAY_NAME[] = QT_TRANSLATE_NOOP("VCS", "Git Rebase Editor");
const char C_GIT_REBASE_EDITOR[] = "Git Rebase Editor";
const char C_GITSUBMITEDITOR[] = "Git Submit Editor";
const char GITSUBMITEDITOR_ID[] = "Git Submit Editor";
......
......@@ -232,14 +232,19 @@ void GitEditor::revertChange()
void GitEditor::init()
{
VcsBase::VcsBaseEditorWidget::init();
if (editor()->id() == Git::Constants::GIT_COMMIT_TEXT_EDITOR_ID)
Core::Id editorId = editor()->id();
if (editorId == Git::Constants::GIT_COMMIT_TEXT_EDITOR_ID)
new GitSubmitHighlighter(baseTextDocument().data());
else if (editorId == Git::Constants::GIT_REBASE_EDITOR_ID)
new GitRebaseHighlighter(baseTextDocument().data());
}
bool GitEditor::open(QString *errorString, const QString &fileName, const QString &realFileName)
{
bool res = VcsBaseEditorWidget::open(errorString, fileName, realFileName);
if (editor()->id() == Git::Constants::GIT_COMMIT_TEXT_EDITOR_ID) {
Core::Id editorId = editor()->id();
if (editorId == Git::Constants::GIT_COMMIT_TEXT_EDITOR_ID
|| editorId == Git::Constants::GIT_REBASE_EDITOR_ID) {
QFileInfo fi(fileName);
setSource(GitPlugin::instance()->gitClient()->findRepositoryForGitDir(fi.absolutePath()));
}
......@@ -298,7 +303,8 @@ QString GitEditor::revisionSubject(const QTextBlock &inBlock) const
bool GitEditor::supportChangeLinks() const
{
return VcsBaseEditorWidget::supportChangeLinks()
|| (editor()->id() == Git::Constants::GIT_COMMIT_TEXT_EDITOR_ID);
|| (editor()->id() == Git::Constants::GIT_COMMIT_TEXT_EDITOR_ID)
|| (editor()->id() == Git::Constants::GIT_REBASE_EDITOR_ID);
}
} // namespace Internal
......
......@@ -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 &regexp,
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
......@@ -33,6 +33,10 @@
#include <texteditor/syntaxhighlighter.h>
#include <texteditor/texteditorconstants.h>
namespace TextEditor {
class FontSettings;
}
namespace Git {
namespace Internal {
......@@ -54,6 +58,31 @@ private:
QChar m_hashChar;
};
// Highlighter for interactive rebase todo. Indicates comments as such
// (retrieving the format from the text editor) and marks up keywords
class GitRebaseHighlighter : public TextEditor::SyntaxHighlighter
{
public:
explicit GitRebaseHighlighter(TextEditor::BaseTextDocument *parent);
void highlightBlock(const QString &text);
private:
class RebaseAction
{
public:
mutable QRegExp exp;
QTextCharFormat format;
RebaseAction(const QString &regexp, const TextEditor::FontSettings &settings,
TextEditor::TextStyle category);
};
QTextCharFormat m_commentFormat;
QTextCharFormat m_changeFormat;
QTextCharFormat m_descFormat;
const QChar m_hashChar;
QRegExp m_changeNumberPattern;
QList<RebaseAction> m_actions;
};
} // namespace Internal
} // namespace Git
......
......@@ -111,6 +111,11 @@ static const VcsBase::VcsBaseEditorParameters editorParameters[] = {
Git::Constants::GIT_COMMIT_TEXT_EDITOR_DISPLAY_NAME,
Git::Constants::C_GIT_COMMIT_TEXT_EDITOR,
"text/vnd.qtcreator.git.commit"},
{ VcsBase::OtherContent,
Git::Constants::GIT_REBASE_EDITOR_ID,
Git::Constants::GIT_REBASE_EDITOR_DISPLAY_NAME,
Git::Constants::C_GIT_REBASE_EDITOR,
"text/vnd.qtcreator.git.rebase"},
};
// Utility to find a parameter set by type
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment