From 1ec88794ec71f5e873e1653f75fbb4eff37aa3e2 Mon Sep 17 00:00:00 2001 From: makejava <1353036300@qq.com> Date: Mon, 15 Jun 2020 20:18:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=BC=E5=AE=B9=E8=87=B32017.1=E5=B9=B6?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=AF=B9=E6=AF=94=E4=BB=A3=E7=A0=81=E6=9C=AA?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=A0=BC=E5=BC=8F=E5=8C=96=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/CodeGenerateServiceImpl.java | 7 ++++ .../sjhy/plugin/tool/CompareFileUtils.java | 40 ++++++++++++++----- .../java/com/sjhy/plugin/tool/FileUtils.java | 21 ++++++++-- .../java/com/sjhy/plugin/tool/NameUtils.java | 6 +-- .../plugin/ui/GlobalConfigSettingPanel.java | 11 +++++ .../java/com/sjhy/plugin/ui/MainSetting.java | 11 +++++ .../com/sjhy/plugin/ui/TableSettingPanel.java | 11 +++++ .../sjhy/plugin/ui/TemplateSettingPanel.java | 11 +++++ .../com/sjhy/plugin/ui/TypeMapperSetting.java | 11 +++++ .../sjhy/plugin/ui/base/TemplateEditor.java | 5 +++ src/main/resources/META-INF/plugin.xml | 2 +- 11 files changed, 118 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/sjhy/plugin/service/impl/CodeGenerateServiceImpl.java b/src/main/java/com/sjhy/plugin/service/impl/CodeGenerateServiceImpl.java index 220f3fa..4d4ef0d 100644 --- a/src/main/java/com/sjhy/plugin/service/impl/CodeGenerateServiceImpl.java +++ b/src/main/java/com/sjhy/plugin/service/impl/CodeGenerateServiceImpl.java @@ -7,8 +7,10 @@ import com.intellij.openapi.module.Module; import com.intellij.openapi.module.ModuleManager; import com.intellij.openapi.module.ModuleUtil; import com.intellij.openapi.project.Project; +import com.intellij.openapi.ui.Messages; import com.intellij.util.ReflectionUtil; import com.sjhy.plugin.config.Settings; +import com.sjhy.plugin.constants.MsgValue; import com.sjhy.plugin.entity.Callback; import com.sjhy.plugin.entity.SaveFile; import com.sjhy.plugin.entity.TableInfo; @@ -66,6 +68,11 @@ public class CodeGenerateServiceImpl implements CodeGenerateService { TableInfo selectedTableInfo = tableInfoService.getTableInfoAndConfig(cacheDataUtils.getSelectDbTable()); // 获取所有选中的表信息 List tableInfoList = tableInfoService.getTableInfoAndConfig(cacheDataUtils.getDbTableList()); + // 校验选中表的保存路径是否正确 + if (StringUtils.isEmpty(selectedTableInfo.getSavePath())) { + Messages.showInfoMessage(selectedTableInfo.getObj().getName() + "表配置信息不正确,请尝试重新配置", MsgValue.TITLE_INFO); + return; + } // 将未配置的表进行配置覆盖 tableInfoList.forEach(tableInfo -> { if (StringUtils.isEmpty(tableInfo.getSavePath())) { diff --git a/src/main/java/com/sjhy/plugin/tool/CompareFileUtils.java b/src/main/java/com/sjhy/plugin/tool/CompareFileUtils.java index 8f31a42..fa480b0 100644 --- a/src/main/java/com/sjhy/plugin/tool/CompareFileUtils.java +++ b/src/main/java/com/sjhy/plugin/tool/CompareFileUtils.java @@ -4,10 +4,15 @@ import com.intellij.diff.DiffContentFactory; import com.intellij.diff.DiffDialogHints; import com.intellij.diff.DiffManager; import com.intellij.diff.DiffRequestFactory; -import com.intellij.diff.actions.impl.MutableDiffRequestChain; +//import com.intellij.diff.actions.impl.MutableDiffRequestChain; +import com.intellij.diff.chains.DiffRequestChain; import com.intellij.diff.contents.DiffContent; +import com.intellij.diff.requests.DiffRequest; import com.intellij.openapi.project.Project; import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.util.ExceptionUtil; + +import java.lang.reflect.InvocationTargetException; /** * @author makejava @@ -24,18 +29,33 @@ public class CompareFileUtils { * @param rightFile 右边的文件 */ public static void showCompareWindow(Project project, VirtualFile leftFile, VirtualFile rightFile) { - DiffContentFactory contentFactory = DiffContentFactory.getInstance(); - DiffRequestFactory requestFactory = DiffRequestFactory.getInstance(); - DiffContent leftContent = contentFactory.create(project, leftFile); - DiffContent rightContent = contentFactory.create(project, rightFile); + try { + Class cls = Class.forName("com.intellij.diff.actions.impl.MutableDiffRequestChain"); + // 新版支持 + DiffContentFactory contentFactory = DiffContentFactory.getInstance(); + DiffRequestFactory requestFactory = DiffRequestFactory.getInstance(); - MutableDiffRequestChain chain = new MutableDiffRequestChain(leftContent, rightContent); + DiffContent leftContent = contentFactory.create(project, leftFile); + DiffContent rightContent = contentFactory.create(project, rightFile); - chain.setWindowTitle(requestFactory.getTitle(leftFile, rightFile)); - chain.setTitle1(requestFactory.getContentTitle(leftFile)); - chain.setTitle2(requestFactory.getContentTitle(rightFile)); - DiffManager.getInstance().showDiff(project, chain, DiffDialogHints.MODAL); + DiffRequestChain chain = (DiffRequestChain) cls.getConstructor(DiffContent.class, DiffContent.class).newInstance(leftContent, rightContent); +// MutableDiffRequestChain chain = new MutableDiffRequestChain(leftContent, rightContent); + + cls.getMethod("setWindowTitle", String.class).invoke(chain, requestFactory.getTitle(leftFile, rightFile)); + cls.getMethod("setTitle1", String.class).invoke(chain, requestFactory.getContentTitle(leftFile)); + cls.getMethod("setTitle2", String.class).invoke(chain, requestFactory.getContentTitle(rightFile)); +// chain.setWindowTitle(requestFactory.getTitle(leftFile, rightFile)); +// chain.setTitle1(requestFactory.getContentTitle(leftFile)); +// chain.setTitle2(requestFactory.getContentTitle(rightFile)); + DiffManager.getInstance().showDiff(project, chain, DiffDialogHints.MODAL); + } catch (ClassNotFoundException e) { + // 旧版兼容 + DiffRequest diffRequest = DiffRequestFactory.getInstance().createFromFiles(project, leftFile, rightFile); + DiffManager.getInstance().showDiff(project, diffRequest, DiffDialogHints.MODAL); + } catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) { + ExceptionUtil.rethrow(e); + } } } diff --git a/src/main/java/com/sjhy/plugin/tool/FileUtils.java b/src/main/java/com/sjhy/plugin/tool/FileUtils.java index 132417f..f871624 100644 --- a/src/main/java/com/sjhy/plugin/tool/FileUtils.java +++ b/src/main/java/com/sjhy/plugin/tool/FileUtils.java @@ -18,6 +18,7 @@ import com.intellij.psi.PsiDirectory; import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.PsiFile; import com.intellij.psi.PsiManager; +import com.intellij.testFramework.LightVirtualFile; import com.intellij.util.ExceptionUtil; import com.sjhy.plugin.constants.MsgValue; import com.sjhy.plugin.entity.SaveFile; @@ -98,7 +99,7 @@ public class FileUtils { LOG.assertTrue(dir != null); return psiManager.findDirectory(dir); } catch (IOException e) { - LOG.error("path {} error", saveFile.getPath()); + LOG.error("path " + saveFile.getPath() + " error"); ExceptionUtil.rethrow(e); return null; } @@ -125,7 +126,19 @@ public class FileUtils { case Messages.NO: // 对比代码时也格式化代码 if (saveFile.isReformat()) { - reformatFile(saveFile.getProject(), Collections.singletonList(PsiManager.getInstance(saveFile.getProject()).findFile(saveFile.getVirtualFile()))); + // 保留旧文件内容,用新文件覆盖旧文件执行格式化,然后再还原旧文件内容 + String oldText = oldFile.getText(); + WriteCommandAction.runWriteCommandAction(saveFile.getProject(), () -> psiDocumentManager.getDocument(oldFile).setText(saveFile.getFile().getText())); + // 提交所有改动,并非VCS中的提交文件 + PsiDocumentManager.getInstance(saveFile.getProject()).commitAllDocuments(); + reformatFile(saveFile.getProject(), Collections.singletonList(oldFile)); + // 提交所有改动,并非VCS中的提交文件 + PsiDocumentManager.getInstance(saveFile.getProject()).commitAllDocuments(); + String newText = oldFile.getText(); + WriteCommandAction.runWriteCommandAction(saveFile.getProject(), () -> psiDocumentManager.getDocument(oldFile).setText(oldText)); + // 提交所有改动,并非VCS中的提交文件 + PsiDocumentManager.getInstance(saveFile.getProject()).commitAllDocuments(); + saveFile.setVirtualFile(new LightVirtualFile(saveFile.getFile().getName(), saveFile.getFile().getFileType(), newText)); } CompareFileUtils.showCompareWindow(saveFile.getProject(), fileDocumentManager.getFile(psiDocumentManager.getDocument(oldFile)), saveFile.getVirtualFile()); return; @@ -150,6 +163,8 @@ public class FileUtils { if (saveFile.isReformat()) { reformatFile(saveFile.getProject(), Collections.singletonList(finalFile)); } + // 提交所有改动,并非VCS中的提交文件 + PsiDocumentManager.getInstance(saveFile.getProject()).commitAllDocuments(); } /** @@ -163,7 +178,7 @@ public class FileUtils { if (CollectionUtil.isEmpty(psiFileList)) { return; } - // 提交所有改动,并非CVS中的提交文件 + // 提交所有改动,并非VCS中的提交文件 PsiDocumentManager.getInstance(project).commitAllDocuments(); // 尝试对文件进行格式化处理 AbstractLayoutCodeProcessor processor = new ReformatCodeProcessor(project, psiFileList.toArray(new PsiFile[0]), null, false); diff --git a/src/main/java/com/sjhy/plugin/tool/NameUtils.java b/src/main/java/com/sjhy/plugin/tool/NameUtils.java index abc8080..ad6e304 100644 --- a/src/main/java/com/sjhy/plugin/tool/NameUtils.java +++ b/src/main/java/com/sjhy/plugin/tool/NameUtils.java @@ -1,7 +1,5 @@ package com.sjhy.plugin.tool; -import org.apache.commons.lang3.ArrayUtils; - import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -91,7 +89,7 @@ public class NameUtils { * @return 类名 */ public String getClsNameByFullName(String fullName) { - return fullName.substring(fullName.lastIndexOf('.') + 1, fullName.length()); + return fullName.substring(fullName.lastIndexOf('.') + 1); } /** @@ -133,7 +131,7 @@ public class NameUtils { */ public String append(Object... objects) { - if (ArrayUtils.isEmpty(objects)) { + if (objects == null || objects.length == 0) { return null; } StringBuilder builder = new StringBuilder(); diff --git a/src/main/java/com/sjhy/plugin/ui/GlobalConfigSettingPanel.java b/src/main/java/com/sjhy/plugin/ui/GlobalConfigSettingPanel.java index 184f53a..bb83d1e 100644 --- a/src/main/java/com/sjhy/plugin/ui/GlobalConfigSettingPanel.java +++ b/src/main/java/com/sjhy/plugin/ui/GlobalConfigSettingPanel.java @@ -109,6 +109,17 @@ public class GlobalConfigSettingPanel implements Configurable { return "Global Config"; } + /** + * Returns the topic in the help file which is shown when help for the configurable is requested. + * + * @return the help topic, or {@code null} if no help is available + */ + @Nullable + @Override + public String getHelpTopic() { + return null; + } + /** * 获取主面板对象 * diff --git a/src/main/java/com/sjhy/plugin/ui/MainSetting.java b/src/main/java/com/sjhy/plugin/ui/MainSetting.java index 1720412..5a02743 100644 --- a/src/main/java/com/sjhy/plugin/ui/MainSetting.java +++ b/src/main/java/com/sjhy/plugin/ui/MainSetting.java @@ -315,6 +315,17 @@ public class MainSetting implements Configurable, Configurable.Composite { return "Easy Code"; } + /** + * Returns the topic in the help file which is shown when help for the configurable is requested. + * + * @return the help topic, or {@code null} if no help is available + */ + @Nullable + @Override + public String getHelpTopic() { + return getDisplayName(); + } + /** * 更多配置 * diff --git a/src/main/java/com/sjhy/plugin/ui/TableSettingPanel.java b/src/main/java/com/sjhy/plugin/ui/TableSettingPanel.java index 28af154..68acb4c 100644 --- a/src/main/java/com/sjhy/plugin/ui/TableSettingPanel.java +++ b/src/main/java/com/sjhy/plugin/ui/TableSettingPanel.java @@ -62,6 +62,17 @@ public class TableSettingPanel extends AbstractTableGroupPanel - +