From 476f2644567c077936854999584d9a2dc0cd6718 Mon Sep 17 00:00:00 2001 From: makejava <1353036300@qq.com> Date: Thu, 11 Jun 2020 17:08:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8=E6=96=87=E4=BB=B6=E8=A6=86=E7=9B=96?= =?UTF-8?q?=E7=9A=84=E5=9F=BA=E7=A1=80=E4=B8=8A=E6=B7=BB=E5=8A=A0=E5=AF=B9?= =?UTF-8?q?=E6=AF=94=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/sjhy/plugin/entity/SaveFile.java | 7 ++++ .../sjhy/plugin/tool/CompareFileUtils.java | 41 +++++++++++++++++++ .../java/com/sjhy/plugin/tool/FileUtils.java | 23 +++++++++-- 3 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/sjhy/plugin/tool/CompareFileUtils.java diff --git a/src/main/java/com/sjhy/plugin/entity/SaveFile.java b/src/main/java/com/sjhy/plugin/entity/SaveFile.java index 4b76130..41dcd55 100644 --- a/src/main/java/com/sjhy/plugin/entity/SaveFile.java +++ b/src/main/java/com/sjhy/plugin/entity/SaveFile.java @@ -3,8 +3,10 @@ package com.sjhy.plugin.entity; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.fileTypes.FileTypes; import com.intellij.openapi.project.Project; +import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiFile; import com.intellij.psi.PsiFileFactory; +import com.intellij.testFramework.LightVirtualFile; import com.sjhy.plugin.tool.FileUtils; import lombok.Data; @@ -26,6 +28,10 @@ public class SaveFile { * 文件保存目录 */ private String path; + /** + * 虚拟文件 + */ + private VirtualFile virtualFile; /** * 需要保存的文件 */ @@ -58,6 +64,7 @@ public class SaveFile { LOG.assertTrue(content != null); // 换行符统一使用\n this.file = psiFileFactory.createFileFromText(fileName, FileTypes.UNKNOWN, content.replace("\r", "")); + this.virtualFile = new LightVirtualFile(fileName, content.replace("\r", "")); this.reformat = reformat; this.operateTitle = operateTitle; } diff --git a/src/main/java/com/sjhy/plugin/tool/CompareFileUtils.java b/src/main/java/com/sjhy/plugin/tool/CompareFileUtils.java new file mode 100644 index 0000000..8f31a42 --- /dev/null +++ b/src/main/java/com/sjhy/plugin/tool/CompareFileUtils.java @@ -0,0 +1,41 @@ +package com.sjhy.plugin.tool; + +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.contents.DiffContent; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.vfs.VirtualFile; + +/** + * @author makejava + * @version 1.0.0 + * @since 2020/06/11 15:47 + */ +public class CompareFileUtils { + + /** + * 显示文件对比框 + * + * @param project 项目 + * @param leftFile 左边的文件 + * @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); + + MutableDiffRequestChain chain = new MutableDiffRequestChain(leftContent, rightContent); + + chain.setWindowTitle(requestFactory.getTitle(leftFile, rightFile)); + chain.setTitle1(requestFactory.getContentTitle(leftFile)); + chain.setTitle2(requestFactory.getContentTitle(rightFile)); + DiffManager.getInstance().showDiff(project, chain, DiffDialogHints.MODAL); + } + +} diff --git a/src/main/java/com/sjhy/plugin/tool/FileUtils.java b/src/main/java/com/sjhy/plugin/tool/FileUtils.java index 11e312b..b477b1c 100644 --- a/src/main/java/com/sjhy/plugin/tool/FileUtils.java +++ b/src/main/java/com/sjhy/plugin/tool/FileUtils.java @@ -6,8 +6,10 @@ import com.intellij.codeInsight.actions.ReformatCodeProcessor; import com.intellij.openapi.command.WriteCommandAction; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.editor.Document; +import com.intellij.openapi.fileEditor.FileDocumentManager; import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.MessageDialogBuilder; +import com.intellij.openapi.ui.Messages; import com.intellij.openapi.util.Computable; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VfsUtil; @@ -109,9 +111,23 @@ public class FileUtils { } // 保存或替换文件 PsiFile oldFile = psiDirectory.findFile(saveFile.getFile().getName()); - if (oldFile != null) { - if (saveFile.isOperateTitle() && MessageDialogBuilder.yesNo(MsgValue.TITLE_INFO, "File " + saveFile.getFile().getName() + " Exists, Confirm Continue?").isYes()) { - return; + PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(saveFile.getProject()); + FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance(); + if (saveFile.isOperateTitle() && oldFile != null) { + MessageDialogBuilder.YesNoCancel yesNoCancel = MessageDialogBuilder.yesNoCancel(MsgValue.TITLE_INFO, "File " + saveFile.getFile().getName() + " Exists, Select Operate Mode?"); + yesNoCancel.yesText("Cover"); + yesNoCancel.noText("Compare"); + yesNoCancel.cancelText("Cancel"); + int result = yesNoCancel.show(); + switch (result) { + case Messages.YES: + break; + case Messages.NO: + CompareFileUtils.showCompareWindow(saveFile.getProject(), fileDocumentManager.getFile(psiDocumentManager.getDocument(oldFile)), saveFile.getVirtualFile()); + return; + case Messages.CANCEL: + default: + return; } } PsiDirectory finalPsiDirectory = psiDirectory; @@ -120,7 +136,6 @@ public class FileUtils { return (PsiFile) finalPsiDirectory.add(saveFile.getFile()); } else { // 对旧文件进行替换操作 - PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(saveFile.getProject()); Document document = psiDocumentManager.getDocument(oldFile); LOG.assertTrue(document != null); document.setText(saveFile.getFile().getText());