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);
}
}
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);
}
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();
}
}
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();
}
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();
});
}
Aggregations