Search in sources :

Example 1 with PyCustomMember

use of com.jetbrains.python.codeInsight.PyCustomMember 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 2 with PyCustomMember

use of com.jetbrains.python.codeInsight.PyCustomMember in project intellij-community by JetBrains.

the class PyStdlibModuleMembersProvider method getMembersByQName.

@Override
protected Collection<PyCustomMember> getMembersByQName(PyFile module, String qName) {
    if (qName.equals("os")) {
        final List<PyCustomMember> results = new ArrayList<>();
        PsiElement path = null;
        if (module != null) {
            final String pathModuleName = SystemInfo.isWindows ? "ntpath" : "posixpath";
            path = ResolveImportUtil.resolveModuleInRoots(QualifiedName.fromDottedString(pathModuleName), module);
        }
        results.add(new PyCustomMember("path", path));
        return results;
    }
    return Collections.emptyList();
}
Also used : PyCustomMember(com.jetbrains.python.codeInsight.PyCustomMember) ArrayList(java.util.ArrayList) PsiElement(com.intellij.psi.PsiElement)

Example 3 with PyCustomMember

use of com.jetbrains.python.codeInsight.PyCustomMember in project intellij-community by JetBrains.

the class NumpyModuleMembersProvider method addTestingModule.

private static void addTestingModule(PyFile module, List<PyCustomMember> members) {
    final PyQualifiedNameResolveContext context = PyResolveImportUtil.fromFoothold(module).copyWithPlainDirectories();
    final PsiElement resolved = PyResolveImportUtil.resolveQualifiedName(QualifiedName.fromDottedString("numpy.testing"), context).stream().findFirst().orElse(null);
    final PsiElement testingModule = PyUtil.turnDirIntoInit(resolved);
    members.add(new PyCustomMember("testing", testingModule));
}
Also used : PyQualifiedNameResolveContext(com.jetbrains.python.psi.resolve.PyQualifiedNameResolveContext) PyCustomMember(com.jetbrains.python.codeInsight.PyCustomMember) PsiElement(com.intellij.psi.PsiElement)

Example 4 with PyCustomMember

use of com.jetbrains.python.codeInsight.PyCustomMember in project intellij-community by JetBrains.

the class PyModuleType method getCompletionVariantsAsLookupElements.

public List<LookupElement> getCompletionVariantsAsLookupElements(PsiElement location, ProcessingContext context, boolean wantAllSubmodules, boolean suppressParentheses) {
    List<LookupElement> result = new ArrayList<>();
    Set<String> namesAlready = context.get(CTX_NAMES);
    PointInImport point = ResolveImportUtil.getPointInImport(location);
    for (PyModuleMembersProvider provider : Extensions.getExtensions(PyModuleMembersProvider.EP_NAME)) {
        for (PyCustomMember member : provider.getMembers(myModule, point)) {
            final String name = member.getName();
            if (namesAlready != null) {
                namesAlready.add(name);
            }
            if (PyUtil.isClassPrivateName(name)) {
                continue;
            }
            final CompletionVariantsProcessor processor = createCompletionVariantsProcessor(location, suppressParentheses, point);
            final PsiElement resolved = member.resolve(location);
            if (resolved != null) {
                processor.execute(resolved, ResolveState.initial());
                final List<LookupElement> lookupList = processor.getResultList();
                if (!lookupList.isEmpty()) {
                    final LookupElement element = lookupList.get(0);
                    if (name.equals(element.getLookupString())) {
                        result.add(element);
                        continue;
                    }
                }
            }
            result.add(LookupElementBuilder.create(name).withIcon(member.getIcon()).withTypeText(member.getShortType()));
        }
    }
    if (point == PointInImport.NONE || point == PointInImport.AS_NAME) {
        // when not imported from, add regular attributes
        final CompletionVariantsProcessor processor = createCompletionVariantsProcessor(location, suppressParentheses, point);
        myModule.processDeclarations(processor, ResolveState.initial(), null, location);
        if (namesAlready != null) {
            for (LookupElement le : processor.getResultList()) {
                String name = le.getLookupString();
                if (!namesAlready.contains(name)) {
                    result.add(le);
                    namesAlready.add(name);
                }
            }
        } else {
            result.addAll(processor.getResultList());
        }
    }
    if (PyUtil.isPackage(myModule)) {
        // our module is a dir, not a single file
        if (point == PointInImport.AS_MODULE || point == PointInImport.AS_NAME || wantAllSubmodules) {
            // when imported from somehow, add submodules
            result.addAll(getSubModuleVariants(myModule.getContainingDirectory(), location, namesAlready));
        } else {
            result.addAll(collectImportedSubmodulesAsLookupElements(myModule, location, namesAlready));
        }
    }
    return result;
}
Also used : PyCustomMember(com.jetbrains.python.codeInsight.PyCustomMember) ArrayList(java.util.ArrayList) LookupElement(com.intellij.codeInsight.lookup.LookupElement)

