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

View File

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

View File

@ -120,8 +120,10 @@ QString LLMClientInterface::сontextBefore(TextEditor::TextEditorWidget *widget,
return QString(); return QString();
DocumentContextReader reader(widget->textDocument()); 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) if (lineNumber < reader.findCopyright().endLine)
return QString(); return QString();
@ -135,9 +137,7 @@ QString LLMClientInterface::сontextBefore(TextEditor::TextEditorWidget *widget,
} }
return QString("%1\n%2\n%3") return QString("%1\n%2\n%3")
.arg(reader.getSpecificInstructions()) .arg(reader.getSpecificInstructions(), reader.getLanguageAndFileInfo(), contextBefore);
.arg(reader.getLanguageAndFileInfo())
.arg(contextBefore);
} }
QString LLMClientInterface::сontextAfter(TextEditor::TextEditorWidget *widget, QString LLMClientInterface::сontextAfter(TextEditor::TextEditorWidget *widget,

View File

@ -1,6 +1,6 @@
{ {
"Name" : "QodeAssist", "Name" : "QodeAssist",
"Version" : "0.0.4", "Version" : "0.0.5",
"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",