From 410a04ba1a1ee705d645207ef802c4305b95b3dc Mon Sep 17 00:00:00 2001 From: Zhang TingAn Date: Mon, 22 Jul 2024 15:56:59 +0800 Subject: [PATCH] feat: [codegeex] add options of locale Log: as title --- src/plugins/codegeex/codegeexmanager.h | 1 + src/plugins/codegeex/copilot.cpp | 7 +++- src/plugins/codegeex/copilot.h | 2 ++ src/plugins/codegeex/option/detailwidget.cpp | 38 ++++++++++++++++++++ src/plugins/codegeex/option/detailwidget.h | 2 ++ 5 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/plugins/codegeex/codegeexmanager.h b/src/plugins/codegeex/codegeexmanager.h index 92e779a53..eb72cd1e1 100644 --- a/src/plugins/codegeex/codegeexmanager.h +++ b/src/plugins/codegeex/codegeexmanager.h @@ -40,6 +40,7 @@ namespace CodeGeeX { }; } Q_DECLARE_METATYPE(CodeGeeX::languageModel) +Q_DECLARE_METATYPE(CodeGeeX::locale) typedef QPair chatRecord; class CodeGeeXManager : public QObject diff --git a/src/plugins/codegeex/copilot.cpp b/src/plugins/codegeex/copilot.cpp index 5e22ec670..c53af6340 100644 --- a/src/plugins/codegeex/copilot.cpp +++ b/src/plugins/codegeex/copilot.cpp @@ -145,6 +145,11 @@ void Copilot::setLocale(const QString &locale) this->locale = locale; } +void Copilot::setCommitsLocale(const QString &locale) +{ + this->commitsLocale = locale; +} + void Copilot::setCurrentModel(CodeGeeX::languageModel model) { copilotApi.setModel(model); @@ -241,7 +246,7 @@ void Copilot::commits() auto diff = QString::fromUtf8(process.readAll()); QString url = QString(kUrlSSEChat) + "?stream=true"; - copilotApi.postCommand(url, diff, locale, commandCommits); + copilotApi.postCommand(url, diff, commitsLocale, commandCommits); switchToCodegeexPage(); }); diff --git a/src/plugins/codegeex/copilot.h b/src/plugins/codegeex/copilot.h index 6d0ffbd3c..3fa6a3353 100644 --- a/src/plugins/codegeex/copilot.h +++ b/src/plugins/codegeex/copilot.h @@ -27,6 +27,7 @@ class Copilot : public QObject void insterText(const QString &text); void setGenerateCodeEnabled(bool enabled); void setLocale(const QString &locale); + void setCommitsLocale(const QString &locale); void setCurrentModel(CodeGeeX::languageModel model); void handleTextChanged(); @@ -54,6 +55,7 @@ public slots: explicit Copilot(QObject *parent = nullptr); QString selectedText() const; QString locale { "zh" }; + QString commitsLocale { "zh" }; void switchToCodegeexPage(); bool responseValid(const QString &response); QString assembleCodeByCurrentFile(const QString &code); diff --git a/src/plugins/codegeex/option/detailwidget.cpp b/src/plugins/codegeex/option/detailwidget.cpp index 313d7d826..47b5b04f4 100644 --- a/src/plugins/codegeex/option/detailwidget.cpp +++ b/src/plugins/codegeex/option/detailwidget.cpp @@ -27,12 +27,16 @@ DWIDGET_USE_NAMESPACE static const char *kCodeCompletion = "codeCompletion"; static const char *kModel = "model"; +static const char *kGlobalLanguage = "globalLanguage"; +static const char *kCommitsLanguage = "commitsLanguage"; class DetailWidgetPrivate { friend class DetailWidget; DCheckBox *cbCodeCompletion = nullptr; + DComboBox *globalLanguageBox = nullptr; + DComboBox *commitsLanguageBox = nullptr; DComboBox *modelBox = nullptr; }; @@ -61,6 +65,22 @@ void DetailWidget::setupUi() completionLayout->addWidget(completionLabel); completionLayout->addWidget(d->cbCodeCompletion); + QHBoxLayout *languageLayout = new QHBoxLayout; + DLabel *languageLabel = new DLabel(QLabel::tr("Global Language Preference:"), this); + d->globalLanguageBox = new DComboBox(this); + d->globalLanguageBox->addItem("English", CodeGeeX::En); + d->globalLanguageBox->addItem("简体中文", CodeGeeX::Zh); + languageLayout->addWidget(languageLabel); + languageLayout->addWidget(d->globalLanguageBox); + + QHBoxLayout *commitsLanguageLayout = new QHBoxLayout; + DLabel *commitsLabel = new DLabel(QLabel::tr("Commits Language Preference:"), this); + d->commitsLanguageBox = new DComboBox(this); + d->commitsLanguageBox->addItem("English", CodeGeeX::En); + d->commitsLanguageBox->addItem("简体中文", CodeGeeX::Zh); + commitsLanguageLayout->addWidget(commitsLabel); + commitsLanguageLayout->addWidget(d->commitsLanguageBox); + QHBoxLayout *modelLayout = new QHBoxLayout; DLabel *modelLabel = new DLabel(QLabel::tr("model"), this); d->modelBox = new DComboBox(this); @@ -77,6 +97,8 @@ void DetailWidget::setupUi() }); vLayout->addLayout(completionLayout); + vLayout->addLayout(languageLayout); + vLayout->addLayout(commitsLanguageLayout); vLayout->addLayout(modelLayout); vLayout->addStretch(); } @@ -86,9 +108,13 @@ bool DetailWidget::getControlValue(QMap &map) CodeGeeXConfig config; config.codeCompletionEnabled = d->cbCodeCompletion->isChecked(); config.model = d->modelBox->currentData().value(); + config.globalLanguage = d->globalLanguageBox->currentData().value(); + config.commitsLanguage = d->commitsLanguageBox->currentData().value(); dataToMap(config, map); Copilot::instance()->setGenerateCodeEnabled(config.codeCompletionEnabled); + Copilot::instance()->setCommitsLocale(config.commitsLanguage == CodeGeeX::Zh ? "zh" : "cn"); + CodeGeeXManager::instance()->setLocale(config.globalLanguage); CodeGeeXManager::instance()->setCurrentModel(config.model); return true; } @@ -104,6 +130,9 @@ void DetailWidget::setControlValue(const QMap &map) if (d->modelBox->itemData(index) == config.model) d->modelBox->setCurrentIndex(index); } + + d->globalLanguageBox->setCurrentText(config.globalLanguage == CodeGeeX::Zh ? "简体中文" : "English"); + d->commitsLanguageBox->setCurrentText(config.commitsLanguage == CodeGeeX::Zh ? "简体中文" : "English"); } bool DetailWidget::dataToMap(const CodeGeeXConfig &config, QMap &map) @@ -111,6 +140,8 @@ bool DetailWidget::dataToMap(const CodeGeeXConfig &config, QMap apiKey; apiKey.insert(kCodeCompletion, config.codeCompletionEnabled); apiKey.insert(kModel, config.model); + apiKey.insert(kGlobalLanguage, config.globalLanguage); + apiKey.insert(kCommitsLanguage, config.commitsLanguage); map.insert("Detail", apiKey); @@ -126,6 +157,13 @@ bool DetailWidget::mapToData(const QMap &map, CodeGeeXConfig var = detail.value(kModel); if (var.isValid()) config.model = var.value(); + var = detail.value(kGlobalLanguage); + if (var.isValid()) + config.globalLanguage = var.value(); + var = detail.value(kCommitsLanguage); + if (var.isValid()) + config.commitsLanguage = var.value(); + return true; } diff --git a/src/plugins/codegeex/option/detailwidget.h b/src/plugins/codegeex/option/detailwidget.h index 88ef12295..eb19e83df 100644 --- a/src/plugins/codegeex/option/detailwidget.h +++ b/src/plugins/codegeex/option/detailwidget.h @@ -11,6 +11,8 @@ struct CodeGeeXConfig{ bool codeCompletionEnabled = true; + CodeGeeX::locale globalLanguage = CodeGeeX::Zh; + CodeGeeX::locale commitsLanguage = CodeGeeX::Zh; CodeGeeX::languageModel model = CodeGeeX::Lite; };