Example 5 with PyCustomMember

use of com.jetbrains.python.codeInsight.PyCustomMember in project intellij-community by JetBrains.

the class PyClassTypeImpl method getCompletionVariants.

public Object[] getCompletionVariants(String prefix, PsiElement location, ProcessingContext context) {
    Set<PyClassType> visited = context.get(CTX_VISITED);
    if (visited == null) {
        visited = new HashSet<>();
        context.put(CTX_VISITED, visited);
    }
    if (visited.contains(this)) {
        return ArrayUtil.EMPTY_OBJECT_ARRAY;
    }
    visited.add(this);
    Set<String> namesAlready = context.get(CTX_NAMES);
    if (namesAlready == null) {
        namesAlready = new HashSet<>();
    }
    List<Object> ret = new ArrayList<>();
    boolean suppressParentheses = context.get(CTX_SUPPRESS_PARENTHESES) != null;
    addOwnClassMembers(location, namesAlready, suppressParentheses, ret, prefix);
    PsiFile origin = (location != null) ? CompletionUtil.getOriginalOrSelf(location).getContainingFile() : null;
    final TypeEvalContext typeEvalContext = TypeEvalContext.codeCompletion(myClass.getProject(), origin);
    addInheritedMembers(prefix, location, namesAlready, context, ret, typeEvalContext);
    // from providers
    for (final PyClassMembersProvider provider : Extensions.getExtensions(PyClassMembersProvider.EP_NAME)) {
        for (final PyCustomMember member : provider.getMembers(this, location, typeEvalContext)) {
            final String name = member.getName();
            if (!namesAlready.contains(name)) {
                ret.add(PyCustomMemberUtils.toLookUpElement(member, getName()));
            }
        }
    }
    if (!myClass.isNewStyleClass(typeEvalContext)) {
        final PyClass instanceClass = as(resolveTopLevelMember(QualifiedName.fromDottedString(PyNames.TYPES_INSTANCE_TYPE), fromFoothold(myClass)), PyClass.class);
        if (instanceClass != null) {
            final PyClassTypeImpl instanceType = new PyClassTypeImpl(instanceClass, false);
            ret.addAll(Arrays.asList(instanceType.getCompletionVariants(prefix, location, context)));
        }
    }
    Collections.addAll(ret, getMetaClassCompletionVariants(prefix, location, context, typeEvalContext));
    return ret.toArray();
}
Also used : PyCustomMember(com.jetbrains.python.codeInsight.PyCustomMember) PsiFile(com.intellij.psi.PsiFile)

Aggregations

PyCustomMember (com.jetbrains.python.codeInsight.PyCustomMember)10 ArrayList (java.util.ArrayList)5 PsiElement (com.intellij.psi.PsiElement)4 NotNull (org.jetbrains.annotations.NotNull)3 PyFunction (com.jetbrains.python.psi.PyFunction)2 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 PsiFile (com.intellij.psi.PsiFile)1 PsiReference (com.intellij.psi.PsiReference)1 PyTargetExpression (com.jetbrains.python.psi.PyTargetExpression)1 PyUtil (com.jetbrains.python.psi.PyUtil)1 PyQualifiedNameResolveContext (com.jetbrains.python.psi.resolve.PyQualifiedNameResolveContext)1