From c52d612e9cc8a82d2b892794de4ef60cfbb0bed4 Mon Sep 17 00:00:00 2001 From: shuzijun Date: Wed, 18 Aug 2021 17:14:28 +0800 Subject: [PATCH] fix bug --- build.gradle | 1 + doc/CustomCode_ZH.md | 1 + .../plugin/actions/editor/PositionAction.java | 1 + .../plugin/editor/QuestionEditorProvider.java | 4 + .../leetcode/plugin/manager/NoteManager.java | 4 +- .../leetcode/plugin/utils/FileUtils.java | 7 +- .../leetcode/plugin/utils/VelocityTool.java | 93 +++++++++++++++++-- .../leetcode/plugin/utils/VelocityUtils.java | 1 + .../leetcode/plugin/window/WindowFactory.java | 5 + 9 files changed, 105 insertions(+), 12 deletions(-) diff --git a/build.gradle b/build.gradle index 5d6e299c..938d0c28 100644 --- a/build.gradle +++ b/build.gradle @@ -24,6 +24,7 @@ dependencies { api 'org.apache.commons:commons-lang3:3.9' api 'com.vladsch.flexmark:flexmark:0.62.2' api 'com.vladsch.flexmark:flexmark-ext-attributes:0.62.2' + api 'io.github.biezhi:TinyPinyin:2.0.3.RELEASE' //api fileTree(dir: 'src/main/resources/lib', include: ['*.jar']) } diff --git a/doc/CustomCode_ZH.md b/doc/CustomCode_ZH.md index 1e341951..66085e95 100644 --- a/doc/CustomCode_ZH.md +++ b/doc/CustomCode_ZH.md @@ -21,6 +21,7 @@ - **${question.content}**:题目描述内容 - **${question.code}**:题目代码部分 - **$!velocityTool.camelCaseName(str)**:一个函数,用来将字符串转化为驼峰样式 + - 更多工具参考[VelocityTool.java](https://github.com/shuzijun/leetcode-editor/blob/master/src/main/java/com/shuzijun/leetcode/plugin/utils/VelocityTool.java) ## 注意 在生成的自定义代码中包含两行关键信息: diff --git a/src/main/java/com/shuzijun/leetcode/plugin/actions/editor/PositionAction.java b/src/main/java/com/shuzijun/leetcode/plugin/actions/editor/PositionAction.java index 08d9d09e..3f739f69 100644 --- a/src/main/java/com/shuzijun/leetcode/plugin/actions/editor/PositionAction.java +++ b/src/main/java/com/shuzijun/leetcode/plugin/actions/editor/PositionAction.java @@ -49,6 +49,7 @@ public void actionPerformed(AnActionEvent anActionEvent, Config config, Question } JBScrollPane scrollPane = WindowFactory.getDataContext(anActionEvent.getProject()).getData(DataKeys.LEETCODE_PROJECTS_SCROLL); ApplicationManager.getApplication().invokeAndWait(() -> { + WindowFactory.activateToolWindow(anActionEvent.getProject()); ViewManager.position(tree, scrollPane, question); }); } diff --git a/src/main/java/com/shuzijun/leetcode/plugin/editor/QuestionEditorProvider.java b/src/main/java/com/shuzijun/leetcode/plugin/editor/QuestionEditorProvider.java index 965e0466..6ec745a9 100644 --- a/src/main/java/com/shuzijun/leetcode/plugin/editor/QuestionEditorProvider.java +++ b/src/main/java/com/shuzijun/leetcode/plugin/editor/QuestionEditorProvider.java @@ -34,6 +34,10 @@ public boolean accept(@NotNull Project project, @NotNull VirtualFile file) { if(leetcodeEditor == null || StringUtils.isBlank(leetcodeEditor.getContentPath())){ return false; } + VirtualFile contentVf = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(new File(leetcodeEditor.getContentPath())); + if(contentVf == null){ + return false; + } return this.myFirstProvider.accept(project, file); } diff --git a/src/main/java/com/shuzijun/leetcode/plugin/manager/NoteManager.java b/src/main/java/com/shuzijun/leetcode/plugin/manager/NoteManager.java index 3603d7e8..aac4bf6c 100644 --- a/src/main/java/com/shuzijun/leetcode/plugin/manager/NoteManager.java +++ b/src/main/java/com/shuzijun/leetcode/plugin/manager/NoteManager.java @@ -1,9 +1,9 @@ package com.shuzijun.leetcode.plugin.manager; import com.alibaba.fastjson.JSONObject; +import com.intellij.openapi.fileEditor.FileDocumentManager; import com.intellij.openapi.project.Project; import com.intellij.openapi.vfs.LocalFileSystem; -import com.intellij.openapi.vfs.VfsUtil; import com.intellij.openapi.vfs.VirtualFile; import com.shuzijun.leetcode.plugin.model.Config; import com.shuzijun.leetcode.plugin.model.Constant; @@ -79,7 +79,7 @@ public static void push(Question question, Project project) { } VirtualFile vf = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file); FileUtils.saveEditDocument(vf); - String note = VfsUtil.loadText(vf); + String note = FileDocumentManager.getInstance().getDocument(vf).getText(); HttpRequest httpRequest = HttpRequest.post(URLUtils.getLeetcodeGraphql(),"application/json"); JSONObject variables = new JSONObject(); variables.put("titleSlug",question.getTitleSlug()); diff --git a/src/main/java/com/shuzijun/leetcode/plugin/utils/FileUtils.java b/src/main/java/com/shuzijun/leetcode/plugin/utils/FileUtils.java index 28938d75..0e63e343 100644 --- a/src/main/java/com/shuzijun/leetcode/plugin/utils/FileUtils.java +++ b/src/main/java/com/shuzijun/leetcode/plugin/utils/FileUtils.java @@ -6,7 +6,6 @@ import com.intellij.openapi.fileEditor.OpenFileDescriptor; import com.intellij.openapi.project.Project; import com.intellij.openapi.vfs.LocalFileSystem; -import com.intellij.openapi.vfs.VfsUtil; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.newvfs.RefreshQueue; import com.shuzijun.leetcode.plugin.model.CodeTypeEnum; @@ -79,7 +78,7 @@ public static String getClearCommentFileBody(File file, CodeTypeEnum codeTypeEnu saveEditDocument(vf); StringBuffer code = new StringBuffer(); try { - String body = VfsUtil.loadText(vf); + String body = FileDocumentManager.getInstance().getDocument(vf).getText(); if (StringUtils.isNotBlank(body)) { List codeList = new LinkedList<>(); @@ -120,8 +119,8 @@ public static String getClearCommentFileBody(File file, CodeTypeEnum codeTypeEnu } } } - } catch (IOException id) { - + } catch (Exception e) { + LogUtils.LOG.error("getClearCommentFileBody error",e); } return code.toString(); } diff --git a/src/main/java/com/shuzijun/leetcode/plugin/utils/VelocityTool.java b/src/main/java/com/shuzijun/leetcode/plugin/utils/VelocityTool.java index 467b5b7d..3f4dd293 100644 --- a/src/main/java/com/shuzijun/leetcode/plugin/utils/VelocityTool.java +++ b/src/main/java/com/shuzijun/leetcode/plugin/utils/VelocityTool.java @@ -1,5 +1,6 @@ package com.shuzijun.leetcode.plugin.utils; +import com.github.promeg.pinyinhelper.Pinyin; import com.shuzijun.leetcode.plugin.model.CodeTypeEnum; import com.shuzijun.leetcode.plugin.model.Config; import com.shuzijun.leetcode.plugin.model.Constant; @@ -10,17 +11,28 @@ import java.util.Date; /** + * Provide static tool class, StringUtils document reference doc
+ * 提供的静态工具类,StringUtils文档参考doc + * * @author shuzijun */ public class VelocityTool extends StringUtils { private static String[] numsAry = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; - public static String leftPadZeros(String s, int resultLength) { - if (s.length() >= resultLength) { + /** + * Fill 0 on the left to reach a fixed length
+ * 在左侧填充0达到固定长度length + * + * @param s + * @param length + * @return + */ + public static String leftPadZeros(String s, int length) { + if (s.length() >= length) { return s; } - int nPads = resultLength - s.length(); + int nPads = length - s.length(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < nPads; ++i) { sb.append('0'); @@ -29,6 +41,13 @@ public static String leftPadZeros(String s, int resultLength) { return sb.toString(); } + /** + * Convert characters to camel case (initial letter capitalized)
+ * 转换字符为驼峰样式(开头字母大写) + * + * @param underscoreName + * @return + */ public static String camelCaseName(String underscoreName) { if (isNotBlank(underscoreName)) { @@ -59,6 +78,13 @@ public static String camelCaseName(String underscoreName) { } } + /** + * Convert characters to camel case (lower case at the beginning)
+ * 转换字符为小驼峰样式(开头字母小写) + * + * @param underscoreName + * @return + */ public static String smallCamelCaseName(String underscoreName) { if (isNotBlank(underscoreName)) { @@ -89,6 +115,13 @@ public static String smallCamelCaseName(String underscoreName) { } } + /** + * Convert characters to snake style
+ * 转换字符为蛇形样式 + * + * @param underscoreName + * @return + */ public static String snakeCaseName(String underscoreName) { @@ -114,25 +147,73 @@ public static String snakeCaseName(String underscoreName) { } } + /** + * Get the current time.
+ * 获取当前时间 + * + * @return + */ public static String date() { - return date("yyyy-MM-dd HH:mm:ss"); + return date("yyyy-MM-dd HH:mm:ss"); } + /** + * Get the current time.
+ * 获取当前时间 + * + * @return + */ public static String date(String format) { return DateFormatUtils.format(new Date(), format); } - public static String SUBMIT_REGION_BEGIN(){ + /** + * Get start tag
+ * 获取开始标记 + * + * @return + */ + public static String SUBMIT_REGION_BEGIN() { Config config = PersistentConfig.getInstance().getInitConfig(); String codeType = config.getCodeType(); CodeTypeEnum codeTypeEnum = CodeTypeEnum.getCodeTypeEnum(codeType); return codeTypeEnum.getComment() + Constant.SUBMIT_REGION_BEGIN; } - public static String SUBMIT_REGION_END(){ + /** + * Get eng tag
+ * 获取结束标记 + * + * @return + */ + public static String SUBMIT_REGION_END() { Config config = PersistentConfig.getInstance().getInitConfig(); String codeType = config.getCodeType(); CodeTypeEnum codeTypeEnum = CodeTypeEnum.getCodeTypeEnum(codeType); return codeTypeEnum.getComment() + Constant.SUBMIT_REGION_END; } + + /** + * Convert Chinese characters to Pinyin and remove all spaces
+ * 将汉字转为为拼音并去除所有空格 + * + * @param str + * @return + */ + public static String toPinyinAndTrims(String str) { + if (isBlank(str)) { + return ""; + } + str = replace(str, " ", ""); + StringBuilder sb = new StringBuilder(); + for (char c : str.toCharArray()) { + if (Pinyin.isChinese(c)) { + String pinYin = Pinyin.toPinyin(c); + sb.append(camelCaseName(pinYin.toLowerCase())); + } else { + sb.append(c); + } + } + return sb.toString(); + } } diff --git a/src/main/java/com/shuzijun/leetcode/plugin/utils/VelocityUtils.java b/src/main/java/com/shuzijun/leetcode/plugin/utils/VelocityUtils.java index cfe04788..e10bdc6a 100644 --- a/src/main/java/com/shuzijun/leetcode/plugin/utils/VelocityUtils.java +++ b/src/main/java/com/shuzijun/leetcode/plugin/utils/VelocityUtils.java @@ -37,6 +37,7 @@ public static String convert(String template, Object data) { VelocityContext velocityContext = new VelocityContext(); velocityContext.put(VM_CONTEXT, data); velocityContext.put("velocityTool", new VelocityTool()); + velocityContext.put("vt", new VelocityTool()); boolean isSuccess = engine.evaluate(velocityContext, writer, VM_LOG_TAG, template); if (!isSuccess) { diff --git a/src/main/java/com/shuzijun/leetcode/plugin/window/WindowFactory.java b/src/main/java/com/shuzijun/leetcode/plugin/window/WindowFactory.java index f63c0bb1..b3abc751 100644 --- a/src/main/java/com/shuzijun/leetcode/plugin/window/WindowFactory.java +++ b/src/main/java/com/shuzijun/leetcode/plugin/window/WindowFactory.java @@ -58,4 +58,9 @@ public static void updateTitle(@NotNull Project project, String userName) { } } + public static void activateToolWindow(@NotNull Project project) { + ToolWindow leetcodeToolWindows = ToolWindowManager.getInstance(project).getToolWindow(ID); + leetcodeToolWindows.activate(null); + } + }