Search in sources :

Example 1 with PyDocstringGenerator

use of com.jetbrains.python.documentation.docstrings.PyDocstringGenerator in project intellij-community by JetBrains.

the class PyGenerateDocstringIntention method generateDocstring.

public static void generateDocstring(@NotNull PyDocStringOwner docStringOwner, @Nullable Editor editor) {
    if (!DocStringUtil.ensureNotPlainDocstringFormat(docStringOwner)) {
        return;
    }
    final PyDocstringGenerator docstringGenerator = PyDocstringGenerator.forDocStringOwner(docStringOwner).withInferredParameters(false).addFirstEmptyLine();
    final PyStringLiteralExpression updated = docstringGenerator.buildAndInsert().getDocStringExpression();
    if (updated != null && editor != null) {
        final int offset = updated.getTextOffset();
        editor.getCaretModel().moveToOffset(offset);
        editor.getCaretModel().moveCaretRelatively(0, 1, false, false, false);
    }
}
Also used : PyDocstringGenerator(com.jetbrains.python.documentation.docstrings.PyDocstringGenerator)

Example 2 with PyDocstringGenerator

use of com.jetbrains.python.documentation.docstrings.PyDocstringGenerator in project intellij-community by JetBrains.

the class PyChangeSignatureUsageProcessor method updateParameterList.

private static void updateParameterList(PyChangeInfo changeInfo, PyFunction baseMethod) {
    final PsiElement parameterList = baseMethod.getParameterList();
    final PyParameterInfo[] parameters = changeInfo.getNewParameters();
    final StringBuilder builder = new StringBuilder("def foo(");
    final PyStringLiteralExpression docstring = baseMethod.getDocStringExpression();
    final PyParameter[] oldParameters = baseMethod.getParameterList().getParameters();
    final PyElementGenerator generator = PyElementGenerator.getInstance(baseMethod.getProject());
    final PyDocstringGenerator docStringGenerator = PyDocstringGenerator.forDocStringOwner(baseMethod);
    boolean newParameterInDocString = false;
    for (int i = 0; i < parameters.length; ++i) {
        final PyParameterInfo info = parameters[i];
        final int oldIndex = info.getOldIndex();
        if (i != 0 && oldIndex < oldParameters.length) {
            builder.append(", ");
        }
        if (docstring != null && oldIndex < 0) {
            newParameterInDocString = true;
            docStringGenerator.withParam(info.getName());
        }
        if (oldIndex < oldParameters.length) {
            builder.append(info.getName());
        }
        if (oldIndex >= 0 && oldIndex < oldParameters.length) {
            final PyParameter parameter = oldParameters[oldIndex];
            if (parameter instanceof PyNamedParameter) {
                final PyAnnotation annotation = ((PyNamedParameter) parameter).getAnnotation();
                if (annotation != null) {
                    builder.append(annotation.getText());
                }
            }
        }
        final String defaultValue = info.getDefaultValue();
        if (defaultValue != null && info.getDefaultInSignature() && StringUtil.isNotEmpty(defaultValue)) {
            builder.append(" = ").append(defaultValue);
        }
    }
    builder.append("): pass");
    if (newParameterInDocString) {
        docStringGenerator.buildAndInsert();
    }
    final PyParameterList newParameterList = generator.createFromText(LanguageLevel.forElement(baseMethod), PyFunction.class, builder.toString()).getParameterList();
    parameterList.replace(newParameterList);
}
Also used : PyDocstringGenerator(com.jetbrains.python.documentation.docstrings.PyDocstringGenerator) PsiElement(com.intellij.psi.PsiElement)

Example 3 with PyDocstringGenerator

use of com.jetbrains.python.documentation.docstrings.PyDocstringGenerator in project intellij-community by JetBrains.

the class DocstringQuickFix method applyFix.

