From 012f2cc1d81215c73458e2fcc022c411133119c2 Mon Sep 17 00:00:00 2001 From: makejava <1353036300@qq.com> Date: Tue, 14 Sep 2021 16:17:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=A6=96=E6=AC=A1=E7=94=9F?= =?UTF-8?q?=E6=88=90=E6=96=B0=E6=96=87=E4=BB=B6=EF=BC=8C=E4=BC=9A=E5=AD=98?= =?UTF-8?q?=E5=9C=A8=E6=96=87=E6=A1=A3=E5=AF=B9=E8=B1=A1=E4=B8=8D=E5=AD=98?= =?UTF-8?q?=E5=9C=A8BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/sjhy/plugin/entity/SaveFile.java | 4 +++- src/main/java/com/sjhy/plugin/tool/FileUtils.java | 9 ++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) 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);