Search in sources :

Example 1 with PyTargetExpression

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

the class PyUserSkeletonsClassMembersProvider method findClassMember.

public static PsiElement findClassMember(@NotNull PyClass cls, @NotNull String name, boolean isDefinition) {
    final PyFunction function = cls.findMethodByName(name, false, null);
    if (function != null) {
        final PyUtil.MethodFlags methodFlags = PyUtil.MethodFlags.of(function);
        final boolean instanceMethod = methodFlags == null || methodFlags.isInstanceMethod();
        if (isDefinition ^ instanceMethod) {
            return function;
        }
    }
    if (!isDefinition) {
        final PyTargetExpression instanceAttribute = cls.findInstanceAttribute(name, false);
        if (instanceAttribute != null) {
            return instanceAttribute;
        }
    }
    final PyTargetExpression classAttribute = cls.findClassAttribute(name, false, null);
    if (classAttribute != null) {
        return classAttribute;
    }
    return null;
}
Also used : PyTargetExpression(com.jetbrains.python.psi.PyTargetExpression) PyFunction(com.jetbrains.python.psi.PyFunction) PyUtil(com.jetbrains.python.psi.PyUtil)

Example 2 with PyTargetExpression

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

the class PyUserSkeletonsClassMembersProvider method getClassMembers.

public static Collection<PyCustomMember> getClassMembers(@NotNull PyClass cls, boolean isDefinition) {
    final List<PyCustomMember> result = new ArrayList<>();
    for (PyFunction function : cls.getMethods()) {
        final String name = function.getName();
        final PyUtil.MethodFlags methodFlags = PyUtil.MethodFlags.of(function);
        final boolean instanceMethod = methodFlags == null || methodFlags.isInstanceMethod();
        if (name != null && (isDefinition ^ instanceMethod)) {
            result.add(new PyCustomMember(name, function));
        }
    }
    if (!isDefinition) {
        for (PyTargetExpression attribute : cls.getInstanceAttributes()) {
            final String name = attribute.getName();
            if (name != null) {
                result.add(new PyCustomMember(name, attribute));
            }
        }
    }
    for (PyTargetExpression attribute : cls.getClassAttributes()) {
        final String name = attribute.getName();
        if (name != null) {
            result.add(new PyCustomMember(name, attribute));
        }
    }
    return result;
}
Also used : PyTargetExpression(com.jetbrains.python.psi.PyTargetExpression) PyFunction(com.jetbrains.python.psi.PyFunction) PyCustomMember(com.jetbrains.python.codeInsight.PyCustomMember) ArrayList(java.util.ArrayList) PyUtil(com.jetbrains.python.psi.PyUtil)

Example 3 with PyTargetExpression

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

the class PyClassRefactoringTest method findField.

private PyElement findField(final String className, final String memberName) {
    final PyClass aClass = findClass(className);
    final PyTargetExpression attribute = aClass.findClassAttribute(memberName, false, null);
    if (attribute != null) {
        return attribute;
    }
    return aClass.findInstanceAttribute(memberName, false);
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyTargetExpression(com.jetbrains.python.psi.PyTargetExpression)

Example 4 with PyTargetExpression

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

the class PyiResolveTest method testModuleAttributePyiOverPy.

// PY-21231
public void testModuleAttributePyiOverPy() {
    final PsiElement result = doResolve();
    assertInstanceOf(result, PyTargetExpression.class);
    final PyTargetExpression target = (PyTargetExpression) result;
    assertEquals("foo", target.getName());
    final PsiFile containingFile = target.getContainingFile();
    assertInstanceOf(containingFile, PyiFile.class);
}
Also used : PyTargetExpression(com.jetbrains.python.psi.PyTargetExpression) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement)

Example 5 with PyTargetExpression

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

the class PyLineMarkerProvider method getAttributeMarker.

@Nullable
private static LineMarkerInfo<PsiElement> getAttributeMarker(PyTargetExpression element) {
    final String name = element.getReferencedName();
    if (name == null) {
        return null;
    }
    PyClass containingClass = PsiTreeUtil.getParentOfType(element, PyClass.class);
    if (containingClass == null)
        return null;
    for (PyClass ancestor : containingClass.getAncestorClasses(TypeEvalContext.codeAnalysis(element.getProject(), element.getContainingFile()))) {
        final PyTargetExpression ancestorAttr = ancestor.findClassAttribute(name, false, null);
        if (ancestorAttr != null) {
            return new LineMarkerInfo<PsiElement>(element, element.getTextRange().getStartOffset(), AllIcons.Gutter.OverridingMethod, Pass.LINE_MARKERS, new TooltipProvider("Overrides attribute in " + ancestor.getName()), ourSuperAttributeNavigator);
        }
    }
    return null;
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) LineMarkerInfo(com.intellij.codeInsight.daemon.LineMarkerInfo) PyTargetExpression(com.jetbrains.python.psi.PyTargetExpression) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PyTargetExpression (com.jetbrains.python.psi.PyTargetExpression)16 PsiElement (com.intellij.psi.PsiElement)11 PyExpression (com.jetbrains.python.psi.PyExpression)7 Pair (com.intellij.openapi.util.Pair)6 PyAssignmentStatement (com.jetbrains.python.psi.PyAssignmentStatement)6 PyClass (com.jetbrains.python.psi.PyClass)3 PyFunction (com.jetbrains.python.psi.PyFunction)3 PyReferenceExpression (com.jetbrains.python.psi.PyReferenceExpression)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PsiReference (com.intellij.psi.PsiReference)2 PyUtil (com.jetbrains.python.psi.PyUtil)2 Nullable (org.jetbrains.annotations.Nullable)2 LineMarkerInfo (com.intellij.codeInsight.daemon.LineMarkerInfo)1 Editor (com.intellij.openapi.editor.Editor)1 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)1 PsiFile (com.intellij.psi.PsiFile)1 PsiNameIdentifierOwner (com.intellij.psi.PsiNameIdentifierOwner)1 RenameProcessor (com.intellij.refactoring.rename.RenameProcessor)1 VariableInplaceRenamer (com.intellij.refactoring.rename.inplace.VariableInplaceRenamer)1 PyCustomMember (com.jetbrains.python.codeInsight.PyCustomMember)1