Search in sources :

Example 1 with PyNamedParameter

use of com.jetbrains.python.psi.PyNamedParameter 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 PyNamedParameter

use of com.jetbrains.python.psi.PyNamedParameter 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 3 with PyNamedParameter

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

the class PyRenameElementQuickFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PsiElement element = descriptor.getPsiElement();
    if (element instanceof PyReferenceExpression) {
        final PsiReference reference = element.getReference();
        if (reference == null)
            return;
        element = reference.resolve();
    }
    final PsiNameIdentifierOwner nameOwner = element instanceof PsiNameIdentifierOwner ? (PsiNameIdentifierOwner) element : PsiTreeUtil.getParentOfType(element, PsiNameIdentifierOwner.class, true);
    if (nameOwner != null) {
        final VirtualFile virtualFile = nameOwner.getContainingFile().getVirtualFile();
        if (virtualFile != null) {
            final Editor editor = FileEditorManager.getInstance(project).openTextEditor(new OpenFileDescriptor(project, virtualFile), true);
            if (ApplicationManager.getApplication().isUnitTestMode()) {
                renameInUnitTestMode(project, nameOwner, editor);
            } else {
                if (checkLocalScope(element) != null && (nameOwner instanceof PyNamedParameter || nameOwner instanceof PyTargetExpression)) {
                    new VariableInplaceRenamer(nameOwner, editor).performInplaceRename();
                } else {
                    PsiElementRenameHandler.invoke(nameOwner, project, ScopeUtil.getScopeOwner(nameOwner), editor);
                }
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiNameIdentifierOwner(com.intellij.psi.PsiNameIdentifierOwner) PyTargetExpression(com.jetbrains.python.psi.PyTargetExpression) PsiReference(com.intellij.psi.PsiReference) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) PyNamedParameter(com.jetbrains.python.psi.PyNamedParameter) Editor(com.intellij.openapi.editor.Editor) VariableInplaceRenamer(com.intellij.refactoring.rename.inplace.VariableInplaceRenamer) PsiElement(com.intellij.psi.PsiElement) PyReferenceExpression(com.jetbrains.python.psi.PyReferenceExpression)

Example 4 with PyNamedParameter

use of com.jetbrains.python.psi.PyNamedParameter 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 5 with PyNamedParameter

use of com.jetbrains.python.psi.PyNamedParameter 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

PyNamedParameter (com.jetbrains.python.psi.PyNamedParameter)5 PsiElement (com.intellij.psi.PsiElement)3 PyParameterList (com.jetbrains.python.psi.PyParameterList)3 Editor (com.intellij.openapi.editor.Editor)2 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 ArrayList (java.util.ArrayList)2 TemplateBuilder (com.intellij.codeInsight.template.TemplateBuilder)1 PsiNameIdentifierOwner (com.intellij.psi.PsiNameIdentifierOwner)1 PsiReference (com.intellij.psi.PsiReference)1 VariableInplaceRenamer (com.intellij.refactoring.rename.inplace.VariableInplaceRenamer)1 PyElementGenerator (com.jetbrains.python.psi.PyElementGenerator)1 PyExpression (com.jetbrains.python.psi.PyExpression)1 PyFunction (com.jetbrains.python.psi.PyFunction)1 PyParameter (com.jetbrains.python.psi.PyParameter)1 PyReferenceExpression (com.jetbrains.python.psi.PyReferenceExpression)1 PyTargetExpression (com.jetbrains.python.psi.PyTargetExpression)1 PyType (com.jetbrains.python.psi.types.PyType)1