aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/copilot/copilotoptionspage.cpp
blob: 001db87834d3086c9642fc9f6228e3680c3c6548 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "copilotoptionspage.h"

#include "authwidget.h"
#include "copilotconstants.h"
#include "copilotsettings.h"
#include "copilottr.h"

#include <coreplugin/icore.h>

#include <utils/layoutbuilder.h>
#include <utils/pathchooser.h>

using namespace Utils;
using namespace LanguageClient;

namespace Copilot {

class CopilotOptionsPageWidget : public Core::IOptionsPageWidget
{
public:
    CopilotOptionsPageWidget()
    {
        using namespace Layouting;

        auto authWidget = new AuthWidget();

        QLabel *helpLabel = new QLabel();
        helpLabel->setTextFormat(Qt::MarkdownText);
        helpLabel->setWordWrap(true);
        helpLabel->setTextInteractionFlags(Qt::LinksAccessibleByMouse
                                           | Qt::LinksAccessibleByKeyboard);
        helpLabel->setOpenExternalLinks(true);

        // clang-format off
        helpLabel->setText(Tr::tr(R"(
The Copilot plugin requires node.js and the Copilot neovim plugin.
If you install the neovim plugin as described in the
[README.md](https://github.com/github/copilot.vim),
the plugin will find the agent.js file automatically.

Otherwise you need to specify the path to the
[agent.js](https://github.com/github/copilot.vim/tree/release/copilot/dist)
file from the Copilot neovim plugin.
        )", "Markdown text for the copilot instruction label"));

        Column {
            authWidget, br,
            CopilotSettings::instance().enableCopilot, br,
            CopilotSettings::instance().nodeJsPath, br,
            CopilotSettings::instance().distPath, br,
            CopilotSettings::instance().autoComplete, br,
            helpLabel, br,
            st
        }.attachTo(this);
        // clang-format on

        auto updateAuthWidget = [authWidget]() {
            authWidget->updateClient(
                FilePath::fromUserInput(
                    CopilotSettings::instance().nodeJsPath.volatileValue().toString()),
                FilePath::fromUserInput(
                    CopilotSettings::instance().distPath.volatileValue().toString()));
        };

        connect(CopilotSettings::instance().nodeJsPath.pathChooser(),
                &PathChooser::textChanged,
                authWidget,
                updateAuthWidget);
        connect(CopilotSettings::instance().distPath.pathChooser(),
                &PathChooser::textChanged,
                authWidget,
                updateAuthWidget);
        updateAuthWidget();

        setOnApply([] {
            CopilotSettings::instance().apply();
            CopilotSettings::instance().writeSettings(Core::ICore::settings());
        });
    }
};

CopilotOptionsPage::CopilotOptionsPage()
{
    setId(Constants::COPILOT_GENERAL_OPTIONS_ID);
    setDisplayName("Copilot");
    setCategory(Constants::COPILOT_GENERAL_OPTIONS_CATEGORY);
    setDisplayCategory(Constants::COPILOT_GENERAL_OPTIONS_DISPLAY_CATEGORY);
    setCategoryIconPath(":/copilot/images/settingscategory_copilot.png");
    setWidgetCreator([] { return new CopilotOptionsPageWidget; });
}

CopilotOptionsPage &CopilotOptionsPage::instance()
{
    static CopilotOptionsPage settingsPage;
    return settingsPage;
}

} // namespace Copilot