Skip to content

Commit c52d612

Browse files
committed
fix bug
1 parent 2345d85 commit c52d612

File tree

9 files changed

+105
-12
lines changed

9 files changed

+105
-12
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ dependencies {
2424
api 'org.apache.commons:commons-lang3:3.9'
2525
api 'com.vladsch.flexmark:flexmark:0.62.2'
2626
api 'com.vladsch.flexmark:flexmark-ext-attributes:0.62.2'
27+
api 'io.github.biezhi:TinyPinyin:2.0.3.RELEASE'
2728
//api fileTree(dir: 'src/main/resources/lib', include: ['*.jar'])
2829

2930
}

doc/CustomCode_ZH.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
- **${question.content}**:题目描述内容
2222
- **${question.code}**:题目代码部分
2323
- **$!velocityTool.camelCaseName(str)**:一个函数,用来将字符串转化为驼峰样式
24+
- 更多工具参考[VelocityTool.java](https://github.com/shuzijun/leetcode-editor/blob/master/src/main/java/com/shuzijun/leetcode/plugin/utils/VelocityTool.java)
2425

2526
## 注意
2627
在生成的自定义代码中包含两行关键信息:

src/main/java/com/shuzijun/leetcode/plugin/actions/editor/PositionAction.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public void actionPerformed(AnActionEvent anActionEvent, Config config, Question
4949
}
5050
JBScrollPane scrollPane = WindowFactory.getDataContext(anActionEvent.getProject()).getData(DataKeys.LEETCODE_PROJECTS_SCROLL);
5151
ApplicationManager.getApplication().invokeAndWait(() -> {
52+
WindowFactory.activateToolWindow(anActionEvent.getProject());
5253
ViewManager.position(tree, scrollPane, question);
5354
});
5455
}

src/main/java/com/shuzijun/leetcode/plugin/editor/QuestionEditorProvider.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public boolean accept(@NotNull Project project, @NotNull VirtualFile file) {
3434
if(leetcodeEditor == null || StringUtils.isBlank(leetcodeEditor.getContentPath())){
3535
return false;
3636
}
37+
VirtualFile contentVf = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(new File(leetcodeEditor.getContentPath()));
38+
if(contentVf == null){
39+
return false;
40+
}
3741

3842
return this.myFirstProvider.accept(project, file);
3943
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.shuzijun.leetcode.plugin.manager;
22

33
import com.alibaba.fastjson.JSONObject;
4+
import com.intellij.openapi.fileEditor.FileDocumentManager;
45
import com.intellij.openapi.project.Project;
56
import com.intellij.openapi.vfs.LocalFileSystem;
6-
import com.intellij.openapi.vfs.VfsUtil;
77
import com.intellij.openapi.vfs.VirtualFile;
88
import com.shuzijun.leetcode.plugin.model.Config;
99
import com.shuzijun.leetcode.plugin.model.Constant;
@@ -79,7 +79,7 @@ public static void push(Question question, Project project) {
7979
}
8080
VirtualFile vf = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file);
8181
FileUtils.saveEditDocument(vf);
82-
String note = VfsUtil.loadText(vf);
82+
String note = FileDocumentManager.getInstance().getDocument(vf).getText();
8383
HttpRequest httpRequest = HttpRequest.post(URLUtils.getLeetcodeGraphql(),"application/json");
8484
JSONObject variables = new JSONObject();
8585
variables.put("titleSlug",question.getTitleSlug());

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
77
import com.intellij.openapi.project.Project;
88
import com.intellij.openapi.vfs.LocalFileSystem;
9-
import com.intellij.openapi.vfs.VfsUtil;
109
import com.intellij.openapi.vfs.VirtualFile;
1110
import com.intellij.openapi.vfs.newvfs.RefreshQueue;
1211
import com.shuzijun.leetcode.plugin.model.CodeTypeEnum;
@@ -79,7 +78,7 @@ public static String getClearCommentFileBody(File file, CodeTypeEnum codeTypeEnu
7978
saveEditDocument(vf);
8079
StringBuffer code = new StringBuffer();
8180
try {
82-
String body = VfsUtil.loadText(vf);
81+
String body = FileDocumentManager.getInstance().getDocument(vf).getText();
8382
if (StringUtils.isNotBlank(body)) {
8483

8584
List<String> codeList = new LinkedList<>();
@@ -120,8 +119,8 @@ public static String getClearCommentFileBody(File file, CodeTypeEnum codeTypeEnu
120119
}
121120
}
122121
}
123-
} catch (IOException id) {
124-
122+
} catch (Exception e) {
123+
LogUtils.LOG.error("getClearCommentFileBody error",e);
125124
}
126125
return code.toString();
127126
}

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

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

3+
import com.github.promeg.pinyinhelper.Pinyin;
34
import com.shuzijun.leetcode.plugin.model.CodeTypeEnum;
45
import com.shuzijun.leetcode.plugin.model.Config;
56
import com.shuzijun.leetcode.plugin.model.Constant;
@@ -10,17 +11,28 @@
1011
import java.util.Date;
1112

