Git: Support user configured comment character

Task-number: QTCREATORBUG-28042
Change-Id: I96aea27434ba138637728a7fd7d1450e1eee260a
Reviewed-by: André Hartmann <aha_1980@gmx.de>
This commit is contained in:
Orgad Shaneh
2022-08-18 09:34:06 +03:00
committed by Orgad Shaneh
parent a50afa486a
commit 45aa6a12c4
10 changed files with 58 additions and 21 deletions

View File

@@ -115,6 +115,7 @@ public:
GitSubmitEditorPanelInfo panelInfo;
GitSubmitEditorPanelData panelData;
bool enablePush = false;
QChar commentChar;
QList<StateFilePair> files;

View File

@@ -2780,6 +2780,8 @@ bool GitClient::getCommitData(const FilePath &workingDirectory,
*errorMessage = msgNoCommits(false);
return false;
}
} else {
commitData.commentChar = commentChar(repoDirectory);
}
const StatusResult status = gitStatus(repoDirectory, ShowAll, &output, errorMessage);
switch (status) {
@@ -3550,6 +3552,12 @@ QString GitClient::readConfigValue(const FilePath &workingDirectory, const QStri
return readOneLine(workingDirectory, {"config", configVar});
}
QChar GitClient::commentChar(const Utils::FilePath &workingDirectory)
{
const QString commentChar = readConfigValue(workingDirectory, "core.commentChar");
return commentChar.isEmpty() ? QChar(Constants::DEFAULT_COMMENT_CHAR) : commentChar.at(0);
}
void GitClient::setConfigValue(const FilePath &workingDirectory, const QString &configVar,
const QString &value) const
{

View File

@@ -300,6 +300,7 @@ public:
QString readGitVar(const Utils::FilePath &workingDirectory, const QString &configVar) const;
QString readConfigValue(const Utils::FilePath &workingDirectory, const QString &configVar) const;
QChar commentChar(const Utils::FilePath &workingDirectory);
void setConfigValue(const Utils::FilePath &workingDirectory, const QString &configVar,
const QString &value) const;

View File

@@ -56,6 +56,7 @@ const int OBSOLETE_COMMIT_AGE_IN_DAYS = 90;
const int MAX_OBSOLETE_COMMITS_TO_DISPLAY = 5;
const char EXPAND_BRANCHES[] = "Branches: <Expand>";
const char DEFAULT_COMMENT_CHAR = '#';
} // namespace Constants
} // namespace Git

View File

@@ -269,10 +269,15 @@ void GitEditorWidget::init()
{
VcsBaseEditorWidget::init();
Utils::Id editorId = textDocument()->id();
if (editorId == Git::Constants::GIT_COMMIT_TEXT_EDITOR_ID)
textDocument()->setSyntaxHighlighter(new GitSubmitHighlighter);
else if (editorId == Git::Constants::GIT_REBASE_EDITOR_ID)
textDocument()->setSyntaxHighlighter(new GitRebaseHighlighter);
const bool isCommitEditor = editorId == Git::Constants::GIT_COMMIT_TEXT_EDITOR_ID;
const bool isRebaseEditor = editorId == Git::Constants::GIT_REBASE_EDITOR_ID;
if (!isCommitEditor && !isRebaseEditor)
return;
const QChar commentChar = GitClient::instance()->commentChar(FilePath::fromString(source()));
if (isCommitEditor)
textDocument()->setSyntaxHighlighter(new GitSubmitHighlighter(commentChar));
else if (isRebaseEditor)
textDocument()->setSyntaxHighlighter(new GitRebaseHighlighter(commentChar));
}
void GitEditorWidget::addDiffActions(QMenu *menu, const DiffChunk &chunk)

View File

