feat: Add default option for custom instructions

This commit is contained in:
Petr Mironychev
2025-11-17 15:54:35 +01:00
parent 944e7fb00a
commit f3aa706227
5 changed files with 52 additions and 1 deletions

View File

@ -20,6 +20,7 @@
#include "AddCustomInstructionDialog.hpp"
#include "QodeAssisttr.h"
#include <QCheckBox>
#include <QDialogButtonBox>
#include <QFormLayout>
#include <QLabel>
@ -46,6 +47,7 @@ AddCustomInstructionDialog::AddCustomInstructionDialog(const CustomInstruction &
setupUi();
m_nameEdit->setText(instruction.name);
m_bodyEdit->setPlainText(instruction.body);
m_defaultCheckBox->setChecked(instruction.isDefault);
resize(500, 400);
}
@ -70,6 +72,11 @@ void AddCustomInstructionDialog::setupUi()
Tr::tr("Enter the refactoring instruction that will be sent to the LLM..."));
mainLayout->addWidget(m_bodyEdit);
m_defaultCheckBox = new QCheckBox(Tr::tr("Set as default instruction"), this);
m_defaultCheckBox->setToolTip(
Tr::tr("This instruction will be automatically selected when opening Quick Refactor dialog"));
mainLayout->addWidget(m_defaultCheckBox);
QDialogButtonBox *buttonBox
= new QDialogButtonBox(QDialogButtonBox::Save | QDialogButtonBox::Cancel, this);
@ -95,6 +102,7 @@ CustomInstruction AddCustomInstructionDialog::getInstruction() const
CustomInstruction instruction = m_instruction;
instruction.name = m_nameEdit->text().trimmed();
instruction.body = m_bodyEdit->toPlainText().trimmed();
instruction.isDefault = m_defaultCheckBox->isChecked();
return instruction;
}

View File

@ -26,6 +26,7 @@
class QLineEdit;
class QPlainTextEdit;
class QCheckBox;
namespace QodeAssist {
@ -45,6 +46,7 @@ private:
QLineEdit *m_nameEdit;
QPlainTextEdit *m_bodyEdit;
QCheckBox *m_defaultCheckBox;
CustomInstruction m_instruction;
};

View File

@ -93,6 +93,7 @@ bool CustomInstructionsManager::loadInstructions()
instruction.id = obj["id"].toString();
instruction.name = obj["name"].toString();
instruction.body = obj["body"].toString();
instruction.isDefault = obj["default"].toBool(false);
if (instruction.id.isEmpty() || instruction.name.isEmpty()) {
LOG_MESSAGE(QString("Invalid instruction in file: %1").arg(fileInfo.fileName()));
@ -132,6 +133,33 @@ bool CustomInstructionsManager::saveInstruction(const CustomInstruction &instruc
}
}
// If this instruction is marked as default, remove default flag from all others
if (newInstruction.isDefault) {
for (int i = 0; i < m_instructions.size(); ++i) {
if (m_instructions[i].id != newInstruction.id && m_instructions[i].isDefault) {
m_instructions[i].isDefault = false;
// Update the file for this instruction
QString sanitizedName = m_instructions[i].name;
sanitizedName.replace(' ', '_');
QString otherFileName = QString("%1/%2_%3.json")
.arg(getInstructionsDirectory(), sanitizedName, m_instructions[i].id);
QJsonObject otherObj;
otherObj["id"] = m_instructions[i].id;
otherObj["name"] = m_instructions[i].name;
otherObj["body"] = m_instructions[i].body;
otherObj["default"] = false;
otherObj["version"] = "0.1";
QFile otherFile(otherFileName);
if (otherFile.open(QIODevice::WriteOnly)) {
otherFile.write(QJsonDocument(otherObj).toJson(QJsonDocument::Indented));
}
}
}
}
int existingIndex = -1;
for (int i = 0; i < m_instructions.size(); ++i) {
if (m_instructions[i].id == newInstruction.id) {
@ -144,6 +172,7 @@ bool CustomInstructionsManager::saveInstruction(const CustomInstruction &instruc
obj["id"] = newInstruction.id;
obj["name"] = newInstruction.name;
obj["body"] = newInstruction.body;
obj["default"] = newInstruction.isDefault;
obj["version"] = "0.1";
QJsonDocument doc(obj);

View File

@ -30,6 +30,7 @@ struct CustomInstruction
QString id;
QString name;
QString body;
bool isDefault = false;
};
class CustomInstructionsManager : public QObject

View File

@ -320,9 +320,16 @@ void QuickRefactorDialog::loadCustomCommands()
const QVector<CustomInstruction> &instructions = manager.instructions();
QStringList instructionNames;
for (const CustomInstruction &instruction : instructions) {
int defaultInstructionIndex = -1;
for (int i = 0; i < instructions.size(); ++i) {
const CustomInstruction &instruction = instructions[i];
m_commandsComboBox->addItem(instruction.name, instruction.id);
instructionNames.append(instruction.name);
if (instruction.isDefault) {
defaultInstructionIndex = i + 1;
}
}
if (m_commandsComboBox->completer()) {
@ -330,6 +337,10 @@ void QuickRefactorDialog::loadCustomCommands()
m_commandsComboBox->completer()->setModel(model);
}
if (defaultInstructionIndex > 0) {
m_commandsComboBox->setCurrentIndex(defaultInstructionIndex);
}
bool hasInstructions = !instructions.isEmpty();
m_editCommandButton->setEnabled(hasInstructions);
m_deleteCommandButton->setEnabled(hasInstructions);