修复删除文件后再重新创建会导致索引异常,采用替换文件的方案来解决。

This commit is contained in:
makejava 2020-04-20 19:07:08 +08:00
parent 15cde844dd
commit e82a8a0a5b
5 changed files with 72 additions and 43 deletions

View File

@ -25,7 +25,7 @@ apply plugin: 'java'
apply plugin: 'org.jetbrains.intellij'
group 'com.sjhy'
version '1.2.3-RELEASE'
version '1.2.4-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

View File

@ -8,6 +8,7 @@ import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.MessageDialogBuilder;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.psi.PsiFile;
import com.sjhy.plugin.config.Settings;
import com.sjhy.plugin.constants.MsgValue;
import com.sjhy.plugin.entity.Callback;
@ -138,7 +139,7 @@ public class CodeGenerateServiceImpl implements CodeGenerateService {
setModulePathAndImportList(param, tableInfo);
// 设置额外代码生成服务
param.put("generateService", new ExtraCodeGenerateUtils(this, tableInfo, title));
List<PsiFile> psiFileList = new ArrayList<>();
for (Template template : templates) {
Callback callback = new Callback();
// 设置回调对象
@ -180,8 +181,13 @@ public class CodeGenerateServiceImpl implements CodeGenerateService {
}
}
// 保存文件
fileUtils.write(project, file, code, callback.isReformat());
PsiFile psiFile = fileUtils.write(project, file, code);
if (psiFile != null && callback.isReformat()) {
psiFileList.add(psiFile);
}
}
// 统一格式化
fileUtils.reformatFile(project, psiFileList);
}
//刷新整个项目
VirtualFileManager.getInstance().syncRefresh();

View File

@ -10,6 +10,7 @@ import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.MessageDialogBuilder;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.psi.PsiFile;
import com.intellij.util.ExceptionUtil;
import com.intellij.util.containers.JBIterable;
import com.sjhy.plugin.constants.MsgValue;
@ -335,7 +336,10 @@ public class TableInfoServiceImpl implements TableInfoService {
// 获取保存文件
File file = new File(dir, getConfigFileName(oldTableInfo.getObj()));
//写入配置文件
fileUtils.write(project, file, content, true);
PsiFile psiFile = fileUtils.write(project, file, content);
if (psiFile != null) {
fileUtils.reformatFile(project, Collections.singletonList(psiFile));
}
// 同步刷新
VirtualFileManager.getInstance().syncRefresh();
}

View File

