static void saveIn(@NotNull Project project,
                     @NotNull Document document,
                     Editor editor,
                     @NotNull VirtualFile virtualFile,
                     @NotNull Charset charset) {
    FileDocumentManager documentManager = FileDocumentManager.getInstance();
    documentManager.saveDocument(document);
    boolean writable = ReadonlyStatusHandler.ensureFilesWritable(project, virtualFile);
    if (!writable) {
      CommonRefactoringUtil.showErrorHint(project, editor,
                                          IdeBundle.message("dialog.message.cannot.save.the.file.0", virtualFile.getPresentableUrl()),
                                          IdeBundle.message("dialog.title.unable.to.save"), null);
      return;
    }

    EncodingProjectManagerImpl.suppressReloadDuring(() -> {
      EncodingProjectManager.getInstance(project).setEncoding(virtualFile, charset);
      try {
        ApplicationManager.getApplication().runWriteAction((ThrowableComputable<Object, IOException>)() -> {
          virtualFile.setCharset(charset);
          LoadTextUtil.write(project, virtualFile, virtualFile, document.getText(), document.getModificationStamp());
          return null;
        });
      }
      catch (IOException io) {
        Messages.showErrorDialog(project, io.getMessage(), IdeBundle.message("dialog.title.error.writing.file"));
      }
    });
  }
static void saveIn(@NotNull Project project,
                     @NotNull Document document,
                     Editor editor,
                     @NotNull VirtualFile virtualFile,
                     @NotNull Charset charset)