fix: Properly omit copyright information (#103)

This commit ensures that copyright information is always excluded and
that context is always split into prefix and suffix at correct position.
This commit is contained in:
Povilas Kanapickas
2025-03-06 14:00:15 +02:00
committed by GitHub
parent 8a167bf248
commit 61ca5c9a1b
3 changed files with 94 additions and 68 deletions

View File

@ -84,18 +84,22 @@ QString DocumentContextReader::getLineText(int lineNumber, int cursorPosition) c
QString DocumentContextReader::getContextBefore(
int lineNumber, int cursorPosition, int linesCount) const
{
int effectiveStartLine = qMax(0, lineNumber - linesCount + 1);
int startLine = lineNumber - linesCount + 1;
if (m_copyrightInfo.found) {
effectiveStartLine = qMax(m_copyrightInfo.endLine + 1, effectiveStartLine);
startLine = qMax(m_copyrightInfo.endLine + 1, startLine);
}
return getContextBetween(effectiveStartLine, -1, lineNumber, cursorPosition);
return getContextBetween(startLine, -1, lineNumber, cursorPosition);
}
QString DocumentContextReader::getContextAfter(
int lineNumber, int cursorPosition, int linesCount) const
{
int endLine = qMin(m_document->blockCount() - 1, lineNumber + linesCount - 1);
int endLine = lineNumber + linesCount - 1;
if (m_copyrightInfo.found && m_copyrightInfo.endLine >= lineNumber) {
lineNumber = m_copyrightInfo.endLine + 1;
cursorPosition = -1;
}
return getContextBetween(lineNumber, cursorPosition, endLine, -1);
}
@ -106,14 +110,17 @@ QString DocumentContextReader::readWholeFileBefore(int lineNumber, int cursorPos
startLine = m_copyrightInfo.endLine + 1;
}
startLine = qMin(startLine, lineNumber);
return getContextBetween(startLine, -1, lineNumber, cursorPosition);
}
QString DocumentContextReader::readWholeFileAfter(int lineNumber, int cursorPosition) const
{
return getContextBetween(lineNumber, cursorPosition, m_document->blockCount() - 1, -1);
int endLine = m_document->blockCount() - 1;
if (m_copyrightInfo.found && m_copyrightInfo.endLine >= lineNumber) {
lineNumber = m_copyrightInfo.endLine + 1;
cursorPosition = -1;
}
return getContextBetween(lineNumber, cursorPosition, endLine, -1);
}
QString DocumentContextReader::getLanguageAndFileInfo() const
@ -173,6 +180,9 @@ QString DocumentContextReader::getContextBetween(
{
QString context;
startLine = qMax(startLine, 0);
endLine = qMin(endLine, m_document->blockCount() - 1);
if (startLine > endLine) {
return context;
}