diff --git a/widgets/AddCustomInstructionDialog.cpp b/widgets/AddCustomInstructionDialog.cpp index 1aba178..ac9e076 100644 --- a/widgets/AddCustomInstructionDialog.cpp +++ b/widgets/AddCustomInstructionDialog.cpp @@ -20,6 +20,7 @@ #include "AddCustomInstructionDialog.hpp" #include "QodeAssisttr.h" +#include #include #include #include @@ -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; } diff --git a/widgets/AddCustomInstructionDialog.hpp b/widgets/AddCustomInstructionDialog.hpp index fc3968b..fd0224c 100644 --- a/widgets/AddCustomInstructionDialog.hpp +++ b/widgets/AddCustomInstructionDialog.hpp @@ -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; }; diff --git a/widgets/CustomInstructionsManager.cpp b/widgets/CustomInstructionsManager.cpp index 93fb3b6..552e4d7 100644 --- a/widgets/CustomInstructionsManager.cpp +++ b/widgets/CustomInstructionsManager.cpp @@ -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); diff --git a/widgets/CustomInstructionsManager.hpp b/widgets/CustomInstructionsManager.hpp index b08432c..1a99305 100644 --- a/widgets/CustomInstructionsManager.hpp +++ b/widgets/CustomInstructionsManager.hpp @@ -30,6 +30,7 @@ struct CustomInstruction QString id; QString name; QString body; + bool isDefault = false; }; class CustomInstructionsManager : public QObject diff --git a/widgets/QuickRefactorDialog.cpp b/widgets/QuickRefactorDialog.cpp index b2180be..6ef490d 100644 --- a/widgets/QuickRefactorDialog.cpp +++ b/widgets/QuickRefactorDialog.cpp @@ -320,9 +320,16 @@ void QuickRefactorDialog::loadCustomCommands() const QVector &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);