From 4d9adf75ffccb654e3e647febd791a5d615f8935 Mon Sep 17 00:00:00 2001 From: Petr Mironychev <9195189+Palm1r@users.noreply.github.com> Date: Sun, 1 Sep 2024 21:41:46 +0200 Subject: [PATCH] Add line inserting --- LLMSuggestion.cpp | 28 ++++++++++++++++++++++++++-- LLMSuggestion.hpp | 1 + QodeAssistHoverHandler.cpp | 16 +++++++++++++++- 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/LLMSuggestion.cpp b/LLMSuggestion.cpp index ab37c93..39f215c 100644 --- a/LLMSuggestion.cpp +++ b/LLMSuggestion.cpp @@ -19,6 +19,9 @@ #include "LLMSuggestion.hpp" +#include +#include + 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() diff --git a/LLMSuggestion.hpp b/LLMSuggestion.hpp index c5536f3..de4a9cd 100644 --- a/LLMSuggestion.hpp +++ b/LLMSuggestion.hpp @@ -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; diff --git a/QodeAssistHoverHandler.cpp b/QodeAssistHoverHandler.cpp index 8db717f..849f8f7 100644 --- a/QodeAssistHoverHandler.cpp +++ b/QodeAssistHoverHandler.cpp @@ -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(m_editor->currentSuggestion())) { if (!suggestion->apply()) return; } ToolTip::hide(); } + void applyWord() + { + if (auto *suggestion = dynamic_cast(m_editor->currentSuggestion())) { + if (!suggestion->applyWord(m_editor)) + return; + } + ToolTip::hide(); + } + TextEditorWidget *m_editor; };