feat: 支持多文件选择生成.

This commit is contained in:
lkqm 2021-04-03 12:55:19 +08:00
parent 44496127bf
commit 7a69cb02d9
4 changed files with 109 additions and 28 deletions

View File

@ -5,25 +5,27 @@ import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.javadoc.PsiDocToken;
import com.sjhy.plugin.entity.ColumnInfo;
import com.sjhy.plugin.entity.TableInfo;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiJavaFile;
import com.intellij.psi.PsiManager;
import com.intellij.psi.PsiModifier;
import com.sjhy.plugin.tool.CacheDataUtils;
import com.sjhy.plugin.ui.SelectSavePath;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* 从Java类生成代码菜单
*
* @author Mario Luo
*/
public class EasyCodeEntityAction extends AnAction {
private CacheDataUtils cacheDataUtils = CacheDataUtils.getInstance();
private final CacheDataUtils cacheDataUtils = CacheDataUtils.getInstance();
@Override
public void actionPerformed(@NotNull AnActionEvent event) {
@ -31,23 +33,47 @@ public class EasyCodeEntityAction extends AnAction {
if (project == null) {
return;
}
PsiFile psiFile = event.getData(CommonDataKeys.PSI_FILE);
if (!(psiFile instanceof PsiJavaFile)) {
// 过滤选择Java文件
VirtualFile[] psiFiles = event.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY);
PsiManager psiManager = PsiManager.getInstance(project);
List<PsiJavaFile> psiJavaFiles = Arrays.stream(psiFiles)
.map(psiManager::findFile)
.filter(f -> f instanceof PsiJavaFile)
.map(f -> (PsiJavaFile) f)
.collect(Collectors.toList());
if (psiJavaFiles.size() == 0) {
return;
}
PsiJavaFile psiJavaFile = (PsiJavaFile) psiFile;
PsiClass psiClass = Arrays.stream(psiJavaFile.getClasses())
.filter(o -> o.getModifierList() != null && o.getModifierList().hasModifierProperty("public"))
.findFirst()
.orElse(null);
if (psiClass == null) {
// 获取选中的类
List<PsiClass> psiClassList = resolvePsiClassByFile(psiJavaFiles);
if (psiClassList.size() == 0) {
return;
}
cacheDataUtils.setSelectPsiClass(psiClass);
cacheDataUtils.setPsiClassList(Lists.newArrayList(psiClass));
// 缓存选中值
cacheDataUtils.setSelectPsiClass(psiClassList.get(0));
cacheDataUtils.setPsiClassList(psiClassList);
new SelectSavePath(project, true).open();
}
/**
* 解析类
*/
private List<PsiClass> resolvePsiClassByFile(List<PsiJavaFile> psiJavaFiles) {
List<PsiClass> psiClassList = Lists.newArrayListWithCapacity(psiJavaFiles.size());
for (PsiJavaFile psiJavaFile : psiJavaFiles) {
PsiClass psiClass = Arrays.stream(psiJavaFile.getClasses())
.filter(o -> o.getModifierList() != null && o.getModifierList().hasModifierProperty(PsiModifier.PUBLIC))
.findFirst()
.orElse(null);
if (psiClass != null) {
psiClassList.add(psiClass);
}
}
return psiClassList;
}
}

View File

@ -24,14 +24,8 @@ import com.sjhy.plugin.entity.SaveFile;
import com.sjhy.plugin.entity.TableInfo;
import com.sjhy.plugin.entity.TypeMapper;
import com.sjhy.plugin.service.TableInfoService;
import com.sjhy.plugin.tool.CloneUtils;
import com.sjhy.plugin.tool.CollectionUtil;
import com.sjhy.plugin.tool.CurrGroupUtils;
import com.sjhy.plugin.tool.FileUtils;
import com.sjhy.plugin.tool.MessageDialogUtils;
import com.sjhy.plugin.tool.NameUtils;
import com.sjhy.plugin.tool.ProjectUtils;
import com.sjhy.plugin.tool.StringUtils;
import com.sjhy.plugin.tool.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
@ -44,6 +38,7 @@ import java.util.Objects;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.jetbrains.annotations.Nullable;
/**
@ -396,13 +391,16 @@ public class TableInfoServiceImpl implements TableInfoService {
tableInfo.setPkColumn(new ArrayList<>());
tableInfo.setOtherColumn(new ArrayList<>());
for (PsiField field : psiClass.getAllFields()) {
if (PsiClassGenerateUtils.isSkipField(field)) {
continue;
}
ColumnInfo columnInfo = new ColumnInfo();
columnInfo.setName(field.getName());
columnInfo.setShortType(field.getType().getPresentableText());
columnInfo.setType(field.getType().getCanonicalText());
columnInfo.setComment(parsePsiClassComment(field.getDocComment()));
tableInfo.getFullColumn().add(columnInfo);
if ("id".equals(field.getName())) {
if (PsiClassGenerateUtils.isPkField(field)) {
tableInfo.getPkColumn().add(columnInfo);
} else {
tableInfo.getOtherColumn().add(columnInfo);
@ -419,6 +417,7 @@ public class TableInfoServiceImpl implements TableInfoService {
return fullName.substring(0, genericIdx);
}
@Override
public TableInfo getTableInfoAndConfigByPsiClass(PsiClass selectPsiClass) {
TableInfo tableInfo = this.getTableInfoByPsiClass(selectPsiClass);

View File

@ -0,0 +1,54 @@
package com.sjhy.plugin.tool;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiModifier;
import com.intellij.psi.PsiModifierList;
/**
* 从实体类生成代码业务工具类
*
* @author Mario Luo
*/
public final class PsiClassGenerateUtils {
private PsiClassGenerateUtils() {
}
/**
* 是否是主键字段
*/
public static boolean isPkField(PsiField field) {
if("id".equals(field.getName())) {
return true;
}
PsiAnnotation idAnnotation = field.getAnnotation("org.springframework.data.annotation.Id");
if (idAnnotation != null) {
return true;
}
idAnnotation = field.getAnnotation("javax.persistence.Id");
if (idAnnotation != null) {
return true;
}
return false;
}
/**
* 是否需要跳过该字段
*/
public static boolean isSkipField(PsiField field) {
PsiModifierList modifierList = field.getModifierList();
if(modifierList != null && modifierList.hasExplicitModifier(PsiModifier.STATIC)) {
return true;
}
PsiAnnotation annotation = field.getAnnotation("org.springframework.data.annotation.Transient");
if (annotation != null) {
return true;
}
annotation = field.getAnnotation("javax.persistence.Transient");
if (annotation != null) {
return true;
}
return false;
}
}

View File

@ -241,9 +241,11 @@
<!--生成代码菜单-->
<add-to-group group-id="DatabaseViewPopupMenu" anchor="first"/>
</group>
<action id="con.sjhy.easy.code.action.EasyCodeFromEntity"
<!-- 实体类生成代码 -->
<action id="com.sjhy.plugin.actions.EasyCodeEntityAction"
class="com.sjhy.plugin.actions.EasyCodeEntityAction" text="EasyCode...">
<add-to-group group-id="GenerateGroup" anchor="last"/>
<add-to-group group-id="ProjectViewPopupMenu" anchor="last"/>
</action>
</actions>