@@ -27,6 +27,7 @@
#include <utils/qtcassert.h>
#include "gitconstants.h"
#include "githighlighters.h"
namespace Git {
@@ -34,12 +35,12 @@ namespace Internal {
static const char CHANGE_PATTERN[] = "\\b[a-f0-9]{7,40}\\b";
GitSubmitHighlighter::GitSubmitHighlighter(QTextEdit * parent) :
GitSubmitHighlighter::GitSubmitHighlighter(QChar commentChar, QTextEdit * parent) :
TextEditor::SyntaxHighlighter(parent),
m_keywordPattern("^[\\w-]+:")
{
setDefaultTextFormatCategories();
m_hashChar = '#';
m_commentChar = commentChar.isNull() ? QChar(Constants::DEFAULT_COMMENT_CHAR) : commentChar;
QTC_CHECK(m_keywordPattern.isValid());
}
@@ -52,7 +53,7 @@ void GitSubmitHighlighter::highlightBlock(const QString &text)
state = Other;
setCurrentBlockState(state);
return;
} else if (text.startsWith(m_hashChar)) {
} else if (text.startsWith(m_commentChar)) {
setFormat(0, text.size(), formatForCategory(TextEditor::C_COMMENT));
setCurrentBlockState(state);
return;
@@ -83,6 +84,19 @@ void GitSubmitHighlighter::highlightBlock(const QString &text)
}
}
QChar GitSubmitHighlighter::commentChar() const
{
return m_commentChar;
}
void GitSubmitHighlighter::setCommentChar(QChar commentChar)
{
if (m_commentChar == commentChar)
return;
m_commentChar = commentChar;
rehighlight();
}
GitRebaseHighlighter::RebaseAction::RebaseAction(const QString &regexp,
const Format formatCategory)
: exp(regexp),
@@ -117,9 +131,9 @@ static TextEditor::TextStyle styleForFormat(int format)
return C_TEXT;
}
GitRebaseHighlighter::GitRebaseHighlighter(QTextDocument *parent) :
GitRebaseHighlighter::GitRebaseHighlighter(QChar commentChar, QTextDocument *parent) :
TextEditor::SyntaxHighlighter(parent),
m_hashChar('#'),
m_commentChar(commentChar),
m_changeNumberPattern(CHANGE_PATTERN)
{
setTextFormatCategories(Format_Count, styleForFormat);
@@ -139,7 +153,7 @@ GitRebaseHighlighter::GitRebaseHighlighter(QTextDocument *parent) :
void GitRebaseHighlighter::highlightBlock(const QString &text)
{
if (text.startsWith(m_hashChar)) {
if (text.startsWith(m_commentChar)) {
setFormat(0, text.size(), formatForCategory(Format_Comment));
QRegularExpressionMatchIterator it = m_changeNumberPattern.globalMatch(text);
while (it.hasNext()) {

View File

@@ -56,13 +56,15 @@ enum Format {
class GitSubmitHighlighter : public TextEditor::SyntaxHighlighter
{
public:
explicit GitSubmitHighlighter(QTextEdit *parent = nullptr);
explicit GitSubmitHighlighter(QChar commentChar = QChar(), QTextEdit *parent = nullptr);
void highlightBlock(const QString &text) override;
QChar commentChar() const;
void setCommentChar(QChar commentChar);
private:
enum State { None = -1, Header, Other };
const QRegularExpression m_keywordPattern;
QChar m_hashChar;
QChar m_commentChar;
};
// Highlighter for interactive rebase todo. Indicates comments as such
@@ -70,7 +72,7 @@ private:
class GitRebaseHighlighter : public TextEditor::SyntaxHighlighter
{
public:
explicit GitRebaseHighlighter(QTextDocument *parent = nullptr);
explicit GitRebaseHighlighter(QChar commentChar, QTextDocument *parent = nullptr);
void highlightBlock(const QString &text) override;
private:
@@ -81,7 +83,7 @@ private:
Format formatCategory;
RebaseAction(const QString &regexp, const Format formatCategory);
};
const QChar m_hashChar;
const QChar m_commentChar;
const QRegularExpression m_changeNumberPattern;
QList<RebaseAction> m_actions;
};

View File

@@ -26,6 +26,7 @@
#include "gitsubmiteditorwidget.h"
#include "commitdata.h"
#include "gitconstants.h"
#include "githighlighters.h"
#include "logchangedialog.h"
@@ -119,7 +120,7 @@ public:
GitSubmitEditorWidget::GitSubmitEditorWidget() :
m_gitSubmitPanel(new GitSubmitPanel)
{
new GitSubmitHighlighter(descriptionEdit());
m_highlighter = new GitSubmitHighlighter(QChar(), descriptionEdit());
m_emailValidator = new QRegularExpressionValidator(QRegularExpression("[^@ ]+@[^@ ]+\\.[a-zA-Z]+"), this);
const QPixmap error = Utils::Icons::CRITICAL.pixmap();
@@ -176,6 +177,10 @@ void GitSubmitEditorWidget::initialize(const FilePath &repository, const CommitD
insertLeftWidget(logChangeGroupBox);
m_gitSubmitPanel->editGroup->hide();
hideDescription();
} else {
m_highlighter->setCommentChar(data.commentChar);
if (data.commentChar != Constants::DEFAULT_COMMENT_CHAR)
verifyDescription();
}
insertTopWidget(m_gitSubmitPanel);
setPanelData(data.panelData);
@@ -248,14 +253,14 @@ bool GitSubmitEditorWidget::canSubmit(QString *whyNot) const
QString GitSubmitEditorWidget::cleanupDescription(const QString &input) const
{
// We need to manually purge out comment lines starting with
// hash '#' since git does not do that when using -F.
// the comment char (default hash '#') since git does not do that when using -F.
const QChar newLine = '\n';
const QChar hash = '#';
const QChar commentChar = m_highlighter->commentChar();
QString message = input;
for (int pos = 0; pos < message.size(); ) {
const int newLinePos = message.indexOf(newLine, pos);
const int startOfNextLine = newLinePos == -1 ? message.size() : newLinePos + 1;
if (message.at(pos) == hash)
if (message.at(pos) == commentChar)
message.remove(pos, startOfNextLine - pos);
else
pos = startOfNextLine;

View File

@@ -44,6 +44,7 @@ namespace Internal {
class GitSubmitPanel;
class GitSubmitEditorPanelInfo;
class GitSubmitEditorPanelData;
class GitSubmitHighlighter;
class LogChangeWidget;
/* Submit editor widget with 2 additional panes:
@@ -87,6 +88,7 @@ private:
PushAction m_pushAction = NoPush;
GitSubmitPanel *m_gitSubmitPanel;
GitSubmitHighlighter *m_highlighter = nullptr;
LogChangeWidget *m_logChangeWidget = nullptr;
QValidator *m_emailValidator;
QString m_originalAuthor;

View File

@@ -118,9 +118,8 @@ protected:
void insertLeftWidget(QWidget *w);
void addSubmitButtonMenu(QMenu *menu);
void hideDescription();
protected slots:
void descriptionTextChanged();
void verifyDescription();
private:
void updateCheckAllComboBox();
@@ -138,7 +137,6 @@ private:
int checkedFilesCount() const;
void wrapDescription();
void trimDescription();
void verifyDescription();
SubmitEditorWidgetPrivate *d;
};