use of com.jetbrains.python.psi.PyParameter in project intellij-community by JetBrains.
the class PyRemoveParameterQuickFix method applyFix.
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final PsiElement parameter = descriptor.getPsiElement();
assert parameter instanceof PyParameter;
final PyFunction function = PsiTreeUtil.getParentOfType(parameter, PyFunction.class);
if (function != null) {
final PyResolveContext resolveContext = PyResolveContext.noImplicits();
StreamEx.of(PyRefactoringUtil.findUsages(function, false)).map(UsageInfo::getElement).nonNull().map(PsiElement::getParent).select(PyCallExpression.class).flatMap(callExpression -> callExpression.multiMapArguments(resolveContext).stream()).flatMap(mapping -> mapping.getMappedParameters().entrySet().stream()).filter(entry -> entry.getValue() == parameter).forEach(entry -> entry.getKey().delete());
final PyStringLiteralExpression docStringExpression = function.getDocStringExpression();
final String parameterName = ((PyParameter) parameter).getName();
if (docStringExpression != null && parameterName != null) {
PyDocstringGenerator.forDocStringOwner(function).withoutParam(parameterName).buildAndInsert();
}
}
parameter.delete();
}
use of com.jetbrains.python.psi.PyParameter in project intellij-community by JetBrains.
the class PyMethodDescriptor method getParameters.
@Override
public List<PyParameterInfo> getParameters() {
List<PyParameterInfo> parameterInfos = new ArrayList<>();
PyParameter[] parameters = myFunction.getParameterList().getParameters();
for (int i = 0; i < parameters.length; i++) {
PyParameter parameter = parameters[i];
final PyExpression defaultValue = parameter.getDefaultValue();
final String name;
if (parameter instanceof PyNamedParameter) {
if (((PyNamedParameter) parameter).isPositionalContainer()) {
name = "*" + parameter.getName();
} else if (((PyNamedParameter) parameter).isKeywordContainer()) {
name = "**" + parameter.getName();
} else {
name = parameter.getName();
}
} else {
name = parameter.getText();
}
parameterInfos.add(new PyParameterInfo(i, name, defaultValue == null ? null : defaultValue.getText(), defaultValue != null && !StringUtil.isEmptyOrSpaces(defaultValue.getText())));
}
return parameterInfos;
}
use of com.jetbrains.python.psi.PyParameter in project intellij-community by JetBrains.
the class PyTargetElementEvaluator method getElementByReference.
@Nullable
@Override
public PsiElement getElementByReference(@NotNull PsiReference ref, int flags) {
if ((flags & TargetElementUtil.ELEMENT_NAME_ACCEPTED) == 0) {
return null;
}
final PsiElement element = ref.getElement();
PsiElement result = ref.resolve();
Set<PsiElement> visited = new HashSet<>();
visited.add(result);
while (result instanceof PyReferenceOwner && (result instanceof PyReferenceExpression || result instanceof PyTargetExpression)) {
PsiElement nextResult = ((PyReferenceOwner) result).getReference(PyResolveContext.noImplicits()).resolve();
if (nextResult != null && !visited.contains(nextResult) && PsiTreeUtil.getParentOfType(element, ScopeOwner.class) == PsiTreeUtil.getParentOfType(result, ScopeOwner.class) && (nextResult instanceof PyReferenceExpression || nextResult instanceof PyTargetExpression || nextResult instanceof PyParameter)) {
visited.add(nextResult);
result = nextResult;
} else {
break;
}
}
return result;
}
Aggregations