diff --git a/src/main/java/com/sjhy/plugin/entity/SaveFile.java b/src/main/java/com/sjhy/plugin/entity/SaveFile.java index 11b3b32..5446793 100644 --- a/src/main/java/com/sjhy/plugin/entity/SaveFile.java +++ b/src/main/java/com/sjhy/plugin/entity/SaveFile.java @@ -249,7 +249,9 @@ public class SaveFile { FileUtils.getInstance().reformatFile(project, file); } // 提交文档改动,并非VCS中的提交文件 - psiDocumentManager.commitDocument(document); + if (document != null) { + psiDocumentManager.commitDocument(document); + } } private String getFileText(VirtualFile file) { diff --git a/src/main/java/com/sjhy/plugin/tool/FileUtils.java b/src/main/java/com/sjhy/plugin/tool/FileUtils.java index a6845b6..5f47649 100644 --- a/src/main/java/com/sjhy/plugin/tool/FileUtils.java +++ b/src/main/java/com/sjhy/plugin/tool/FileUtils.java @@ -109,7 +109,14 @@ public class FileUtils { FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance(); Document document = fileDocumentManager.getDocument(file); 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)); PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project);