Skip to content
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
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'])

}
Expand Down
1 change: 1 addition & 0 deletions doc/CustomCode_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

## 注意
在生成的自定义代码中包含两行关键信息:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> codeList = new LinkedList<>();
Expand Down Expand Up @@ -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();
}
Expand Down
93 changes: 87 additions & 6 deletions src/main/java/com/shuzijun/leetcode/plugin/utils/VelocityTool.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,17 +11,28 @@
import java.util.Date;

/**
* 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>
* 提供的静态工具类,StringUtils文档参考<a href="https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html">doc</a>
*
* @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 <br>
* 在左侧填充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');
Expand All @@ -29,6 +41,13 @@ public static String leftPadZeros(String s, int resultLength) {
return sb.toString();
}

/**
* Convert characters to camel case (initial letter capitalized) <br>
* 转换字符为驼峰样式(开头字母大写)
*
* @param underscoreName
* @return
*/
public static String camelCaseName(String underscoreName) {

if (isNotBlank(underscoreName)) {
Expand Down Expand Up @@ -59,6 +78,13 @@ public static String camelCaseName(String underscoreName) {
}
}

/**
* Convert characters to camel case (lower case at the beginning) <br>
* 转换字符为小驼峰样式(开头字母小写)
*
* @param underscoreName
* @return
*/
public static String smallCamelCaseName(String underscoreName) {

if (isNotBlank(underscoreName)) {
Expand Down Expand Up @@ -89,6 +115,13 @@ public static String smallCamelCaseName(String underscoreName) {
}
}

/**
* Convert characters to snake style <br>
* 转换字符为蛇形样式
*
* @param underscoreName
* @return
*/

public static String snakeCaseName(String underscoreName) {

Expand All @@ -114,25 +147,73 @@ public static String snakeCaseName(String underscoreName) {
}
}

/**
* Get the current time. <br>
* 获取当前时间
*
* @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. <br>
* 获取当前时间
*
* @return
*/
public static String date(String format) {
return DateFormatUtils.format(new Date(), format);
}

public static String SUBMIT_REGION_BEGIN(){
/**
* Get start tag <br>
* 获取开始标记
*
* @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 <br>
* 获取结束标记
*
* @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<br>
* 将汉字转为为拼音并去除所有空格
*
* @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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}