From b4a981e1830a7ae87d834292f00fa4105b422904 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Giraud?= Date: Tue, 24 Jan 2023 10:25:46 +0100 Subject: [PATCH] use utf-8 when formatting rescript file #398 --- CHANGELOG.md | 2 ++ src/com/reason/comp/rescript/ResFormatProcess.java | 2 +- src/com/reason/ide/annotations/ORErrorAnnotator.java | 4 ++-- src/jpsplugin/com/reason/AutoDeletingTempFile.java | 8 +++++--- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7df466c2f..cbad6044c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ ## Unreleased +- :bug: [#398](https://github.com/giraud/reasonml-idea-plugin/issues/398) Rescript format doesn’t respect UTF-8 encoding + ## 0.109.1 - 2022/12/06 - :bug: [#392](https://github.com/giraud/reasonml-idea-plugin/issues/392) Attempting to open editorconfig causes IDE exception diff --git a/src/com/reason/comp/rescript/ResFormatProcess.java b/src/com/reason/comp/rescript/ResFormatProcess.java index 287e613b4..d7f4a9e51 100644 --- a/src/com/reason/comp/rescript/ResFormatProcess.java +++ b/src/com/reason/comp/rescript/ResFormatProcess.java @@ -32,7 +32,7 @@ public String format(@NotNull VirtualFile sourceFile, boolean isInterface, @NotN } try (AutoDeletingTempFile tempFile = new AutoDeletingTempFile("fileToFormat", isInterface ? ".resi" : ".res")) { - tempFile.write(code); + tempFile.write(code, UTF8); ProcessBuilder processBuilder = new ProcessBuilder(fmtBin.getPath(), "-format", tempFile.getPath()); Process fmt = null; diff --git a/src/com/reason/ide/annotations/ORErrorAnnotator.java b/src/com/reason/ide/annotations/ORErrorAnnotator.java index 7f718c9d1..b1c1703bf 100644 --- a/src/com/reason/ide/annotations/ORErrorAnnotator.java +++ b/src/com/reason/ide/annotations/ORErrorAnnotator.java @@ -200,7 +200,7 @@ static void cleanTempDirectory(@NotNull File directory, @NotNull String fileName } } - @Nullable static File copyToTempFile(@NotNull File tempCompilationDirectory, @NotNull PsiFile psiFile, @NotNull String nameWithoutExtension) { + static @Nullable File copyToTempFile(@NotNull File tempCompilationDirectory, @NotNull PsiFile psiFile, @NotNull String nameWithoutExtension) { File sourceTempFile; try { @@ -216,7 +216,7 @@ static void cleanTempDirectory(@NotNull File directory, @NotNull String fileName try { String psiText = ReadAction.compute(psiFile::getText); - FileUtil.writeToFile(sourceTempFile, psiText.getBytes()); + FileUtil.writeToFile(sourceTempFile, psiText.getBytes(Platform.UTF8)); } catch (IOException e) { // Sometimes, file is locked by another process, not a big deal, skip it LOG.trace("Write failed: " + e.getLocalizedMessage()); diff --git a/src/jpsplugin/com/reason/AutoDeletingTempFile.java b/src/jpsplugin/com/reason/AutoDeletingTempFile.java index 4a215fa02..d0dbea14e 100644 --- a/src/jpsplugin/com/reason/AutoDeletingTempFile.java +++ b/src/jpsplugin/com/reason/AutoDeletingTempFile.java @@ -5,6 +5,7 @@ import org.jetbrains.annotations.*; import java.io.*; +import java.nio.charset.*; public class AutoDeletingTempFile implements AutoCloseable { private final File myFile; @@ -17,11 +18,12 @@ public AutoDeletingTempFile(@NotNull String prefix, @NotNull String extension) t return myFile.getPath(); } - public void write(@NotNull String text) throws IOException { - FileUtil.writeToFile(myFile, text.getBytes()); + public void write(@NotNull String text, @NotNull Charset charset) throws IOException { + FileUtil.writeToFile(myFile, text, charset); } - @Override public void close() { + @Override + public void close() { FileUtilRt.delete(myFile); } }