Copilot: Allow user to disable Copilot

Fixes: QTCREATORBUG-29179
Change-Id: I274de1f13f773fb61b376643b61056b6f14dabaf
Reviewed-by: BogDan Vatra <bogdan@kdab.com>
Reviewed-by: David Schulz <david.schulz@qt.io>
Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io>
This commit is contained in:
Marcus Tillmanns
2023-05-23 08:58:07 +02:00
parent f385324bc8
commit bdb31d4348
18 changed files with 353 additions and 24 deletions

View File

@@ -4,22 +4,30 @@
#include "copilotplugin.h"
#include "copilotclient.h"
#include "copilotconstants.h"
#include "copiloticons.h"
#include "copilotoptionspage.h"
#include "copilotprojectpanel.h"
#include "copilotsettings.h"
#include "copilottr.h"
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/icore.h>
#include <coreplugin/statusbarmanager.h>
#include <languageclient/languageclientmanager.h>
#include <projectexplorer/projectpanelfactory.h>
#include <texteditor/texteditor.h>
#include <QAction>
#include <QToolButton>
using namespace Utils;
using namespace Core;
using namespace ProjectExplorer;
namespace Copilot {
namespace Internal {
@@ -35,17 +43,73 @@ void CopilotPlugin::initialize()
this,
&CopilotPlugin::restartClient);
QAction *action = new QAction(this);
action->setText(Tr::tr("Request Copilot Suggestion"));
action->setToolTip(Tr::tr("Request Copilot Suggestion at the current editors cursor position."));
QAction *requestAction = new QAction(this);
requestAction->setText(Tr::tr("Request Copilot Suggestion"));
requestAction->setToolTip(
Tr::tr("Request Copilot suggestion at the current editor's cursor position."));
QObject::connect(action, &QAction::triggered, this, [this] {
connect(requestAction, &QAction::triggered, this, [this] {
if (auto editor = TextEditor::TextEditorWidget::currentTextEditorWidget()) {
if (m_client->reachable())
m_client->requestCompletions(editor);
}
});
ActionManager::registerAction(action, "Copilot.RequestSuggestion");
ActionManager::registerAction(requestAction, Constants::COPILOT_REQUEST_SUGGESTION);
QAction *disableAction = new QAction(this);
disableAction->setText(Tr::tr("Disable Copilot"));
disableAction->setToolTip(Tr::tr("Disable Copilot."));
connect(disableAction, &QAction::triggered, this, [] {
CopilotSettings::instance().enableCopilot.setValue(true);
CopilotSettings::instance().apply();
});
ActionManager::registerAction(disableAction, Constants::COPILOT_DISABLE);
QAction *enableAction = new QAction(this);
enableAction->setText(Tr::tr("Enable Copilot"));
enableAction->setToolTip(Tr::tr("Enable Copilot."));
connect(enableAction, &QAction::triggered, this, [] {
CopilotSettings::instance().enableCopilot.setValue(false);
CopilotSettings::instance().apply();
});
ActionManager::registerAction(enableAction, Constants::COPILOT_ENABLE);
QAction *toggleAction = new QAction(this);
toggleAction->setText(Tr::tr("Toggle Copilot"));
toggleAction->setCheckable(true);
toggleAction->setChecked(CopilotSettings::instance().enableCopilot.value());
toggleAction->setIcon(COPILOT_ICON.icon());
connect(toggleAction, &QAction::toggled, this, [](bool checked) {
CopilotSettings::instance().enableCopilot.setValue(checked);
CopilotSettings::instance().apply();
});
ActionManager::registerAction(toggleAction, Constants::COPILOT_TOGGLE);
auto updateActions = [toggleAction, requestAction] {
const bool enabled = CopilotSettings::instance().enableCopilot.value();
toggleAction->setToolTip(enabled ? Tr::tr("Disable Copilot.") : Tr::tr("Enable Copilot."));
toggleAction->setChecked(enabled);
requestAction->setEnabled(enabled);
};
connect(&CopilotSettings::instance().enableCopilot,
&BoolAspect::valueChanged,
this,
updateActions);
updateActions();
auto toggleButton = new QToolButton;
toggleButton->setDefaultAction(toggleAction);
StatusBarManager::addStatusBarWidget(toggleButton, StatusBarManager::RightCorner);
auto panelFactory = new ProjectPanelFactory;
panelFactory->setPriority(1000);
panelFactory->setDisplayName(Tr::tr("Copilot"));
panelFactory->setCreateWidgetFunction(&Internal::createCopilotProjectPanel);
ProjectPanelFactory::registerFactory(panelFactory);
}
void CopilotPlugin::extensionsInitialized()