@ -4,9 +4,12 @@ import com.intellij.codeInsight.actions.AbstractLayoutCodeProcessor;
import com.intellij.codeInsight.actions.OptimizeImportsProcessor;
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.fileTypes.FileTypes;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
@ -16,6 +19,7 @@ import com.sjhy.plugin.constants.MsgValue;
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
/**
* 文件工具类
@ -25,6 +29,7 @@ import java.lang.reflect.InvocationTargetException;
* @since 2018/07/17 13:10
*/
public class FileUtils {
private static final Logger LOG = Logger.getInstance(FileUtils.class);
private static volatile FileUtils fileUtils;
/**
@ -47,8 +52,8 @@ public class FileUtils {
/**
* 读取文件内容文本文件
*
* @param project 项目对象
* @param file 文件对象
* @param project 项目对象
* @param file 文件对象
* @return 文件内容
*/
public String read(Project project, File file) {
@ -70,9 +75,8 @@ public class FileUtils {
* @param project 项目对象
* @param file 文件
* @param content 文件内容
* @param reformat 是否对代码进行重新格式化
*/
public void write(Project project, File file, String content, boolean reformat) {
public PsiFile write(Project project, File file, String content) {
// 替换换行符
content = content.replace("\r\n", "\n");
// 创建文件
@ -83,10 +87,45 @@ public class FileUtils {
if (parentVirtualFile == null) {
// 自动创建目录
Messages.showWarningDialog("目录不存在", MsgValue.TITLE_INFO);
return;
return null;
}
PsiDirectory psiDirectory = psiManager.findDirectory(parentVirtualFile);
saveFileAndFormatCode(project, psiDirectory, psiFile, reformat);
return saveFileAndFormatCode(project, psiDirectory, psiFile);
}
/**
* 执行格式化
*
* @param project 项目对象
* @param psiFileList 文件列表
*/
@SuppressWarnings("unchecked")
public void reformatFile(Project project, List<PsiFile> psiFileList) {
if (CollectionUtil.isEmpty(psiFileList)) {
return;
}
// 提交所有改动并非CVS中的提交文件
PsiDocumentManager.getInstance(project).commitAllDocuments();
// 尝试对文件进行格式化处理
AbstractLayoutCodeProcessor processor = new ReformatCodeProcessor(project, psiFileList.toArray(new PsiFile[0]), null, false);
// 优化导入
processor = new OptimizeImportsProcessor(processor);
// 重新编排代码会将代码中的属性与方法的顺序进行重新调整
// processor = new RearrangeCodeProcessor(processor);
// 清理代码进行旧版本兼容旧版本的IDEA尚未提供该处理器
try {
Class<AbstractLayoutCodeProcessor> codeCleanupCodeProcessorCls = (Class<AbstractLayoutCodeProcessor>) Class.forName("com.intellij.codeInsight.actions.CodeCleanupCodeProcessor");
Constructor<AbstractLayoutCodeProcessor> constructor = codeCleanupCodeProcessorCls.getConstructor(AbstractLayoutCodeProcessor.class);
processor = constructor.newInstance(processor);
} catch (ClassNotFoundException ignored) {
// 类不存在直接忽略
} catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
// 抛出未知异常
ExceptionUtil.rethrow(e);
}
// 执行处理
processor.run();
}
/**
@ -95,42 +134,21 @@ public class FileUtils {
* @param project 项目
* @param psiDirectory 目录
* @param psiFile 文件
* @param reformat 是否对代码进行重新格式化
*/
@SuppressWarnings("unchecked")
private void saveFileAndFormatCode(Project project, PsiDirectory psiDirectory, PsiFile psiFile, boolean reformat) {
WriteCommandAction.runWriteCommandAction(project, () -> {
private PsiFile saveFileAndFormatCode(Project project, PsiDirectory psiDirectory, PsiFile psiFile) {
return WriteCommandAction.runWriteCommandAction(project, (Computable<PsiFile>) () -> {
PsiFile oldFile = psiDirectory.findFile(psiFile.getName());
PsiFile temFile;
if (oldFile != null) {
oldFile.delete();
Document document = PsiDocumentManager.getInstance(project).getDocument(oldFile);
LOG.assertTrue(document != null);
// 替换文件
document.setText(psiFile.getText());
temFile = oldFile;
} else {
temFile = (PsiFile) psiDirectory.add(psiFile);
}
PsiFile newFile = (PsiFile) psiDirectory.add(psiFile);
// 提交所有改动并非CVS中的提交文件
PsiDocumentManager.getInstance(project).commitAllDocuments();
// 判断是否需要格式化代码
if (!reformat) {
return;
}
// 尝试对文件进行格式化处理
AbstractLayoutCodeProcessor processor = new ReformatCodeProcessor(project, new PsiFile[]{newFile}, null, false);
// 优化导入
processor = new OptimizeImportsProcessor(processor);
// 重新编排代码会将代码中的属性与方法的顺序进行重新调整
// processor = new RearrangeCodeProcessor(processor);
// 清理代码进行旧版本兼容旧版本的IDEA尚未提供该处理器
try {
Class<AbstractLayoutCodeProcessor> codeCleanupCodeProcessorCls = (Class<AbstractLayoutCodeProcessor>) Class.forName("com.intellij.codeInsight.actions.CodeCleanupCodeProcessor");
Constructor<AbstractLayoutCodeProcessor> constructor = codeCleanupCodeProcessorCls.getConstructor(AbstractLayoutCodeProcessor.class);
processor = constructor.newInstance(processor);
} catch (ClassNotFoundException ignored) {
// 类不存在直接忽略
} catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
// 抛出未知异常
ExceptionUtil.rethrow(e);
}
// 执行处理
processor.run();
return temFile;
});
}
}

View File

@ -1,6 +1,7 @@
<idea-plugin>
<id>com.sjhy.plugin.easycode</id>
<vendor email="1353036300@qq.com" url="http://www.shujuhaiyang.com">数据海洋</vendor>
<name>Easy Code</name>
<vendor email="1353036300@qq.com" url="http://www.shujuhaiyang.com">Easy Code Office Website</vendor>
<description><![CDATA[
<tag>EasyCode,Easy Code,easy,code,code generate,code tools</tag>