mirror of
https://github.com/Palm1r/QodeAssist.git
synced 2026-02-13 10:33:05 -05:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e799d0bd00 | |||
| fc58c38f63 | |||
| 989718017a | |||
| a324df8dbf |
@ -45,4 +45,5 @@ add_qtc_plugin(QodeAssist
|
|||||||
QodeAssistHoverHandler.hpp QodeAssistHoverHandler.cpp
|
QodeAssistHoverHandler.hpp QodeAssistHoverHandler.cpp
|
||||||
QodeAssistClient.hpp QodeAssistClient.cpp
|
QodeAssistClient.hpp QodeAssistClient.cpp
|
||||||
QodeAssistUtils.hpp
|
QodeAssistUtils.hpp
|
||||||
|
DocumentContextReader.hpp DocumentContextReader.cpp
|
||||||
)
|
)
|
||||||
|
|||||||
155
DocumentContextReader.cpp
Normal file
155
DocumentContextReader.cpp
Normal file
@ -0,0 +1,155 @@
|
|||||||
|
/*
|
||||||
|
* 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 "DocumentContextReader.hpp"
|
||||||
|
|
||||||
|
#include <QFileInfo>
|
||||||
|
#include <QTextBlock>
|
||||||
|
#include <languageserverprotocol/lsptypes.h>
|
||||||
|
|
||||||
|
#include "QodeAssistSettings.hpp"
|
||||||
|
|
||||||
|
namespace QodeAssist {
|
||||||
|
|
||||||
|
QString DocumentContextReader::getLineText(int lineNumber, int cursorPosition) const
|
||||||
|
{
|
||||||
|
if (!m_document || lineNumber < 0)
|
||||||
|
return QString();
|
||||||
|
|
||||||
|
QTextBlock block = m_document->begin();
|
||||||
|
int currentLine = 0;
|
||||||
|
|
||||||
|
while (block.isValid()) {
|
||||||
|
if (currentLine == lineNumber) {
|
||||||
|
QString text = block.text();
|
||||||
|
if (cursorPosition >= 0 && cursorPosition <= text.length()) {
|
||||||
|
text = text.left(cursorPosition);
|
||||||
|
}
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
|
||||||
|
block = block.next();
|
||||||
|
currentLine++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return QString();
|
||||||
|
}
|
||||||
|
|
||||||
|
QString DocumentContextReader::getContextBefore(int lineNumber,
|
||||||
|
int cursorPosition,
|
||||||
|
int linesCount) const
|
||||||
|
{
|
||||||
|
QString context;
|
||||||
|
for (int i = qMax(0, lineNumber - linesCount); i <= lineNumber; ++i) {
|
||||||
|
QString line = getLineText(i, i == lineNumber ? cursorPosition : -1);
|
||||||
|
context += line;
|
||||||
|
if (i < lineNumber)
|
||||||
|
context += "\n";
|
||||||
|
}
|
||||||
|
return context;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString DocumentContextReader::getContextAfter(int lineNumber,
|
||||||
|
int cursorPosition,
|
||||||
|
int linesCount) const
|
||||||
|
{
|
||||||
|
QString context;
|
||||||
|
int maxLine = lineNumber + linesCount;
|
||||||
|
for (int i = lineNumber; i <= maxLine; ++i) {
|
||||||
|
QString line = getLineText(i);
|
||||||
|
if (i == lineNumber && cursorPosition >= 0) {
|
||||||
|
line = line.mid(cursorPosition);
|
||||||
|
}
|
||||||
|
context += line;
|
||||||
|
if (i < maxLine && !line.isEmpty())
|
||||||
|
context += "\n";
|
||||||
|
}
|
||||||
|
return context;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString DocumentContextReader::readWholeFileBefore(int lineNumber, int cursorPosition) const
|
||||||
|
{
|
||||||
|
QString content;
|
||||||
|
QTextBlock block = m_document->begin();
|
||||||
|
int currentLine = 0;
|
||||||
|
|
||||||
|
while (block.isValid() && currentLine <= lineNumber) {
|
||||||
|
if (currentLine == lineNumber) {
|
||||||
|
content += block.text().left(cursorPosition);
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
content += block.text() + "\n";
|
||||||
|
}
|
||||||
|
block = block.next();
|
||||||
|
currentLine++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString DocumentContextReader::readWholeFileAfter(int lineNumber, int cursorPosition) const
|
||||||
|
{
|
||||||
|
QString content;
|
||||||
|
QTextBlock block = m_document->begin();
|
||||||
|
int currentLine = 0;
|
||||||
|
|
||||||
|
while (block.isValid() && currentLine < lineNumber) {
|
||||||
|
block = block.next();
|
||||||
|
currentLine++;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (block.isValid()) {
|
||||||
|
if (currentLine == lineNumber) {
|
||||||
|
content += block.text().mid(cursorPosition) + "\n";
|
||||||
|
} else {
|
||||||
|
content += block.text() + "\n";
|
||||||
|
}
|
||||||
|
block = block.next();
|
||||||
|
currentLine++;
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
48
DocumentContextReader.hpp
Normal file
48
DocumentContextReader.hpp
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* 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 <QTextDocument>
|
||||||
|
#include <texteditor/textdocument.h>
|
||||||
|
|
||||||
|
namespace QodeAssist {
|
||||||
|
|
||||||
|
class DocumentContextReader
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
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;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace QodeAssist
|
||||||
@ -25,6 +25,7 @@
|
|||||||
|
|
||||||
#include <texteditor/textdocument.h>
|
#include <texteditor/textdocument.h>
|
||||||
|
|
||||||
|
#include "DocumentContextReader.hpp"
|
||||||
#include "LLMProvidersManager.hpp"
|
#include "LLMProvidersManager.hpp"
|
||||||
#include "PromptTemplateManager.hpp"
|
#include "PromptTemplateManager.hpp"
|
||||||
#include "QodeAssistSettings.hpp"
|
#include "QodeAssistSettings.hpp"
|
||||||
@ -97,39 +98,22 @@ QString LLMClientInterface::сontextBefore(TextEditor::TextEditorWidget *widget,
|
|||||||
if (!widget)
|
if (!widget)
|
||||||
return QString();
|
return QString();
|
||||||
|
|
||||||
QTextDocument *doc = widget->document();
|
DocumentContextReader reader(widget->textDocument());
|
||||||
int totalLines = doc->blockCount();
|
QString languageAndFileInfo = reader.getLanguageAndFileInfo();
|
||||||
QTextBlock currentBlock = doc->findBlockByLineNumber(lineNumber);
|
|
||||||
|
|
||||||
QString beforeCursor;
|
QString contextBefore;
|
||||||
|
if (settings().readFullFile()) {
|
||||||
if (settings().readFullFile() && totalLines < settings().maxFileThreshold()) {
|
contextBefore = reader.readWholeFileBefore(lineNumber, cursorPosition);
|
||||||
// Read all content from the beginning of the file to the cursor
|
|
||||||
QTextBlock block = doc->begin();
|
|
||||||
while (block.isValid() && block.blockNumber() <= lineNumber) {
|
|
||||||
if (block.blockNumber() == lineNumber) {
|
|
||||||
beforeCursor += block.text().left(cursorPosition);
|
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
beforeCursor += block.text() + "\n";
|
|
||||||
}
|
|
||||||
block = block.next();
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// Read only the specified number of lines before the cursor
|
contextBefore = reader.getContextBefore(lineNumber,
|
||||||
int contextLinesBefore = settings().readStringsBeforeCursor();
|
cursorPosition,
|
||||||
QTextBlock block = currentBlock;
|
settings().readStringsBeforeCursor());
|
||||||
for (int i = 0; i < contextLinesBefore && block.isValid(); ++i) {
|
|
||||||
if (block.blockNumber() == lineNumber) {
|
|
||||||
beforeCursor = block.text().left(cursorPosition) + beforeCursor;
|
|
||||||
} else {
|
|
||||||
beforeCursor = block.text() + "\n" + beforeCursor;
|
|
||||||
}
|
|
||||||
block = block.previous();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return beforeCursor;
|
return QString("%1\n%2\n%3")
|
||||||
|
.arg(reader.getSpecificInstructions())
|
||||||
|
.arg(reader.getLanguageAndFileInfo())
|
||||||
|
.arg(contextBefore);
|
||||||
}
|
}
|
||||||
|
|
||||||
QString LLMClientInterface::сontextAfter(TextEditor::TextEditorWidget *widget,
|
QString LLMClientInterface::сontextAfter(TextEditor::TextEditorWidget *widget,
|
||||||
@ -139,41 +123,18 @@ QString LLMClientInterface::сontextAfter(TextEditor::TextEditorWidget *widget,
|
|||||||
if (!widget)
|
if (!widget)
|
||||||
return QString();
|
return QString();
|
||||||
|
|
||||||
QTextDocument *doc = widget->document();
|
DocumentContextReader reader(widget->textDocument());
|
||||||
int totalLines = doc->blockCount();
|
|
||||||
QTextBlock currentBlock = doc->findBlockByLineNumber(lineNumber);
|
|
||||||
|
|
||||||
QString afterCursor;
|
QString contextAfter;
|
||||||
|
if (settings().readFullFile()) {
|
||||||
if (settings().readFullFile() && totalLines < settings().maxFileThreshold()) {
|
contextAfter = reader.readWholeFileAfter(lineNumber, cursorPosition);
|
||||||
// Read all content from the cursor to the end of the file
|
|
||||||
QTextBlock block = currentBlock;
|
|
||||||
bool isFirstBlock = true;
|
|
||||||
while (block.isValid()) {
|
|
||||||
if (isFirstBlock) {
|
|
||||||
afterCursor += block.text().mid(cursorPosition) + "\n";
|
|
||||||
isFirstBlock = false;
|
|
||||||
} else {
|
|
||||||
afterCursor += block.text() + "\n";
|
|
||||||
}
|
|
||||||
block = block.next();
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// Read only the specified number of lines after the cursor
|
contextAfter = reader.getContextAfter(lineNumber,
|
||||||
int contextLinesAfter = settings().readStringsAfterCursor();
|
cursorPosition,
|
||||||
QTextBlock block = currentBlock;
|
settings().readStringsAfterCursor());
|
||||||
for (int i = 0; i < contextLinesAfter && block.isValid(); ++i) {
|
|
||||||
if (block.blockNumber() == lineNumber) {
|
|
||||||
afterCursor += block.text().mid(cursorPosition);
|
|
||||||
} else {
|
|
||||||
afterCursor += block.text();
|
|
||||||
}
|
|
||||||
afterCursor += "\n";
|
|
||||||
block = block.next();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return afterCursor;
|
return contextAfter;
|
||||||
}
|
}
|
||||||
|
|
||||||
void LLMClientInterface::handleInitialize(const QJsonObject &request)
|
void LLMClientInterface::handleInitialize(const QJsonObject &request)
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"Name" : "QodeAssist",
|
"Name" : "QodeAssist",
|
||||||
"Version" : "0.0.1",
|
"Version" : "0.0.3",
|
||||||
"CompatVersion" : "${IDE_VERSION_COMPAT}",
|
"CompatVersion" : "${IDE_VERSION_COMPAT}",
|
||||||
"Vendor" : "Petr Mironychev",
|
"Vendor" : "Petr Mironychev",
|
||||||
"Copyright" : "(C) ${IDE_COPYRIGHT_YEAR} Petr Mironychev, (C) ${IDE_COPYRIGHT_YEAR} The Qt Company Ltd",
|
"Copyright" : "(C) ${IDE_COPYRIGHT_YEAR} Petr Mironychev, (C) ${IDE_COPYRIGHT_YEAR} The Qt Company Ltd",
|
||||||
|
|||||||
@ -52,6 +52,7 @@ const char PROVIDER_PATHS[] = "QodeAssist.providerPaths";
|
|||||||
const char START_SUGGESTION_TIMER[] = "QodeAssist.startSuggestionTimer";
|
const char START_SUGGESTION_TIMER[] = "QodeAssist.startSuggestionTimer";
|
||||||
const char MAX_FILE_THRESHOLD[] = "QodeAssist.maxFileThreshold";
|
const char MAX_FILE_THRESHOLD[] = "QodeAssist.maxFileThreshold";
|
||||||
const char OLLAMA_LIVETIME[] = "QodeAssist.ollamaLivetime";
|
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_ID[] = "QodeAssist.GeneralOptions";
|
||||||
const char QODE_ASSIST_GENERAL_OPTIONS_CATEGORY[] = "QodeAssist.Category";
|
const char QODE_ASSIST_GENERAL_OPTIONS_CATEGORY[] = "QodeAssist.Category";
|
||||||
|
|||||||
@ -97,7 +97,7 @@ QodeAssistSettings::QodeAssistSettings()
|
|||||||
|
|
||||||
readFullFile.setSettingsKey(Constants::READ_FULL_FILE);
|
readFullFile.setSettingsKey(Constants::READ_FULL_FILE);
|
||||||
readFullFile.setLabelText(Tr::tr("Read Full File"));
|
readFullFile.setLabelText(Tr::tr("Read Full File"));
|
||||||
readFullFile.setDefaultValue(true);
|
readFullFile.setDefaultValue(false);
|
||||||
|
|
||||||
maxFileThreshold.setSettingsKey(Constants::MAX_FILE_THRESHOLD);
|
maxFileThreshold.setSettingsKey(Constants::MAX_FILE_THRESHOLD);
|
||||||
maxFileThreshold.setLabelText(Tr::tr("Max File Threshold:"));
|
maxFileThreshold.setLabelText(Tr::tr("Max File Threshold:"));
|
||||||
@ -106,11 +106,11 @@ QodeAssistSettings::QodeAssistSettings()
|
|||||||
|
|
||||||
readStringsBeforeCursor.setSettingsKey(Constants::READ_STRINGS_BEFORE_CURSOR);
|
readStringsBeforeCursor.setSettingsKey(Constants::READ_STRINGS_BEFORE_CURSOR);
|
||||||
readStringsBeforeCursor.setLabelText(Tr::tr("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.setSettingsKey(Constants::READ_STRINGS_AFTER_CURSOR);
|
||||||
readStringsAfterCursor.setLabelText(Tr::tr("Read Strings After Cursor"));
|
readStringsAfterCursor.setLabelText(Tr::tr("Read Strings After Cursor"));
|
||||||
readStringsAfterCursor.setDefaultValue(40);
|
readStringsAfterCursor.setDefaultValue(30);
|
||||||
|
|
||||||
maxTokens.setSettingsKey(Constants::MAX_TOKENS);
|
maxTokens.setSettingsKey(Constants::MAX_TOKENS);
|
||||||
maxTokens.setLabelText(Tr::tr("Max Tokens"));
|
maxTokens.setLabelText(Tr::tr("Max Tokens"));
|
||||||
@ -157,6 +157,14 @@ QodeAssistSettings::QodeAssistSettings()
|
|||||||
startSuggestionTimer.setRange(10, 10000);
|
startSuggestionTimer.setRange(10, 10000);
|
||||||
startSuggestionTimer.setDefaultValue(500);
|
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");
|
resetToDefaults.m_buttonText = Tr::tr("Reset to Defaults");
|
||||||
|
|
||||||
const auto &manager = LLMProvidersManager::instance();
|
const auto &manager = LLMProvidersManager::instance();
|
||||||
@ -206,6 +214,7 @@ QodeAssistSettings::QodeAssistSettings()
|
|||||||
readFullFile,
|
readFullFile,
|
||||||
maxFileThreshold,
|
maxFileThreshold,
|
||||||
ollamaLivetime,
|
ollamaLivetime,
|
||||||
|
specificInstractions,
|
||||||
temperature,
|
temperature,
|
||||||
maxTokens,
|
maxTokens,
|
||||||
readStringsBeforeCursor,
|
readStringsBeforeCursor,
|
||||||
@ -349,6 +358,7 @@ void QodeAssistSettings::resetSettingsToDefaults()
|
|||||||
resetAspect(startSuggestionTimer);
|
resetAspect(startSuggestionTimer);
|
||||||
resetAspect(enableLogging);
|
resetAspect(enableLogging);
|
||||||
resetAspect(ollamaLivetime);
|
resetAspect(ollamaLivetime);
|
||||||
|
resetAspect(specificInstractions);
|
||||||
|
|
||||||
updateProviderSettings();
|
updateProviderSettings();
|
||||||
apply();
|
apply();
|
||||||
|
|||||||
@ -95,6 +95,7 @@ public:
|
|||||||
Utils::IntegerAspect maxFileThreshold{this};
|
Utils::IntegerAspect maxFileThreshold{this};
|
||||||
|
|
||||||
Utils::StringAspect ollamaLivetime{this};
|
Utils::StringAspect ollamaLivetime{this};
|
||||||
|
Utils::StringAspect specificInstractions{this};
|
||||||
|
|
||||||
ButtonAspect resetToDefaults{this};
|
ButtonAspect resetToDefaults{this};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user