diff --git a/DocumentContextReader.cpp b/DocumentContextReader.cpp index 2718d92..dd0e394 100644 --- a/DocumentContextReader.cpp +++ b/DocumentContextReader.cpp @@ -19,7 +19,11 @@ #include "DocumentContextReader.hpp" +#include #include +#include + +#include "QodeAssistSettings.hpp" namespace QodeAssist { @@ -123,4 +127,29 @@ QString DocumentContextReader::readWholeFileAfter(int lineNumber, int cursorPosi return content.trimmed(); } +QString DocumentContextReader::getLanguageAndFileInfo() const +{ + if (!m_textDocument) + return QString(); + + QString language = LanguageServerProtocol::TextDocumentItem::mimeTypeToLanguageId( + m_textDocument->mimeType()); + QString mimeType = m_textDocument->mimeType(); + QString filePath = m_textDocument->filePath().toString(); + QString fileExtension = QFileInfo(filePath).suffix(); + + return QString("//Language: %1 (MIME: %2) filepath: %3(%4)\n\n") + .arg(language) + .arg(mimeType) + .arg(filePath) + .arg(fileExtension); +} + +QString DocumentContextReader::getSpecificInstructions() const +{ + QString specificInstruction = settings().specificInstractions().arg( + LanguageServerProtocol::TextDocumentItem::mimeTypeToLanguageId(m_textDocument->mimeType())); + return QString("//Instructions: %1").arg(specificInstruction); +} + } // namespace QodeAssist diff --git a/DocumentContextReader.hpp b/DocumentContextReader.hpp index 5c473ec..7a2c229 100644 --- a/DocumentContextReader.hpp +++ b/DocumentContextReader.hpp @@ -20,14 +20,16 @@ #pragma once #include +#include namespace QodeAssist { class DocumentContextReader { public: - DocumentContextReader(QTextDocument *doc) - : m_document(doc) + DocumentContextReader(TextEditor::TextDocument *textDocument) + : m_textDocument(textDocument) + , m_document(textDocument->document()) {} QString getLineText(int lineNumber, int cursorPosition = -1) const; @@ -35,8 +37,11 @@ class DocumentContextReader QString getContextAfter(int lineNumber, int cursorPosition, int linesCount) const; QString readWholeFileBefore(int lineNumber, int cursorPosition) const; QString readWholeFileAfter(int lineNumber, int cursorPosition) const; + QString getLanguageAndFileInfo() const; + QString getSpecificInstructions() const; private: + TextEditor::TextDocument *m_textDocument; QTextDocument *m_document; }; diff --git a/LLMClientInterface.cpp b/LLMClientInterface.cpp index 9689d70..064c9ef 100644 --- a/LLMClientInterface.cpp +++ b/LLMClientInterface.cpp @@ -98,8 +98,8 @@ QString LLMClientInterface::сontextBefore(TextEditor::TextEditorWidget *widget, if (!widget) return QString(); - QTextDocument *doc = widget->document(); - DocumentContextReader reader(doc); + DocumentContextReader reader(widget->textDocument()); + QString languageAndFileInfo = reader.getLanguageAndFileInfo(); QString contextBefore; if (settings().readFullFile()) { @@ -110,7 +110,10 @@ QString LLMClientInterface::сontextBefore(TextEditor::TextEditorWidget *widget, settings().readStringsBeforeCursor()); } - return contextBefore; + return QString("%1\n%2\n%3") + .arg(reader.getSpecificInstructions()) + .arg(reader.getLanguageAndFileInfo()) + .arg(contextBefore); } QString LLMClientInterface::сontextAfter(TextEditor::TextEditorWidget *widget, @@ -120,8 +123,7 @@ QString LLMClientInterface::сontextAfter(TextEditor::TextEditorWidget *widget, if (!widget) return QString(); - QTextDocument *doc = widget->document(); - DocumentContextReader reader(doc); + DocumentContextReader reader(widget->textDocument()); QString contextAfter; if (settings().readFullFile()) { diff --git a/QodeAssistConstants.hpp b/QodeAssistConstants.hpp index 26df563..3254c4f 100644 --- a/QodeAssistConstants.hpp +++ b/QodeAssistConstants.hpp @@ -52,6 +52,7 @@ const char PROVIDER_PATHS[] = "QodeAssist.providerPaths"; const char START_SUGGESTION_TIMER[] = "QodeAssist.startSuggestionTimer"; const char MAX_FILE_THRESHOLD[] = "QodeAssist.maxFileThreshold"; const char OLLAMA_LIVETIME[] = "QodeAssist.ollamaLivetime"; +const char SPECIFIC_INSTRUCTIONS[] = "QodeAssist.specificInstractions"; const char QODE_ASSIST_GENERAL_OPTIONS_ID[] = "QodeAssist.GeneralOptions"; const char QODE_ASSIST_GENERAL_OPTIONS_CATEGORY[] = "QodeAssist.Category"; diff --git a/QodeAssistSettings.cpp b/QodeAssistSettings.cpp index 21aa942..becb576 100644 --- a/QodeAssistSettings.cpp +++ b/QodeAssistSettings.cpp @@ -97,7 +97,7 @@ QodeAssistSettings::QodeAssistSettings() readFullFile.setSettingsKey(Constants::READ_FULL_FILE); readFullFile.setLabelText(Tr::tr("Read Full File")); - readFullFile.setDefaultValue(true); + readFullFile.setDefaultValue(false); maxFileThreshold.setSettingsKey(Constants::MAX_FILE_THRESHOLD); maxFileThreshold.setLabelText(Tr::tr("Max File Threshold:")); @@ -106,11 +106,11 @@ QodeAssistSettings::QodeAssistSettings() readStringsBeforeCursor.setSettingsKey(Constants::READ_STRINGS_BEFORE_CURSOR); readStringsBeforeCursor.setLabelText(Tr::tr("Read Strings Before Cursor")); - readStringsBeforeCursor.setDefaultValue(60); + readStringsBeforeCursor.setDefaultValue(50); readStringsAfterCursor.setSettingsKey(Constants::READ_STRINGS_AFTER_CURSOR); readStringsAfterCursor.setLabelText(Tr::tr("Read Strings After Cursor")); - readStringsAfterCursor.setDefaultValue(40); + readStringsAfterCursor.setDefaultValue(30); maxTokens.setSettingsKey(Constants::MAX_TOKENS); maxTokens.setLabelText(Tr::tr("Max Tokens")); @@ -157,6 +157,14 @@ QodeAssistSettings::QodeAssistSettings() startSuggestionTimer.setRange(10, 10000); startSuggestionTimer.setDefaultValue(500); + 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"); const auto &manager = LLMProvidersManager::instance(); @@ -206,6 +214,7 @@ QodeAssistSettings::QodeAssistSettings() readFullFile, maxFileThreshold, ollamaLivetime, + specificInstractions, temperature, maxTokens, readStringsBeforeCursor, @@ -349,6 +358,7 @@ void QodeAssistSettings::resetSettingsToDefaults() resetAspect(startSuggestionTimer); resetAspect(enableLogging); resetAspect(ollamaLivetime); + resetAspect(specificInstractions); updateProviderSettings(); apply(); diff --git a/QodeAssistSettings.hpp b/QodeAssistSettings.hpp index f6b4583..b512234 100644 --- a/QodeAssistSettings.hpp +++ b/QodeAssistSettings.hpp @@ -95,6 +95,7 @@ class QodeAssistSettings : public Utils::AspectContainer Utils::IntegerAspect maxFileThreshold{this}; Utils::StringAspect ollamaLivetime{this}; + Utils::StringAspect specificInstractions{this}; ButtonAspect resetToDefaults{this};