Document (String value, @Nullable Language language, @Nullable Project project, @NotNull SimpleDocumentCreator documentCreator) { final FileType fileType = language != null ? language.getAssociatedFileType() : null; if (fileType != null) { final Project notNullProject = project != null ? project : ProjectManager.getInstance().getDefaultProject(); final PsiFileFactory factory = PsiFileFactory.getInstance(notNullProject); final long stamp = LocalTimeCounter.currentTime(); final PsiFile psiFile = factory.createFileFromText("Dummy." + fileType.getDefaultExtension(), fileType, value, stamp, true, false); documentCreator.customizePsiFile(psiFile); // No need to guess project in getDocument - we already know it Document document; try (AccessToken ignored = ProjectLocator.withPreferredProject(psiFile.getVirtualFile(), notNullProject)) { document = ReadAction.compute(() -> PsiDocumentManager.getInstance(notNullProject).getDocument(psiFile)); } assert document != null; return document; } else { return EditorFactory.getInstance().createDocument(value); } }