use of com.jetbrains.python.debugger.PySignature in project intellij-community by JetBrains.
the class SpecifyTypeInPy3AnnotationsIntention method parameterType.
static String parameterType(PyParameter parameter) {
String paramType = PyNames.OBJECT;
PyFunction function = PsiTreeUtil.getParentOfType(parameter, PyFunction.class);
if (function != null) {
final PySignature signature = PySignatureCacheManager.getInstance(parameter.getProject()).findSignature(function);
String parameterName = parameter.getName();
if (signature != null && parameterName != null) {
paramType = ObjectUtils.chooseNotNull(signature.getArgTypeQualifiedName(parameterName), paramType);
}
}
return paramType;
}
use of com.jetbrains.python.debugger.PySignature in project intellij-community by JetBrains.
the class SpecifyTypeInPy3AnnotationsIntention method returnType.
static String returnType(@NotNull PyFunction function) {
String returnType = PyNames.OBJECT;
final PySignature signature = PySignatureCacheManager.getInstance(function.getProject()).findSignature(function);
if (signature != null) {
returnType = ObjectUtils.chooseNotNull(signature.getReturnTypeQualifiedName(), returnType);
}
return returnType;
}
use of com.jetbrains.python.debugger.PySignature in project intellij-community by JetBrains.
the class PyDocstringGenerator method prepareParameters.
/**
* Populate parameters for function if nothing was specified.
* Order parameters, remove duplicates and merge parameters with and without type according to docstring format.
*/
private void prepareParameters() {
if (myParametersPrepared) {
return;
}
final Set<Pair<String, Boolean>> withoutType = Sets.newHashSet();
final Map<Pair<String, Boolean>, String> paramTypes = Maps.newHashMap();
for (DocstringParam param : myAddedParams) {
if (param.getType() == null) {
withoutType.add(Pair.create(param.getName(), param.isReturnValue()));
} else {
// leave only the last type for parameter
paramTypes.put(Pair.create(param.getName(), param.isReturnValue()), param.getType());
}
}
// Sanitize parameters
PySignature signature = null;
if (myDocStringOwner instanceof PyFunction && myUseTypesFromDebuggerSignature) {
signature = PySignatureCacheManager.getInstance(myDocStringOwner.getProject()).findSignature((PyFunction) myDocStringOwner);
}
final DocStringFormat format = myDocStringFormat;
final ArrayList<DocstringParam> filtered = Lists.newArrayList();
final Set<Pair<String, Boolean>> processed = Sets.newHashSet();
for (DocstringParam param : myAddedParams) {
final Pair<String, Boolean> paramCoordinates = Pair.create(param.getName(), param.isReturnValue());
if (processed.contains(paramCoordinates)) {
continue;
}
if (param.getType() == null) {
String type = paramTypes.get(paramCoordinates);
if (type == null && PyCodeInsightSettings.getInstance().INSERT_TYPE_DOCSTUB) {
if (signature != null) {
type = StringUtil.notNullize(param.isReturnValue() ? signature.getReturnTypeQualifiedName() : signature.getArgTypeQualifiedName(param.getName()));
} else {
type = "";
}
}
if (type != null) {
// if both declaration with type and without it are requested, we should filter out duplicates
if (format == DocStringFormat.GOOGLE || format == DocStringFormat.NUMPY) {
filtered.add(new DocstringParam(param.getName(), type, param.isReturnValue()));
} else {
// In reST and Epydoc for each parameter add two tags, e.g. in reST (Sphinx)
// :param foo:
// :type foo:
filtered.add(param);
filtered.add(new DocstringParam(param.getName(), type, param.isReturnValue()));
}
} else {
// no type was given and it's not required by settings
filtered.add(param);
}
} else if (!withoutType.contains(paramCoordinates)) {
filtered.add(param);
}
processed.add(paramCoordinates);
}
myAddedParams.clear();
myAddedParams.addAll(filtered);
myParametersPrepared = true;
}
use of com.jetbrains.python.debugger.PySignature 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