Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [codegeex] add options of locale #803

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/plugins/codegeex/codegeexmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ namespace CodeGeeX {
};
}
Q_DECLARE_METATYPE(CodeGeeX::languageModel)
Q_DECLARE_METATYPE(CodeGeeX::locale)

typedef QPair<QString, QString> chatRecord;
class CodeGeeXManager : public QObject
Expand Down
7 changes: 6 additions & 1 deletion src/plugins/codegeex/copilot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
});

Expand Down
2 changes: 2 additions & 0 deletions src/plugins/codegeex/copilot.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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);
Expand Down
38 changes: 38 additions & 0 deletions src/plugins/codegeex/option/detailwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

后续可以考虑将这些写到配置文件里面,避免以后新增语言又要修改代码

commitsLanguageLayout->addWidget(commitsLabel);
commitsLanguageLayout->addWidget(d->commitsLanguageBox);

QHBoxLayout *modelLayout = new QHBoxLayout;
DLabel *modelLabel = new DLabel(QLabel::tr("model"), this);
d->modelBox = new DComboBox(this);
Expand All @@ -77,6 +97,8 @@ void DetailWidget::setupUi()
});

vLayout->addLayout(completionLayout);
vLayout->addLayout(languageLayout);
vLayout->addLayout(commitsLanguageLayout);
vLayout->addLayout(modelLayout);
vLayout->addStretch();
}
Expand All @@ -86,9 +108,13 @@ bool DetailWidget::getControlValue(QMap<QString, QVariant> &map)
CodeGeeXConfig config;
config.codeCompletionEnabled = d->cbCodeCompletion->isChecked();
config.model = d->modelBox->currentData().value<CodeGeeX::languageModel>();
config.globalLanguage = d->globalLanguageBox->currentData().value<CodeGeeX::locale>();
config.commitsLanguage = d->commitsLanguageBox->currentData().value<CodeGeeX::locale>();
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;
}
Expand All @@ -104,13 +130,18 @@ void DetailWidget::setControlValue(const QMap<QString, QVariant> &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<QString, QVariant> &map)
{
QMap<QString, QVariant> 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);

Expand All @@ -126,6 +157,13 @@ bool DetailWidget::mapToData(const QMap<QString, QVariant> &map, CodeGeeXConfig
var = detail.value(kModel);
if (var.isValid())
config.model = var.value<CodeGeeX::languageModel>();
var = detail.value(kGlobalLanguage);
if (var.isValid())
config.globalLanguage = var.value<CodeGeeX::locale>();
var = detail.value(kCommitsLanguage);
if (var.isValid())
config.commitsLanguage = var.value<CodeGeeX::locale>();


return true;
}
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/codegeex/option/detailwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

struct CodeGeeXConfig{
bool codeCompletionEnabled = true;
CodeGeeX::locale globalLanguage = CodeGeeX::Zh;
CodeGeeX::locale commitsLanguage = CodeGeeX::Zh;
CodeGeeX::languageModel model = CodeGeeX::Lite;
};

Expand Down
Loading