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-05-23 08:58:07 +02:00
|
|
|
#include "copilotconstants.h"
|
2023-01-20 07:09:01 +01:00
|
|
|
#include "copilottr.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>
|
|
|
|
|
|
|
|
|
|
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."));
|
|
|
|
|
enableCopilot.setDefaultValue(true);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-20 07:09:01 +01:00
|
|
|
CopilotSettings &CopilotSettings::instance()
|
|
|
|
|
{
|
|
|
|
|
static CopilotSettings settings;
|
|
|
|
|
return settings;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CopilotSettings::CopilotSettings()
|
|
|
|
|
{
|
|
|
|
|
setAutoApply(false);
|
|
|
|
|
|
|
|
|
|
const FilePath nodeFromPath = FilePath("node").searchInPath();
|
|
|
|
|
|
|
|
|
|
const FilePaths searchDirs
|
|
|
|
|
= {FilePath::fromUserInput("~/.vim/pack/github/start/copilot.vim/copilot/dist/agent.js"),
|
|
|
|
|
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);
|
|
|
|
|
nodeJsPath.setDefaultFilePath(nodeFromPath);
|
|
|
|
|
nodeJsPath.setSettingsKey("Copilot.NodeJsPath");
|
|
|
|
|
nodeJsPath.setLabelText(Tr::tr("Node.js path:"));
|
|
|
|
|
nodeJsPath.setHistoryCompleter("Copilot.NodePath.History");
|
|
|
|
|
nodeJsPath.setDisplayName(Tr::tr("Node.js Path"));
|
|
|
|
|
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);
|
|
|
|
|
distPath.setDefaultFilePath(distFromVim);
|
|
|
|
|
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-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);
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CopilotProjectSettings::CopilotProjectSettings(ProjectExplorer::Project *project, QObject *parent)
|
|
|
|
|
: AspectContainer(parent)
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
connect(&enableCopilot, &BoolAspect::valueChanged, this, [this, project] { save(project); });
|
|
|
|
|
connect(&useGlobalSettings, &BoolAspect::valueChanged, this, [this, project] { save(project); });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CopilotProjectSettings::setUseGlobalSettings(bool useGlobal)
|
|
|
|
|
{
|
|
|
|
|
useGlobalSettings.setValue(useGlobal);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CopilotProjectSettings::isEnabled() const
|
|
|
|
|
{
|
|
|
|
|
if (useGlobalSettings())
|
|
|
|
|
return CopilotSettings::instance().enableCopilot();
|
|
|
|
|
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.
|
|
|
|
|
CopilotSettings::instance().apply();
|
2023-01-20 07:09:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Copilot
|