From 0192ae2f9179cde36c9d0fd94708fb42a387f2e8 Mon Sep 17 00:00:00 2001 From: makejava <1353036300@qq.com> Date: Tue, 17 Aug 2021 10:58:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BD=BB=E5=BA=95=E5=BA=9F=E9=99=A4=E6=97=A7?= =?UTF-8?q?=E7=89=88=E8=A1=A8=E9=85=8D=E7=BD=AE=E6=9C=8D=E5=8A=A1=EF=BC=8C?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=9C=AA=E7=9F=A5=E7=B1=BB=E5=9E=8B=E7=9A=84?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/sjhy/plugin/actions/MainAction.java | 116 +++- .../com/sjhy/plugin/dto/ColumnInfoDTO.java | 22 +- .../com/sjhy/plugin/dto/GenerateOptions.java | 36 ++ .../java/com/sjhy/plugin/entity/Callback.java | 6 +- .../java/com/sjhy/plugin/entity/SaveFile.java | 95 +-- .../plugin/service/CodeGenerateService.java | 13 +- .../sjhy/plugin/service/TableInfoService.java | 111 ---- .../service/impl/CodeGenerateServiceImpl.java | 81 ++- .../service/impl/TableInfoServiceImpl.java | 541 ------------------ .../plugin/tool/ExtraCodeGenerateUtils.java | 11 +- .../com/sjhy/plugin/tool/VelocityUtils.java | 8 +- .../com/sjhy/plugin/ui/SelectSavePath.form | 34 +- .../com/sjhy/plugin/ui/SelectSavePath.java | 32 +- .../ui/component/RealtimeDebugComponent.java | 4 +- src/main/resources/META-INF/plugin.xml | 1 - 15 files changed, 321 insertions(+), 790 deletions(-) create mode 100644 src/main/java/com/sjhy/plugin/dto/GenerateOptions.java delete mode 100644 src/main/java/com/sjhy/plugin/service/TableInfoService.java delete mode 100644 src/main/java/com/sjhy/plugin/service/impl/TableInfoServiceImpl.java diff --git a/src/main/java/com/sjhy/plugin/actions/MainAction.java b/src/main/java/com/sjhy/plugin/actions/MainAction.java index ea6ad3f..8c1c367 100644 --- a/src/main/java/com/sjhy/plugin/actions/MainAction.java +++ b/src/main/java/com/sjhy/plugin/actions/MainAction.java @@ -1,14 +1,34 @@ package com.sjhy.plugin.actions; +import com.intellij.database.model.DasColumn; +import com.intellij.database.psi.DbTable; +import com.intellij.database.util.DasUtil; import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.project.Project; -import com.sjhy.plugin.service.TableInfoService; +import com.intellij.openapi.ui.ComboBox; +import com.intellij.openapi.ui.DialogWrapper; +import com.intellij.openapi.ui.Messages; +import com.intellij.util.containers.JBIterable; +import com.intellij.util.ui.JBUI; +import com.sjhy.plugin.dict.GlobalDict; +import com.sjhy.plugin.entity.TypeMapper; +import com.sjhy.plugin.enums.MatchType; import com.sjhy.plugin.tool.CacheDataUtils; +import com.sjhy.plugin.tool.CurrGroupUtils; +import com.sjhy.plugin.tool.StringUtils; import com.sjhy.plugin.ui.SelectSavePath; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import javax.swing.*; +import java.awt.*; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; + /** * 代码生成菜单 * @@ -39,11 +59,103 @@ public class MainAction extends AnAction { } // 校验类型映射 - if (!TableInfoService.getInstance(project).typeValidator(CacheDataUtils.getInstance().getSelectDbTable())) { + if (!typeValidator(project, CacheDataUtils.getInstance().getSelectDbTable())) { // 没通过不打开窗口 return; } //开始处理 new SelectSavePath(event.getProject()).show(); } + + + /** + * 类型校验,如果存在未知类型则引导用于去条件类型 + * + * @param dbTable 原始表对象 + * @return 是否验证通过 + */ + private boolean typeValidator(Project project, DbTable dbTable) { + // 处理所有列 + JBIterable columns = DasUtil.getColumns(dbTable); + List typeMapperList = CurrGroupUtils.getCurrTypeMapperGroup().getElementList(); + + // 简单的记录报错弹窗次数,避免重复报错 + Set errorCount = new HashSet<>(); + + FLAG: + for (DasColumn column : columns) { + String typeName = column.getDataType().getSpecification(); + for (TypeMapper typeMapper : typeMapperList) { + try { + if (typeMapper.getMatchType() == MatchType.ORDINARY) { + if (typeName.equalsIgnoreCase(typeMapper.getColumnType())) { + continue FLAG; + } + } else { + // 不区分大小写的正则匹配模式 + if (Pattern.compile(typeMapper.getColumnType(), Pattern.CASE_INSENSITIVE).matcher(typeName).matches()) { + continue FLAG; + } + } + } catch (PatternSyntaxException e) { + if (!errorCount.contains(typeMapper.getColumnType())) { + Messages.showWarningDialog( + "类型映射《" + typeMapper.getColumnType() + "》存在语法错误,请及时修正。报错信息:" + e.getMessage(), + GlobalDict.TITLE_INFO); + errorCount.add(typeMapper.getColumnType()); + } + } + } + // 没找到类型,提示用户选择输入类型 + new Dialog(project, typeName).showAndGet(); + } + return true; + } + + public static class Dialog extends DialogWrapper { + + private String typeName; + + private JPanel mainPanel; + + private ComboBox comboBox; + + protected Dialog(@Nullable Project project, String typeName) { + super(project); + this.typeName = typeName; + this.initPanel(); + } + + private void initPanel() { + setTitle(GlobalDict.TITLE_INFO); + String msg = String.format("数据库类型%s,没有找到映射关系,请输入想转换的类型?", typeName); + JLabel label = new JLabel(msg); + this.mainPanel = new JPanel(new BorderLayout()); + this.mainPanel.setBorder(JBUI.Borders.empty(5, 10, 7, 10)); + mainPanel.add(label, BorderLayout.NORTH); + this.comboBox = new ComboBox<>(GlobalDict.DEFAULT_JAVA_TYPE_LIST); + this.comboBox.setEditable(true); + this.mainPanel.add(this.comboBox, BorderLayout.CENTER); + init(); + } + + @Override + protected @Nullable JComponent createCenterPanel() { + return this.mainPanel; + } + + @Override + protected void doOKAction() { + super.doOKAction(); + String selectedItem = (String) this.comboBox.getSelectedItem(); + if (StringUtils.isEmpty(selectedItem)) { + return; + } + TypeMapper typeMapper = new TypeMapper(); + typeMapper.setMatchType(MatchType.ORDINARY); + typeMapper.setJavaType(selectedItem); + typeMapper.setColumnType(typeName); + CurrGroupUtils.getCurrTypeMapperGroup().getElementList().add(typeMapper); + } + } } diff --git a/src/main/java/com/sjhy/plugin/dto/ColumnInfoDTO.java b/src/main/java/com/sjhy/plugin/dto/ColumnInfoDTO.java index 372d659..1dd5434 100644 --- a/src/main/java/com/sjhy/plugin/dto/ColumnInfoDTO.java +++ b/src/main/java/com/sjhy/plugin/dto/ColumnInfoDTO.java @@ -3,6 +3,7 @@ package com.sjhy.plugin.dto; import com.intellij.database.model.DasColumn; import com.intellij.psi.PsiField; import com.sjhy.plugin.entity.TypeMapper; +import com.sjhy.plugin.enums.MatchType; import com.sjhy.plugin.tool.CurrGroupUtils; import com.sjhy.plugin.tool.DocCommentUtils; import com.sjhy.plugin.tool.NameUtils; @@ -42,18 +43,15 @@ public class ColumnInfoDTO { private String getJavaType(String dbType) { for (TypeMapper typeMapper : CurrGroupUtils.getCurrTypeMapperGroup().getElementList()) { - switch (typeMapper.getMatchType()) { - case REGEX: - if (Pattern.compile(typeMapper.getColumnType()).matcher(dbType).matches()) { - return typeMapper.getJavaType(); - } - break; - case ORDINARY: - default: - if (dbType.equalsIgnoreCase(typeMapper.getColumnType())) { - return typeMapper.getJavaType(); - } - break; + if (typeMapper.getMatchType() == MatchType.ORDINARY) { + if (dbType.equalsIgnoreCase(typeMapper.getColumnType())) { + return typeMapper.getJavaType(); + } + } else { + // 不区分大小写的正则匹配模式 + if (Pattern.compile(typeMapper.getColumnType(), Pattern.CASE_INSENSITIVE).matcher(dbType).matches()) { + return typeMapper.getJavaType(); + } } } return "java.lang.Object"; diff --git a/src/main/java/com/sjhy/plugin/dto/GenerateOptions.java b/src/main/java/com/sjhy/plugin/dto/GenerateOptions.java new file mode 100644 index 0000000..1f1d5b4 --- /dev/null +++ b/src/main/java/com/sjhy/plugin/dto/GenerateOptions.java @@ -0,0 +1,36 @@ +package com.sjhy.plugin.dto; + +import lombok.Builder; +import lombok.Data; + +/** + * 生成选项 + * + * @author makejava + * @version 1.0.0 + * @date 2021/08/17 09:08 + */ +@Data +@Builder +public class GenerateOptions { + /** + * 实体类模式 + */ + private Boolean entityModel; + /** + * 统一配置 + */ + private Boolean unifiedConfig; + /** + * 重新格式化代码 + */ + private Boolean reFormat; + /** + * 提示选是 + */ + private Boolean titleSure; + /** + * 提示选否 + */ + private Boolean titleRefuse; +} diff --git a/src/main/java/com/sjhy/plugin/entity/Callback.java b/src/main/java/com/sjhy/plugin/entity/Callback.java index a57b33b..25cbaea 100644 --- a/src/main/java/com/sjhy/plugin/entity/Callback.java +++ b/src/main/java/com/sjhy/plugin/entity/Callback.java @@ -22,5 +22,9 @@ public class Callback { /** * 是否重新格式化代码 */ - private boolean reformat = true; + private Boolean reformat; + /** + * 是否写入文件,部分模块不需要写入文件。例如debug.json模板 + */ + private Boolean writeFile; } diff --git a/src/main/java/com/sjhy/plugin/entity/SaveFile.java b/src/main/java/com/sjhy/plugin/entity/SaveFile.java index d1c3e66..11b3b32 100644 --- a/src/main/java/com/sjhy/plugin/entity/SaveFile.java +++ b/src/main/java/com/sjhy/plugin/entity/SaveFile.java @@ -11,6 +11,7 @@ import com.intellij.openapi.vfs.VfsUtil; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiDocumentManager; import com.intellij.testFramework.LightVirtualFile; +import com.sjhy.plugin.dto.GenerateOptions; import com.sjhy.plugin.tool.CompareFileUtils; import com.sjhy.plugin.tool.FileUtils; import com.sjhy.plugin.tool.MessageDialogUtils; @@ -36,49 +37,36 @@ public class SaveFile { * 所属项目 */ private Project project; - /** - * 文件保存目录 - */ - private String path; - /** - * 文件名 - */ - private String fileName; /** * 文件内容 */ private String content; - /** - * 是否需要重新格式化代码 - */ - private boolean reformat; - /** - * 是否显示操作提示 - */ - private boolean operateTitle; /** * 文件工具类 */ private FileUtils fileUtils = FileUtils.getInstance(); + /** + * 回调对象 + */ + private Callback callback; + /** + * 生成配置 + */ + private GenerateOptions generateOptions; /** - * 构建对象 + * 保存文件 * - * @param project 项目对象 - * @param path 绝对路径 - * @param fileName 文件名 - * @param content 文件内容 - * @param reformat 是否重新格式化代码 - * @param operateTitle 操作提示 + * @param project 项目 + * @param content 内容 + * @param callback 回调 + * @param generateOptions 生成选项 */ - public SaveFile(@NonNull Project project, @NonNull String path, @NonNull String fileName, String content, boolean reformat, boolean operateTitle) { - this.path = path; + public SaveFile(@NonNull Project project, @NonNull String content, @NonNull Callback callback, @NonNull GenerateOptions generateOptions) { this.project = project; - this.fileName = fileName; - LOG.assertTrue(content != null); + this.callback = callback; this.content = content.replace("\r", ""); - this.reformat = reformat; - this.operateTitle = operateTitle; + this.generateOptions = generateOptions; } /** @@ -94,7 +82,7 @@ public class SaveFile { } // 路径对比,判断项目路径是否为文件保存路径的子路径 String projectPath = handlerPath(baseDir.getPath()); - String tmpFilePath = handlerPath(this.path); + String tmpFilePath = handlerPath(callback.getSavePath()); if (tmpFilePath.length() > projectPath.length()) { if (!"/".equals(tmpFilePath.substring(projectPath.length(), projectPath.length() + 1))) { return false; @@ -133,13 +121,16 @@ public class SaveFile { * 通过IDEA自带的Psi文件方式写入 */ public void write() { + if (!Boolean.TRUE.equals(callback.getWriteFile())) { + return; + } // 判断目录是否存在 VirtualFile baseDir = ProjectUtils.getBaseDir(project); if (baseDir == null) { throw new IllegalStateException("项目基本路径不存在"); } // 处理保存路径 - String savePath = handlerPath(this.path, false); + String savePath = handlerPath(callback.getSavePath(), false); if (isProjectFile()) { // 删除保存路径的前面部分 savePath = savePath.substring(handlerPath(baseDir.getPath()).length()); @@ -165,7 +156,7 @@ public class SaveFile { if (directory == null) { return; } - VirtualFile psiFile = directory.findChild(this.fileName); + VirtualFile psiFile = directory.findChild(callback.getFileName()); // 保存或覆盖 saveOrReplaceFile(psiFile, directory); } @@ -181,12 +172,19 @@ public class SaveFile { return saveDir; } // 尝试创建目录 - String msg = String.format("Directory %s Not Found, Confirm Create?", this.path); - if (this.operateTitle && !MessageDialogUtils.yesNo(project, msg)) { + String msg = String.format("Directory %s Not Found, Confirm Create?", callback.getSavePath()); + if (generateOptions.getTitleSure()) { + saveDir = fileUtils.createChildDirectory(project, baseDir, savePath); + return saveDir; + } else if (generateOptions.getTitleRefuse()) { return null; + } else { + if (MessageDialogUtils.yesNo(project, msg)) { + saveDir = fileUtils.createChildDirectory(project, baseDir, savePath); + return saveDir; + } } - saveDir = fileUtils.createChildDirectory(project, baseDir, savePath); - return saveDir; + return null; } /** @@ -200,16 +198,22 @@ public class SaveFile { Document document; // 文件不存在直接创建 if (file == null) { - file = fileUtils.createChildFile(project, directory, this.fileName); + file = fileUtils.createChildFile(project, directory, callback.getFileName()); if (file == null) { return; } document = coverFile(file); } else { // 提示覆盖文件 - if (operateTitle) { + if (generateOptions.getTitleSure()) { + // 默认选是 + document = coverFile(file); + } else if (generateOptions.getTitleRefuse()) { + // 默认选否 + return; + } else { String msg = String.format("File %s Exists, Select Operate Mode?", file.getPath()); - int result = MessageDialogUtils.yesNoCancel(project, msg, "Conver", "Compare", "Cancel"); + int result = MessageDialogUtils.yesNoCancel(project, msg, "Convert", "Compare", "Cancel"); switch (result) { case Messages.YES: // 覆盖文件 @@ -218,7 +222,7 @@ public class SaveFile { case Messages.NO: // 对比代码时也格式化代码 String newText = content; - if (reformat) { + if (Boolean.TRUE.equals(callback.getReformat())) { // 保留旧文件内容,用新文件覆盖旧文件执行格式化,然后再还原旧文件内容 String oldText = getFileText(file); Document tmpDoc = coverFile(file); @@ -231,20 +235,17 @@ public class SaveFile { // 还原旧文件 coverFile(file, oldText); } - FileType fileType = FileTypeManager.getInstance().getFileTypeByFileName(fileName); - CompareFileUtils.showCompareWindow(project, file, new LightVirtualFile(fileName, fileType, newText)); + FileType fileType = FileTypeManager.getInstance().getFileTypeByFileName(callback.getFileName()); + CompareFileUtils.showCompareWindow(project, file, new LightVirtualFile(callback.getFileName(), fileType, newText)); return; case Messages.CANCEL: default: return; } - } else { - // 没有操作提示的情况下直接覆盖 - document = coverFile(file); } } // 执行代码格式化操作 - if (reformat) { + if (Boolean.TRUE.equals(callback.getReformat())) { FileUtils.getInstance().reformatFile(project, file); } // 提交文档改动,并非VCS中的提交文件 @@ -278,6 +279,6 @@ public class SaveFile { * @return 覆盖后的文档对象 */ private Document coverFile(VirtualFile file, String text) { - return FileUtils.getInstance().writeFileContent(project, file, fileName, text); + return FileUtils.getInstance().writeFileContent(project, file, callback.getFileName(), text); } } diff --git a/src/main/java/com/sjhy/plugin/service/CodeGenerateService.java b/src/main/java/com/sjhy/plugin/service/CodeGenerateService.java index 629f6ba..7507522 100644 --- a/src/main/java/com/sjhy/plugin/service/CodeGenerateService.java +++ b/src/main/java/com/sjhy/plugin/service/CodeGenerateService.java @@ -2,6 +2,7 @@ package com.sjhy.plugin.service; import com.intellij.openapi.components.ServiceManager; import com.intellij.openapi.project.Project; +import com.sjhy.plugin.dto.GenerateOptions; import com.sjhy.plugin.entity.TableInfo; import com.sjhy.plugin.entity.Template; import org.jetbrains.annotations.NotNull; @@ -27,14 +28,12 @@ public interface CodeGenerateService { } /** - * 生成代码,并自动保存到对应位置,使用统一配置 - * @param templates 模板 - * @param unifiedConfig 是否使用统一配置 - * @param title 是否显示提示 - * @param entityMode + * 生成 + * + * @param templates 模板 + * @param generateOptions 生成选项 */ - void generateByUnifiedConfig(Collection