diff --git a/build.gradle b/build.gradle
index 3ae9e72..0addfd9 100644
--- a/build.gradle
+++ b/build.gradle
@@ -5,7 +5,7 @@ plugins {
}
group 'com.star.easygenerate'
-version '1.10'
+version '1.2.1'
repositories {
mavenCentral()
@@ -30,6 +30,10 @@ intellij {
patchPluginXml {
changeNotes """
+
v1.2.1 fix bugs
+
v1.10 create unit test
- support generate constant for field
diff --git a/src/main/java/com/star/easygenerate/action/AddAboveFieldAction.java b/src/main/java/com/star/easygenerate/action/AddAboveFieldAction.java
new file mode 100644
index 0000000..a72be0a
--- /dev/null
+++ b/src/main/java/com/star/easygenerate/action/AddAboveFieldAction.java
@@ -0,0 +1,89 @@
+package com.star.easygenerate.action;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import com.intellij.openapi.actionSystem.AnAction;
+import com.intellij.openapi.actionSystem.AnActionEvent;
+import com.intellij.openapi.actionSystem.CommonDataKeys;
+import com.intellij.openapi.actionSystem.LangDataKeys;
+import com.intellij.openapi.command.WriteCommandAction;
+import com.intellij.openapi.project.Project;
+import com.intellij.psi.PsiClass;
+import com.intellij.psi.PsiElement;
+import com.intellij.psi.PsiElementFactory;
+import com.intellij.psi.PsiField;
+import com.intellij.psi.PsiFile;
+import com.intellij.psi.PsiJavaFile;
+import com.intellij.psi.codeStyle.CodeStyleManager;
+import com.star.easygenerate.view.AboveFieldView;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * 生成常量
+ *
+ * @author wangchao
+ * @date 2020/12/13
+ */
+public class AddAboveFieldAction extends AnAction {
+
+ @Override
+ public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
+ // 获取project
+ Project project = anActionEvent.getData(CommonDataKeys.PROJECT);
+ if (project == null) {
+ return;
+ }
+ // 获取factory
+ PsiElementFactory factory = PsiElementFactory.SERVICE.getInstance(project);
+
+ // 判断java文件
+ PsiFile psiFile = anActionEvent.getData(CommonDataKeys.PSI_FILE);
+ if (!(psiFile instanceof PsiJavaFile)) {
+ return;
+ }
+ PsiJavaFile psiJavaFile = (PsiJavaFile)psiFile;
+
+ // 在类上
+ PsiElement psiElement = anActionEvent.getData(LangDataKeys.PSI_ELEMENT);
+ if (!(psiElement instanceof PsiClass)) {
+ return;
+ }
+ PsiClass psiClass = (PsiClass)psiElement;
+ AboveFieldView view = new AboveFieldView();
+ if (view.showAndGet()) {
+ String codeText = view.getCodeText();
+ List fieldElements = getFromChildren(psiClass, PsiField.class);
+ for (PsiField fieldElement : fieldElements) {
+ PsiElement something = factory.createIdentifier(codeText);
+ write(project, fieldElement, something);
+ }
+ }
+ }
+
+ /**
+ * 写入代码
+ */
+ private void write(Project project, PsiField fieldElement, PsiElement something) {
+ // 写入代码
+ WriteCommandAction.writeCommandAction(project).run(
+ () -> {
+ fieldElement.addBefore(fieldElement.getFirstChild(), something);
+
+ // 格式化文档注释
+ CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
+ int startOffset = fieldElement.getFirstChild().getTextOffset();
+ int endOffset = fieldElement.getLastChild().getTextOffset() + something.getText().length();
+ codeStyleManager.reformatText(fieldElement.getContainingFile(), startOffset, endOffset + 1);
+ });
+ }
+
+ @SuppressWarnings("unchecked")
+ private List getFromChildren(PsiElement psiElement, Class targetElementClass) {
+ return Arrays.stream(psiElement.getChildren())
+ .filter(targetElementClass::isInstance)
+ .map(psiElement1 -> (T)psiElement1)
+ .collect(Collectors.toList());
+ }
+}
diff --git a/src/main/java/com/star/easygenerate/config/EasyGenerateConfig.java b/src/main/java/com/star/easygenerate/config/EasyGenerateConfig.java
new file mode 100644
index 0000000..44c97b2
--- /dev/null
+++ b/src/main/java/com/star/easygenerate/config/EasyGenerateConfig.java
@@ -0,0 +1,19 @@
+package com.star.easygenerate.config;
+
+/**
+ * @author wangchao
+ * @date 2021/07/11
+ */
+public class EasyGenerateConfig {
+
+ /** 属性上面添加字段 */
+ private String addAboveFields;
+
+ public String getAddAboveFields() {
+ return addAboveFields;
+ }
+
+ public void setAddAboveFields(String addAboveFields) {
+ this.addAboveFields = addAboveFields;
+ }
+}
diff --git a/src/main/java/com/star/easygenerate/config/EasyGenerateConfigComponent.java b/src/main/java/com/star/easygenerate/config/EasyGenerateConfigComponent.java
new file mode 100644
index 0000000..be8ef0f
--- /dev/null
+++ b/src/main/java/com/star/easygenerate/config/EasyGenerateConfigComponent.java
@@ -0,0 +1,34 @@
+package com.star.easygenerate.config;
+
+import java.util.Objects;
+
+import com.intellij.openapi.components.PersistentStateComponent;
+import com.intellij.openapi.components.State;
+import com.intellij.openapi.components.Storage;
+import com.intellij.util.xmlb.XmlSerializerUtil;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * @author wangchao
+ * @date 2021/07/11
+ */
+@State(name = "easyGenerate", storages = {@Storage("easyGenerate.xml")})
+public class EasyGenerateConfigComponent implements PersistentStateComponent {
+
+ private EasyGenerateConfig configuration;
+
+ @Nullable
+ @Override
+ public EasyGenerateConfig getState() {
+ if (configuration == null) {
+ configuration = new EasyGenerateConfig();
+ }
+ return configuration;
+ }
+
+ @Override
+ public void loadState(@NotNull EasyGenerateConfig state) {
+ XmlSerializerUtil.copyBean(state, this);
+ }
+}
diff --git a/src/main/java/com/star/easygenerate/dialog/CreateUnitTestDialog.java b/src/main/java/com/star/easygenerate/dialog/CreateUnitTestDialog.java
index 36e4731..04237ba 100644
--- a/src/main/java/com/star/easygenerate/dialog/CreateUnitTestDialog.java
+++ b/src/main/java/com/star/easygenerate/dialog/CreateUnitTestDialog.java
@@ -20,7 +20,6 @@
import javax.swing.*;
import com.intellij.CommonBundle;
-import com.intellij.codeInsight.CodeInsightBundle;
import com.intellij.ide.util.PropertiesComponent;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
@@ -95,10 +94,9 @@ public class CreateUnitTestDialog extends DialogWrapper {
private EditorTextField myTargetClassNameField;
private ReferenceEditorComboWithBrowseButton myTargetPackageField;
- private final JCheckBox myGenerateBeforeBox = new JCheckBox(CodeInsightBundle.message("intention.create.test.dialog.setUp"));
- private final JCheckBox myGenerateAfterBox = new JCheckBox(CodeInsightBundle.message("intention.create.test.dialog.tearDown"));
- private final JCheckBox myShowInheritedMethodsBox = new JCheckBox(
- CodeInsightBundle.message("intention.create.test.dialog.show.inherited"));
+ private final JCheckBox myGenerateBeforeBox = new JCheckBox("setUp/@Before");
+ private final JCheckBox myGenerateAfterBox = new JCheckBox("tearDown/@After");
+ private final JCheckBox myShowInheritedMethodsBox = new JCheckBox("Show inherited methods");
private final MemberSelectionTable myMethodsTable = new MemberSelectionTable(Collections.emptyList(), null);
public CreateUnitTestDialog(@NotNull Project project,
@@ -213,7 +211,7 @@ protected JComponent createCenterPanel() {
constr.gridx = 0;
constr.weightx = 0;
constr.gridwidth = 1;
- panel.add(new JLabel(CodeInsightBundle.message("intention.create.test.dialog.class.name")), constr);
+ panel.add(new JLabel("Class name:"), constr);
myTargetClassNameField = new EditorTextField(suggestTestClassName(myTargetClass));
myTargetClassNameField.getDocument().addDocumentListener(new DocumentListener() {
@@ -231,14 +229,14 @@ public void documentChanged(@NotNull DocumentEvent e) {
constr.gridy = gridy++;
constr.gridx = 0;
constr.weightx = 0;
- panel.add(new JLabel(CodeInsightBundle.message("dialog.create.class.destination.package.label")), constr);
+ panel.add(new JLabel("Destination package:"), constr);
constr.gridx = 1;
constr.weightx = 1;
String targetPackageName = myTargetPackage != null ? myTargetPackage.getQualifiedName() : "";
myTargetPackageField = new PackageNameReferenceEditorCombo(targetPackageName, myProject, RECENTS_KEY,
- CodeInsightBundle.message("dialog.create.class.package.chooser.title"));
+ "Choose Destination Package");
new AnAction() {
@Override
@@ -255,7 +253,7 @@ public void actionPerformed(@NotNull AnActionEvent e) {
constr.gridy = gridy++;
constr.gridx = 0;
constr.weightx = 0;
- panel.add(new JLabel(CodeInsightBundle.message("intention.create.test.dialog.generate")), constr);
+ panel.add(new JLabel("Generate:"), constr);
constr.gridx = 1;
constr.weightx = 1;
@@ -269,7 +267,7 @@ public void actionPerformed(@NotNull AnActionEvent e) {
constr.gridy = gridy++;
constr.gridx = 0;
constr.weightx = 0;
- final JLabel membersLabel = new JLabel(CodeInsightBundle.message("intention.create.test.dialog.select.methods"));
+ final JLabel membersLabel = new JLabel("Generate test methods for:");
membersLabel.setLabelFor(myMethodsTable);
panel.add(membersLabel, constr);
@@ -405,7 +403,7 @@ private PsiDirectory selectTargetDirectory() throws IncorrectOperationException
}
return WriteCommandAction.writeCommandAction(myProject)
- .withName(CodeInsightBundle.message("create.directory.command"))
+ .withName("Create directory")
.compute(() -> RefactoringUtil.createPackageDirectoryInSourceRoot(targetPackage, selectedRoot));
}
diff --git a/src/main/java/com/star/easygenerate/view/AboveFieldView.form b/src/main/java/com/star/easygenerate/view/AboveFieldView.form
new file mode 100644
index 0000000..19057ce
--- /dev/null
+++ b/src/main/java/com/star/easygenerate/view/AboveFieldView.form
@@ -0,0 +1,61 @@
+
+
diff --git a/src/main/java/com/star/easygenerate/view/AboveFieldView.java b/src/main/java/com/star/easygenerate/view/AboveFieldView.java
new file mode 100644
index 0000000..8988385
--- /dev/null
+++ b/src/main/java/com/star/easygenerate/view/AboveFieldView.java
@@ -0,0 +1,74 @@
+package com.star.easygenerate.view;
+
+import java.util.AbstractMap.SimpleEntry;
+import java.util.List;
+import java.util.Map.Entry;
+
+import javax.swing.*;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
+import com.intellij.openapi.application.ApplicationManager;
+import com.intellij.openapi.components.ServiceManager;
+import com.intellij.openapi.ui.DialogWrapper;
+import com.intellij.ui.CollectionListModel;
+import com.intellij.ui.ListCellRendererWrapper;
+import com.intellij.ui.components.JBList;
+import com.star.easygenerate.config.EasyGenerateConfig;
+import com.star.easygenerate.config.EasyGenerateConfigComponent;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * 属性添加视图
+ *
+ * @author wangchao
+ * @date 2021/07/11
+ */
+public class AboveFieldView extends DialogWrapper {
+ private JPanel contentPane;
+ private JTextArea codeTextArea;
+ private JList> innerVariablesList;
+
+ /** 内部变量映射 */
+ private static final List> INNER_VARIABLE_LIST = ImmutableList
+ .>builder()
+ .add(new SimpleEntry<>("fieldName", "field name"))
+ .add(new SimpleEntry<>("fieldType", "field type"))
+ .build();
+
+ public AboveFieldView() {
+ super(false);
+ init();
+ setTitle("Add Something Above Fields");
+ }
+
+ @Nullable
+ @Override
+ protected JComponent createCenterPanel() {
+ return contentPane;
+ }
+
+ public String getCodeText() {
+ return codeTextArea.getText();
+ }
+
+ private void createUIComponents() {
+ // 内置变量初始化
+ innerVariablesList = new JBList<>(new CollectionListModel<>(Lists.newArrayList()));
+ innerVariablesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
+ innerVariablesList.setCellRenderer(new ListCellRendererWrapper>() {
+ @Override
+ public void customize(JList list, Entry value, int index, boolean selected,
+ boolean hasFocus) {
+ setText(value.getKey() + " : " + value.getValue());
+ }
+ });
+ innerVariablesList.setSelectedIndex(0);
+ innerVariablesList.setModel(new CollectionListModel<>(INNER_VARIABLE_LIST));
+
+ // 代码区域初始化
+ codeTextArea = new JTextArea();
+ EasyGenerateConfig config = ServiceManager.getService(EasyGenerateConfigComponent.class).getState();
+ codeTextArea.setText(config.getAddAboveFields());
+ }
+}
diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml
index 8e5b196..d83b5fe 100644
--- a/src/main/resources/META-INF/plugin.xml
+++ b/src/main/resources/META-INF/plugin.xml
@@ -10,6 +10,10 @@
easy_generate source code and document
+ v1.2.1 fix bugs
+
v1.10 create unit test template
- support generate unit test template
diff --git a/src/main/resources/template/unit-test.vm b/src/main/resources/template/unit-test.vm
index f33037d..acfb28a 100644
--- a/src/main/resources/template/unit-test.vm
+++ b/src/main/resources/template/unit-test.vm
@@ -2,8 +2,10 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
+import org.powermock.modules.junit4.PowerMockRunner;
#foreach($field in $fieldList)
import ${field.packageName};
@@ -15,6 +17,7 @@ import ${field.packageName};
* @author ${user}
* @date ${date}
*/
+@RunWith(PowerMockRunner.class)
public class ${className}UnitTest {
@InjectMocks