Add line inserting

This commit is contained in:
Petr Mironychev 2024-09-01 21:41:46 +02:00
parent a974b0aa82
commit 4d9adf75ff
3 changed files with 42 additions and 3 deletions

View File

@ -19,6 +19,9 @@
#include "LLMSuggestion.hpp"
#include <texteditor/texteditor.h>
#include <utils/stringutils.h>
namespace QodeAssist {
LLMSuggestion::LLMSuggestion(const Completion &completion, QTextDocument *origin)
@ -63,8 +66,29 @@ bool LLMSuggestion::apply()
bool LLMSuggestion::applyWord(TextEditor::TextEditorWidget *widget)
{
Q_UNUSED(widget)
return apply();
return applyNextLine(widget);
}
bool LLMSuggestion::applyNextLine(TextEditor::TextEditorWidget *widget)
{
QTextCursor currentCursor = widget->textCursor();
const QString text = m_completion.text();
int endPos = currentCursor.position();
while (endPos < text.length() && !text[endPos].isSpace()) {
++endPos;
}
QString wordToInsert = text.left(endPos);
wordToInsert = wordToInsert.split('\n').first();
if (!wordToInsert.isEmpty()) {
currentCursor.insertText(wordToInsert);
widget->setTextCursor(currentCursor);
}
return false;
}
void LLMSuggestion::reset()

View File

@ -32,6 +32,7 @@ public:
bool apply() final;
bool applyWord(TextEditor::TextEditorWidget *widget) final;
bool applyNextLine(TextEditor::TextEditorWidget *widget);
void reset() final;
int position() final;

View File

@ -54,18 +54,32 @@ public:
{
auto apply = addAction(Tr::tr("Apply (%1)").arg(QKeySequence(Qt::Key_Tab).toString()));
connect(apply, &QAction::triggered, this, &QodeAssistCompletionToolTip::apply);
auto applyWord = addAction(Tr::tr("Apply Next Line (%1)")
.arg(QKeySequence(QKeySequence::MoveToNextLine).toString()));
connect(applyWord, &QAction::triggered, this, &QodeAssistCompletionToolTip::applyWord);
qDebug() << "applyWord sequence" << applyWord->shortcut();
}
private:
void apply()
{
if (TextSuggestion *suggestion = m_editor->currentSuggestion()) {
if (auto *suggestion = dynamic_cast<LLMSuggestion *>(m_editor->currentSuggestion())) {
if (!suggestion->apply())
return;
}
ToolTip::hide();
}
void applyWord()
{
if (auto *suggestion = dynamic_cast<LLMSuggestion *>(m_editor->currentSuggestion())) {
if (!suggestion->applyWord(m_editor))
return;
}
ToolTip::hide();
}
TextEditorWidget *m_editor;
};