Search in sources :

Example 1 with PyParameter

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();
}
Also used : PyBundle(com.jetbrains.python.PyBundle) PyStringLiteralExpression(com.jetbrains.python.psi.PyStringLiteralExpression) UsageInfo(com.intellij.usageView.UsageInfo) PyParameter(com.jetbrains.python.psi.PyParameter) PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) PyRefactoringUtil(com.jetbrains.python.refactoring.PyRefactoringUtil) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) PyCallExpression(com.jetbrains.python.psi.PyCallExpression) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) StreamEx(one.util.streamex.StreamEx) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) PyDocstringGenerator(com.jetbrains.python.documentation.docstrings.PyDocstringGenerator) NotNull(org.jetbrains.annotations.NotNull) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) PyFunction(com.jetbrains.python.psi.PyFunction) PyFunction(com.jetbrains.python.psi.PyFunction) PyStringLiteralExpression(com.jetbrains.python.psi.PyStringLiteralExpression) PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) PsiElement(com.intellij.psi.PsiElement) PyParameter(com.jetbrains.python.psi.PyParameter)

Example 2 with PyParameter

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;
}
Also used : ArrayList(java.util.ArrayList) PyExpression(com.jetbrains.python.psi.PyExpression) PyNamedParameter(com.jetbrains.python.psi.PyNamedParameter) PyParameter(com.jetbrains.python.psi.PyParameter)

Example 3 with PyParameter

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;
}
Also used : PyReferenceOwner(com.jetbrains.python.psi.PyReferenceOwner) PyTargetExpression(com.jetbrains.python.psi.PyTargetExpression) ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) PsiElement(com.intellij.psi.PsiElement) HashSet(java.util.HashSet) PyReferenceExpression(com.jetbrains.python.psi.PyReferenceExpression) PyParameter(com.jetbrains.python.psi.PyParameter) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PyParameter (com.jetbrains.python.psi.PyParameter)3 PsiElement (com.intellij.psi.PsiElement)2 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)1 Project (com.intellij.openapi.project.Project)1 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)1 UsageInfo (com.intellij.usageView.UsageInfo)1 PyBundle (com.jetbrains.python.PyBundle)1 ScopeOwner (com.jetbrains.python.codeInsight.controlflow.ScopeOwner)1 PyDocstringGenerator (com.jetbrains.python.documentation.docstrings.PyDocstringGenerator)1 PyCallExpression (com.jetbrains.python.psi.PyCallExpression)1 PyExpression (com.jetbrains.python.psi.PyExpression)1 PyFunction (com.jetbrains.python.psi.PyFunction)1 PyNamedParameter (com.jetbrains.python.psi.PyNamedParameter)1 PyReferenceExpression (com.jetbrains.python.psi.PyReferenceExpression)1 PyReferenceOwner (com.jetbrains.python.psi.PyReferenceOwner)1 PyStringLiteralExpression (com.jetbrains.python.psi.PyStringLiteralExpression)1 PyTargetExpression (com.jetbrains.python.psi.PyTargetExpression)1 PyResolveContext (com.jetbrains.python.psi.resolve.PyResolveContext)1 PyRefactoringUtil (com.jetbrains.python.refactoring.PyRefactoringUtil)1