Add basic cache manager

This commit is contained in:
Petr Mironychev 2024-09-11 01:25:00 +02:00
parent f07610df5c
commit cd1a9e16e0
5 changed files with 163 additions and 6 deletions

View File

@ -54,4 +54,5 @@ add_qtc_plugin(QodeAssist
settings/CustomPromptSettings.hpp settings/CustomPromptSettings.cpp
settings/PresetPromptsSettings.hpp settings/PresetPromptsSettings.cpp
settings/SettingsUtils.hpp
core/ChangesManager.h core/ChangesManager.cpp
)

View File

@ -29,6 +29,7 @@
#include "LLMProvidersManager.hpp"
#include "PromptTemplateManager.hpp"
#include "QodeAssistUtils.hpp"
#include "core/ChangesManager.h"
#include "settings/ContextSettings.hpp"
#include "settings/GeneralSettings.hpp"
@ -277,14 +278,17 @@ ContextData LLMClientInterface::prepareContext(const QJsonObject &request,
DocumentContextReader reader(widget->textDocument());
QString recentChanges = ChangesManager::instance().getRecentChangesContext();
QString contextBefore = сontextBefore(widget, lineNumber, cursorPosition);
QString contextAfter = сontextAfter(widget, lineNumber, cursorPosition);
QString instructions = QString("%1%2").arg(Settings::contextSettings().useSpecificInstructions()
? reader.getSpecificInstructions()
: QString(),
Settings::contextSettings().useFilePathInContext()
? reader.getLanguageAndFileInfo()
: QString());
QString instructions = QString("%1%2%3").arg(Settings::contextSettings().useSpecificInstructions()
? reader.getSpecificInstructions()
: QString(),
Settings::contextSettings().useFilePathInContext()
? reader.getLanguageAndFileInfo()
: QString(),
recentChanges);
return {QString("%1%2").arg(contextBefore, accumulatedCompletion), contextAfter, instructions};
}

View File

@ -31,6 +31,7 @@
#include "LLMClientInterface.hpp"
#include "LLMSuggestion.hpp"
#include "core/ChangesManager.h"
#include "settings/GeneralSettings.hpp"
using namespace LanguageServerProtocol;
@ -83,6 +84,9 @@ void QodeAssistClient::openDocument(TextEditor::TextDocument *document)
auto textEditor = BaseTextEditor::currentTextEditor();
if (!textEditor || textEditor->document() != document)
return;
ChangesManager::instance().addChange(document, position, charsRemoved, charsAdded);
TextEditorWidget *widget = textEditor->editorWidget();
if (widget->isReadOnly() || widget->multiTextCursor().hasMultipleCursors())
return;

86
core/ChangesManager.cpp Normal file
View File

@ -0,0 +1,86 @@
/*
* Copyright (C) 2024 Petr Mironychev
*
* This file is part of QodeAssist.
*
* QodeAssist is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QodeAssist is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QodeAssist. If not, see <https://www.gnu.org/licenses/>.
*/
#include "ChangesManager.h"
#include "QodeAssistUtils.hpp"
namespace QodeAssist {
ChangesManager &ChangesManager::instance()
{
static ChangesManager instance;
return instance;
}
ChangesManager::ChangesManager()
: QObject(nullptr)
{
}
ChangesManager::~ChangesManager()
{
}
void ChangesManager::addChange(TextEditor::TextDocument *document,
int position,
int charsRemoved,
int charsAdded)
{
auto &documentQueue = m_documentChanges[document];
QTextBlock block = document->document()->findBlock(position);
int lineNumber = block.blockNumber();
QString lineContent = block.text();
QString fileName = document->filePath().fileName();
ChangeInfo change{fileName, lineNumber, lineContent};
auto it = std::find_if(documentQueue.begin(),
documentQueue.end(),
[lineNumber](const ChangeInfo &c) { return c.lineNumber == lineNumber; });
if (it != documentQueue.end()) {
it->lineContent = lineContent;
} else {
documentQueue.enqueue(change);
if (documentQueue.size() > MAX_CACHED_CHANGES) {
documentQueue.dequeue();
}
}
logMessage(QString("ChangesManager: Updated %1 line %2: '%3'")
.arg(fileName)
.arg(lineNumber)
.arg(lineContent));
logMessage(QString("ChangesManager: Document queue size %1").arg(documentQueue.size()));
}
QString ChangesManager::getRecentChangesContext() const
{
QString context;
for (const auto &documentChanges : m_documentChanges) {
for (const auto &change : documentChanges) {
context += change.lineContent + "\n";
}
}
return context;
}
} // namespace QodeAssist

62
core/ChangesManager.h Normal file
View File

@ -0,0 +1,62 @@
/*
* Copyright (C) 2024 Petr Mironychev
*
* This file is part of QodeAssist.
*
* QodeAssist is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QodeAssist is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QodeAssist. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <QDateTime>
#include <QHash>
#include <QQueue>
#include <QTimer>
#include <texteditor/textdocument.h>
namespace QodeAssist {
class ChangesManager : public QObject
{
Q_OBJECT
public:
struct ChangeInfo
{
QString fileName;
int lineNumber;
QString lineContent;
};
static ChangesManager &instance();
void addChange(TextEditor::TextDocument *document,
int position,
int charsRemoved,
int charsAdded);
QString getRecentChangesContext() const;
private:
ChangesManager();
~ChangesManager();
ChangesManager(const ChangesManager &) = delete;
ChangesManager &operator=(const ChangesManager &) = delete;
void cleanupOldChanges();
static const int MAX_CACHED_CHANGES = 50;
QHash<TextEditor::TextDocument *, QQueue<ChangeInfo>> m_documentChanges;
};
} // namespace QodeAssist