mirror of
https://github.com/Palm1r/QodeAssist.git
synced 2025-05-28 03:10:28 -04:00
refactor: Make DocumentContextReader usable outside Qt Creator context (#89)
This makes it possible to write simple unit tests for it without running full Qt Creator. Not coupling DocumentContextReader to TextEditor::TextDocument unnecessarily is also a better design in general.
This commit is contained in:
parent
29a3939c64
commit
bcf7b6c226
@ -265,7 +265,8 @@ LLMCore::ContextData LLMClientInterface::prepareContext(
|
|||||||
int cursorPosition = position["character"].toInt();
|
int cursorPosition = position["character"].toInt();
|
||||||
int lineNumber = position["line"].toInt();
|
int lineNumber = position["line"].toInt();
|
||||||
|
|
||||||
Context::DocumentContextReader reader(textDocument);
|
Context::DocumentContextReader reader(
|
||||||
|
textDocument->document(), textDocument->mimeType(), textDocument->filePath().toString());
|
||||||
return reader.prepareContext(lineNumber, cursorPosition);
|
return reader.prepareContext(lineNumber, cursorPosition);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,9 +48,11 @@ const QRegularExpression &getCommentRegex()
|
|||||||
|
|
||||||
namespace QodeAssist::Context {
|
namespace QodeAssist::Context {
|
||||||
|
|
||||||
DocumentContextReader::DocumentContextReader(TextEditor::TextDocument *textDocument)
|
DocumentContextReader::DocumentContextReader(
|
||||||
: m_textDocument(textDocument)
|
QTextDocument *document, const QString &mimeType, const QString &filePath)
|
||||||
, m_document(textDocument->document())
|
: m_document(document)
|
||||||
|
, m_mimeType(mimeType)
|
||||||
|
, m_filePath(filePath)
|
||||||
{
|
{
|
||||||
m_copyrightInfo = findCopyright();
|
m_copyrightInfo = findCopyright();
|
||||||
}
|
}
|
||||||
@ -120,17 +122,11 @@ QString DocumentContextReader::readWholeFileAfter(int lineNumber, int cursorPosi
|
|||||||
|
|
||||||
QString DocumentContextReader::getLanguageAndFileInfo() const
|
QString DocumentContextReader::getLanguageAndFileInfo() const
|
||||||
{
|
{
|
||||||
if (!m_textDocument)
|
QString language = LanguageServerProtocol::TextDocumentItem::mimeTypeToLanguageId(m_mimeType);
|
||||||
return QString();
|
QString fileExtension = QFileInfo(m_filePath).suffix();
|
||||||
|
|
||||||
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")
|
return QString("Language: %1 (MIME: %2) filepath: %3(%4)\n\n")
|
||||||
.arg(language, mimeType, filePath, fileExtension);
|
.arg(language, m_mimeType, m_filePath, fileExtension);
|
||||||
}
|
}
|
||||||
|
|
||||||
CopyrightInfo DocumentContextReader::findCopyright()
|
CopyrightInfo DocumentContextReader::findCopyright()
|
||||||
|
@ -36,7 +36,8 @@ struct CopyrightInfo
|
|||||||
class DocumentContextReader
|
class DocumentContextReader
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
DocumentContextReader(TextEditor::TextDocument *textDocument);
|
DocumentContextReader(
|
||||||
|
QTextDocument *m_document, const QString &mimeType, const QString &filePath);
|
||||||
|
|
||||||
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;
|
||||||
@ -58,6 +59,8 @@ private:
|
|||||||
private:
|
private:
|
||||||
TextEditor::TextDocument *m_textDocument;
|
TextEditor::TextDocument *m_textDocument;
|
||||||
QTextDocument *m_document;
|
QTextDocument *m_document;
|
||||||
|
QString m_mimeType;
|
||||||
|
QString m_filePath;
|
||||||
CopyrightInfo m_copyrightInfo;
|
CopyrightInfo m_copyrightInfo;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user