void (Project project, PyFunction function) { StringBuilder replacementTextBuilder = new StringBuilder("# type: ("); PyParameter[] params = function.getParameterList().getParameters(); List<Pair<Integer, String>> templates = new ArrayList<>(); for (int i = 0; i < params.length; i++) { if (!params[i].isSelf()) { String type = SpecifyTypeInPy3AnnotationsIntention.parameterType(params[i]); templates.add(Pair.create(replacementTextBuilder.length(), type)); replacementTextBuilder.append(type); if (i < params.length - 1) { replacementTextBuilder.append(", "); } } } replacementTextBuilder.append(") -> "); String returnType = SpecifyTypeInPy3AnnotationsIntention.returnType(function); templates.add(Pair.create(replacementTextBuilder.length(), returnType)); replacementTextBuilder.append(returnType); final PyStatementList statements = function.getStatementList(); final String indentation = PyIndentUtil.getElementIndent(statements); replacementTextBuilder.insert(0, indentation); replacementTextBuilder.insert(0, "\n"); final PsiDocumentManager manager = PsiDocumentManager.getInstance(project); final Document document = manager.getDocument(function.getContainingFile()); if (document != null) { final PsiElement beforeStatements = statements.getPrevSibling(); int offset = beforeStatements.getTextRange().getStartOffset(); if (":".equals(beforeStatements.getText())) { offset += 1; } try { document.insertString(offset, replacementTextBuilder.toString()); } finally { manager.commitDocument(document); } PsiElement element = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(function); while (element != null && !element.getText().contains(replacementTextBuilder.toString())) { element = element.getParent(); } if (element != null) { final TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(element); for (Pair<Integer, String> template : templates) { builder.replaceRange(TextRange.from( offset - element.getTextRange().getStartOffset() + replacementTextBuilder.toString().indexOf('#') + template.first, template.second.length()), template.second); } startTemplate(project, element, builder); } } }