修复首次生成新文件,会存在文档对象不存在BUG

This commit is contained in:
makejava 2021-09-14 16:17:57 +08:00
parent 3e8b0a56cc
commit 012f2cc1d8
2 changed files with 11 additions and 2 deletions

View File

@ -249,7 +249,9 @@ public class SaveFile {
FileUtils.getInstance().reformatFile(project, file); FileUtils.getInstance().reformatFile(project, file);
} }
// 提交文档改动并非VCS中的提交文件 // 提交文档改动并非VCS中的提交文件
psiDocumentManager.commitDocument(document); if (document != null) {
psiDocumentManager.commitDocument(document);
}
} }
private String getFileText(VirtualFile file) { private String getFileText(VirtualFile file) {

View File

@ -109,7 +109,14 @@ public class FileUtils {
FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance(); FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
Document document = fileDocumentManager.getDocument(file); Document document = fileDocumentManager.getDocument(file);
if (document == null) { if (document == null) {
throw new IllegalStateException("获取文档对象失败fileName" + fileName); WriteCommandAction.runWriteCommandAction(project, () -> {
try {
file.setBinaryContent(text.getBytes());
} catch (IOException e) {
throw new IllegalStateException("二进制文件写入失败fileName" + fileName);
}
});
return fileDocumentManager.getDocument(file);
} }
WriteCommandAction.runWriteCommandAction(project, () -> document.setText(text)); WriteCommandAction.runWriteCommandAction(project, () -> document.setText(text));
PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project); PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project);