Skip to content

Commit

Permalink
Move context settings to page
Browse files Browse the repository at this point in the history
  • Loading branch information
Palm1r committed Sep 7, 2024
1 parent b5ca11e commit 356f28a
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 88 deletions.
3 changes: 2 additions & 1 deletion DocumentContextReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <languageserverprotocol/lsptypes.h>

#include "QodeAssistSettings.hpp"
#include "settings/ContextSettings.hpp"

const QRegularExpression &getYearRegex()
{
Expand Down Expand Up @@ -136,7 +137,7 @@ QString DocumentContextReader::getLanguageAndFileInfo() const

QString DocumentContextReader::getSpecificInstructions() const
{
QString specificInstruction = settings().specificInstractions().arg(
QString specificInstruction = Settings::contextSettings().specificInstractions().arg(
LanguageServerProtocol::TextDocumentItem::mimeTypeToLanguageId(m_textDocument->mimeType()));
return QString("%1").arg(specificInstruction);
}
Expand Down
20 changes: 11 additions & 9 deletions LLMClientInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "PromptTemplateManager.hpp"
#include "QodeAssistSettings.hpp"
#include "QodeAssistUtils.hpp"
#include "settings/ContextSettings.hpp"
#include "settings/GeneralSettings.hpp"

namespace QodeAssist {
Expand Down Expand Up @@ -131,12 +132,13 @@ QString LLMClientInterface::сontextBefore(TextEditor::TextEditorWidget *widget,
return QString();

QString contextBefore;
if (settings().readFullFile()) {
if (Settings::contextSettings().readFullFile()) {
contextBefore = reader.readWholeFileBefore(lineNumber, cursorPosition);
} else {
contextBefore = reader.getContextBefore(lineNumber,
cursorPosition,
settings().readStringsBeforeCursor());
contextBefore
= reader.getContextBefore(lineNumber,
cursorPosition,
Settings::contextSettings().readStringsBeforeCursor());
}

return contextBefore;
Expand All @@ -154,12 +156,12 @@ QString LLMClientInterface::сontextAfter(TextEditor::TextEditorWidget *widget,
return QString();

QString contextAfter;
if (settings().readFullFile()) {
if (Settings::contextSettings().readFullFile()) {
contextAfter = reader.readWholeFileAfter(lineNumber, cursorPosition);
} else {
contextAfter = reader.getContextAfter(lineNumber,
cursorPosition,
settings().readStringsAfterCursor());
Settings::contextSettings().readStringsAfterCursor());
}

return contextAfter;
Expand Down Expand Up @@ -228,7 +230,7 @@ void LLMClientInterface::handleLLMResponse(QNetworkReply *reply, const QJsonObje

QJsonObject position = request["params"].toObject()["doc"].toObject()["position"].toObject();

if (!settings().multiLineCompletion()
if (!Settings::generalSettings().multiLineCompletion()
&& processSingleLineCompletion(reply, request, accumulatedResponse)) {
return;
}
Expand Down Expand Up @@ -278,10 +280,10 @@ ContextData LLMClientInterface::prepareContext(const QJsonObject &request,

QString contextBefore = сontextBefore(widget, lineNumber, cursorPosition);
QString contextAfter = сontextAfter(widget, lineNumber, cursorPosition);
QString instructions = QString("%1%2").arg(settings().useSpecificInstructions()
QString instructions = QString("%1%2").arg(Settings::contextSettings().useSpecificInstructions()
? reader.getSpecificInstructions()
: QString(),
settings().useFilePathInContext()
Settings::contextSettings().useFilePathInContext()
? reader.getLanguageAndFileInfo()
: QString());

Expand Down
79 changes: 17 additions & 62 deletions QodeAssistSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,6 @@ QodeAssistSettings::QodeAssistSettings()
ollamaLivetime.setDefaultValue("5m");
ollamaLivetime.setDisplayStyle(Utils::StringAspect::LineEditDisplay);

readFullFile.setSettingsKey(Constants::READ_FULL_FILE);
readFullFile.setLabelText(Tr::tr("Read Full File"));
readFullFile.setDefaultValue(false);

maxFileThreshold.setSettingsKey(Constants::MAX_FILE_THRESHOLD);
maxFileThreshold.setLabelText(Tr::tr("Max File Threshold:"));
maxFileThreshold.setRange(10, 100000);
maxFileThreshold.setDefaultValue(600);

readStringsBeforeCursor.setSettingsKey(Constants::READ_STRINGS_BEFORE_CURSOR);
readStringsBeforeCursor.setLabelText(Tr::tr("Read Strings Before Cursor"));
readStringsBeforeCursor.setDefaultValue(50);

readStringsAfterCursor.setSettingsKey(Constants::READ_STRINGS_AFTER_CURSOR);
readStringsAfterCursor.setLabelText(Tr::tr("Read Strings After Cursor"));
readStringsAfterCursor.setDefaultValue(30);

maxTokens.setSettingsKey(Constants::MAX_TOKENS);
maxTokens.setLabelText(Tr::tr("Max Tokens"));
maxTokens.setRange(-1, 10000);
Expand Down Expand Up @@ -117,26 +100,7 @@ QodeAssistSettings::QodeAssistSettings()
startSuggestionTimer.setRange(10, 10000);
startSuggestionTimer.setDefaultValue(500);

useFilePathInContext.setSettingsKey(Constants::USE_FILE_PATH_IN_CONTEXT);
useFilePathInContext.setDefaultValue(false);
useFilePathInContext.setLabelText(Tr::tr("Use File Path in Context"));

useSpecificInstructions.setSettingsKey(Constants::USE_SPECIFIC_INSTRUCTIONS);
useSpecificInstructions.setDefaultValue(false);
useSpecificInstructions.setLabelText(Tr::tr("Use Specific Instructions"));

specificInstractions.setSettingsKey(Constants::SPECIFIC_INSTRUCTIONS);
specificInstractions.setDisplayStyle(Utils::StringAspect::TextEditDisplay);
specificInstractions.setLabelText(
Tr::tr("Instructions: Please keep %1 for languge name, warning, it shouldn't too big"));
specificInstractions.setDefaultValue(
"You are an expert %1 code completion AI."
"CRITICAL: Please provide minimal the best possible code completion suggestions.\n");

resetToDefaults.m_buttonText = Tr::tr("Reset to Defaults");
multiLineCompletion.setSettingsKey(Constants::MULTILINE_COMPLETION);
multiLineCompletion.setDefaultValue(true);
multiLineCompletion.setLabelText(Tr::tr("Enable Multiline Completion"));

apiKey.setSettingsKey(Constants::API_KEY);
apiKey.setLabelText(Tr::tr("API Key:"));
Expand Down Expand Up @@ -175,9 +139,7 @@ QodeAssistSettings::QodeAssistSettings()
topP.setEnabled(useTopP());
presencePenalty.setEnabled(usePresencePenalty());
frequencyPenalty.setEnabled(useFrequencyPenalty());
readStringsAfterCursor.setEnabled(!readFullFile());
readStringsBeforeCursor.setEnabled(!readFullFile());
specificInstractions.setEnabled(useSpecificInstructions());

customJsonTemplate.setVisible(PromptTemplateManager::instance().getCurrentTemplate()->name()
== "Custom Template");

Expand All @@ -189,15 +151,15 @@ QodeAssistSettings::QodeAssistSettings()
Row{saveCustomTemplateButton,
loadCustomTemplateButton,
Stretch{1}}},
readFullFile,
maxFileThreshold,
readStringsBeforeCursor,
readStringsAfterCursor,
// readFullFile,
// maxFileThreshold,
// readStringsBeforeCursor,
// readStringsAfterCursor,
ollamaLivetime,
apiKey,
useFilePathInContext,
useSpecificInstructions,
specificInstractions,
// useFilePathInContext,
// useSpecificInstructions,
// specificInstractions,
temperature,
maxTokens,
startSuggestionTimer,
Expand Down Expand Up @@ -225,19 +187,12 @@ void QodeAssistSettings::setupConnections()
connect(&useFrequencyPenalty, &Utils::BoolAspect::volatileValueChanged, this, [this]() {
frequencyPenalty.setEnabled(useFrequencyPenalty.volatileValue());
});
connect(&readFullFile, &Utils::BoolAspect::volatileValueChanged, this, [this]() {
readStringsAfterCursor.setEnabled(!readFullFile.volatileValue());
readStringsBeforeCursor.setEnabled(!readFullFile.volatileValue());
});

connect(&resetToDefaults,
&ButtonAspect::clicked,
this,
&QodeAssistSettings::resetSettingsToDefaults);

connect(&useSpecificInstructions, &Utils::BoolAspect::volatileValueChanged, this, [this]() {
specificInstractions.setEnabled(useSpecificInstructions.volatileValue());
});

connect(&saveCustomTemplateButton,
&ButtonAspect::clicked,
this,
Expand Down Expand Up @@ -277,10 +232,10 @@ void QodeAssistSettings::resetSettingsToDefaults()
// resetAspect(fimPrompts);
resetAspect(temperature);
resetAspect(maxTokens);
resetAspect(readFullFile);
resetAspect(maxFileThreshold);
resetAspect(readStringsBeforeCursor);
resetAspect(readStringsAfterCursor);
// resetAspect(readFullFile);
// resetAspect(maxFileThreshold);
// resetAspect(readStringsBeforeCursor);
// resetAspect(readStringsAfterCursor);
resetAspect(useTopP);
resetAspect(topP);
resetAspect(useTopK);
Expand All @@ -292,10 +247,10 @@ void QodeAssistSettings::resetSettingsToDefaults()
resetAspect(startSuggestionTimer);
// resetAspect(enableLogging);
resetAspect(ollamaLivetime);
resetAspect(specificInstractions);
resetAspect(multiLineCompletion);
resetAspect(useFilePathInContext);
resetAspect(useSpecificInstructions);
// resetAspect(specificInstractions);
// resetAspect(multiLineCompletion);
// resetAspect(useFilePathInContext);
// resetAspect(useSpecificInstructions);
resetAspect(customJsonTemplate);

// fimPrompts.setStringValue("StarCoder2");
Expand Down
9 changes: 0 additions & 9 deletions QodeAssistSettings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ class QodeAssistSettings : public Utils::AspectContainer
Utils::DoubleAspect temperature{this};
Utils::IntegerAspect maxTokens{this};

Utils::BoolAspect readFullFile{this};
Utils::IntegerAspect readStringsBeforeCursor{this};
Utils::IntegerAspect readStringsAfterCursor{this};

Utils::BoolAspect useTopP{this};
Utils::DoubleAspect topP{this};

Expand All @@ -77,13 +73,8 @@ class QodeAssistSettings : public Utils::AspectContainer
Utils::DoubleAspect frequencyPenalty{this};

Utils::IntegerAspect startSuggestionTimer{this};
Utils::IntegerAspect maxFileThreshold{this};

Utils::StringAspect ollamaLivetime{this};
Utils::StringAspect specificInstractions{this};
Utils::BoolAspect useSpecificInstructions{this};
Utils::BoolAspect useFilePathInContext{this};
Utils::BoolAspect multiLineCompletion{this};

Utils::StringAspect customJsonTemplate{this};
ButtonAspect saveCustomTemplateButton{this};
Expand Down
85 changes: 84 additions & 1 deletion settings/ContextSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@

#include "ContextSettings.hpp"

#include <QMessageBox>
#include <coreplugin/dialogs/ioptionspage.h>
#include <coreplugin/icore.h>
#include <utils/layoutbuilder.h>

#include "QodeAssistConstants.hpp"
Expand All @@ -40,12 +42,93 @@ ContextSettings::ContextSettings()

setDisplayName(Tr::tr("Context"));

readFullFile.setSettingsKey(Constants::READ_FULL_FILE);
readFullFile.setLabelText(Tr::tr("Read Full File"));
readFullFile.setDefaultValue(false);

readStringsBeforeCursor.setSettingsKey(Constants::READ_STRINGS_BEFORE_CURSOR);
readStringsBeforeCursor.setLabelText(Tr::tr("Read Strings Before Cursor"));
readStringsBeforeCursor.setRange(0, 10000);
readStringsBeforeCursor.setDefaultValue(50);

readStringsAfterCursor.setSettingsKey(Constants::READ_STRINGS_AFTER_CURSOR);
readStringsAfterCursor.setLabelText(Tr::tr("Read Strings After Cursor"));
readStringsAfterCursor.setRange(0, 10000);
readStringsAfterCursor.setDefaultValue(30);

useFilePathInContext.setSettingsKey(Constants::USE_FILE_PATH_IN_CONTEXT);
useFilePathInContext.setDefaultValue(false);
useFilePathInContext.setLabelText(Tr::tr("Use File Path in Context"));

useSpecificInstructions.setSettingsKey(Constants::USE_SPECIFIC_INSTRUCTIONS);
useSpecificInstructions.setDefaultValue(false);
useSpecificInstructions.setLabelText(Tr::tr("Use Specific Instructions"));

specificInstractions.setSettingsKey(Constants::SPECIFIC_INSTRUCTIONS);
specificInstractions.setDisplayStyle(Utils::StringAspect::TextEditDisplay);
specificInstractions.setLabelText(
Tr::tr("Instructions: Please keep %1 for languge name, warning, it shouldn't too big"));
specificInstractions.setDefaultValue(
"You are an expert %1 code completion AI."
"CRITICAL: Please provide minimal the best possible code completion suggestions.\n");

resetToDefaults.m_buttonText = Tr::tr("Reset Page to Defaults");

readSettings();

readStringsAfterCursor.setEnabled(!readFullFile());
readStringsBeforeCursor.setEnabled(!readFullFile());
specificInstractions.setEnabled(useSpecificInstructions());

setupConnection();

setLayouter([this]() {
using namespace Layouting;
return Column{Stretch{1}};
return Column{Row{readFullFile, Stretch{1}, resetToDefaults},
readStringsBeforeCursor,
readStringsAfterCursor,
useFilePathInContext,
useSpecificInstructions,
specificInstractions,
Stretch{1}};
});
}

void ContextSettings::setupConnection()
{
connect(&readFullFile, &Utils::BoolAspect::volatileValueChanged, this, [this]() {
readStringsAfterCursor.setEnabled(!readFullFile.volatileValue());
readStringsBeforeCursor.setEnabled(!readFullFile.volatileValue());
});
connect(&useSpecificInstructions, &Utils::BoolAspect::volatileValueChanged, this, [this]() {
specificInstractions.setEnabled(useSpecificInstructions.volatileValue());
});
connect(&resetToDefaults, &ButtonAspect::clicked, this, &ContextSettings::resetPageToDefaults);
}

void ContextSettings::resetPageToDefaults()
{
QMessageBox::StandardButton reply;
reply = QMessageBox::question(
Core::ICore::dialogParent(),
Tr::tr("Reset Settings"),
Tr::tr("Are you sure you want to reset all settings to default values?"),
QMessageBox::Yes | QMessageBox::No);

if (reply == QMessageBox::Yes) {
resetAspect(readFullFile);
resetAspect(readStringsBeforeCursor);
resetAspect(readStringsAfterCursor);
resetAspect(useFilePathInContext);
resetAspect(useSpecificInstructions);
resetAspect(specificInstractions);
}

QMessageBox::information(Core::ICore::dialogParent(),
Tr::tr("Settings Reset"),
Tr::tr("All settings have been reset to their default values."));
}

class ContextSettingsPage : public Core::IOptionsPage
{
public:
Expand Down
16 changes: 16 additions & 0 deletions settings/ContextSettings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,28 @@

#include <utils/aspects.h>

#include "SettingsUtils.hpp"

namespace QodeAssist::Settings {

class ContextSettings : public Utils::AspectContainer
{
public:
ContextSettings();

Utils::BoolAspect readFullFile{this};
Utils::IntegerAspect readStringsBeforeCursor{this};
Utils::IntegerAspect readStringsAfterCursor{this};

Utils::StringAspect specificInstractions{this};
Utils::BoolAspect useSpecificInstructions{this};
Utils::BoolAspect useFilePathInContext{this};

ButtonAspect resetToDefaults{this};

private:
void setupConnection();
void resetPageToDefaults();
};

ContextSettings &contextSettings();
Expand Down
Loading

0 comments on commit 356f28a

Please sign in to comment.