mirror of
https://github.com/Palm1r/QodeAssist.git
synced 2025-07-18 13:04:28 -04:00
Add multiline insert support
This commit is contained in:
@ -19,13 +19,17 @@
|
||||
|
||||
#include "LLMSuggestion.hpp"
|
||||
|
||||
#include <QTextCursor>
|
||||
#include <QtWidgets/qtoolbar.h>
|
||||
#include <texteditor/texteditor.h>
|
||||
#include <utils/stringutils.h>
|
||||
#include <utils/tooltip/tooltip.h>
|
||||
|
||||
namespace QodeAssist {
|
||||
|
||||
LLMSuggestion::LLMSuggestion(const Completion &completion, QTextDocument *origin)
|
||||
: m_completion(completion)
|
||||
, m_linesCount(0)
|
||||
{
|
||||
int startPos = completion.range().start().toPositionInDocument(origin);
|
||||
int endPos = completion.range().end().toPositionInDocument(origin);
|
||||
@ -71,24 +75,30 @@ bool LLMSuggestion::applyWord(TextEditor::TextEditorWidget *widget)
|
||||
|
||||
bool LLMSuggestion::applyNextLine(TextEditor::TextEditorWidget *widget)
|
||||
{
|
||||
QTextCursor currentCursor = widget->textCursor();
|
||||
const QString text = m_completion.text();
|
||||
QStringList lines = text.split('\n');
|
||||
|
||||
int endPos = currentCursor.position();
|
||||
while (endPos < text.length() && !text[endPos].isSpace()) {
|
||||
++endPos;
|
||||
}
|
||||
if (m_linesCount < lines.size())
|
||||
m_linesCount++;
|
||||
|
||||
QString wordToInsert = text.left(endPos);
|
||||
showTooltip(widget, m_linesCount);
|
||||
|
||||
wordToInsert = wordToInsert.split('\n').first();
|
||||
return m_linesCount == lines.size() && !Utils::ToolTip::isVisible();
|
||||
}
|
||||
|
||||
if (!wordToInsert.isEmpty()) {
|
||||
currentCursor.insertText(wordToInsert);
|
||||
widget->setTextCursor(currentCursor);
|
||||
}
|
||||
void LLMSuggestion::onCounterFinished(int count)
|
||||
{
|
||||
Utils::ToolTip::hide();
|
||||
m_linesCount = 0;
|
||||
QTextCursor cursor = m_completion.range().toSelection(m_start.document());
|
||||
cursor.beginEditBlock();
|
||||
cursor.removeSelectedText();
|
||||
|
||||
return false;
|
||||
QStringList lines = m_completion.text().split('\n');
|
||||
QString textToInsert = lines.mid(0, count).join('\n');
|
||||
|
||||
cursor.insertText(textToInsert);
|
||||
cursor.endEditBlock();
|
||||
}
|
||||
|
||||
void LLMSuggestion::reset()
|
||||
@ -101,4 +111,14 @@ int LLMSuggestion::position()
|
||||
return m_start.position();
|
||||
}
|
||||
|
||||
void LLMSuggestion::showTooltip(TextEditor::TextEditorWidget *widget, int count)
|
||||
{
|
||||
Utils::ToolTip::hide();
|
||||
QPoint pos = widget->mapToGlobal(widget->cursorRect().topRight());
|
||||
pos += QPoint(-10, -50);
|
||||
m_counterTooltip = new CounterTooltip(count);
|
||||
Utils::ToolTip::show(pos, m_counterTooltip, widget);
|
||||
connect(m_counterTooltip, &CounterTooltip::finished, this, &LLMSuggestion::onCounterFinished);
|
||||
}
|
||||
|
||||
} // namespace QodeAssist
|
||||
|
Reference in New Issue
Block a user