Search in sources :

Example 1 with PyParameterList

use of com.jetbrains.python.psi.PyParameterList in project intellij-community by JetBrains.

the class AddSelfQuickFix method applyFix.

public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
    PsiElement element = descriptor.getPsiElement();
    if (element instanceof PyParameterList) {
        final PyParameterList parameterList = (PyParameterList) element;
        PyNamedParameter newParameter = PyElementGenerator.getInstance(project).createParameter(myParamName);
        parameterList.addParameter(newParameter);
    }
}
Also used : PyNamedParameter(com.jetbrains.python.psi.PyNamedParameter) PyParameterList(com.jetbrains.python.psi.PyParameterList) PsiElement(com.intellij.psi.PsiElement)

Example 2 with PyParameterList

use of com.jetbrains.python.psi.PyParameterList in project intellij-community by JetBrains.

the class PyDeclarationRangeHandler method getDeclarationRange.

@NotNull
@Override
public TextRange getDeclarationRange(@NotNull PsiElement container) {
    int start = container.getTextRange().getStartOffset();
    if (container instanceof PyFunction) {
        PyParameterList parameterList = ((PyFunction) container).getParameterList();
        return new TextRange(start, parameterList.getTextRange().getEndOffset());
    }
    if (container instanceof PyClass) {
        PyArgumentList argumentList = ((PyClass) container).getSuperClassExpressionList();
        if (argumentList != null) {
            return new TextRange(start, argumentList.getTextRange().getEndOffset());
        }
        ASTNode nameNode = ((PyClass) container).getNameNode();
        if (nameNode != null) {
            return new TextRange(start, nameNode.getStartOffset() + nameNode.getTextLength());
        }
    }
    return container.getTextRange();
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyArgumentList(com.jetbrains.python.psi.PyArgumentList) PyFunction(com.jetbrains.python.psi.PyFunction) ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) PyParameterList(com.jetbrains.python.psi.PyParameterList) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with PyParameterList

use of com.jetbrains.python.psi.PyParameterList in project intellij-community by JetBrains.

the class PyFunctionFixer method doApply.

@Override
public void doApply(@NotNull Editor editor, @NotNull PySmartEnterProcessor processor, @NotNull PyFunction function) throws IncorrectOperationException {
    final PsiElement colon = PyPsiUtils.getFirstChildOfType(function, PyTokenTypes.COLON);
    if (!isFakeFunction(function) && colon == null) {
        final PyParameterList parameterList = function.getParameterList();
        if (function.getNameNode() == null) {
            processor.registerUnresolvedError(parameterList.getTextOffset());
        }
        final int colonOffset;
        final PyAnnotation annotation = function.getAnnotation();
        if (annotation != null) {
            colonOffset = annotation.getTextRange().getEndOffset();
        } else {
            colonOffset = parameterList.getTextRange().getEndOffset();
        }
        editor.getDocument().insertString(colonOffset, ":");
    }
}
Also used : PyAnnotation(com.jetbrains.python.psi.PyAnnotation) PyParameterList(com.jetbrains.python.psi.PyParameterList) PsiElement(com.intellij.psi.PsiElement)

Example 4 with PyParameterList

use of com.jetbrains.python.psi.PyParameterList in project intellij-community by JetBrains.

the class UnresolvedReferenceAddParameterQuickFix method applyFix.

public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    final PsiElement element = descriptor.getPsiElement();
    final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
    final PyNamedParameter parameter = elementGenerator.createParameter(element.getText() + "=None");
    final PyFunction function = PsiTreeUtil.getParentOfType(element, PyFunction.class);
    if (function != null) {
        final PyParameterList parameterList = function.getParameterList();
        parameterList.addParameter(parameter);
        CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(parameterList);
        final TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(parameter);
        builder.replaceRange(TextRange.create(parameter.getTextLength() - 4, parameter.getTextLength()), "None");
        final VirtualFile virtualFile = function.getContainingFile().getVirtualFile();
        if (virtualFile == null)
            return;
        final Editor editor = FileEditorManager.getInstance(project).openTextEditor(new OpenFileDescriptor(project, virtualFile), true);
        if (editor == null)
            return;
        builder.run(editor, false);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PyFunction(com.jetbrains.python.psi.PyFunction) TemplateBuilder(com.intellij.codeInsight.template.TemplateBuilder) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator) PyNamedParameter(com.jetbrains.python.psi.PyNamedParameter) Editor(com.intellij.openapi.editor.Editor) PyParameterList(com.jetbrains.python.psi.PyParameterList) PsiElement(com.intellij.psi.PsiElement)

Example 5 with PyParameterList

use of com.jetbrains.python.psi.PyParameterList in project intellij-community by JetBrains.

the class PyJavaTypeProvider method getParameterType.

public Ref<PyType> getParameterType(@NotNull final PyNamedParameter param, @NotNull final PyFunction func, @NotNull TypeEvalContext context) {
    if (!(param.getParent() instanceof PyParameterList))
        return null;
    List<PyNamedParameter> params = ParamHelper.collectNamedParameters((PyParameterList) param.getParent());
    final int index = params.indexOf(param);
    if (index < 0)
        return null;
    final List<PyType> superMethodParameterTypes = new ArrayList<>();
    PySuperMethodsSearch.search(func, context).forEach(psiElement -> {
        if (psiElement instanceof PsiMethod) {
            final PsiMethod method = (PsiMethod) psiElement;
            final PsiParameter[] psiParameters = method.getParameterList().getParameters();
            int javaIndex = method.hasModifierProperty(PsiModifier.STATIC) ? index : index - 1;
            if (javaIndex < psiParameters.length) {
                PsiType paramType = psiParameters[javaIndex].getType();
                if (paramType instanceof PsiClassType) {
                    final PsiClass psiClass = ((PsiClassType) paramType).resolve();
                    if (psiClass != null) {
                        superMethodParameterTypes.add(new PyJavaClassType(psiClass, false));
                    }
                }
            }
        }
        return true;
    });
    if (superMethodParameterTypes.size() > 0) {
        final PyType type = superMethodParameterTypes.get(0);
        if (type != null) {
            return Ref.create(type);
        }
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) PyType(com.jetbrains.python.psi.types.PyType) PyNamedParameter(com.jetbrains.python.psi.PyNamedParameter) PyParameterList(com.jetbrains.python.psi.PyParameterList)

Aggregations

PyParameterList (com.jetbrains.python.psi.PyParameterList)5 PsiElement (com.intellij.psi.PsiElement)3 PyNamedParameter (com.jetbrains.python.psi.PyNamedParameter)3 PyFunction (com.jetbrains.python.psi.PyFunction)2 TemplateBuilder (com.intellij.codeInsight.template.TemplateBuilder)1 ASTNode (com.intellij.lang.ASTNode)1 Editor (com.intellij.openapi.editor.Editor)1 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)1 TextRange (com.intellij.openapi.util.TextRange)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PyAnnotation (com.jetbrains.python.psi.PyAnnotation)1 PyArgumentList (com.jetbrains.python.psi.PyArgumentList)1 PyClass (com.jetbrains.python.psi.PyClass)1 PyElementGenerator (com.jetbrains.python.psi.PyElementGenerator)1 PyType (com.jetbrains.python.psi.types.PyType)1 ArrayList (java.util.ArrayList)1 NotNull (org.jetbrains.annotations.NotNull)1