Fix remove comments from request

This commit is contained in:
Petr Mironychev 2024-08-29 22:26:40 +02:00
parent 7797007160
commit d6320b04f7
4 changed files with 43 additions and 20 deletions

View File

@ -25,6 +25,26 @@
#include "QodeAssistSettings.hpp"
const QRegularExpression &getYearRegex()
{
static const QRegularExpression yearRegex("\\b(19|20)\\d{2}\\b");
return yearRegex;
}
const QRegularExpression &getNameRegex()
{
static const QRegularExpression nameRegex("\\b[A-Z][a-z.]+ [A-Z][a-z.]+\\b");
return nameRegex;
}
const QRegularExpression &getCommentRegex()
{
static const QRegularExpression
commentRegex(R"((/\*[\s\S]*?\*/|//.*$|#.*$|//{2,}[\s\S]*?//{2,}))",
QRegularExpression::MultilineOption);
return commentRegex;
}
namespace QodeAssist {
DocumentContextReader::DocumentContextReader(TextEditor::TextDocument *textDocument)
@ -126,21 +146,27 @@ CopyrightInfo DocumentContextReader::findCopyright()
CopyrightInfo result = {-1, -1, false};
QString text = m_document->toPlainText();
QRegularExpressionMatchIterator matchIterator = getCopyrightRegex().globalMatch(text);
QRegularExpressionMatchIterator matchIterator = getCommentRegex().globalMatch(text);
QList<CopyrightInfo> copyrightBlocks;
while (matchIterator.hasNext()) {
QRegularExpressionMatch match = matchIterator.next();
int startPos = match.capturedStart();
int endPos = match.capturedEnd();
QString matchedText = match.captured().toLower();
CopyrightInfo info;
info.startLine = m_document->findBlock(startPos).blockNumber();
info.endLine = m_document->findBlock(endPos).blockNumber();
info.found = true;
if (matchedText.contains("copyright") || matchedText.contains("(C)")
|| matchedText.contains("(c)") || matchedText.contains("©")
|| getYearRegex().match(text).hasMatch() || getNameRegex().match(text).hasMatch()) {
int startPos = match.capturedStart();
int endPos = match.capturedEnd();
copyrightBlocks.append(info);
CopyrightInfo info;
info.startLine = m_document->findBlock(startPos).blockNumber();
info.endLine = m_document->findBlock(endPos).blockNumber();
info.found = true;
copyrightBlocks.append(info);
}
}
for (int i = 0; i < copyrightBlocks.size() - 1; ++i) {
@ -178,12 +204,9 @@ QString DocumentContextReader::getContextBetween(int startLine,
return context;
}
const QRegularExpression &DocumentContextReader::getCopyrightRegex()
CopyrightInfo DocumentContextReader::copyrightInfo() const
{
static const QRegularExpression copyrightRegex(
R"((?:/\*[\s\S]*?Copyright[\s\S]*?\*/| // Copyright[\s\S]*?(?:\n\s*//.*)*|///.*Copyright[\s\S]*?(?:\n\s*///.*)*)|(?://))",
QRegularExpression::MultilineOption | QRegularExpression::CaseInsensitiveOption);
return copyrightRegex;
return m_copyrightInfo;
}
} // namespace QodeAssist

View File

@ -46,12 +46,12 @@ public:
CopyrightInfo findCopyright();
QString getContextBetween(int startLine, int endLine, int cursorPosition) const;
CopyrightInfo copyrightInfo() const;
private:
TextEditor::TextDocument *m_textDocument;
QTextDocument *m_document;
CopyrightInfo m_copyrightInfo;
static const QRegularExpression &getCopyrightRegex();
};
} // namespace QodeAssist

View File

@ -120,8 +120,10 @@ QString LLMClientInterface::сontextBefore(TextEditor::TextEditorWidget *widget,
return QString();
DocumentContextReader reader(widget->textDocument());
QString languageAndFileInfo = reader.getLanguageAndFileInfo();
const auto &copyright = reader.copyrightInfo();
logMessage(QString{"Line Number: %1"}.arg(lineNumber));
logMessage(QString("Copyright found %1 %2").arg(copyright.found).arg(copyright.endLine));
if (lineNumber < reader.findCopyright().endLine)
return QString();
@ -135,9 +137,7 @@ QString LLMClientInterface::сontextBefore(TextEditor::TextEditorWidget *widget,
}
return QString("%1\n%2\n%3")
.arg(reader.getSpecificInstructions())
.arg(reader.getLanguageAndFileInfo())
.arg(contextBefore);
.arg(reader.getSpecificInstructions(), reader.getLanguageAndFileInfo(), contextBefore);
}
QString LLMClientInterface::сontextAfter(TextEditor::TextEditorWidget *widget,

View File

@ -1,6 +1,6 @@
{
"Name" : "QodeAssist",
"Version" : "0.0.4",
"Version" : "0.0.5",
"CompatVersion" : "${IDE_VERSION_COMPAT}",
"Vendor" : "Petr Mironychev",
"Copyright" : "(C) ${IDE_COPYRIGHT_YEAR} Petr Mironychev, (C) ${IDE_COPYRIGHT_YEAR} The Qt Company Ltd",