mirror of
https://github.com/Palm1r/QodeAssist.git
synced 2025-05-28 03:10:28 -04:00
Remove copyright from request
This commit is contained in:
parent
089bd0594e
commit
61aae8e3ba
@ -27,6 +27,13 @@
|
|||||||
|
|
||||||
namespace QodeAssist {
|
namespace QodeAssist {
|
||||||
|
|
||||||
|
DocumentContextReader::DocumentContextReader(TextEditor::TextDocument *textDocument)
|
||||||
|
: m_textDocument(textDocument)
|
||||||
|
, m_document(textDocument->document())
|
||||||
|
{
|
||||||
|
m_copyrightInfo = findCopyright();
|
||||||
|
}
|
||||||
|
|
||||||
QString DocumentContextReader::getLineText(int lineNumber, int cursorPosition) const
|
QString DocumentContextReader::getLineText(int lineNumber, int cursorPosition) const
|
||||||
{
|
{
|
||||||
if (!m_document || lineNumber < 0)
|
if (!m_document || lineNumber < 0)
|
||||||
@ -55,76 +62,41 @@ QString DocumentContextReader::getContextBefore(int lineNumber,
|
|||||||
int cursorPosition,
|
int cursorPosition,
|
||||||
int linesCount) const
|
int linesCount) const
|
||||||
{
|
{
|
||||||
QString context;
|
int effectiveStartLine;
|
||||||
for (int i = qMax(0, lineNumber - linesCount); i <= lineNumber; ++i) {
|
if (m_copyrightInfo.found) {
|
||||||
QString line = getLineText(i, i == lineNumber ? cursorPosition : -1);
|
effectiveStartLine = qMax(m_copyrightInfo.endLine + 1, lineNumber - linesCount);
|
||||||
context += line;
|
} else {
|
||||||
if (i < lineNumber)
|
effectiveStartLine = qMax(0, lineNumber - linesCount);
|
||||||
context += "\n";
|
|
||||||
}
|
}
|
||||||
return context;
|
|
||||||
|
return getContextBetween(effectiveStartLine, lineNumber, cursorPosition);
|
||||||
}
|
}
|
||||||
|
|
||||||
QString DocumentContextReader::getContextAfter(int lineNumber,
|
QString DocumentContextReader::getContextAfter(int lineNumber,
|
||||||
int cursorPosition,
|
int cursorPosition,
|
||||||
int linesCount) const
|
int linesCount) const
|
||||||
{
|
{
|
||||||
QString context;
|
int endLine = qMin(m_document->blockCount() - 1, lineNumber + linesCount);
|
||||||
int maxLine = lineNumber + linesCount;
|
return getContextBetween(lineNumber + 1, endLine, cursorPosition);
|
||||||
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 DocumentContextReader::readWholeFileBefore(int lineNumber, int cursorPosition) const
|
||||||
{
|
{
|
||||||
QString content;
|
int startLine = 0;
|
||||||
QTextBlock block = m_document->begin();
|
if (m_copyrightInfo.found) {
|
||||||
int currentLine = 0;
|
startLine = m_copyrightInfo.endLine + 1;
|
||||||
|
|
||||||
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;
|
startLine = qMin(startLine, lineNumber);
|
||||||
|
|
||||||
|
QString result = getContextBetween(startLine, lineNumber, cursorPosition);
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString DocumentContextReader::readWholeFileAfter(int lineNumber, int cursorPosition) const
|
QString DocumentContextReader::readWholeFileAfter(int lineNumber, int cursorPosition) const
|
||||||
{
|
{
|
||||||
QString content;
|
return getContextBetween(lineNumber, m_document->blockCount() - 1, cursorPosition);
|
||||||
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
|
QString DocumentContextReader::getLanguageAndFileInfo() const
|
||||||
@ -139,10 +111,7 @@ QString DocumentContextReader::getLanguageAndFileInfo() const
|
|||||||
QString fileExtension = QFileInfo(filePath).suffix();
|
QString fileExtension = QFileInfo(filePath).suffix();
|
||||||
|
|
||||||
return QString("//Language: %1 (MIME: %2) filepath: %3(%4)\n\n")
|
return QString("//Language: %1 (MIME: %2) filepath: %3(%4)\n\n")
|
||||||
.arg(language)
|
.arg(language, mimeType, filePath, fileExtension);
|
||||||
.arg(mimeType)
|
|
||||||
.arg(filePath)
|
|
||||||
.arg(fileExtension);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString DocumentContextReader::getSpecificInstructions() const
|
QString DocumentContextReader::getSpecificInstructions() const
|
||||||
@ -152,4 +121,69 @@ QString DocumentContextReader::getSpecificInstructions() const
|
|||||||
return QString("//Instructions: %1").arg(specificInstruction);
|
return QString("//Instructions: %1").arg(specificInstruction);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CopyrightInfo DocumentContextReader::findCopyright()
|
||||||
|
{
|
||||||
|
CopyrightInfo result = {-1, -1, false};
|
||||||
|
|
||||||
|
QString text = m_document->toPlainText();
|
||||||
|
QRegularExpressionMatchIterator matchIterator = getCopyrightRegex().globalMatch(text);
|
||||||
|
|
||||||
|
QList<CopyrightInfo> copyrightBlocks;
|
||||||
|
|
||||||
|
while (matchIterator.hasNext()) {
|
||||||
|
QRegularExpressionMatch match = matchIterator.next();
|
||||||
|
int startPos = match.capturedStart();
|
||||||
|
int endPos = match.capturedEnd();
|
||||||
|
|
||||||
|
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) {
|
||||||
|
if (copyrightBlocks[i].endLine + 1 >= copyrightBlocks[i + 1].startLine) {
|
||||||
|
copyrightBlocks[i].endLine = copyrightBlocks[i + 1].endLine;
|
||||||
|
copyrightBlocks.removeAt(i + 1);
|
||||||
|
--i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!copyrightBlocks.isEmpty()) { // temproary solution, need cache
|
||||||
|
return copyrightBlocks.first();
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString DocumentContextReader::getContextBetween(int startLine,
|
||||||
|
int endLine,
|
||||||
|
int cursorPosition) const
|
||||||
|
{
|
||||||
|
QString context;
|
||||||
|
for (int i = startLine; i <= endLine; ++i) {
|
||||||
|
QTextBlock block = m_document->findBlockByNumber(i);
|
||||||
|
if (!block.isValid()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (i == endLine) {
|
||||||
|
context += block.text().left(cursorPosition);
|
||||||
|
} else {
|
||||||
|
context += block.text() + "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return context;
|
||||||
|
}
|
||||||
|
|
||||||
|
const QRegularExpression &DocumentContextReader::getCopyrightRegex()
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace QodeAssist
|
} // namespace QodeAssist
|
||||||
|
@ -24,13 +24,17 @@
|
|||||||
|
|
||||||
namespace QodeAssist {
|
namespace QodeAssist {
|
||||||
|
|
||||||
|
struct CopyrightInfo
|
||||||
|
{
|
||||||
|
int startLine;
|
||||||
|
int endLine;
|
||||||
|
bool found;
|
||||||
|
};
|
||||||
|
|
||||||
class DocumentContextReader
|
class DocumentContextReader
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
DocumentContextReader(TextEditor::TextDocument *textDocument)
|
DocumentContextReader(TextEditor::TextDocument *textDocument);
|
||||||
: m_textDocument(textDocument)
|
|
||||||
, m_document(textDocument->document())
|
|
||||||
{}
|
|
||||||
|
|
||||||
QString getLineText(int lineNumber, int cursorPosition = -1) const;
|
QString getLineText(int lineNumber, int cursorPosition = -1) const;
|
||||||
QString getContextBefore(int lineNumber, int cursorPosition, int linesCount) const;
|
QString getContextBefore(int lineNumber, int cursorPosition, int linesCount) const;
|
||||||
@ -39,10 +43,15 @@ public:
|
|||||||
QString readWholeFileAfter(int lineNumber, int cursorPosition) const;
|
QString readWholeFileAfter(int lineNumber, int cursorPosition) const;
|
||||||
QString getLanguageAndFileInfo() const;
|
QString getLanguageAndFileInfo() const;
|
||||||
QString getSpecificInstructions() const;
|
QString getSpecificInstructions() const;
|
||||||
|
CopyrightInfo findCopyright();
|
||||||
|
QString getContextBetween(int startLine, int endLine, int cursorPosition) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TextEditor::TextDocument *m_textDocument;
|
TextEditor::TextDocument *m_textDocument;
|
||||||
QTextDocument *m_document;
|
QTextDocument *m_document;
|
||||||
|
CopyrightInfo m_copyrightInfo;
|
||||||
|
|
||||||
|
static const QRegularExpression &getCopyrightRegex();
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QodeAssist
|
} // namespace QodeAssist
|
||||||
|
@ -101,6 +101,9 @@ QString LLMClientInterface::сontextBefore(TextEditor::TextEditorWidget *widget,
|
|||||||
DocumentContextReader reader(widget->textDocument());
|
DocumentContextReader reader(widget->textDocument());
|
||||||
QString languageAndFileInfo = reader.getLanguageAndFileInfo();
|
QString languageAndFileInfo = reader.getLanguageAndFileInfo();
|
||||||
|
|
||||||
|
if (lineNumber < reader.findCopyright().endLine)
|
||||||
|
return QString();
|
||||||
|
|
||||||
QString contextBefore;
|
QString contextBefore;
|
||||||
if (settings().readFullFile()) {
|
if (settings().readFullFile()) {
|
||||||
contextBefore = reader.readWholeFileBefore(lineNumber, cursorPosition);
|
contextBefore = reader.readWholeFileBefore(lineNumber, cursorPosition);
|
||||||
@ -124,6 +127,8 @@ QString LLMClientInterface::сontextAfter(TextEditor::TextEditorWidget *widget,
|
|||||||
return QString();
|
return QString();
|
||||||
|
|
||||||
DocumentContextReader reader(widget->textDocument());
|
DocumentContextReader reader(widget->textDocument());
|
||||||
|
if (lineNumber < reader.findCopyright().endLine)
|
||||||
|
return QString();
|
||||||
|
|
||||||
QString contextAfter;
|
QString contextAfter;
|
||||||
if (settings().readFullFile()) {
|
if (settings().readFullFile()) {
|
||||||
|
@ -211,12 +211,12 @@ QodeAssistSettings::QodeAssistSettings()
|
|||||||
Form{Column{Row{selectModels, modelName}}}},
|
Form{Column{Row{selectModels, modelName}}}},
|
||||||
Group{title(Tr::tr("FIM Prompt Settings")),
|
Group{title(Tr::tr("FIM Prompt Settings")),
|
||||||
Form{Column{fimPrompts,
|
Form{Column{fimPrompts,
|
||||||
readFullFile,
|
|
||||||
maxFileThreshold,
|
maxFileThreshold,
|
||||||
ollamaLivetime,
|
ollamaLivetime,
|
||||||
specificInstractions,
|
specificInstractions,
|
||||||
temperature,
|
temperature,
|
||||||
maxTokens,
|
maxTokens,
|
||||||
|
readFullFile,
|
||||||
readStringsBeforeCursor,
|
readStringsBeforeCursor,
|
||||||
readStringsAfterCursor,
|
readStringsAfterCursor,
|
||||||
startSuggestionTimer,
|
startSuggestionTimer,
|
||||||
|
Loading…
Reference in New Issue
Block a user