Skip to content

Commit

Permalink
v1.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
wangchao committed Aug 16, 2021
1 parent 8c3f0d4 commit 0edf57e
Show file tree
Hide file tree
Showing 9 changed files with 298 additions and 12 deletions.
6 changes: 5 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group 'com.star.easygenerate'
version '1.10'
version '1.2.1'

repositories {
mavenCentral()
Expand All @@ -30,6 +30,10 @@ intellij {

patchPluginXml {
changeNotes """
<h3>v1.2.1 fix bugs</h3>
<ul>
<li>fix some bugs</li>
</ul>
<h3>v1.10 create unit test</h3>
<ul>
<li>support generate constant for field</li>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<PsiField> 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 <T extends PsiElement> List<T> getFromChildren(PsiElement psiElement, Class<T> targetElementClass) {
return Arrays.stream(psiElement.getChildren())
.filter(targetElementClass::isInstance)
.map(psiElement1 -> (T)psiElement1)
.collect(Collectors.toList());
}
}
19 changes: 19 additions & 0 deletions src/main/java/com/star/easygenerate/config/EasyGenerateConfig.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<EasyGenerateConfig> {

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

Expand Down Expand Up @@ -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));
}

Expand Down
61 changes: 61 additions & 0 deletions src/main/java/com/star/easygenerate/view/AboveFieldView.form
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.star.easygenerate.view.AboveFieldView">
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<xy x="48" y="54" width="600" height="800"/>
</constraints>
<properties>
<minimumSize width="400" height="600"/>
</properties>
<border type="none"/>
<children>
<grid id="e3588" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="1b964" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none" title="add the following code above fields"/>
<children>
<component id="5c3ca" class="javax.swing.JTextArea" binding="codeTextArea" custom-create="true">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="6" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="150" height="50"/>
</grid>
</constraints>
<properties/>
</component>
</children>
</grid>
<grid id="52c26" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none" title="inner variables"/>
<children>
<component id="ced50" class="javax.swing.JList" binding="innerVariablesList" custom-create="true">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="2" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="150" height="50"/>
</grid>
</constraints>
<properties/>
</component>
</children>
</grid>
</children>
</grid>
</children>
</grid>
</form>
74 changes: 74 additions & 0 deletions src/main/java/com/star/easygenerate/view/AboveFieldView.java
Original file line number Diff line number Diff line change
@@ -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<Entry<String, String>> innerVariablesList;

/** 内部变量映射 */
private static final List<Entry<String, String>> INNER_VARIABLE_LIST = ImmutableList
.<Entry<String, String>>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<Entry<String, String>>() {
@Override
public void customize(JList list, Entry<String, String> 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());
}
}
4 changes: 4 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
<a href="https://github.com/starcwang/easy_generate">easy_generate source code and document</a><br>
<img src="https://raw.githubusercontent.com/starcwang/easy_generate/main/doc/1.gif" /><br>
<h3>v1.2.1 fix bugs</h3>
<ul>
<li>fix some bugs</li>
</ul>
<h3>v1.10 create unit test template</h3>
<ul>
<li>support generate unit test template</li>
Expand Down
Loading

0 comments on commit 0edf57e

Please sign in to comment.