2023-01-20 07:09:01 +01:00
|
|
|
// 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 "copilotsettings.h"
|
2023-07-14 16:29:11 +02:00
|
|
|
|
|
|
|
|
#include "authwidget.h"
|
2023-05-23 08:58:07 +02:00
|
|
|
#include "copilotconstants.h"
|
2023-01-20 07:09:01 +01:00
|
|
|
#include "copilottr.h"
|
|
|
|
|
|
2023-07-14 16:29:11 +02:00
|
|
|
#include <coreplugin/dialogs/ioptionspage.h>
|
|
|
|
|
|
2023-05-23 08:58:07 +02:00
|
|
|
#include <projectexplorer/project.h>
|
|
|
|
|
|
2023-01-20 07:09:01 +01:00
|
|
|
#include <utils/algorithm.h>
|
|
|
|
|
#include <utils/environment.h>
|
2023-07-14 16:29:11 +02:00
|
|
|
#include <utils/layoutbuilder.h>
|
|
|
|
|
#include <utils/pathchooser.h>
|
|
|
|
|
|
|
|
|
|
#include <QToolTip>
|
2023-01-20 07:09:01 +01:00
|
|
|
|
|
|
|
|
using namespace Utils;
|
|
|
|
|
|
|
|
|
|
namespace Copilot {
|
|
|
|
|
|
2023-05-23 08:58:07 +02:00
|
|
|
static void initEnableAspect(BoolAspect &enableCopilot)
|
|
|
|
|
{
|
|
|
|
|
enableCopilot.setSettingsKey(Constants::ENABLE_COPILOT);
|
|
|
|
|
enableCopilot.setDisplayName(Tr::tr("Enable Copilot"));
|
|
|
|
|
enableCopilot.setLabelText(Tr::tr("Enable Copilot"));
|
|
|
|
|
enableCopilot.setToolTip(Tr::tr("Enables the Copilot integration."));
|
2023-06-23 08:44:49 +02:00
|
|
|
enableCopilot.setDefaultValue(false);
|
2023-05-23 08:58:07 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-14 16:29:11 +02:00
|
|
|
CopilotSettings &settings()
|
2023-01-20 07:09:01 +01:00
|
|
|
{
|
|
|
|
|
static CopilotSettings settings;
|
|
|
|
|
return settings;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CopilotSettings::CopilotSettings()
|
|
|
|
|
{
|
|
|
|
|
setAutoApply(false);
|
|
|
|
|
|
|
|
|
|
const FilePath nodeFromPath = FilePath("node").searchInPath();
|
|
|
|
|
|
|
|
|
|
const FilePaths searchDirs
|
2023-06-29 15:00:11 +02:00
|
|
|
|
|
|
|
|
= {FilePath::fromUserInput("~/.vim/pack/github/start/copilot.vim/dist/agent.js"),
|
|
|
|
|
FilePath::fromUserInput("~/.vim/pack/github/start/copilot.vim/copilot/dist/agent.js"),
|
2023-01-20 07:09:01 +01:00
|
|
|
FilePath::fromUserInput(
|
|
|
|
|
"~/.config/nvim/pack/github/start/copilot.vim/copilot/dist/agent.js"),
|
|
|
|
|
FilePath::fromUserInput(
|
|
|
|
|
"~/vimfiles/pack/github/start/copilot.vim/copilot/dist/agent.js"),
|
|
|
|
|
FilePath::fromUserInput(
|
|
|
|
|
"~/AppData/Local/nvim/pack/github/start/copilot.vim/copilot/dist/agent.js")};
|
|
|
|
|
|
2023-05-23 08:58:07 +02:00
|
|
|
const FilePath distFromVim = findOrDefault(searchDirs, &FilePath::exists);
|
2023-01-20 07:09:01 +01:00
|
|
|
|
|
|
|
|
nodeJsPath.setExpectedKind(PathChooser::ExistingCommand);
|
2023-06-29 16:45:11 +02:00
|
|
|
nodeJsPath.setDefaultValue(nodeFromPath);
|
2023-01-20 07:09:01 +01:00
|
|
|
nodeJsPath.setSettingsKey("Copilot.NodeJsPath");
|
|
|
|
|
nodeJsPath.setLabelText(Tr::tr("Node.js path:"));
|
|
|
|
|
nodeJsPath.setHistoryCompleter("Copilot.NodePath.History");
|
|
|
|
|
nodeJsPath.setDisplayName(Tr::tr("Node.js Path"));
|
2023-06-23 08:44:49 +02:00
|
|
|
nodeJsPath.setEnabler(&enableCopilot);
|
2023-01-20 07:09:01 +01:00
|
|
|
nodeJsPath.setToolTip(
|
2023-05-30 16:28:37 +02:00
|
|
|
Tr::tr("Select path to node.js executable. See https://nodejs.org/en/download/"
|
2023-01-20 07:09:01 +01:00
|
|
|
"for installation instructions."));
|
|
|
|
|
|
|
|
|
|
distPath.setExpectedKind(PathChooser::File);
|
2023-06-29 16:45:11 +02:00
|
|
|
distPath.setDefaultValue(distFromVim);
|
2023-01-20 07:09:01 +01:00
|
|
|
distPath.setSettingsKey("Copilot.DistPath");
|
|
|
|
|
distPath.setLabelText(Tr::tr("Path to agent.js:"));
|
2023-05-15 18:32:22 +02:00
|
|
|
distPath.setHistoryCompleter("Copilot.DistPath.History");
|
|
|
|
|
distPath.setDisplayName(Tr::tr("Agent.js path"));
|
2023-06-23 08:44:49 +02:00
|
|
|
distPath.setEnabler(&enableCopilot);
|
2023-01-20 07:09:01 +01:00
|
|
|
distPath.setToolTip(Tr::tr(
|
2023-05-30 16:28:37 +02:00
|
|
|
"Select path to agent.js in Copilot Neovim plugin. See "
|
2023-01-20 07:09:01 +01:00
|
|
|
"https://github.com/github/copilot.vim#getting-started for installation instructions."));
|
|
|
|
|
|
2023-04-18 15:09:01 +02:00
|
|
|
autoComplete.setDisplayName(Tr::tr("Auto Complete"));
|
2023-05-23 08:58:07 +02:00
|
|
|
autoComplete.setSettingsKey("Copilot.Autocomplete");
|
2023-04-18 15:09:01 +02:00
|
|
|
autoComplete.setLabelText(Tr::tr("Request completions automatically"));
|
|
|
|
|
autoComplete.setDefaultValue(true);
|
2023-06-23 08:44:49 +02:00
|
|
|
autoComplete.setEnabler(&enableCopilot);
|
2023-04-18 15:09:01 +02:00
|
|
|
autoComplete.setToolTip(Tr::tr("Automatically request suggestions for the current text cursor "
|
2023-05-30 16:28:37 +02:00
|
|
|
"position after changes to the document."));
|
2023-05-23 08:58:07 +02:00
|
|
|
|
|
|
|
|
initEnableAspect(enableCopilot);
|
2023-07-14 16:29:11 +02:00
|
|
|
|
|
|
|
|
readSettings();
|
2023-05-23 08:58:07 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-27 15:13:31 +02:00
|
|
|
CopilotProjectSettings::CopilotProjectSettings(ProjectExplorer::Project *project)
|
2023-05-23 08:58:07 +02:00
|
|
|
{
|
|
|
|
|
setAutoApply(true);
|
|
|
|
|
|
|
|
|
|
useGlobalSettings.setSettingsKey(Constants::COPILOT_USE_GLOBAL_SETTINGS);
|
|
|
|
|
useGlobalSettings.setDefaultValue(true);
|
|
|
|
|
|
|
|
|
|
initEnableAspect(enableCopilot);
|
|
|
|
|
|
|
|
|
|
QVariantMap map = project->namedSettings(Constants::COPILOT_PROJECT_SETTINGS_ID).toMap();
|
|
|
|
|
fromMap(map);
|
|
|
|
|
|
2023-06-26 11:02:42 +02:00
|
|
|
connect(&enableCopilot, &BaseAspect::changed, this, [this, project] { save(project); });
|
|
|
|
|
connect(&useGlobalSettings, &BaseAspect::changed, this, [this, project] { save(project); });
|
2023-05-23 08:58:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CopilotProjectSettings::setUseGlobalSettings(bool useGlobal)
|
|
|
|
|
{
|
|
|
|
|
useGlobalSettings.setValue(useGlobal);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CopilotProjectSettings::isEnabled() const
|
|
|
|
|
{
|
|
|
|
|
if (useGlobalSettings())
|
2023-07-14 16:29:11 +02:00
|
|
|
return settings().enableCopilot();
|
2023-05-23 08:58:07 +02:00
|
|
|
return enableCopilot();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CopilotProjectSettings::save(ProjectExplorer::Project *project)
|
|
|
|
|
{
|
|
|
|
|
QVariantMap map;
|
|
|
|
|
toMap(map);
|
|
|
|
|
project->setNamedSettings(Constants::COPILOT_PROJECT_SETTINGS_ID, map);
|
|
|
|
|
|
|
|
|
|
// This triggers a restart of the Copilot language server.
|
2023-07-14 16:29:11 +02:00
|
|
|
settings().apply();
|
2023-01-20 07:09:01 +01:00
|
|
|
}
|
|
|
|
|
|
2023-07-14 16:29:11 +02:00
|
|
|
// CopilotOptionsPageWidget
|
|
|
|
|
|
|
|
|
|
class CopilotOptionsPageWidget : public Core::IOptionsPageWidget
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
CopilotOptionsPageWidget()
|
|
|
|
|
{
|
|
|
|
|
using namespace Layouting;
|
|
|
|
|
|
|
|
|
|
auto warningLabel = new QLabel;
|
|
|
|
|
warningLabel->setWordWrap(true);
|
|
|
|
|
warningLabel->setTextInteractionFlags(Qt::LinksAccessibleByMouse
|
|
|
|
|
| Qt::LinksAccessibleByKeyboard
|
|
|
|
|
| Qt::TextSelectableByMouse);
|
|
|
|
|
warningLabel->setText(Tr::tr(
|
|
|
|
|
"Enabling %1 is subject to your agreement and abidance with your applicable "
|
|
|
|
|
"%1 terms. It is your responsibility to know and accept the requirements and "
|
|
|
|
|
"parameters of using tools like %1. This may include, but is not limited to, "
|
|
|
|
|
"ensuring you have the rights to allow %1 access to your code, as well as "
|
|
|
|
|
"understanding any implications of your use of %1 and suggestions produced "
|
|
|
|
|
"(like copyright, accuracy, etc.)." ).arg("Copilot"));
|
|
|
|
|
|
|
|
|
|
auto authWidget = new AuthWidget();
|
|
|
|
|
|
|
|
|
|
auto helpLabel = new QLabel();
|
|
|
|
|
helpLabel->setTextFormat(Qt::MarkdownText);
|
|
|
|
|
helpLabel->setWordWrap(true);
|
|
|
|
|
helpLabel->setTextInteractionFlags(Qt::LinksAccessibleByMouse
|
|
|
|
|
| Qt::LinksAccessibleByKeyboard
|
|
|
|
|
| Qt::TextSelectableByMouse);
|
|
|
|
|
helpLabel->setOpenExternalLinks(true);
|
|
|
|
|
connect(helpLabel, &QLabel::linkHovered, [](const QString &link) {
|
|
|
|
|
QToolTip::showText(QCursor::pos(), link);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// clang-format off
|
|
|
|
|
helpLabel->setText(Tr::tr(
|
|
|
|
|
"The Copilot plugin requires node.js and the Copilot neovim plugin. "
|
|
|
|
|
"If you install the neovim plugin as described in %1, "
|
|
|
|
|
"the plugin will find the agent.js file automatically.\n\n"
|
|
|
|
|
"Otherwise you need to specify the path to the %2 "
|
|
|
|
|
"file from the Copilot neovim plugin.",
|
|
|
|
|
"Markdown text for the copilot instruction label")
|
|
|
|
|
.arg("[README.md](https://github.com/github/copilot.vim)")
|
2023-08-01 14:24:38 +02:00
|
|
|
.arg("[agent.js](https://github.com/github/copilot.vim/tree/release/dist)"));
|
2023-07-14 16:29:11 +02:00
|
|
|
|
|
|
|
|
Column {
|
|
|
|
|
QString("<b>" + Tr::tr("Note:") + "</b>"), br,
|
|
|
|
|
warningLabel, br,
|
|
|
|
|
settings().enableCopilot, br,
|
|
|
|
|
authWidget, br,
|
|
|
|
|
settings().nodeJsPath, br,
|
|
|
|
|
settings().distPath, br,
|
|
|
|
|
settings().autoComplete, br,
|
|
|
|
|
helpLabel, br,
|
|
|
|
|
st
|
|
|
|
|
}.attachTo(this);
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
|
|
setOnApply([] {
|
|
|
|
|
settings().apply();
|
|
|
|
|
settings().writeSettings();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class CopilotSettingsPage : public Core::IOptionsPage
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
CopilotSettingsPage()
|
|
|
|
|
{
|
|
|
|
|
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; });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const CopilotSettingsPage settingsPage;
|
|
|
|
|
|
|
|
|
|
} // Copilot
|