-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
wangchao
committed
Aug 16, 2021
1 parent
8c3f0d4
commit 0edf57e
Showing
9 changed files
with
298 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/main/java/com/star/easygenerate/action/AddAboveFieldAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
src/main/java/com/star/easygenerate/config/EasyGenerateConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/star/easygenerate/config/EasyGenerateConfigComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/com/star/easygenerate/view/AboveFieldView.form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
74
src/main/java/com/star/easygenerate/view/AboveFieldView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.