Skip to content

support multiline comment and html content #357

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

Merged
merged 1 commit into from
Jul 22, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static void openCode(Question question, Project project) {
} else {

if (getQuestion(question, codeTypeEnum, project)) {
question.setContent(CommentUtils.createComment(question.getContent(), codeTypeEnum));
question.setContent(CommentUtils.createComment(question.getContent(), codeTypeEnum,config));
FileUtils.saveFile(file, VelocityUtils.convert(config.getCustomTemplate(), question));
FileUtils.openFileEditorAndSaveState(file,project,question,fillPath,true);
}
Expand Down Expand Up @@ -312,7 +312,7 @@ private static String getContent(JSONObject jsonObject) {
sb.append("</li>");
}
sb.append("</div></div>");
sb.append("\\n");
sb.append("<br>");
}
sb.append("<div><li>\uD83D\uDC4D "+jsonObject.getInteger("likes")+"</li><li>\uD83D\uDC4E "+jsonObject.getInteger("dislikes")+"</li></div>");
return sb.toString();
Expand Down
46 changes: 26 additions & 20 deletions src/main/java/com/shuzijun/leetcode/plugin/model/CodeTypeEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,40 @@
* @author shuzijun
*/
public enum CodeTypeEnum {
JAVA("Java", "java", ".java", "//"),
PYTHON("Python", "python", ".py", "# "),
CPP("C++", "cpp", ".cpp", "//"),
PYTHON3("Python3", "python3", ".py", "# "),
C("C", "c", ".c", "//"),
CSHARP("C#", "csharp", ".cs", "//"),
JAVASCRIPT("JavaScript", "javascript", ".js", "//"),
RUBY("Ruby", "ruby", ".rb", "#"),
SWIFT("Swift", "swift", ".swift", "///"),
GO("Go", "golang", ".go", "//"),
SCALA("Scala", "scala", ".scala", "//"),
KOTLIN("Kotlin", "kotlin", ".kt", "//"),
RUST("Rust", "rust", ".rs", "//"),
PHP("PHP", "php", ".php", "//"),
BASH("Bash", "bash", ".sh", "#"),
MYSQL("MySQL", "mysql", ".sql", "#"),
ORACLE("Oracle", "oraclesql", ".sql", "#"),
MSSQLSERVER("MS SQL Server", "mssql", ".sql", "#"),
TypeScript("TypeScript", "typescript", ".ts", "//"),
JAVA("Java", "java", ".java", "//", "/**\n%s\n*/"),
PYTHON("Python", "python", ".py", "# ","\"\"\"\n%s\n\"\"\""),
CPP("C++", "cpp", ".cpp", "//", "/**\n%s\n*/"),
PYTHON3("Python3", "python3", ".py", "# ","\"\"\"\n%s\n\"\"\""),
C("C", "c", ".c", "//", "/**\n%s\n*/"),
CSHARP("C#", "csharp", ".cs", "//", "/**\n%s\n*/"),
JAVASCRIPT("JavaScript", "javascript", ".js", "//", "/**\n%s\n*/"),
RUBY("Ruby", "ruby", ".rb", "#","=begin\n%s\n=end"),
SWIFT("Swift", "swift", ".swift", "///", "/**\n%s\n*/"),
GO("Go", "golang", ".go", "//", "/**\n%s\n*/"),
SCALA("Scala", "scala", ".scala", "//", "/**\n%s\n*/"),
KOTLIN("Kotlin", "kotlin", ".kt", "//", "/**\n%s\n*/"),
RUST("Rust", "rust", ".rs", "//", "/**\n%s\n*/"),
PHP("PHP", "php", ".php", "//", "/**\n%s\n*/"),
BASH("Bash", "bash", ".sh", "#",": '\n%s\n'"),
MYSQL("MySQL", "mysql", ".sql", "#", "/**\n%s\n*/"),
ORACLE("Oracle", "oraclesql", ".sql", "#", "/**\n%s\n*/"),
MSSQLSERVER("MS SQL Server", "mssql", ".sql", "#", "/**\n%s\n*/"),
TypeScript("TypeScript", "typescript", ".ts", "//", "/**\n%s\n*/"),
;


private String type;
private String langSlug;
private String suffix;
private String comment;
private String multiLineComment;

CodeTypeEnum(String type, String langSlug, String suffix, String comment) {
CodeTypeEnum(String type, String langSlug, String suffix, String comment, String multiLineComment) {
this.type = type;
this.langSlug = langSlug;
this.suffix = suffix;
this.comment = comment;
this.multiLineComment = multiLineComment;
}

private static Map<String, CodeTypeEnum> MAP = new HashMap<String, CodeTypeEnum>();
Expand Down Expand Up @@ -70,4 +72,8 @@ public static CodeTypeEnum getCodeTypeEnumByLangSlug(String langSlug) {
public String getComment() {
return comment;
}

public String getMultiLineComment() {
return multiLineComment;
}
}
30 changes: 30 additions & 0 deletions src/main/java/com/shuzijun/leetcode/plugin/model/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ public class Config {
*/
private Boolean questionEditor = true;

/**
* Content Multiline Comment
*/
private Boolean multilineComment = false;

/**
* html Content
*/
private Boolean htmlContent = false;

private List<String> favoriteList;

public String getId() {
Expand Down Expand Up @@ -321,6 +331,22 @@ public void setQuestionEditor(Boolean questionEditor) {
this.questionEditor = questionEditor;
}

public Boolean getMultilineComment() {
return multilineComment;
}

public void setMultilineComment(Boolean multilineComment) {
this.multilineComment = multilineComment;
}

public Boolean getHtmlContent() {
return htmlContent;
}

public void setHtmlContent(Boolean htmlContent) {
this.htmlContent = htmlContent;
}

public boolean isModified(Config config){
if(config ==null){
return false;
Expand All @@ -343,6 +369,10 @@ public boolean isModified(Config config){
return false;
if (questionEditor != null ? !questionEditor.equals(config.questionEditor) : config.questionEditor != null)
return false;
if (multilineComment != null ? !multilineComment.equals(config.multilineComment) : config.multilineComment != null)
return false;
if (htmlContent != null ? !htmlContent.equals(config.htmlContent) : config.htmlContent != null)
return false;
return levelColour != null ? levelColour.equals(config.levelColour) : config.levelColour == null;
}

Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/shuzijun/leetcode/plugin/setting/SettingUI.form
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,27 @@
<text value="QuestionEditor"/>
</properties>
</component>
<component id="4fb71" class="javax.swing.JCheckBox" binding="multilineCheckBox" default-binding="true">
<constraints>
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false">
<maximum-size width="150" height="-1"/>
</grid>
</constraints>
<properties>
<text value="Multiline"/>
<toolTipText value="Content Multiline Comment"/>
</properties>
</component>
<component id="33e74" class="javax.swing.JCheckBox" binding="htmlContentCheckBox" default-binding="true">
<constraints>
<grid row="2" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false">
<maximum-size width="150" height="-1"/>
</grid>
</constraints>
<properties>
<text value="HtmlContent"/>
</properties>
</component>
</children>
</grid>
<grid id="14d81" layout-manager="GridLayoutManager" row-count="4" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public class SettingUI {
private JPanel codeTemplate;
private JPanel templateConstant;
private JCheckBox jcefCheckBox;
private JCheckBox multilineCheckBox;
private JCheckBox htmlContentCheckBox;


private Editor fileNameEditor = null;
Expand Down Expand Up @@ -186,6 +188,8 @@ private void loadSetting() {

jcefCheckBox.setSelected(config.getJcef());
questionEditorCheckBox.setSelected(config.getQuestionEditor());
multilineCheckBox.setSelected(config.getMultilineComment());
htmlContentCheckBox.setSelected(config.getHtmlContent());
} else {
Color[] colors = new Config().getFormatLevelColour();
easyLabel.setForeground(colors[0]);
Expand Down Expand Up @@ -254,6 +258,8 @@ public void process(Config config) {
config.setEnglishContent(englishContentBox.isSelected());
config.setJcef(jcefCheckBox.isSelected());
config.setQuestionEditor(questionEditorCheckBox.isSelected());
config.setMultilineComment(multilineCheckBox.isSelected());
config.setHtmlContent(htmlContentCheckBox.isSelected());
}


Expand Down
35 changes: 29 additions & 6 deletions src/main/java/com/shuzijun/leetcode/plugin/utils/CommentUtils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.shuzijun.leetcode.plugin.utils;

import com.shuzijun.leetcode.plugin.model.CodeTypeEnum;
import org.apache.commons.lang.StringUtils;
import com.shuzijun.leetcode.plugin.model.Config;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;

import java.util.regex.Matcher;
Expand All @@ -14,14 +15,22 @@ public class CommentUtils {

private static final Pattern subPattern = Pattern.compile("<sup>(<span.*>?)?([0-9abcdeghijklmnoprstuvwxyz\\+\\-\\*=\\(\\)\\.\\/]+)(</span>)?</sup>?");

public static String createComment(String html, CodeTypeEnum codeTypeEnum) {
public static String createComment(String html, CodeTypeEnum codeTypeEnum, Config config) {
html = html.replaceAll("(\\r\\n|\\r|\\n|\\n\\r)", "\\\\n").replaceAll(" "," ");
if(config.getHtmlContent()) {
if(config.getMultilineComment()){
return String.format(codeTypeEnum.getMultiLineComment(),html.replaceAll("\\\\n", "\n"));
}else {
return codeTypeEnum.getComment() + html.replaceAll("\\\\n", "\n" + codeTypeEnum.getComment());
}
}
Matcher subMatcher = subPattern.matcher(html);
while (subMatcher.find()) {
String subStr = SuperscriptUtils.getSup(subMatcher.group(2));
html = html.replace(subMatcher.group(), "<sup>" + subStr + "</sup>");
}
html = html.replaceAll("(\\r\\n|\\r|\\n|\\n\\r)", "\\\\n").replaceAll(" "," ");
String body = codeTypeEnum.getComment() + Jsoup.parse(html).text().replaceAll("\\\\n", "\n" + codeTypeEnum.getComment());
String comment = config.getMultilineComment()?"":codeTypeEnum.getComment();
String body = comment + Jsoup.parse(html).text().replaceAll("\\\\n", "\n" + comment);
String[] lines = body.split("\n");
StringBuilder sb = new StringBuilder();
for (String line : lines) {
Expand All @@ -31,12 +40,19 @@ public static String createComment(String html, CodeTypeEnum codeTypeEnum) {
} else {
StringBuilder lineBuilder = new StringBuilder(line);
for (int i = c; i > 0; i--) {
lineBuilder.insert(80 * i, "\n" + codeTypeEnum.getComment());
int idx = 80 * i;
while (idx > (80 * i - 20)) {
if (isSplit(lineBuilder.charAt(idx - 1))) {
break;
}
idx = idx - 1;
}
lineBuilder.insert(idx, "\n" + comment);
}
sb.append(lineBuilder).append("\n");
}
}
return sb.toString();
return config.getMultilineComment()?String.format(codeTypeEnum.getMultiLineComment(),sb):sb.toString();
}

public static String createSubmissions(String html) {
Expand All @@ -49,4 +65,11 @@ public static String createSubmissions(String html) {
pageData = pageData.replaceAll("status_code: parseInt\\('\\d+', \\d+\\),", "");
return pageData;
}

private static boolean isSplit(char c) {
if (c == 34 || c == 39 || (c >= 65 && c <= 90) || (c >= 97 && c <= 122)) {
return false;
}
return true;
}
}