Skip to content

Commit 807da67

Browse files
authored
Merge pull request #357 from shuzijun/gradle
support multiline comment and html content
2 parents 6c52db7 + d751a15 commit 807da67

File tree

6 files changed

+114
-28
lines changed

6 files changed

+114
-28
lines changed

src/main/java/com/shuzijun/leetcode/plugin/manager/CodeManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static void openCode(Question question, Project project) {
4848
} else {
4949

5050
if (getQuestion(question, codeTypeEnum, project)) {
51-
question.setContent(CommentUtils.createComment(question.getContent(), codeTypeEnum));
51+
question.setContent(CommentUtils.createComment(question.getContent(), codeTypeEnum,config));
5252
FileUtils.saveFile(file, VelocityUtils.convert(config.getCustomTemplate(), question));
5353
FileUtils.openFileEditorAndSaveState(file,project,question,fillPath,true);
5454
}
@@ -312,7 +312,7 @@ private static String getContent(JSONObject jsonObject) {
312312
sb.append("</li>");
313313
}
314314
sb.append("</div></div>");
315-
sb.append("\\n");
315+
sb.append("<br>");
316316
}
317317
sb.append("<div><li>\uD83D\uDC4D "+jsonObject.getInteger("likes")+"</li><li>\uD83D\uDC4E "+jsonObject.getInteger("dislikes")+"</li></div>");
318318
return sb.toString();

