aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/fossil/fossilcommitwidget.cpp
blob: 72fba5ef86f985e49868610d92ea51c700132d02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright (c) 2018 Artur Shepilko
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "fossilcommitwidget.h"

#include "branchinfo.h"
#include "fossiltr.h"

#include <texteditor/texteditorsettings.h>
#include <texteditor/fontsettings.h>
#include <texteditor/texteditorconstants.h>

#include <utils/completingtextedit.h>
#include <utils/filepath.h>
#include <utils/infolabel.h>
#include <utils/layoutbuilder.h>
#include <utils/qtcassert.h>

#include <QCheckBox>
#include <QDir>
#include <QLineEdit>
#include <QRegularExpression>
#include <QSyntaxHighlighter>

using namespace Utils;

namespace Fossil {
namespace Internal {

// Retrieve the comment char format from the text editor.
static QTextCharFormat commentFormat()
{
    const TextEditor::FontSettings settings = TextEditor::TextEditorSettings::instance()->fontSettings();
    return settings.toTextCharFormat(TextEditor::C_COMMENT);
}

// Highlighter for Fossil submit messages.
// Marks up [ticket-id] fields in the message.
class FossilSubmitHighlighter : QSyntaxHighlighter
{
public:
    explicit FossilSubmitHighlighter(CompletingTextEdit *parent);
    void highlightBlock(const QString &text) final;

private:
    const QTextCharFormat m_commentFormat;
    const QRegularExpression m_keywordPattern;
};

FossilSubmitHighlighter::FossilSubmitHighlighter(CompletingTextEdit *parent) : QSyntaxHighlighter(parent),
    m_commentFormat(commentFormat()),
    m_keywordPattern("\\[([0-9a-f]{5,40})\\]")
{
    QTC_CHECK(m_keywordPattern.isValid());
}

void FossilSubmitHighlighter::highlightBlock(const QString &text)
{
    // Fossil commit message allows listing of [ticket-id],
    // where ticket-id is a partial SHA1.
    // Match the ticket-ids and highlight them for convenience.

    // Format keywords
    QRegularExpressionMatchIterator i = m_keywordPattern.globalMatch(text);
    while (i.hasNext()) {
        const QRegularExpressionMatch keywordMatch = i.next();
        QTextCharFormat charFormat = format(0);
        charFormat.setFontItalic(true);
        setFormat(keywordMatch.capturedStart(0), keywordMatch.capturedLength(0), charFormat);
    }
}


FossilCommitWidget::FossilCommitWidget() : m_commitPanel(new QWidget)
{
    m_localRootLineEdit = new QLineEdit;
    m_localRootLineEdit->setFocusPolicy(Qt::NoFocus);
    m_localRootLineEdit->setReadOnly(true);

    m_currentBranchLineEdit = new QLineEdit;
    m_currentBranchLineEdit->setFocusPolicy(Qt::NoFocus);
    m_currentBranchLineEdit->setReadOnly(true);

    m_currentTagsLineEdit = new QLineEdit;
    m_currentTagsLineEdit->setFocusPolicy(Qt::NoFocus);
    m_currentTagsLineEdit->setReadOnly(true);

    m_branchLineEdit = new QLineEdit;

    m_invalidBranchLabel = new InfoLabel;
    m_invalidBranchLabel->setMinimumSize(QSize(50, 20));
    m_invalidBranchLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);
    m_invalidBranchLabel->setType(InfoLabel::Error);

    m_isPrivateCheckBox = new QCheckBox(Tr::tr("Private"));
    m_isPrivateCheckBox->setToolTip("<html>" + Tr::tr("Create a private check-in that is never synced. "
                                       "Children of private check-ins are automatically private. "
                                       "Private check-ins are not pushed to the remote repository by default."));

    m_tagsLineEdit = new QLineEdit;
    m_tagsLineEdit->setToolTip(Tr::tr("Tag names to apply; comma-separated."));

    m_authorLineEdit = new QLineEdit;

    using namespace Layouting;

    Column {
        Group {
            title(Tr::tr("Current Information")),
            Form {
                Tr::tr("Local root:"), m_localRootLineEdit,
                Tr::tr("Branch:"), m_currentBranchLineEdit,
                Tr::tr("Tags:"), m_currentTagsLineEdit
            }
        },
        Group {
            title(Tr::tr("Commit Information")),
            Grid {
                Tr::tr("New branch:"), m_branchLineEdit,  m_invalidBranchLabel, m_isPrivateCheckBox, br,
                Tr::tr("Tags:"), m_tagsLineEdit, br,
                Tr::tr("Author:"),  m_authorLineEdit, st,
            }
        },
        noMargin
    }.attachTo(m_commitPanel);

    insertTopWidget(m_commitPanel);
    new FossilSubmitHighlighter(descriptionEdit());
    m_branchValidator = new QRegularExpressionValidator(QRegularExpression("[^\\n]*"), this);

    connect(m_branchLineEdit, &QLineEdit::textChanged,
            this, &FossilCommitWidget::branchChanged);
}

void FossilCommitWidget::setFields(const FilePath &repoPath, const BranchInfo &branch,
                                   const QStringList &tags, const QString &userName)
{
    m_localRootLineEdit->setText(repoPath.toUserOutput());
    m_currentBranchLineEdit->setText(branch.name);
    m_currentTagsLineEdit->setText(tags.join(", "));
    m_authorLineEdit->setText(userName);

    branchChanged();
}

QString FossilCommitWidget::newBranch() const
{
    return m_branchLineEdit->text().trimmed();
}

QStringList FossilCommitWidget::tags() const
{
    QString tagsText = m_tagsLineEdit->text().trimmed();
    if (tagsText.isEmpty())
        return {};

    return tagsText.replace(',', ' ').split(' ', Qt::SkipEmptyParts);
}

QString FossilCommitWidget::committer() const
{
    return m_authorLineEdit->text();
}

bool FossilCommitWidget::isPrivateOptionEnabled() const
{
    return m_isPrivateCheckBox->isChecked();
}

bool FossilCommitWidget::canSubmit(QString *whyNot) const
{
    QString message = cleanupDescription(descriptionText()).trimmed();

    if (m_invalidBranchLabel->isVisible() || message.isEmpty()) {
        if (whyNot)
            *whyNot = Tr::tr("Message check failed.");
        return false;
    }

    return VcsBase::SubmitEditorWidget::canSubmit();
}

void FossilCommitWidget::branchChanged()
{
    m_invalidBranchLabel->setVisible(!isValidBranch());

    updateSubmitAction();
}

bool FossilCommitWidget::isValidBranch() const
{
    int pos = m_branchLineEdit->cursorPosition();
    QString text = m_branchLineEdit->text();
    return m_branchValidator->validate(text, pos) == QValidator::Acceptable;
}

} // namespace Internal
} // namespace Fossil