Skip to content

Commit c78a0be

Browse files
committed
修复插件BUG:
1.修改GenerateO2O组件在方法上下文以外环境时的可见性问题 2.重构插件部分逻辑 3.新增插件logo
1 parent 3e23613 commit c78a0be

File tree

7 files changed

+198
-122
lines changed

7 files changed

+198
-122
lines changed

object-helper.jar

2.12 KB
Binary file not shown.

resources/META-INF/plugin.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<idea-plugin>
22
<id>cn.bigcoder.plugin.objecthelper</id>
33
<name>ObjectHelper</name>
4-
<version>1.0.1</version>
4+
<version>1.0.2</version>
55
<vendor email="bigcoder84@gmail.com" url="https://github.com/tianjindong/object-helper-plugin">HearingSmile</vendor>
66

77
<description><![CDATA[
@@ -11,7 +11,8 @@
1111
]]></description>
1212

1313
<change-notes><![CDATA[
14-
<li>Fix bug:fixed bugs in exception scenarios.Improve plugin compatibility.</li>
14+
<li>Fix bug:Fixed location visibility of GenerateO2O components outside of methods</li>
15+
<li>Add logo</li>
1516
]]>
1617
</change-notes>
1718

@@ -28,7 +29,7 @@
2829
</extensions>
2930

