Copilot: Move closer to latest settings setup and usage pattern

Change-Id: I86983f55cd53a540e2fe1a5b307ebf67f7ea7a98
Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
This commit is contained in:
hjk
2023-07-14 16:29:11 +02:00
parent bd24f18dd5
commit 17267bb15c
9 changed files with 130 additions and 175 deletions

View File

@@ -2,13 +2,21 @@
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
#include "copilotsettings.h"
#include "authwidget.h"
#include "copilotconstants.h"
#include "copilottr.h"
#include <coreplugin/dialogs/ioptionspage.h>
#include <projectexplorer/project.h>
#include <utils/algorithm.h>
#include <utils/environment.h>
#include <utils/layoutbuilder.h>
#include <utils/pathchooser.h>
#include <QToolTip>
using namespace Utils;
@@ -23,7 +31,7 @@ static void initEnableAspect(BoolAspect &enableCopilot)
enableCopilot.setDefaultValue(false);
}
CopilotSettings &CopilotSettings::instance()
CopilotSettings &settings()
{
static CopilotSettings settings;
return settings;
@@ -79,6 +87,8 @@ CopilotSettings::CopilotSettings()
"position after changes to the document."));
initEnableAspect(enableCopilot);
readSettings();
}
CopilotProjectSettings::CopilotProjectSettings(ProjectExplorer::Project *project, QObject *parent)
@@ -106,7 +116,7 @@ void CopilotProjectSettings::setUseGlobalSettings(bool useGlobal)
bool CopilotProjectSettings::isEnabled() const
{
if (useGlobalSettings())
return CopilotSettings::instance().enableCopilot();
return settings().enableCopilot();
return enableCopilot();
}
@@ -117,7 +127,105 @@ void CopilotProjectSettings::save(ProjectExplorer::Project *project)
project->setNamedSettings(Constants::COPILOT_PROJECT_SETTINGS_ID, map);
// This triggers a restart of the Copilot language server.
CopilotSettings::instance().apply();
settings().apply();
}
} // namespace Copilot
// 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)")
.arg("[agent.js](https://github.com/github/copilot.vim/tree/release/copilot/dist)"));
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
auto updateAuthWidget = [authWidget]() {
authWidget->updateClient(
FilePath::fromUserInput(settings().nodeJsPath.volatileValue()),
FilePath::fromUserInput(settings().distPath.volatileValue()));
};
connect(settings().nodeJsPath.pathChooser(),
&PathChooser::textChanged,
authWidget,
updateAuthWidget);
connect(settings().distPath.pathChooser(),
&PathChooser::textChanged,
authWidget,
updateAuthWidget);
updateAuthWidget();
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