void (@NotNull Environment env) { PsiDocumentManager.getInstance(getProject()).commitDocument(getDocument()); PsiFile file = getFile(); if (file instanceof PsiJavaFile) { PsiImportList importList = ((PsiJavaFile)file).getImportList(); if (importList != null) { PsiImportStatement[] statements = importList.getImportStatements(); if (statements.length > 0) { int offset = statements[0].getTextRange().getStartOffset(); String text = file.getText(); String toInsert = "import unresolved.*;\n"; env.logMessage("AddImportExternally: inserting " + StringUtil.escapeStringCharacters(toInsert) + " at " + file.getName() + ":" + offset); FileDocumentManager.getInstance().saveDocument(getDocument()); try { WriteAction.run(() -> VfsUtil.saveText(getVirtualFile(), text.substring(0, offset) + toInsert + text.substring(offset))); } catch (IOException e) { throw new RuntimeException(e); } } } } }