src/main/java/com/shuzijun/leetcode/plugin/model/CodeTypeEnum.java

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,40 @@
77
* @author shuzijun
88
*/
99
public enum CodeTypeEnum {
10-
JAVA("Java", "java", ".java", "//"),
11-
PYTHON("Python", "python", ".py", "# "),
12-
CPP("C++", "cpp", ".cpp", "//"),
13-
PYTHON3("Python3", "python3", ".py", "# "),
14-
C("C", "c", ".c", "//"),
15-
CSHARP("C#", "csharp", ".cs", "//"),
16-
JAVASCRIPT("JavaScript", "javascript", ".js", "//"),
17-
RUBY("Ruby", "ruby", ".rb", "#"),
18-
SWIFT("Swift", "swift", ".swift", "///"),
19-
GO("Go", "golang", ".go", "//"),
20-
SCALA("Scala", "scala", ".scala", "//"),
21-
KOTLIN("Kotlin", "kotlin", ".kt", "//"),
22-
RUST("Rust", "rust", ".rs", "//"),
23-
PHP("PHP", "php", ".php", "//"),
24-
BASH("Bash", "bash", ".sh", "#"),
25-
MYSQL("MySQL", "mysql", ".sql", "#"),
26-
ORACLE("Oracle", "oraclesql", ".sql", "#"),
27-
MSSQLSERVER("MS SQL Server", "mssql", ".sql", "#"),
28-
TypeScript("TypeScript", "typescript", ".ts", "//"),
10+
JAVA("Java", "java", ".java", "//", "/**\n%s\n*/"),
11+
PYTHON("Python", "python", ".py", "# ","\"\"\"\n%s\n\"\"\""),
12+
CPP("C++", "cpp", ".cpp", "//", "/**\n%s\n*/"),
13+
PYTHON3("Python3", "python3", ".py", "# ","\"\"\"\n%s\n\"\"\""),
14+
C("C", "c", ".c", "//", "/**\n%s\n*/"),
15+
CSHARP("C#", "csharp", ".cs", "//", "/**\n%s\n*/"),
16+
JAVASCRIPT("JavaScript", "javascript", ".js", "//", "/**\n%s\n*/"),
17+
RUBY("Ruby", "ruby", ".rb", "#","=begin\n%s\n=end"),
18+
SWIFT("Swift", "swift", ".swift", "///", "/**\n%s\n*/"),
19+
GO("Go", "golang", ".go", "//", "/**\n%s\n*/"),
20+
SCALA("Scala", "scala", ".scala", "//", "/**\n%s\n*/"),
21+
KOTLIN("Kotlin", "kotlin", ".kt", "//", "/**\n%s\n*/"),
22+
RUST("Rust", "rust", ".rs", "//", "/**\n%s\n*/"),
23+
PHP("PHP", "php", ".php", "//", "/**\n%s\n*/"),
24+
BASH("Bash", "bash", ".sh", "#",": '\n%s\n'"),
25+
MYSQL("MySQL", "mysql", ".sql", "#", "/**\n%s\n*/"),
26+
ORACLE("Oracle", "oraclesql", ".sql", "#", "/**\n%s\n*/"),
27+
MSSQLSERVER("MS SQL Server", "mssql", ".sql", "#", "/**\n%s\n*/"),
28+
TypeScript("TypeScript", "typescript", ".ts", "//", "/**\n%s\n*/"),
2929
;
3030

3131

3232
private String type;
3333
private String langSlug;
3434
private String suffix;
3535
private String comment;
36+
private String multiLineComment;
3637

37-
CodeTypeEnum(String type, String langSlug, String suffix, String comment) {
38+
CodeTypeEnum(String type, String langSlug, String suffix, String comment, String multiLineComment) {
3839
this.type = type;
3940
this.langSlug = langSlug;
4041
this.suffix = suffix;
4142
this.comment = comment;
43+
this.multiLineComment = multiLineComment;
4244
}
4345

4446
private static Map<String, CodeTypeEnum> MAP = new HashMap<String, CodeTypeEnum>();
@@ -70,4 +72,8 @@ public static CodeTypeEnum getCodeTypeEnumByLangSlug(String langSlug) {
7072
public String getComment() {
7173
return comment;
7274
}
75+
76+
public String getMultiLineComment() {
77+
return multiLineComment;
78+
}
7379
}

src/main/java/com/shuzijun/leetcode/plugin/model/Config.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ public class Config {
9595
*/
9696
private Boolean questionEditor = true;
9797

98+
/**
99+
* Content Multiline Comment
100+
*/
101+
private Boolean multilineComment = false;
102+
103+
/**
104+
* html Content
105+
*/
106+
private Boolean htmlContent = false;
107+
98108
private List<String> favoriteList;
99109

100110
public String getId() {
@@ -321,6 +331,22 @@ public void setQuestionEditor(Boolean questionEditor) {
321331
this.questionEditor = questionEditor;
322332
}
323333

334+
public Boolean getMultilineComment() {
335+
return multilineComment;
336+
}
337+
338+
public void setMultilineComment(Boolean multilineComment) {
339+
this.multilineComment = multilineComment;
340+
}
341+
342+
public Boolean getHtmlContent() {
343+
return htmlContent;
344+
}
345+
346+
public void setHtmlContent(Boolean htmlContent) {
347+
this.htmlContent = htmlContent;
348+
}
349+
324350
public boolean isModified(Config config){
325351
if(config ==null){
326352
return false;
@@ -343,6 +369,10 @@ public boolean isModified(Config config){
343369
return false;
344370
if (questionEditor != null ? !questionEditor.equals(config.questionEditor) : config.questionEditor != null)
345371
return false;
372+
if (multilineComment != null ? !multilineComment.equals(config.multilineComment) : config.multilineComment != null)
373+
return false;
374+
if (htmlContent != null ? !htmlContent.equals(config.htmlContent) : config.htmlContent != null)
375+
return false;
346376
return levelColour != null ? levelColour.equals(config.levelColour) : config.levelColour == null;
347377
}
348378

src/main/java/com/shuzijun/leetcode/plugin/setting/SettingUI.form

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,27 @@
136136
<text value="QuestionEditor"/>
137137
</properties>
138138
</component>
139+
<component id="4fb71" class="javax.swing.JCheckBox" binding="multilineCheckBox" default-binding="true">
140+
<constraints>
141+
<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">
142+
<maximum-size width="150" height="-1"/>
143+
</grid>
144+
</constraints>
145+
<properties>
146+
<text value="Multiline"/>
147+
<toolTipText value="Content Multiline Comment"/>
148+
</properties>
149+
</component>
150+
<component id="33e74" class="javax.swing.JCheckBox" binding="htmlContentCheckBox" default-binding="true">
151+
<constraints>
152+
<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">
153+
<maximum-size width="150" height="-1"/>
154+
</grid>
155+
</constraints>
156+
<properties>
157+
<text value="HtmlContent"/>
158+
</properties>
159+
</component>
139160
</children>
140161
</grid>
141162
<grid id="14d81" layout-manager="GridLayoutManager" row-count="4" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">

src/main/java/com/shuzijun/leetcode/plugin/setting/SettingUI.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public class SettingUI {
5757
private JPanel codeTemplate;
5858
private JPanel templateConstant;
5959
private JCheckBox jcefCheckBox;
60+
private JCheckBox multilineCheckBox;
61+
private JCheckBox htmlContentCheckBox;
6062

6163

6264
private Editor fileNameEditor = null;
@@ -186,6 +188,8 @@ private void loadSetting() {
186188

187189
jcefCheckBox.setSelected(config.getJcef());
188190
questionEditorCheckBox.setSelected(config.getQuestionEditor());
191+
multilineCheckBox.setSelected(config.getMultilineComment());
192+
htmlContentCheckBox.setSelected(config.getHtmlContent());
189193
} else {
190194
Color[] colors = new Config().getFormatLevelColour();
191195
easyLabel.setForeground(colors[0]);
@@ -254,6 +258,8 @@ public void process(Config config) {
254258
config.setEnglishContent(englishContentBox.isSelected());
255259
config.setJcef(jcefCheckBox.isSelected());
256260
config.setQuestionEditor(questionEditorCheckBox.isSelected());
261+
config.setMultilineComment(multilineCheckBox.isSelected());
262+
config.setHtmlContent(htmlContentCheckBox.isSelected());
257263
}
258264

259265

src/main/java/com/shuzijun/leetcode/plugin/utils/CommentUtils.java

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.shuzijun.leetcode.plugin.utils;
22

33
import com.shuzijun.leetcode.plugin.model.CodeTypeEnum;
4-
import org.apache.commons.lang.StringUtils;
4+
import com.shuzijun.leetcode.plugin.model.Config;
5+
import org.apache.commons.lang3.StringUtils;
56
import org.jsoup.Jsoup;
67

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

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

17-
public static String createComment(String html, CodeTypeEnum codeTypeEnum) {
18+
public static String createComment(String html, CodeTypeEnum codeTypeEnum, Config config) {
19+
html = html.replaceAll("(\\r\\n|\\r|\\n|\\n\\r)", "\\\\n").replaceAll(" "," ");
20+
if(config.getHtmlContent()) {
21+
if(config.getMultilineComment()){
22+
return String.format(codeTypeEnum.getMultiLineComment(),html.replaceAll("\\\\n", "\n"));
23+
}else {
24+
return codeTypeEnum.getComment() + html.replaceAll("\\\\n", "\n" + codeTypeEnum.getComment());
25+
}
26+
}
1827
Matcher subMatcher = subPattern.matcher(html);
1928
while (subMatcher.find()) {
2029
String subStr = SuperscriptUtils.getSup(subMatcher.group(2));
2130
html = html.replace(subMatcher.group(), "<sup>" + subStr + "</sup>");
2231
}
23-
html = html.replaceAll("(\\r\\n|\\r|\\n|\\n\\r)", "\\\\n").replaceAll(" "," ");
24-
String body = codeTypeEnum.getComment() + Jsoup.parse(html).text().replaceAll("\\\\n", "\n" + codeTypeEnum.getComment());
32+
String comment = config.getMultilineComment()?"":codeTypeEnum.getComment();
33+
String body = comment + Jsoup.parse(html).text().replaceAll("\\\\n", "\n" + comment);
2534
String[] lines = body.split("\n");
2635
StringBuilder sb = new StringBuilder();
2736
for (String line : lines) {
@@ -31,12 +40,19 @@ public static String createComment(String html, CodeTypeEnum codeTypeEnum) {
3140
} else {
3241
StringBuilder lineBuilder = new StringBuilder(line);
3342
for (int i = c; i > 0; i--) {
34-
lineBuilder.insert(80 * i, "\n" + codeTypeEnum.getComment());
43+
int idx = 80 * i;
44+
while (idx > (80 * i - 20)) {
45+
if (isSplit(lineBuilder.charAt(idx - 1))) {
46+
break;
47+
}
48+
idx = idx - 1;
49+
}
50+
lineBuilder.insert(idx, "\n" + comment);
3551
}
3652
sb.append(lineBuilder).append("\n");
3753
}
3854
}
39-
return sb.toString();
55+
return config.getMultilineComment()?String.format(codeTypeEnum.getMultiLineComment(),sb):sb.toString();
4056
}
4157

4258
public static String createSubmissions(String html) {
@@ -49,4 +65,11 @@ public static String createSubmissions(String html) {
4965
pageData = pageData.replaceAll("status_code: parseInt\\('\\d+', \\d+\\),", "");
5066
return pageData;
5167
}
68+
69+
private static boolean isSplit(char c) {
70+
if (c == 34 || c == 39 || (c >= 65 && c <= 90) || (c >= 97 && c <= 122)) {
71+
return false;
72+
}
73+
return true;
74+
}
5275
}

0 commit comments

Comments
 (0)