PsiFile (@NotNull Project project, @NotNull String filename, @NotNull String request) { final String fileName = PathUtil.makeFileName(filename, "json"); try { final ThrowableComputable<PsiFile, Exception> computable = () -> { final ScratchFileService fileService = ScratchFileService.getInstance(); final VirtualFile file = fileService.findFile(RootType.findById("scratches"), fileName, ScratchFileService.Option.create_if_missing); fileService.getScratchesMapping().setMapping(file, Language.findLanguageByID("JSON")); final PsiFile psiFile = PsiManager.getInstance(project).findFile(file); final Document document = psiFile != null ? PsiDocumentManager.getInstance(project).getDocument(psiFile) : null; if (document == null) { return null; } document.insertString(document.getTextLength(), request); PsiDocumentManager.getInstance(project).commitDocument(document); return psiFile; }; return writeCommandAction(project) .withName(StatisticsBundle.message("stats.creating.temp.json.file.for.event.log")) .withGlobalUndo().shouldRecordActionForActiveDocument(false) .withUndoConfirmationPolicy(UndoConfirmationPolicy.REQUEST_CONFIRMATION) .compute(computable); } catch (Exception e) { // ignore } return null; }