aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/copilot/copilotplugin.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/copilot/copilotplugin.cpp')
-rw-r--r--src/plugins/copilot/copilotplugin.cpp217
1 files changed, 114 insertions, 103 deletions
diff --git a/src/plugins/copilot/copilotplugin.cpp b/src/plugins/copilot/copilotplugin.cpp
index 2ad525f5f37..78ca70b3fd9 100644
--- a/src/plugins/copilot/copilotplugin.cpp
+++ b/src/plugins/copilot/copilotplugin.cpp
@@ -1,8 +1,6 @@
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
-#include "copilotplugin.h"
-
#include "copilotclient.h"
#include "copilotconstants.h"
#include "copiloticons.h"
@@ -15,6 +13,8 @@
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/statusbarmanager.h>
+#include <extensionsystem/iplugin.h>
+
#include <languageclient/languageclientmanager.h>
#include <texteditor/textdocumentlayout.h>
@@ -22,20 +22,20 @@
#include <QAction>
#include <QToolButton>
+#include <QPointer>
using namespace Utils;
using namespace Core;
using namespace ProjectExplorer;
-namespace Copilot {
-namespace Internal {
+namespace Copilot::Internal {
enum Direction { Previous, Next };
-void cycleSuggestion(TextEditor::TextEditorWidget *editor, Direction direction)
+static void cycleSuggestion(TextEditor::TextEditorWidget *editor, Direction direction)
{
QTextBlock block = editor->textCursor().block();
- if (auto *suggestion = dynamic_cast<CopilotSuggestion *>(
+ if (auto suggestion = dynamic_cast<CopilotSuggestion *>(
TextEditor::TextDocumentLayout::suggestion(block))) {
int index = suggestion->currentCompletion();
if (direction == Previous)
@@ -53,108 +53,119 @@ void cycleSuggestion(TextEditor::TextEditorWidget *editor, Direction direction)
}
}
-void CopilotPlugin::initialize()
+class CopilotPlugin final : public ExtensionSystem::IPlugin
{
- ActionBuilder requestAction(this, Constants::COPILOT_REQUEST_SUGGESTION);
- requestAction.setText(Tr::tr("Request Copilot Suggestion"));
- requestAction.setToolTip(Tr::tr(
- "Request Copilot suggestion at the current editor's cursor position."));
- requestAction.setOnTriggered(this, [this] {
- if (auto editor = TextEditor::TextEditorWidget::currentTextEditorWidget()) {
- if (m_client && m_client->reachable())
- m_client->requestCompletions(editor);
- }
- });
-
- ActionBuilder nextSuggestionAction(this, Constants::COPILOT_NEXT_SUGGESTION);
- nextSuggestionAction.setText(Tr::tr("Show Next Copilot Suggestion"));
- nextSuggestionAction.setToolTip(Tr::tr(
- "Cycles through the received Copilot Suggestions showing the next available Suggestion."));
- nextSuggestionAction.setOnTriggered(this, [] {
- if (auto editor = TextEditor::TextEditorWidget::currentTextEditorWidget())
- cycleSuggestion(editor, Next);
- });
-
- ActionBuilder previousSuggestionAction(this, Constants::COPILOT_PREVIOUS_SUGGESTION);
- previousSuggestionAction.setText(Tr::tr("Show Previous Copilot Suggestion"));
- previousSuggestionAction.setToolTip(Tr::tr("Cycles through the received Copilot Suggestions "
- "showing the previous available Suggestion."));
- previousSuggestionAction.setOnTriggered(this, [] {
- if (auto editor = TextEditor::TextEditorWidget::currentTextEditorWidget())
- cycleSuggestion(editor, Previous);
- });
-
- ActionBuilder disableAction(this, Constants::COPILOT_DISABLE);
- disableAction.setText(Tr::tr("Disable Copilot"));
- disableAction.setToolTip(Tr::tr("Disable Copilot."));
- disableAction.setOnTriggered(this, [] {
- settings().enableCopilot.setValue(true);
- settings().apply();
- });
-
- ActionBuilder enableAction(this, Constants::COPILOT_ENABLE);
- enableAction.setText(Tr::tr("Enable Copilot"));
- enableAction.setToolTip(Tr::tr("Enable Copilot."));
- enableAction.setOnTriggered(this, [] {
- settings().enableCopilot.setValue(false);
- settings().apply();
- });
-
- ActionBuilder toggleAction(this, Constants::COPILOT_TOGGLE);
- toggleAction.setText(Tr::tr("Toggle Copilot"));
- toggleAction.setCheckable(true);
- toggleAction.setChecked(settings().enableCopilot());
- toggleAction.setIcon(COPILOT_ICON.icon());
- toggleAction.setOnTriggered(this, [](bool checked) {
- settings().enableCopilot.setValue(checked);
- settings().apply();
- });
-
- QAction *toggleAct = toggleAction.contextAction();
- QAction *requestAct = requestAction.contextAction();
- auto updateActions = [toggleAct, requestAct] {
- const bool enabled = settings().enableCopilot();
- toggleAct->setToolTip(enabled ? Tr::tr("Disable Copilot.") : Tr::tr("Enable Copilot."));
- toggleAct->setChecked(enabled);
- requestAct->setEnabled(enabled);
- };
-
- connect(&settings().enableCopilot, &BaseAspect::changed, this, updateActions);
-
- updateActions();
-
- auto toggleButton = new QToolButton;
- toggleButton->setDefaultAction(toggleAction.contextAction());
- StatusBarManager::addStatusBarWidget(toggleButton, StatusBarManager::RightCorner);
-
- setupCopilotProjectPanel();
-}
+ Q_OBJECT
+ Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QtCreatorPlugin" FILE "Copilot.json")
+
+public:
+ void initialize() final
+ {
+ ActionBuilder requestAction(this, Constants::COPILOT_REQUEST_SUGGESTION);
+ requestAction.setText(Tr::tr("Request Copilot Suggestion"));
+ requestAction.setToolTip(Tr::tr(
+ "Request Copilot suggestion at the current editor's cursor position."));
+ requestAction.setOnTriggered(this, [this] {
+ if (auto editor = TextEditor::TextEditorWidget::currentTextEditorWidget()) {
+ if (m_client && m_client->reachable())
+ m_client->requestCompletions(editor);
+ }
+ });
+
+ ActionBuilder nextSuggestionAction(this, Constants::COPILOT_NEXT_SUGGESTION);
+ nextSuggestionAction.setText(Tr::tr("Show Next Copilot Suggestion"));
+ nextSuggestionAction.setToolTip(Tr::tr(
+ "Cycles through the received Copilot Suggestions showing the next available Suggestion."));
+ nextSuggestionAction.setOnTriggered(this, [] {
+ if (auto editor = TextEditor::TextEditorWidget::currentTextEditorWidget())
+ cycleSuggestion(editor, Next);
+ });
+
+ ActionBuilder previousSuggestionAction(this, Constants::COPILOT_PREVIOUS_SUGGESTION);
+ previousSuggestionAction.setText(Tr::tr("Show Previous Copilot Suggestion"));
+ previousSuggestionAction.setToolTip(Tr::tr("Cycles through the received Copilot Suggestions "
+ "showing the previous available Suggestion."));
+ previousSuggestionAction.setOnTriggered(this, [] {
+ if (auto editor = TextEditor::TextEditorWidget::currentTextEditorWidget())
+ cycleSuggestion(editor, Previous);
+ });
+
+ ActionBuilder disableAction(this, Constants::COPILOT_DISABLE);
+ disableAction.setText(Tr::tr("Disable Copilot"));
+ disableAction.setToolTip(Tr::tr("Disable Copilot."));
+ disableAction.setOnTriggered(this, [] {
+ settings().enableCopilot.setValue(true);
+ settings().apply();
+ });
+
+ ActionBuilder enableAction(this, Constants::COPILOT_ENABLE);
+ enableAction.setText(Tr::tr("Enable Copilot"));
+ enableAction.setToolTip(Tr::tr("Enable Copilot."));
+ enableAction.setOnTriggered(this, [] {
+ settings().enableCopilot.setValue(false);
+ settings().apply();
+ });
+
+ ActionBuilder toggleAction(this, Constants::COPILOT_TOGGLE);
+ toggleAction.setText(Tr::tr("Toggle Copilot"));
+ toggleAction.setCheckable(true);
+ toggleAction.setChecked(settings().enableCopilot());
+ toggleAction.setIcon(COPILOT_ICON.icon());
+ toggleAction.setOnTriggered(this, [](bool checked) {
+ settings().enableCopilot.setValue(checked);
+ settings().apply();
+ });
+
+ QAction *toggleAct = toggleAction.contextAction();
+ QAction *requestAct = requestAction.contextAction();
+ auto updateActions = [toggleAct, requestAct] {
+ const bool enabled = settings().enableCopilot();
+ toggleAct->setToolTip(enabled ? Tr::tr("Disable Copilot.") : Tr::tr("Enable Copilot."));
+ toggleAct->setChecked(enabled);
+ requestAct->setEnabled(enabled);
+ };
+
+ connect(&settings().enableCopilot, &BaseAspect::changed, this, updateActions);
+
+ updateActions();
+
+ auto toggleButton = new QToolButton;
+ toggleButton->setDefaultAction(toggleAction.contextAction());
+ StatusBarManager::addStatusBarWidget(toggleButton, StatusBarManager::RightCorner);
+
+ setupCopilotProjectPanel();
+ }
-bool CopilotPlugin::delayedInitialize()
-{
- restartClient();
+ bool delayedInitialize() final
+ {
+ restartClient();
- connect(&settings(), &AspectContainer::applied, this, &CopilotPlugin::restartClient);
+ connect(&settings(), &AspectContainer::applied, this, &CopilotPlugin::restartClient);
- return true;
-}
+ return true;
+ }
-void CopilotPlugin::restartClient()
-{
- LanguageClient::LanguageClientManager::shutdownClient(m_client);
+ void restartClient()
+ {
+ LanguageClient::LanguageClientManager::shutdownClient(m_client);
- if (!settings().nodeJsPath().isExecutableFile())
- return;
- m_client = new CopilotClient(settings().nodeJsPath(), settings().distPath());
-}
+ if (!settings().nodeJsPath().isExecutableFile())
+ return;
+ m_client = new CopilotClient(settings().nodeJsPath(), settings().distPath());
+ }
-ExtensionSystem::IPlugin::ShutdownFlag CopilotPlugin::aboutToShutdown()
-{
- if (!m_client)
- return SynchronousShutdown;
- connect(m_client, &QObject::destroyed, this, &IPlugin::asynchronousShutdownFinished);
- return AsynchronousShutdown;
-}
+ ShutdownFlag aboutToShutdown() final
+ {
+ if (!m_client)
+ return SynchronousShutdown;
+ connect(m_client, &QObject::destroyed, this, &IPlugin::asynchronousShutdownFinished);
+ return AsynchronousShutdown;
+ }
+
+private:
+ QPointer<CopilotClient> m_client;
+};
+
+} // Copilot::Internal
-} // namespace Internal
-} // namespace Copilot
+#include "copilotplugin.moc"