1213
/**
14+
* Provide static tool class, StringUtils document reference <a href="https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html">doc</a><br>
15+
* 提供的静态工具类,StringUtils文档参考<a href="https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html">doc</a>
16+
*
1317
* @author shuzijun
1418
*/
1519
public class VelocityTool extends StringUtils {
1620

1721
private static String[] numsAry = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
1822

19-
public static String leftPadZeros(String s, int resultLength) {
20-
if (s.length() >= resultLength) {
23+
/**
24+
* Fill 0 on the left to reach a fixed length <br>
25+
* 在左侧填充0达到固定长度length
26+
*
27+
* @param s
28+
* @param length
29+
* @return
30+
*/
31+
public static String leftPadZeros(String s, int length) {
32+
if (s.length() >= length) {
2133
return s;
2234
}
23-
int nPads = resultLength - s.length();
35+
int nPads = length - s.length();
2436
StringBuilder sb = new StringBuilder();
2537
for (int i = 0; i < nPads; ++i) {
2638
sb.append('0');
@@ -29,6 +41,13 @@ public static String leftPadZeros(String s, int resultLength) {
2941
return sb.toString();
3042
}
3143

44+
/**
45+
* Convert characters to camel case (initial letter capitalized) <br>
46+
* 转换字符为驼峰样式(开头字母大写)
47+
*
48+
* @param underscoreName
49+
* @return
50+
*/
3251
public static String camelCaseName(String underscoreName) {
3352

3453
if (isNotBlank(underscoreName)) {
@@ -59,6 +78,13 @@ public static String camelCaseName(String underscoreName) {
5978
}
6079
}
6180

81+
/**
82+
* Convert characters to camel case (lower case at the beginning) <br>
83+
* 转换字符为小驼峰样式(开头字母小写)
84+
*
85+
* @param underscoreName
86+
* @return
87+
*/
6288
public static String smallCamelCaseName(String underscoreName) {
6389

6490
if (isNotBlank(underscoreName)) {
@@ -89,6 +115,13 @@ public static String smallCamelCaseName(String underscoreName) {
89115
}
90116
}
91117

118+
/**
119+
* Convert characters to snake style <br>
120+
* 转换字符为蛇形样式
121+
*
122+
* @param underscoreName
123+
* @return
124+
*/
92125

93126
public static String snakeCaseName(String underscoreName) {
94127

@@ -114,25 +147,73 @@ public static String snakeCaseName(String underscoreName) {
114147
}
115148
}
116149

150+
/**
151+
* Get the current time. <br>
152+
* 获取当前时间
153+
*
154+
* @return
155+
*/
117156
public static String date() {
118-
return date("yyyy-MM-dd HH:mm:ss");
157+
return date("yyyy-MM-dd HH:mm:ss");
119158
}
120159

160+
/**
161+
* Get the current time. <br>
162+
* 获取当前时间
163+
*
164+
* @return
165+
*/
121166
public static String date(String format) {
122167
return DateFormatUtils.format(new Date(), format);
123168
}
124169

125-
public static String SUBMIT_REGION_BEGIN(){
170+
/**
171+
* Get start tag <br>
172+
* 获取开始标记
173+
*
174+
* @return
175+
*/
176+
public static String SUBMIT_REGION_BEGIN() {
126177
Config config = PersistentConfig.getInstance().getInitConfig();
127178
String codeType = config.getCodeType();
128179
CodeTypeEnum codeTypeEnum = CodeTypeEnum.getCodeTypeEnum(codeType);
129180
return codeTypeEnum.getComment() + Constant.SUBMIT_REGION_BEGIN;
130181
}
131182

132-
public static String SUBMIT_REGION_END(){
183+
/**
184+
* Get eng tag <br>
185+
* 获取结束标记
186+
*
187+
* @return
188+
*/
189+
public static String SUBMIT_REGION_END() {
133190
Config config = PersistentConfig.getInstance().getInitConfig();
134191
String codeType = config.getCodeType();
135192
CodeTypeEnum codeTypeEnum = CodeTypeEnum.getCodeTypeEnum(codeType);
136193
return codeTypeEnum.getComment() + Constant.SUBMIT_REGION_END;
137194
}
195+
196+
/**
197+
* Convert Chinese characters to Pinyin and remove all spaces<br>
198+
* 将汉字转为为拼音并去除所有空格
199+
*
200+
* @param str
201+
* @return
202+
*/
203+
public static String toPinyinAndTrims(String str) {
204+
if (isBlank(str)) {
205+
return "";
206+
}
207+
str = replace(str, " ", "");
208+
StringBuilder sb = new StringBuilder();
209+
for (char c : str.toCharArray()) {
210+
if (Pinyin.isChinese(c)) {
211+
String pinYin = Pinyin.toPinyin(c);
212+
sb.append(camelCaseName(pinYin.toLowerCase()));
213+
} else {
214+
sb.append(c);
215+
}
216+
}
217+
return sb.toString();
218+
}
138219
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public static String convert(String template, Object data) {
3737
VelocityContext velocityContext = new VelocityContext();
3838
velocityContext.put(VM_CONTEXT, data);
3939
velocityContext.put("velocityTool", new VelocityTool());
40+
velocityContext.put("vt", new VelocityTool());
4041
boolean isSuccess = engine.evaluate(velocityContext, writer, VM_LOG_TAG, template);
4142
if (!isSuccess) {
4243

src/main/java/com/shuzijun/leetcode/plugin/window/WindowFactory.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,9 @@ public static void updateTitle(@NotNull Project project, String userName) {
5858
}
5959
}
6060

61+
public static void activateToolWindow(@NotNull Project project) {
62+
ToolWindow leetcodeToolWindows = ToolWindowManager.getInstance(project).getToolWindow(ID);
63+
leetcodeToolWindows.activate(null);
64+
}
65+
6166
}

0 commit comments

Comments
 (0)