void (@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) { final PsiElement element = descriptor.getPsiElement(); final PsiFile file = element.getContainingFile(); if (file == null) return; PsiElement firstLine = file.getFirstChild(); if (firstLine instanceof PsiComment && firstLine.getText().startsWith("#!")) { firstLine = firstLine.getNextSibling(); } final LanguageLevel languageLevel = LanguageLevel.forElement(file); final String commentText = String.format(PyEncodingUtil.ENCODING_FORMAT_PATTERN[myEncodingFormatIndex], myDefaultEncoding); final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project); PsiComment encodingComment = elementGenerator.createFromText(languageLevel, PsiComment.class, commentText); encodingComment = (PsiComment)file.addBefore(encodingComment, firstLine); final Editor editor = PythonUiService.getInstance().openTextEditor(project, element.getContainingFile().getVirtualFile()); if (encodingComment.getNextSibling() == null || !encodingComment.getNextSibling().textContains('\n')) { file.addAfter(elementGenerator.createFromText(languageLevel, PsiWhiteSpace.class, "\n"), encodingComment); } final Document document = editor.getDocument(); final int insertedLineNumber = document.getLineNumber(encodingComment.getTextOffset()); editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(insertedLineNumber + 1, 0)); }