mirror of
https://github.com/Palm1r/QodeAssist.git
synced 2025-06-04 01:28:58 -04:00
Add basic cache manager
This commit is contained in:
parent
f07610df5c
commit
cd1a9e16e0
@ -54,4 +54,5 @@ add_qtc_plugin(QodeAssist
|
|||||||
settings/CustomPromptSettings.hpp settings/CustomPromptSettings.cpp
|
settings/CustomPromptSettings.hpp settings/CustomPromptSettings.cpp
|
||||||
settings/PresetPromptsSettings.hpp settings/PresetPromptsSettings.cpp
|
settings/PresetPromptsSettings.hpp settings/PresetPromptsSettings.cpp
|
||||||
settings/SettingsUtils.hpp
|
settings/SettingsUtils.hpp
|
||||||
|
core/ChangesManager.h core/ChangesManager.cpp
|
||||||
)
|
)
|
||||||
|
@ -29,6 +29,7 @@
|
|||||||
#include "LLMProvidersManager.hpp"
|
#include "LLMProvidersManager.hpp"
|
||||||
#include "PromptTemplateManager.hpp"
|
#include "PromptTemplateManager.hpp"
|
||||||
#include "QodeAssistUtils.hpp"
|
#include "QodeAssistUtils.hpp"
|
||||||
|
#include "core/ChangesManager.h"
|
||||||
#include "settings/ContextSettings.hpp"
|
#include "settings/ContextSettings.hpp"
|
||||||
#include "settings/GeneralSettings.hpp"
|
#include "settings/GeneralSettings.hpp"
|
||||||
|
|
||||||
@ -277,14 +278,17 @@ ContextData LLMClientInterface::prepareContext(const QJsonObject &request,
|
|||||||
|
|
||||||
DocumentContextReader reader(widget->textDocument());
|
DocumentContextReader reader(widget->textDocument());
|
||||||
|
|
||||||
|
QString recentChanges = ChangesManager::instance().getRecentChangesContext();
|
||||||
|
|
||||||
QString contextBefore = сontextBefore(widget, lineNumber, cursorPosition);
|
QString contextBefore = сontextBefore(widget, lineNumber, cursorPosition);
|
||||||
QString contextAfter = сontextAfter(widget, lineNumber, cursorPosition);
|
QString contextAfter = сontextAfter(widget, lineNumber, cursorPosition);
|
||||||
QString instructions = QString("%1%2").arg(Settings::contextSettings().useSpecificInstructions()
|
QString instructions = QString("%1%2%3").arg(Settings::contextSettings().useSpecificInstructions()
|
||||||
? reader.getSpecificInstructions()
|
? reader.getSpecificInstructions()
|
||||||
: QString(),
|
: QString(),
|
||||||
Settings::contextSettings().useFilePathInContext()
|
Settings::contextSettings().useFilePathInContext()
|
||||||
? reader.getLanguageAndFileInfo()
|
? reader.getLanguageAndFileInfo()
|
||||||
: QString());
|
: QString(),
|
||||||
|
recentChanges);
|
||||||
|
|
||||||
return {QString("%1%2").arg(contextBefore, accumulatedCompletion), contextAfter, instructions};
|
return {QString("%1%2").arg(contextBefore, accumulatedCompletion), contextAfter, instructions};
|
||||||
}
|
}
|
||||||
|
@ -31,6 +31,7 @@
|
|||||||
|
|
||||||
#include "LLMClientInterface.hpp"
|
#include "LLMClientInterface.hpp"
|
||||||
#include "LLMSuggestion.hpp"
|
#include "LLMSuggestion.hpp"
|
||||||
|
#include "core/ChangesManager.h"
|
||||||
#include "settings/GeneralSettings.hpp"
|
#include "settings/GeneralSettings.hpp"
|
||||||
|
|
||||||
using namespace LanguageServerProtocol;
|
using namespace LanguageServerProtocol;
|
||||||
@ -83,6 +84,9 @@ void QodeAssistClient::openDocument(TextEditor::TextDocument *document)
|
|||||||
auto textEditor = BaseTextEditor::currentTextEditor();
|
auto textEditor = BaseTextEditor::currentTextEditor();
|
||||||
if (!textEditor || textEditor->document() != document)
|
if (!textEditor || textEditor->document() != document)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
ChangesManager::instance().addChange(document, position, charsRemoved, charsAdded);
|
||||||
|
|
||||||
TextEditorWidget *widget = textEditor->editorWidget();
|
TextEditorWidget *widget = textEditor->editorWidget();
|
||||||
if (widget->isReadOnly() || widget->multiTextCursor().hasMultipleCursors())
|
if (widget->isReadOnly() || widget->multiTextCursor().hasMultipleCursors())
|
||||||
return;
|
return;
|
||||||
|
86
core/ChangesManager.cpp
Normal file
86
core/ChangesManager.cpp
Normal 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
62
core/ChangesManager.h
Normal 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
|
Loading…
x
Reference in New Issue
Block a user