3031
<actions>
31-
<action id="ObjectHelper" class="cn.bigcoder.plugin.objecthelper.GenerateO2O" text="generateo2o">
32+
<action id="ObjectHelper" class="cn.bigcoder.plugin.objecthelper.component.GenerateO2O" text="GenerateO2O">
3233
<add-to-group group-id="GenerateGroup" anchor="last"/>
3334
</action>
3435
</actions>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cn.bigcoder.plugin.objecthelper.common.constant;
2+
3+
/**
4+
* @author: Jindong.Tian
5+
* @date: 2021-01-31
6+
**/
7+
public class JavaSeparator {
8+
public static final String EMPTY_BODY = "";
9+
public static final String BLANK_SEPARATOR = " ";
10+
public static final String LINE_SEPARATOR = "\n";
11+
public static final String COMMA_SEPARATOR = ",";
12+
public static final String SEMICOLON_SEPARATOR = ";\n";
13+
14+
public static final String VOID = "void";
15+
16+
public static final String PARENTHESIS_OPEN = "(";
17+
public static final String PARENTHESIS_CLOSE = ")";
18+
public static final String PARENTHESIS = "()";
19+
public static final String BRACE_OPEN = "{";
20+
public static final String BRACE_CLOSE = "}";
21+
public static final String BRACE = "{}";
22+
public static final String BRACKET_OPEN = "[";
23+
public static final String BRACKET_CLOSE = "]";
24+
public static final String BRACKET = "[]";
25+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package cn.bigcoder.plugin.objecthelper.common.util;
2+
3+
import cn.bigcoder.plugin.objecthelper.common.constant.JavaSeparator;
4+
import cn.bigcoder.plugin.objecthelper.common.enums.JavaModify;
5+
import com.intellij.openapi.actionSystem.AnActionEvent;
6+
import com.intellij.openapi.actionSystem.LangDataKeys;
7+
import com.intellij.openapi.actionSystem.PlatformDataKeys;
8+
import com.intellij.openapi.editor.Editor;
9+
import com.intellij.openapi.project.Project;
10+
import com.intellij.psi.*;
11+
import com.intellij.psi.search.GlobalSearchScope;
12+
import org.jetbrains.annotations.NotNull;
13+
14+
import java.util.ArrayList;
15+
import java.util.Arrays;
16+
import java.util.List;
17+
18+
/**
19+
* @author: Jindong.Tian
20+
* @date: 2021-01-31
21+
**/
22+
public class PsiUtils {
23+
24+
/**
25+
* 获取光标所在处的{@code PsiElement}
26+
* @param anActionEvent
27+
* @return
28+
*/
29+
public static PsiElement getCursorPsiElement(AnActionEvent anActionEvent) {
30+
PsiFile psiFile = anActionEvent.getData(LangDataKeys.PSI_FILE);
31+
Editor editor = anActionEvent.getData(PlatformDataKeys.EDITOR);
32+
if (psiFile == null || editor == null) {
33+
anActionEvent.getPresentation().setEnabled(false);
34+
return null;
35+
}
36+
//获取当前光标处的PsiElement
37+
int offset = editor.getCaretModel().getOffset();
38+
return psiFile.findElementAt(offset);
39+
}
40+
41+
/**
42+
* 根据{@code PsiType}获取对应的{@code PsiClass}
43+
*
44+
* @param psiType
45+
* @param project
46+
* @return
47+
*/
48+
public static PsiClass getPsiClass(PsiType psiType, Project project) {
49+
if (psiType == null) {
50+
return null;
51+
}
52+
//带package的class名称
53+
String parameterClassWithPackage = psiType.getInternalCanonicalText();
54+
//为了解析字段,这里需要加载参数的class
55+
JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
56+
return facade.findClass(parameterClassWithPackage, GlobalSearchScope.allScope(project));
57+
}
58+
59+
/**
60+
* 获取方法名称
61+
* @param psiMethod
62+
* @return
63+
*/
64+
public static String getMethodName(PsiMethod psiMethod) {
65+
if (psiMethod == null) {
66+
return null;
67+
}
68+
return psiMethod.getName();
69+
}
70+
71+
/**
72+
* 获取方法返回名称
73+
* @param psiMethod
74+
* @return
75+
*/
76+
@NotNull
77+
public static String getMethodReturnClassName(PsiMethod psiMethod) {
78+
PsiType returnType = psiMethod.getReturnType();
79+
if (returnType == null) {
80+
return JavaSeparator.VOID;
81+
}
82+
return returnType.getPresentableText();
83+
}
84+
85+
/**
86+
* 获取方法的参数列表
87+
* @param psiMethod
88+
* @return
89+
*/
90+
@NotNull
91+
public static List<PsiParameter> getPsiParameters(PsiMethod psiMethod){
92+
return Arrays.asList(psiMethod.getParameterList().getParameters());
93+
}
94+
95+
/**
96+
* 获取方法的修饰符
97+
*
98+
* @param modifierList
99+
* @return
100+
*/
101+
@NotNull
102+
public static List<JavaModify> getMethodModifies(PsiModifierList modifierList) {
103+
List<JavaModify> result = new ArrayList<>();
104+
if (modifierList.hasModifierProperty(JavaModify.PUBLIC.getName())) {
105+
result.add(JavaModify.PUBLIC);
106+
} else if (modifierList.hasModifierProperty(JavaModify.PROTECTED.getName())) {
107+
result.add(JavaModify.PROTECTED);
108+
} else if (modifierList.hasModifierProperty(JavaModify.PRIVATE.getName())) {
109+
result.add(JavaModify.PRIVATE);
110+
}
111+
if (modifierList.hasModifierProperty(JavaModify.STATIC.getName())) {
112+
result.add(JavaModify.STATIC);
113+
}
114+
if (modifierList.hasModifierProperty(JavaModify.FINAL.getName())) {
115+
result.add(JavaModify.FINAL);
116+
}
117+
return result;
118+
}
119+
}
Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
package cn.bigcoder.plugin.objecthelper;
1+
package cn.bigcoder.plugin.objecthelper.component;
22

3+
import cn.bigcoder.plugin.objecthelper.common.util.PsiUtils;
34
import cn.bigcoder.plugin.objecthelper.generator.Generator;
45
import cn.bigcoder.plugin.objecthelper.generator.method.ObjectCopyMethodGenerator;
56
import com.intellij.openapi.actionSystem.AnAction;
67
import com.intellij.openapi.actionSystem.AnActionEvent;
7-
import com.intellij.openapi.actionSystem.LangDataKeys;
8-
import com.intellij.openapi.actionSystem.PlatformDataKeys;
98
import com.intellij.openapi.command.WriteCommandAction;
10-
import com.intellij.openapi.editor.Editor;
11-
import com.intellij.openapi.project.Project;
12-
import com.intellij.psi.*;
9+
import com.intellij.psi.JavaPsiFacade;
10+
import com.intellij.psi.PsiElement;
11+
import com.intellij.psi.PsiElementFactory;
12+
import com.intellij.psi.PsiMethod;
1313
import com.intellij.psi.util.PsiTreeUtil;
14+
import org.jetbrains.annotations.NotNull;
1415

1516
public class GenerateO2O extends AnAction {
1617

@@ -21,6 +22,15 @@ public void actionPerformed(AnActionEvent anActionEvent) {
2122
});
2223
}
2324

