void (@NotNull Project project, @NotNull ProblemDescriptor descriptor) { final PyReferenceExpression referenceExpr = as(descriptor.getPsiElement(), PyReferenceExpression.class); final PyCallExpression callExpr = PsiTreeUtil.getParentOfType(referenceExpr, PyCallExpression.class); if (referenceExpr == null || callExpr == null) { return; } final PyFunctionBuilder functionBuilder = new PyFunctionBuilder(referenceExpr.getText(), callExpr); // if function is actually an argument of a call, don't use other arguments of the call to create parameter list of new function final PyArgumentList argumentList = callExpr.getArgumentList(); if (argumentList != null && !PsiTreeUtil.isAncestor(argumentList, referenceExpr, false)) { for (PyExpression param : argumentList.getArguments()) { if (param instanceof PyKeywordArgument) { functionBuilder.parameter(((PyKeywordArgument)param).getKeyword()); } else if (param instanceof PyReferenceExpression refex) { functionBuilder.parameter(refex.getReferencedName()); } else { functionBuilder.parameter("param"); } } } else { functionBuilder.parameter("args"); } PyFunction function = functionBuilder.buildFunction(); final InjectedLanguageManager instance = InjectedLanguageManager.getInstance(project); final PsiLanguageInjectionHost host = instance.getInjectionHost(callExpr); final PsiElement insertAnchor = host != null ? host : callExpr; final PyFunction parentFunction = PsiTreeUtil.getTopmostParentOfType(insertAnchor, PyFunction.class); if (parentFunction != null) { final PyClass parentClass = PsiTreeUtil.getTopmostParentOfType(parentFunction, PyClass.class); if (parentClass != null) { final PsiElement parent = parentClass.getParent(); function = (PyFunction)parent.addBefore(function, parentClass); } else { final PsiElement parent = parentFunction.getParent(); function = (PyFunction)parent.addBefore(function, parentFunction); } } else { final PyStatement statement = PsiTreeUtil.getTopmostParentOfType(insertAnchor, PyStatement.class); if (statement != null) { final PsiElement parent = statement.getParent(); if (parent != null) { function = (PyFunction)parent.addBefore(function, statement); } } } function = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(function); final TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(function); ParamHelper.walkDownParamArray( function.getParameterList().getParameters(), new ParamHelper.ParamVisitor() { @Override public void visitNamedParameter(PyNamedParameter param, boolean first, boolean last) { builder.replaceElement(param, param.getName()); } } ); builder.replaceElement(function.getStatementList(), PyNames.PASS); PythonTemplateRunner.runTemplateInSelectedEditor(project, insertAnchor, builder); }