public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PyDocStringOwner docStringOwner = PsiTreeUtil.getParentOfType(descriptor.getPsiElement(), PyDocStringOwner.class);
    if (docStringOwner == null)
        return;
    PyStringLiteralExpression docStringExpression = docStringOwner.getDocStringExpression();
    if (docStringExpression == null && myMissingParam == null && myUnexpectedParamName == null) {
        addEmptyDocstring(docStringOwner);
        return;
    }
    if (docStringExpression != null) {
        final PyDocstringGenerator generator = PyDocstringGenerator.forDocStringOwner(docStringOwner);
        if (myMissingParam != null) {
            final PyNamedParameter param = myMissingParam.getElement();
            if (param != null) {
                generator.withParam(param);
            }
        } else if (myUnexpectedParamName != null) {
            generator.withoutParam(myUnexpectedParamName.trim());
        }
        generator.buildAndInsert();
    }
}
Also used : PyDocstringGenerator(com.jetbrains.python.documentation.docstrings.PyDocstringGenerator)

Example 4 with PyDocstringGenerator

use of com.jetbrains.python.documentation.docstrings.PyDocstringGenerator in project intellij-community by JetBrains.

the class PyChangeSignatureUsageProcessor method fixDoc.

private static void fixDoc(PyChangeInfo changeInfo, @NotNull PyFunction function) {
    PyStringLiteralExpression docStringExpression = function.getDocStringExpression();
    if (docStringExpression == null)
        return;
    final PyParameterInfo[] parameters = changeInfo.getNewParameters();
    Set<String> names = new HashSet<>();
    for (PyParameterInfo info : parameters) {
        names.add(StringUtil.trimLeading(info.getName(), '*').trim());
    }
    final Module module = ModuleUtilCore.findModuleForPsiElement(function);
    if (module == null)
        return;
    final PyDocstringGenerator generator = PyDocstringGenerator.forDocStringOwner(function);
    for (PyParameter p : function.getParameterList().getParameters()) {
        final String paramName = p.getName();
        if (!names.contains(paramName) && paramName != null) {
            generator.withoutParam(paramName);
        }
    }
    generator.buildAndInsert();
}
Also used : Module(com.intellij.openapi.module.Module) PyDocstringGenerator(com.jetbrains.python.documentation.docstrings.PyDocstringGenerator) HashSet(com.intellij.util.containers.HashSet)

Example 5 with PyDocstringGenerator

use of com.jetbrains.python.documentation.docstrings.PyDocstringGenerator in project intellij-community by JetBrains.

the class SpecifyTypeInDocstringIntention method generateDocstring.

private static void generateDocstring(@Nullable PyNamedParameter param, @NotNull PyFunction pyFunction) {
    if (!DocStringUtil.ensureNotPlainDocstringFormat(pyFunction)) {
        return;
    }
    final PyDocstringGenerator docstringGenerator = PyDocstringGenerator.forDocStringOwner(pyFunction);
    String type = PyNames.OBJECT;
    if (param != null) {
        final String paramName = StringUtil.notNullize(param.getName());
        final PySignature signature = PySignatureCacheManager.getInstance(pyFunction.getProject()).findSignature(pyFunction);
        if (signature != null) {
            type = ObjectUtils.chooseNotNull(signature.getArgTypeQualifiedName(paramName), type);
        }
        docstringGenerator.withParamTypedByName(param, type);
    } else {
        final PySignature signature = PySignatureCacheManager.getInstance(pyFunction.getProject()).findSignature(pyFunction);
        if (signature != null) {
            type = ObjectUtils.chooseNotNull(signature.getReturnTypeQualifiedName(), type);
        }
        docstringGenerator.withReturnValue(type);
    }
    WriteAction.run(() -> {
        docstringGenerator.addFirstEmptyLine().buildAndInsert();
        docstringGenerator.startTemplate();
    });
}
Also used : PySignature(com.jetbrains.python.debugger.PySignature) PyDocstringGenerator(com.jetbrains.python.documentation.docstrings.PyDocstringGenerator)

Aggregations

PyDocstringGenerator (com.jetbrains.python.documentation.docstrings.PyDocstringGenerator)5 Module (com.intellij.openapi.module.Module)1 PsiElement (com.intellij.psi.PsiElement)1 HashSet (com.intellij.util.containers.HashSet)1 PySignature (com.jetbrains.python.debugger.PySignature)1