void (@NotNull VirtualFile virtualFile, @NotNull Charset charset, @NotNull Project project) { Consumer<VirtualFile> setEncoding = file -> EncodingProjectManager.getInstance(project).setEncoding(file, charset); FileDocumentManager documentManager = FileDocumentManager.getInstance(); if (documentManager.getCachedDocument(virtualFile) == null) { // no need to reload document setEncoding.accept(virtualFile); return; } final Disposable disposable = Disposer.newDisposable(); MessageBusConnection connection = ApplicationManager.getApplication().getMessageBus().connect(disposable); connection.subscribe(FileDocumentManagerListener.TOPIC, new FileDocumentManagerListener() { @Override public void beforeFileContentReload(@NotNull VirtualFile file, @NotNull Document document) { if (!file.equals(virtualFile)) return; Disposer.dispose(disposable); // disconnect setEncoding.accept(file); LoadTextUtil.clearCharsetAutoDetectionReason(file); } }); // if file was modified, the user will be asked here try { VFileContentChangeEvent event = new VFileContentChangeEvent(null, virtualFile, 0, 0); EncodingProjectManagerImpl.suppressReloadDuring(() -> ((FileDocumentManagerImpl)documentManager).contentsChanged(event)); } finally { Disposer.dispose(disposable); } }