在文件覆盖的基础上添加对比功能

This commit is contained in:
makejava 2020-06-11 17:08:57 +08:00
parent 6ad2ecf06f
commit 476f264456
3 changed files with 67 additions and 4 deletions

View File

@ -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;
}

View File

@ -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);
}
}

View File

@ -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,8 +111,22 @@ 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()) {
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;
}
}
@ -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());