25+
@Override
26+
public void update(@NotNull AnActionEvent anActionEvent) {
27+
// 如果当前光标不在方法中,则不显示GernerateO2O组件
28+
if (getPsiMethodFromContext(anActionEvent) == null) {
29+
anActionEvent.getPresentation().setEnabled(false);
30+
}
31+
super.update(anActionEvent);
32+
}
33+
2434
private void generateO2O(PsiMethod psiMethod) {
2535
if (psiMethod == null) {
2636
return;
@@ -34,22 +44,10 @@ private void generateO2O(PsiMethod psiMethod) {
3444
}
3545

3646
private PsiMethod getPsiMethodFromContext(AnActionEvent e) {
37-
PsiElement elementAt = getPsiElement(e);
47+
PsiElement elementAt = PsiUtils.getCursorPsiElement(e);
3848
if (elementAt == null) {
3949
return null;
4050
}
4151
return PsiTreeUtil.getParentOfType(elementAt, PsiMethod.class);
4252
}
43-
44-
private PsiElement getPsiElement(AnActionEvent e) {
45-
PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE);
46-
Editor editor = e.getData(PlatformDataKeys.EDITOR);
47-
if (psiFile == null || editor == null) {
48-
e.getPresentation().setEnabled(false);
49-
return null;
50-
}
51-
//获取当前光标处的PsiElement
52-
int offset = editor.getCaretModel().getOffset();
53-
return psiFile.findElementAt(offset);
54-
}
5553
}
Lines changed: 19 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,27 @@
11
package cn.bigcoder.plugin.objecthelper.generator.method;
22

3-
import cn.bigcoder.plugin.objecthelper.common.enums.JavaModify;
4-
import cn.bigcoder.plugin.objecthelper.generator.Generator;
3+
import cn.bigcoder.plugin.objecthelper.common.util.PsiUtils;
54
import cn.bigcoder.plugin.objecthelper.common.util.StringUtils;
6-
import com.intellij.psi.PsiClass;
5+
import cn.bigcoder.plugin.objecthelper.generator.Generator;
6+
import com.intellij.openapi.project.Project;
7+
import com.intellij.psi.PsiMethod;
78
import com.intellij.psi.PsiParameter;
8-
import org.apache.commons.collections.CollectionUtils;
99

1010
import java.util.List;
1111

12+
import static cn.bigcoder.plugin.objecthelper.common.constant.JavaSeparator.*;
13+
import static cn.bigcoder.plugin.objecthelper.common.util.PsiUtils.*;
14+
1215
/**
1316
* @author: Jindong.Tian
1417
* @date: 2021-01-09
1518
**/
1619
public abstract class AbstractMethodGenerator implements Generator {
1720

1821
protected static final int FIRST_INDEX = 0;
19-
protected static final String EMPTY_BODY = "";
20-
protected static final String BLANK_SEPARATOR = " ";
21-
protected static final String LINE_SEPARATOR = "\n";
22-
protected static final String COMMA_SEPARATOR = ",";
23-
24-
protected static final String VOID_KEYWORD = "void";
25-
26-
/**
27-
* 方法修饰符
28-
*/
29-
protected List<JavaModify> methodModifies;
30-
/**
31-
* 方法返回类型名称
32-
*/
33-
protected String returnClassName;
34-
/**
35-
* 方法名称
36-
*/
37-
protected String methodName;
38-
/**
39-
* 方法参数
40-
*/
41-
protected List<PsiParameter> parameters;
42-
/**
43-
* 方法参数
44-
*/
45-
protected List<PsiClass> parameterClass;
4622

23+
protected Project project;
24+
protected PsiMethod psiMethod;
4725

4826
public String generate() {
4927
StringBuilder result = generateMethodFirstLine()
@@ -52,28 +30,20 @@ public String generate() {
5230
return result.toString();
5331
}
5432

33+
abstract String generateMethodBody();
34+
5535
protected StringBuilder generateMethodFirstLine() {
5636
StringBuilder builder = new StringBuilder();
57-
methodModifies.forEach(e -> builder.append(e.getName()).append(BLANK_SEPARATOR));
58-
builder.append(returnClassName + BLANK_SEPARATOR)
59-
.append(methodName)
60-
.append("(")
61-
.append(StringUtils.join(parameters, PsiParameter::getText, COMMA_SEPARATOR))
62-
.append("){");
37+
PsiUtils.getMethodModifies(psiMethod.getModifierList()).forEach(e -> builder.append(e.getName()).append(BLANK_SEPARATOR));
38+
builder.append(getMethodReturnClassName(psiMethod) + BLANK_SEPARATOR)
39+
.append(getMethodName(psiMethod))
40+
.append(PARENTHESIS_OPEN)
41+
.append(StringUtils.join(getParameters(), PsiParameter::getText, COMMA_SEPARATOR))
42+
.append(PARENTHESIS_CLOSE + BRACE_OPEN);
6343
return builder;
6444
}
6545

66-
protected boolean hasModifierProperty(String modify) {
67-
if (CollectionUtils.isEmpty(methodModifies)) {
68-
return false;
69-
}
70-
for (JavaModify methodModify : methodModifies) {
71-
if (methodModify.getName().equals(modify)) {
72-
return true;
73-
}
74-
}
75-
return false;
46+
protected List<PsiParameter> getParameters() {
47+
return getPsiParameters(psiMethod);
7648
}
77-
78-
abstract String generateMethodBody();
7949
}

0 commit comments

Comments
 (0)