Skip to content

Commit

Permalink
Add extended context
Browse files Browse the repository at this point in the history
  • Loading branch information
Palm1r committed Aug 28, 2024
1 parent 9897180 commit fc58c38
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 10 deletions.
29 changes: 29 additions & 0 deletions DocumentContextReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@

#include "DocumentContextReader.hpp"

#include <QFileInfo>
#include <QTextBlock>
#include <languageserverprotocol/lsptypes.h>

#include "QodeAssistSettings.hpp"

namespace QodeAssist {

Expand Down Expand Up @@ -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
9 changes: 7 additions & 2 deletions DocumentContextReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,28 @@
#pragma once

#include <QTextDocument>
#include <texteditor/textdocument.h>

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;
QString getContextBefore(int lineNumber, int cursorPosition, int linesCount) const;
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;
};

Expand Down
12 changes: 7 additions & 5 deletions LLMClientInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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,
Expand All @@ -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()) {
Expand Down
1 change: 1 addition & 0 deletions QodeAssistConstants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
16 changes: 13 additions & 3 deletions QodeAssistSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:"));
Expand All @@ -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"));
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -206,6 +214,7 @@ QodeAssistSettings::QodeAssistSettings()
readFullFile,
maxFileThreshold,
ollamaLivetime,
specificInstractions,
temperature,
maxTokens,
readStringsBeforeCursor,
Expand Down Expand Up @@ -349,6 +358,7 @@ void QodeAssistSettings::resetSettingsToDefaults()
resetAspect(startSuggestionTimer);
resetAspect(enableLogging);
resetAspect(ollamaLivetime);
resetAspect(specificInstractions);

updateProviderSettings();
apply();
Expand Down
1 change: 1 addition & 0 deletions QodeAssistSettings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class QodeAssistSettings : public Utils::AspectContainer
Utils::IntegerAspect maxFileThreshold{this};

Utils::StringAspect ollamaLivetime{this};
Utils::StringAspect specificInstractions{this};

ButtonAspect resetToDefaults{this};

Expand Down

0 comments on commit fc58c38

Please sign in to comment.