summaryrefslogtreecommitdiff
path: root/src/plugins/copilot/copilotplugin.cpp
blob: 61753b29c30d74cdc79a219dd4b744b8fd869f12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// 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 "copilotoptionspage.h"
#include "copilotsettings.h"
#include "copilottr.h"

#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/icore.h>

#include <languageclient/languageclientmanager.h>

#include <texteditor/texteditor.h>

#include <QAction>

using namespace Utils;
using namespace Core;

namespace Copilot {
namespace Internal {

void CopilotPlugin::initialize()
{
    CopilotSettings::instance().readSettings(ICore::settings());

    restartClient();

    connect(&CopilotSettings::instance(),
            &CopilotSettings::applied,
            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."));

    QObject::connect(action, &QAction::triggered, this, [this] {
        if (auto editor = TextEditor::TextEditorWidget::currentTextEditorWidget()) {
            if (m_client->reachable())
                m_client->requestCompletions(editor);
        }
    });
    ActionManager::registerAction(action, "Copilot.RequestSuggestion");
}

void CopilotPlugin::extensionsInitialized()
{
    (void)CopilotOptionsPage::instance();
}

void CopilotPlugin::restartClient()
{
    LanguageClient::LanguageClientManager::shutdownClient(m_client);
    m_client = new CopilotClient(CopilotSettings::instance().nodeJsPath(),
                                 CopilotSettings::instance().distPath());
}

} // namespace Internal
} // namespace Copilot