mirror of
https://github.com/Palm1r/QodeAssist.git
synced 2025-11-22 02:22:44 -05:00
feat: Add default option for custom instructions
This commit is contained in:
@ -20,6 +20,7 @@
|
|||||||
#include "AddCustomInstructionDialog.hpp"
|
#include "AddCustomInstructionDialog.hpp"
|
||||||
#include "QodeAssisttr.h"
|
#include "QodeAssisttr.h"
|
||||||
|
|
||||||
|
#include <QCheckBox>
|
||||||
#include <QDialogButtonBox>
|
#include <QDialogButtonBox>
|
||||||
#include <QFormLayout>
|
#include <QFormLayout>
|
||||||
#include <QLabel>
|
#include <QLabel>
|
||||||
@ -46,6 +47,7 @@ AddCustomInstructionDialog::AddCustomInstructionDialog(const CustomInstruction &
|
|||||||
setupUi();
|
setupUi();
|
||||||
m_nameEdit->setText(instruction.name);
|
m_nameEdit->setText(instruction.name);
|
||||||
m_bodyEdit->setPlainText(instruction.body);
|
m_bodyEdit->setPlainText(instruction.body);
|
||||||
|
m_defaultCheckBox->setChecked(instruction.isDefault);
|
||||||
resize(500, 400);
|
resize(500, 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,6 +72,11 @@ void AddCustomInstructionDialog::setupUi()
|
|||||||
Tr::tr("Enter the refactoring instruction that will be sent to the LLM..."));
|
Tr::tr("Enter the refactoring instruction that will be sent to the LLM..."));
|
||||||
mainLayout->addWidget(m_bodyEdit);
|
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
|
QDialogButtonBox *buttonBox
|
||||||
= new QDialogButtonBox(QDialogButtonBox::Save | QDialogButtonBox::Cancel, this);
|
= new QDialogButtonBox(QDialogButtonBox::Save | QDialogButtonBox::Cancel, this);
|
||||||
|
|
||||||
@ -95,6 +102,7 @@ CustomInstruction AddCustomInstructionDialog::getInstruction() const
|
|||||||
CustomInstruction instruction = m_instruction;
|
CustomInstruction instruction = m_instruction;
|
||||||
instruction.name = m_nameEdit->text().trimmed();
|
instruction.name = m_nameEdit->text().trimmed();
|
||||||
instruction.body = m_bodyEdit->toPlainText().trimmed();
|
instruction.body = m_bodyEdit->toPlainText().trimmed();
|
||||||
|
instruction.isDefault = m_defaultCheckBox->isChecked();
|
||||||
return instruction;
|
return instruction;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -26,6 +26,7 @@
|
|||||||
|
|
||||||
class QLineEdit;
|
class QLineEdit;
|
||||||
class QPlainTextEdit;
|
class QPlainTextEdit;
|
||||||
|
class QCheckBox;
|
||||||
|
|
||||||
namespace QodeAssist {
|
namespace QodeAssist {
|
||||||
|
|
||||||
@ -45,6 +46,7 @@ private:
|
|||||||
|
|
||||||
QLineEdit *m_nameEdit;
|
QLineEdit *m_nameEdit;
|
||||||
QPlainTextEdit *m_bodyEdit;
|
QPlainTextEdit *m_bodyEdit;
|
||||||
|
QCheckBox *m_defaultCheckBox;
|
||||||
CustomInstruction m_instruction;
|
CustomInstruction m_instruction;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -93,6 +93,7 @@ bool CustomInstructionsManager::loadInstructions()
|
|||||||
instruction.id = obj["id"].toString();
|
instruction.id = obj["id"].toString();
|
||||||
instruction.name = obj["name"].toString();
|
instruction.name = obj["name"].toString();
|
||||||
instruction.body = obj["body"].toString();
|
instruction.body = obj["body"].toString();
|
||||||
|
instruction.isDefault = obj["default"].toBool(false);
|
||||||
|
|
||||||
if (instruction.id.isEmpty() || instruction.name.isEmpty()) {
|
if (instruction.id.isEmpty() || instruction.name.isEmpty()) {
|
||||||
LOG_MESSAGE(QString("Invalid instruction in file: %1").arg(fileInfo.fileName()));
|
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;
|
int existingIndex = -1;
|
||||||
for (int i = 0; i < m_instructions.size(); ++i) {
|
for (int i = 0; i < m_instructions.size(); ++i) {
|
||||||
if (m_instructions[i].id == newInstruction.id) {
|
if (m_instructions[i].id == newInstruction.id) {
|
||||||
@ -144,6 +172,7 @@ bool CustomInstructionsManager::saveInstruction(const CustomInstruction &instruc
|
|||||||
obj["id"] = newInstruction.id;
|
obj["id"] = newInstruction.id;
|
||||||
obj["name"] = newInstruction.name;
|
obj["name"] = newInstruction.name;
|
||||||
obj["body"] = newInstruction.body;
|
obj["body"] = newInstruction.body;
|
||||||
|
obj["default"] = newInstruction.isDefault;
|
||||||
obj["version"] = "0.1";
|
obj["version"] = "0.1";
|
||||||
|
|
||||||
QJsonDocument doc(obj);
|
QJsonDocument doc(obj);
|
||||||
|
|||||||
@ -30,6 +30,7 @@ struct CustomInstruction
|
|||||||
QString id;
|
QString id;
|
||||||
QString name;
|
QString name;
|
||||||
QString body;
|
QString body;
|
||||||
|
bool isDefault = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
class CustomInstructionsManager : public QObject
|
class CustomInstructionsManager : public QObject
|
||||||
|
|||||||
@ -320,9 +320,16 @@ void QuickRefactorDialog::loadCustomCommands()
|
|||||||
const QVector<CustomInstruction> &instructions = manager.instructions();
|
const QVector<CustomInstruction> &instructions = manager.instructions();
|
||||||
|
|
||||||
QStringList instructionNames;
|
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);
|
m_commandsComboBox->addItem(instruction.name, instruction.id);
|
||||||
instructionNames.append(instruction.name);
|
instructionNames.append(instruction.name);
|
||||||
|
|
||||||
|
if (instruction.isDefault) {
|
||||||
|
defaultInstructionIndex = i + 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_commandsComboBox->completer()) {
|
if (m_commandsComboBox->completer()) {
|
||||||
@ -330,6 +337,10 @@ void QuickRefactorDialog::loadCustomCommands()
|
|||||||
m_commandsComboBox->completer()->setModel(model);
|
m_commandsComboBox->completer()->setModel(model);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (defaultInstructionIndex > 0) {
|
||||||
|
m_commandsComboBox->setCurrentIndex(defaultInstructionIndex);
|
||||||
|
}
|
||||||
|
|
||||||
bool hasInstructions = !instructions.isEmpty();
|
bool hasInstructions = !instructions.isEmpty();
|
||||||
m_editCommandButton->setEnabled(hasInstructions);
|
m_editCommandButton->setEnabled(hasInstructions);
|
||||||
m_deleteCommandButton->setEnabled(hasInstructions);
|
m_deleteCommandButton->setEnabled(hasInstructions);
|
||||||
|
|||||||
Reference in New Issue
Block a user