Skip to content

Commit

Permalink
Implement multi-select functionality for commit messages.
Browse files Browse the repository at this point in the history
 Add checkbox support for selecting multiple commit messages.
  • Loading branch information
soramimi committed Sep 26, 2024
1 parent 374e928 commit 0c4cd1c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 22 deletions.
10 changes: 7 additions & 3 deletions src/CommitDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,14 @@ void CommitDialog::on_pushButton_generate_with_ai_clicked()
dlg.show();
dlg.generate(diff_);
if (dlg.exec() == QDialog::Accepted) {
QString text = dlg.message();
if (!text.isEmpty()) {
QStringList list = dlg.message();
if (!list.isEmpty()) {
QString text;
for (QString const &line : list) {
text.append(line);
text.append('\n');
}
setText(text);
QString diff = dlg.diffText();
}
}
ui->plainTextEdit->setFocus();
Expand Down
48 changes: 46 additions & 2 deletions src/GenerateCommitMessageDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
struct GenerateCommitMessageDialog::Private {
QString diff;
GenerateCommitMessageThread generator;
QStringList checked_items;
};

GenerateCommitMessageDialog::GenerateCommitMessageDialog(QWidget *parent, QString const &model_name)
Expand Down Expand Up @@ -38,7 +39,18 @@ void GenerateCommitMessageDialog::generate(QString const &diff)

QApplication::setOverrideCursor(Qt::WaitCursor);

m->checked_items = message();

ui->listWidget->clear();

ui->listWidget->addItems(m->checked_items);
for (int i = 0; i < ui->listWidget->count(); i++) {
auto *item = ui->listWidget->item(i);
if (m->checked_items.contains(item->text())) {
item->setCheckState(Qt::Checked);
}
}

ui->pushButton_regenerate->setEnabled(false);

m->generator.request(CommitMessageGenerator::CommitMessage, diff);
Expand All @@ -49,9 +61,17 @@ QString GenerateCommitMessageDialog::diffText() const
return m->diff;
}

QString GenerateCommitMessageDialog::message() const
QStringList GenerateCommitMessageDialog::message() const
{
return ui->listWidget->currentItem()->text();
QStringList list;
int n = ui->listWidget->count();
for (int i = 0; i < n; i++) {
auto *item = ui->listWidget->item(i);
if (item->checkState() == Qt::Checked) {
list.append(item->text());
}
}
return list;
}

void GenerateCommitMessageDialog::on_pushButton_regenerate_clicked()
Expand All @@ -65,7 +85,13 @@ void GenerateCommitMessageDialog::onReady(const GeneratedCommitMessage &result)
QApplication::restoreOverrideCursor();

if (result) {
int i = ui->listWidget->count();
ui->listWidget->addItems(result.messages);
int n = ui->listWidget->count();;
while (i < n) {
ui->listWidget->item(i)->setCheckState(Qt::Unchecked);
i++;
}
ui->listWidget->setCurrentRow(0);
} else {
QString text = result.error_status + "\n\n" + result.error_message;
Expand All @@ -75,5 +101,23 @@ void GenerateCommitMessageDialog::onReady(const GeneratedCommitMessage &result)
ui->pushButton_regenerate->setEnabled(true);
}

void GenerateCommitMessageDialog::on_listWidget_itemDoubleClicked(QListWidgetItem *item)
{
item->setCheckState(Qt::Checked);
done(QDialog::Accepted);
}

void GenerateCommitMessageDialog::done(int stat)
{
if (stat == QDialog::Accepted) {
QStringList list = message();
if (list.empty()) {
auto *item = ui->listWidget->currentItem();
if (item) {
item->setCheckState(Qt::Checked);
}
}
}
QDialog::done(stat);
}

9 changes: 8 additions & 1 deletion src/GenerateCommitMessageDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace Ui {
class GenerateCommitMessageDialog;
}

class QListWidgetItem;

class GenerateCommitMessageDialog : public QDialog {
Q_OBJECT
private:
Expand All @@ -20,10 +22,15 @@ class GenerateCommitMessageDialog : public QDialog {
~GenerateCommitMessageDialog();
void generate(QString const &diff);
QString diffText() const;
QString message() const;
QStringList message() const;
private slots:
void on_pushButton_regenerate_clicked();
void onReady(GeneratedCommitMessage const &list);
void on_listWidget_itemDoubleClicked(QListWidgetItem *item);

// QDialog interface
public slots:
void done(int stat);
};

#endif // GENERATECOMMITMESSAGEDIALOG_H
16 changes: 0 additions & 16 deletions src/GenerateCommitMessageDialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,5 @@
</hint>
</hints>
</connection>
<connection>
<sender>listWidget</sender>
<signal>doubleClicked(QModelIndex)</signal>
<receiver>GenerateCommitMessageDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>219</x>
<y>84</y>
</hint>
<hint type="destinationlabel">
<x>225</x>
<y>169</y>
</hint>
</hints>
</connection>
</connections>
</ui>

0 comments on commit 0c4cd1c

Please sign in to comment.