Copilot: add shortcuts to cycle through suggestions

Change-Id: Id626c386f3b986ba0d1493387d542539b3d3005d
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
This commit is contained in:
David Schulz
2023-06-21 08:14:00 +02:00
parent c4619ca9de
commit a41577f8bd
2 changed files with 50 additions and 0 deletions

View File

@@ -12,6 +12,8 @@ const char COPILOT_TOGGLE[] = "Copilot.Toggle";
const char COPILOT_ENABLE[] = "Copilot.Enable"; const char COPILOT_ENABLE[] = "Copilot.Enable";
const char COPILOT_DISABLE[] = "Copilot.Disable"; const char COPILOT_DISABLE[] = "Copilot.Disable";
const char COPILOT_REQUEST_SUGGESTION[] = "Copilot.RequestSuggestion"; 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_ID[] = "Copilot.General";
const char COPILOT_GENERAL_OPTIONS_CATEGORY[] = "ZY.Copilot"; const char COPILOT_GENERAL_OPTIONS_CATEGORY[] = "ZY.Copilot";

View File

@@ -9,6 +9,7 @@
#include "copilotoptionspage.h" #include "copilotoptionspage.h"
#include "copilotprojectpanel.h" #include "copilotprojectpanel.h"
#include "copilotsettings.h" #include "copilotsettings.h"
#include "copilotsuggestion.h"
#include "copilottr.h" #include "copilottr.h"
#include <coreplugin/actionmanager/actionmanager.h> #include <coreplugin/actionmanager/actionmanager.h>
@@ -20,6 +21,7 @@
#include <projectexplorer/projectpanelfactory.h> #include <projectexplorer/projectpanelfactory.h>
#include <texteditor/textdocumentlayout.h>
#include <texteditor/texteditor.h> #include <texteditor/texteditor.h>
#include <QAction> #include <QAction>
@@ -32,6 +34,28 @@ using namespace ProjectExplorer;
namespace Copilot { namespace Copilot {
namespace Internal { namespace Internal {
enum Direction { Previous, Next };
void cycleSuggestion(TextEditor::TextEditorWidget *editor, Direction direction)
{
QTextBlock block = editor->textCursor().block();
if (auto *suggestion = dynamic_cast<CopilotSuggestion *>(
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<CopilotSuggestion>(suggestion->completions(),
editor->document(),
index));
}
}
void CopilotPlugin::initialize() void CopilotPlugin::initialize()
{ {
CopilotSettings::instance().readSettings(ICore::settings()); CopilotSettings::instance().readSettings(ICore::settings());
@@ -57,6 +81,30 @@ void CopilotPlugin::initialize()
ActionManager::registerAction(requestAction, Constants::COPILOT_REQUEST_SUGGESTION); 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); QAction *disableAction = new QAction(this);
disableAction->setText(Tr::tr("Disable Copilot")); disableAction->setText(Tr::tr("Disable Copilot"));
disableAction->setToolTip(Tr::tr("Disable Copilot.")); disableAction->setToolTip(Tr::tr("Disable Copilot."));