Skip to content

feat: 根据不同语言选择不同的自定义代码模板 (#732) #749

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static void openCode(String titleSlug, Project project) {
try{
question.setLangSlug(codeTypeEnum.getLangSlug());
question.setContent(CommentUtils.createComment(content, codeTypeEnum, config));
FileUtils.saveFile(file, VelocityUtils.convert(config.getCustomTemplate(), question));
FileUtils.saveFile(file, VelocityUtils.convert(config.getLangCustomTemplate(config.getCodeType()), question));
FileUtils.openFileEditorAndSaveState(file, project, question, fillPath, true);
}finally {
question.setContent(content);
Expand Down
47 changes: 42 additions & 5 deletions src/main/java/com/shuzijun/leetcode/plugin/model/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.intellij.util.xmlb.annotations.Transient;
import com.shuzijun.leetcode.plugin.utils.MessageUtils;
import com.shuzijun.leetcode.plugin.utils.PropertiesUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;

import java.awt.*;
Expand Down Expand Up @@ -66,8 +67,9 @@ public class Config implements Cloneable {
* 自定义代码生成
*/
private Boolean customCode = false;

/**
* 适应英文描述
* 使用英文描述
*/
private Boolean englishContent = false;

Expand All @@ -78,7 +80,13 @@ public class Config implements Cloneable {
/**
* 自定义代码
*/
@Deprecated
@Transient
private String customTemplate = Constant.CUSTOM_TEMPLATE;


private Map<String, String> langCustomTemplate = new HashMap<>();

/**
* 用户cookie
*/
Expand Down Expand Up @@ -234,16 +242,39 @@ public void setCustomFileName(String customFileName) {
this.customFileName = customFileName;
}

@Deprecated
public String getCustomTemplate() {
return customTemplate;
}

@Deprecated
public void setCustomTemplate(String customTemplate) {
this.customTemplate = customTemplate;
}

public Map<String, String> getLangCustomTemplate() {
return langCustomTemplate;
}

public void setLangCustomTemplate(Map<String, String> langCustomTemplate) {
this.langCustomTemplate = langCustomTemplate;
}

public String getLangCustomTemplate(String codeType) {
if (!customCode) {
return Constant.CUSTOM_TEMPLATE;
} else {
return customTemplate;
return langCustomTemplate.getOrDefault(codeType, Constant.CUSTOM_TEMPLATE);
}
}

public void setCustomTemplate(String customTemplate) {
this.customTemplate = customTemplate;
public void setLangCustomTemplate(String codeType, String customTemplate) {
System.out.println("setLangCustomTemplate: " + codeType + " " + customTemplate);
if (StringUtils.isBlank(customTemplate)) {
this.langCustomTemplate.remove(codeType);
} else {
this.langCustomTemplate.put(codeType, customTemplate);
}
}

public Map<String, String> getUserCookie() {
Expand Down Expand Up @@ -446,7 +477,7 @@ public boolean isModified(Config config) {
.append(customCode, config.customCode)
.append(englishContent, config.englishContent)
.append(customFileName, config.customFileName)
.append(customTemplate, config.customTemplate)
.append(langCustomTemplate, config.langCustomTemplate)
.append(levelColour, config.levelColour)
.append(cookie, config.cookie)
.append(questionEditor, config.questionEditor)
Expand All @@ -468,4 +499,10 @@ public Config clone() {
}
return config;
}

public void printLangCustomTemplate() {
for (Map.Entry<String, String> entry : langCustomTemplate.entrySet()) {
System.out.println(entry.getKey() + " :\n " + entry.getValue());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public class Constant {
public static final Integer PLUGIN_CONFIG_VERSION_2 = 2;
//第三版本,域名更新,需要将cookie更改一下域名
public static final Integer PLUGIN_CONFIG_VERSION_3 = 3;
//第四版本,不同语言有各自的自定义代码模板
public static final Integer PLUGIN_CONFIG_VERSION_4 = 4;

/**
* 默认题目颜色
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,37 @@ public void loadState(@NotNull PersistentConfig persistentConfig) {
@Nullable
public Config getInitConfig() {
Config config = initConfig.get(INITNAME);
if (config != null && config.getVersion() != null && config.getVersion() < Constant.PLUGIN_CONFIG_VERSION_3) {
if (URLUtils.leetcodecnOld.equals(config.getUrl())) {
config.setUrl(URLUtils.leetcodecn);
}
Iterator<String> iterator = config.getUserCookie().keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next();
String value = config.getCookie(key);
if (StringUtils.isBlank(value) || key.startsWith(URLUtils.leetcodecnOld)) {
iterator.remove();
if (config != null && config.getVersion() != null) {
if (config.getVersion() < Constant.PLUGIN_CONFIG_VERSION_3) {
if (URLUtils.leetcodecnOld.equals(config.getUrl())) {
config.setUrl(URLUtils.leetcodecn);
}
Iterator<String> iterator = config.getUserCookie().keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next();
String value = config.getCookie(key);
if (StringUtils.isBlank(value) || key.startsWith(URLUtils.leetcodecnOld)) {
iterator.remove();
}
}
config.setVersion(Constant.PLUGIN_CONFIG_VERSION_3);
setInitConfig(config);
}
if (config.getVersion() < Constant.PLUGIN_CONFIG_VERSION_4) {
System.out.println("1 version < 4");
config.printLangCustomTemplate();

String codeType = config.getCodeType();
System.out.println("2 version < 4");
String legacy = config.getCustomTemplate();
System.out.println("3 version < 4");
config.setLangCustomTemplate(codeType,legacy);
System.out.println("4 version < 4");
config.setVersion(Constant.PLUGIN_CONFIG_VERSION_4);
System.out.println("5 version < 4");
setInitConfig(config);
config.printLangCustomTemplate();
}
config.setVersion(Constant.PLUGIN_CONFIG_VERSION_3);
setInitConfig(config);
}
return config;
}
Expand Down
28 changes: 24 additions & 4 deletions src/main/java/com/shuzijun/leetcode/plugin/setting/SettingUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ public class SettingUI {
private Editor templateEditor = null;
private Editor templateHelpEditor = null;

/**
* 上一次的语言,用于在切换语言的时候保存上一个语言的模板
*/
private String previousLang;

public SettingUI() {
initUI();
Expand Down Expand Up @@ -161,6 +165,20 @@ public void mouseClicked(MouseEvent e) {
questionEditorBox.addItem("Left");
questionEditorBox.addItem("Right");

codeComboBox.addActionListener(e -> {
String newCodeType = (String) codeComboBox.getSelectedItem();
String oldTemplate = templateEditor.getDocument().getText();
Config config = PersistentConfig.getInstance().getInitConfig();
if (config != null && config.getCustomCode()) {
config.setLangCustomTemplate(previousLang, oldTemplate); // 上一个语言的模板保存
String customTemplate = config.getLangCustomTemplate(newCodeType);
ApplicationManager.getApplication().runWriteAction(() -> {
templateEditor.getDocument().setText(customTemplate);
});
}
previousLang = newCodeType;
});

loadSetting();
}

Expand All @@ -179,14 +197,15 @@ private void loadSetting() {
if (StringUtils.isNotBlank(config.getCodeType())) {
codeComboBox.setSelectedItem(config.getCodeType());
}
previousLang = (String) codeComboBox.getSelectedItem();
if (StringUtils.isNotBlank(config.getUrl())) {
webComboBox.setSelectedItem(config.getUrl());
}
updateCheckBox.setSelected(config.getUpdate());
customCodeBox.setSelected(config.getCustomCode());
ApplicationManager.getApplication().runWriteAction(() -> {
fileNameEditor.getDocument().setText(config.getCustomFileName());
templateEditor.getDocument().setText(config.getCustomTemplate());
templateEditor.getDocument().setText(config.getLangCustomTemplate(config.getCodeType()));
});
englishContentBox.setSelected(config.getEnglishContent());

Expand Down Expand Up @@ -276,16 +295,17 @@ public void run(@NotNull ProgressIndicator progressIndicator) {

public void process(Config config) {
if (config.getVersion() == null) {
config.setVersion(Constant.PLUGIN_CONFIG_VERSION_3);
config.setVersion(Constant.PLUGIN_CONFIG_VERSION_4);
}
config.setLoginName(userNameField.getText());
config.setFilePath(fileFolderBtn.getText());
config.setCodeType(codeComboBox.getSelectedItem().toString());
String codeType = codeComboBox.getSelectedItem().toString();
config.setCodeType(codeType);
config.setUrl(webComboBox.getSelectedItem().toString());
config.setUpdate(updateCheckBox.isSelected());
config.setCustomCode(customCodeBox.isSelected());
config.setCustomFileName(fileNameEditor.getDocument().getText());
config.setCustomTemplate(templateEditor.getDocument().getText());
config.setLangCustomTemplate(codeType, templateEditor.getDocument().getText());
config.setFormatLevelColour(easyLabel.getForeground(), mediumLabel.getForeground(), hardLabel.getForeground());
config.setEnglishContent(englishContentBox.isSelected());
config.setCookie(cookieCheckBox.isSelected());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void helpBuildingEvent(EventBuilder eventBuilder) {
userConfig.put("proxy", config.getProxy());
userConfig.put("customCode", config.getCustomCode());
userConfig.put("customFileName", config.getCustomFileName());
userConfig.put("customTemplate", config.getCustomTemplate());
userConfig.put("customTemplate", config.getLangCustomTemplate(config.getCodeType()));
userBuilder.setData(userConfig);
context.setUser(userBuilder.build());

Expand Down