summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOrgad Shaneh <orgad.shaneh@audiocodes.com>2022-08-18 09:34:06 +0300
committerOrgad Shaneh <orgads@gmail.com>2022-08-26 10:41:27 +0000
commit45aa6a12c47af3310ca31eaae555d9c7bae9391d (patch)
treee09cafd3e61ee649564df68a554c53cb3bafb962
parenta50afa486a656c6a2de7296342ebdda878bbc0c6 (diff)
downloadqt-creator-45aa6a12c47af3310ca31eaae555d9c7bae9391d.tar.gz
Git: Support user configured comment character
Task-number: QTCREATORBUG-28042 Change-Id: I96aea27434ba138637728a7fd7d1450e1eee260a Reviewed-by: André Hartmann <aha_1980@gmx.de>
-rw-r--r--src/plugins/git/commitdata.h1
-rw-r--r--src/plugins/git/gitclient.cpp8
-rw-r--r--src/plugins/git/gitclient.h1
-rw-r--r--src/plugins/git/gitconstants.h1
-rw-r--r--src/plugins/git/giteditor.cpp13
-rw-r--r--src/plugins/git/githighlighters.cpp26
-rw-r--r--src/plugins/git/githighlighters.h10
-rw-r--r--src/plugins/git/gitsubmiteditorwidget.cpp13
-rw-r--r--src/plugins/git/gitsubmiteditorwidget.h2
-rw-r--r--src/plugins/vcsbase/submiteditorwidget.h4
10 files changed, 58 insertions, 21 deletions
diff --git a/src/plugins/git/commitdata.h b/src/plugins/git/commitdata.h
index efa94f633c..28ab4eda15 100644
--- a/src/plugins/git/commitdata.h
+++ b/src/plugins/git/commitdata.h
@@ -115,6 +115,7 @@ public:
GitSubmitEditorPanelInfo panelInfo;
GitSubmitEditorPanelData panelData;
bool enablePush = false;
+ QChar commentChar;
QList<StateFilePair> files;
diff --git a/src/plugins/git/gitclient.cpp b/src/plugins/git/gitclient.cpp
index 2a27ab546e..0c3365e5ad 100644
--- a/src/plugins/git/gitclient.cpp
+++ b/src/plugins/git/gitclient.cpp
@@ -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
{
diff --git a/src/plugins/git/gitclient.h b/src/plugins/git/gitclient.h
index 75f38bc625..a428df723b 100644
--- a/src/plugins/git/gitclient.h
+++ b/src/plugins/git/gitclient.h
@@ -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;
diff --git a/src/plugins/git/gitconstants.h b/src/plugins/git/gitconstants.h
index 38a8a104ab..920b2ed803 100644
--- a/src/plugins/git/gitconstants.h
+++ b/src/plugins/git/gitconstants.h
@@ -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
diff --git a/src/plugins/git/giteditor.cpp b/src/plugins/git/giteditor.cpp
index 8fd16e2607..52e59a5cfc 100644
--- a/src/plugins/git/giteditor.cpp
+++ b/src/plugins/git/giteditor.cpp
@@ -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)
diff --git a/src/plugins/git/githighlighters.cpp b/src/plugins/git/githighlighters.cpp
index 37c9af8699..a317a566d3 100644
--- a/src/plugins/git/githighlighters.cpp
+++ b/src/plugins/git/githighlighters.cpp
@@ -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()) {
diff --git a/src/plugins/git/githighlighters.h b/src/plugins/git/githighlighters.h
index 004e8b72cd..8312916b52 100644
--- a/src/plugins/git/githighlighters.h
+++ b/src/plugins/git/githighlighters.h
@@ -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;
};
diff --git a/src/plugins/git/gitsubmiteditorwidget.cpp b/src/plugins/git/gitsubmiteditorwidget.cpp
index a9ff043526..73f2d721a7 100644
--- a/src/plugins/git/gitsubmiteditorwidget.cpp
+++ b/src/plugins/git/gitsubmiteditorwidget.cpp
@@ -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;
diff --git a/src/plugins/git/gitsubmiteditorwidget.h b/src/plugins/git/gitsubmiteditorwidget.h
index 3000417082..1fba4265a8 100644
--- a/src/plugins/git/gitsubmiteditorwidget.h
+++ b/src/plugins/git/gitsubmiteditorwidget.h
@@ -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;
diff --git a/src/plugins/vcsbase/submiteditorwidget.h b/src/plugins/vcsbase/submiteditorwidget.h
index 1ad8477b0d..968158dac9 100644
--- a/src/plugins/vcsbase/submiteditorwidget.h
+++ b/src/plugins/vcsbase/submiteditorwidget.h
@@ -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;
};