diff --git a/src/plugins/copilot/copilotconstants.h b/src/plugins/copilot/copilotconstants.h index 6ace5f2f6f9..1b557556ad4 100644 --- a/src/plugins/copilot/copilotconstants.h +++ b/src/plugins/copilot/copilotconstants.h @@ -12,6 +12,8 @@ const char COPILOT_TOGGLE[] = "Copilot.Toggle"; const char COPILOT_ENABLE[] = "Copilot.Enable"; const char COPILOT_DISABLE[] = "Copilot.Disable"; const char COPILOT_REQUEST_SUGGESTION[] = "Copilot.RequestSuggestion"; +const char COPILOT_NEXT_SUGGESTION[] = "Copilot.NextSuggestion"; +const char COPILOT_PREVIOUS_SUGGESTION[] = "Copilot.PreviousSuggestion"; const char COPILOT_GENERAL_OPTIONS_ID[] = "Copilot.General"; const char COPILOT_GENERAL_OPTIONS_CATEGORY[] = "ZY.Copilot"; diff --git a/src/plugins/copilot/copilotplugin.cpp b/src/plugins/copilot/copilotplugin.cpp index 526a6061cbe..1a554e13977 100644 --- a/src/plugins/copilot/copilotplugin.cpp +++ b/src/plugins/copilot/copilotplugin.cpp @@ -9,6 +9,7 @@ #include "copilotoptionspage.h" #include "copilotprojectpanel.h" #include "copilotsettings.h" +#include "copilotsuggestion.h" #include "copilottr.h" #include @@ -20,6 +21,7 @@ #include +#include #include #include @@ -32,6 +34,28 @@ using namespace ProjectExplorer; namespace Copilot { namespace Internal { +enum Direction { Previous, Next }; +void cycleSuggestion(TextEditor::TextEditorWidget *editor, Direction direction) +{ + QTextBlock block = editor->textCursor().block(); + if (auto *suggestion = dynamic_cast( + TextEditor::TextDocumentLayout::suggestion(block))) { + int index = suggestion->currentCompletion(); + if (direction == Previous) + --index; + else + ++index; + if (index < 0) + index = suggestion->completions().count() - 1; + else if (index >= suggestion->completions().count()) + index = 0; + suggestion->reset(); + editor->insertSuggestion(std::make_unique(suggestion->completions(), + editor->document(), + index)); + } +} + void CopilotPlugin::initialize() { CopilotSettings::instance().readSettings(ICore::settings()); @@ -57,6 +81,30 @@ void CopilotPlugin::initialize() ActionManager::registerAction(requestAction, Constants::COPILOT_REQUEST_SUGGESTION); + QAction *nextSuggestionAction = new QAction(this); + nextSuggestionAction->setText(Tr::tr("Show next Copilot Suggestion")); + nextSuggestionAction->setToolTip(Tr::tr( + "Cycles through the received Copilot Suggestions showing the next available Suggestion.")); + + connect(nextSuggestionAction, &QAction::triggered, this, [] { + if (auto editor = TextEditor::TextEditorWidget::currentTextEditorWidget()) + cycleSuggestion(editor, Next); + }); + + ActionManager::registerAction(nextSuggestionAction, Constants::COPILOT_NEXT_SUGGESTION); + + QAction *previousSuggestionAction = new QAction(this); + previousSuggestionAction->setText(Tr::tr("Show previos Copilot Suggestion")); + previousSuggestionAction->setToolTip(Tr::tr("Cycles through the received Copilot Suggestions " + "showing the previous available Suggestion.")); + + connect(previousSuggestionAction, &QAction::triggered, this, [] { + if (auto editor = TextEditor::TextEditorWidget::currentTextEditorWidget()) + cycleSuggestion(editor, Previous); + }); + + ActionManager::registerAction(previousSuggestionAction, Constants::COPILOT_PREVIOUS_SUGGESTION); + QAction *disableAction = new QAction(this); disableAction->setText(Tr::tr("Disable Copilot")); disableAction->setToolTip(Tr::tr("Disable Copilot."));