void (@NotNull Project project, @NotNull ProblemDescriptor descriptor) { try { final PsiElement problemElement = descriptor.getPsiElement(); if (!(problemElement instanceof PyQualifiedExpression)) return; PyExpression qualifier = ((PyQualifiedExpression)problemElement).getQualifier(); if (qualifier == null) { final PyFromImportStatement fromImport = PsiTreeUtil.getParentOfType(problemElement, PyFromImportStatement.class); if (fromImport == null) return; qualifier = fromImport.getImportSource(); } if (qualifier == null) return; final PyType type = TypeEvalContext.userInitiated(problemElement.getProject(), problemElement.getContainingFile()).getType(qualifier); if (!(type instanceof PyModuleType)) return; final PyFile file = ((PyModuleType)type).getModule(); sure(file); sure(FileModificationService.getInstance().preparePsiElementForWrite(file)); // try to at least match parameter count // TODO: get parameter style from code style PyFunctionBuilder builder = new PyFunctionBuilder(myIdentifier, problemElement); PsiElement problemParent = problemElement.getParent(); if (problemParent instanceof PyCallExpression) { PyArgumentList arglist = ((PyCallExpression)problemParent).getArgumentList(); if (arglist == null) return; final PyExpression[] args = arglist.getArguments(); for (PyExpression arg : args) { if (arg instanceof PyKeywordArgument) { // foo(bar) -> def foo(bar_1) builder.parameter(((PyKeywordArgument)arg).getKeyword()); } else if (arg instanceof PyReferenceExpression refex) { builder.parameter(refex.getReferencedName()); } else { // use a boring name builder.parameter("param"); } } } else if (problemParent != null) { for (PyInspectionExtension extension : PyInspectionExtension.EP_NAME.getExtensionList()) { List<String> params = extension.getFunctionParametersFromUsage(problemElement); if (params != null) { for (String param : params) { builder.parameter(param); } break; } } } // else: no arglist, use empty args WriteAction.run(() -> { PyFunction function = builder.buildFunction(); // add to the bottom function = (PyFunction)file.add(function); showTemplateBuilder(function, file); }); } catch (IncorrectOperationException ignored) { // we failed. tell about this PythonUiService.getInstance().showBalloonError(project, PyPsiBundle.message("QFIX.failed.to.add.function")); } }