forked from qt-creator/qt-creator
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: Tobias Hunger <tobias.hunger@digia.com>
This commit is contained in:
committed by
Orgad Shaneh
parent
3e022beb12
commit
3f1901cb1f
@@ -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 ®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
|
||||
|
||||
@@ -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 ®exp, 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
|
||||
|
||||
Reference in New Issue
Block a user