Search in sources :

Example 1 with PyImportElement

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

the class PyDunderAllReference method getVariants.

@NotNull
@Override
public Object[] getVariants() {
    final List<LookupElement> result = new ArrayList<>();
    PyFile containingFile = (PyFile) getElement().getContainingFile().getOriginalFile();
    final List<String> dunderAll = containingFile.getDunderAll();
    final Set<String> seenNames = new HashSet<>();
    if (dunderAll != null) {
        seenNames.addAll(dunderAll);
    }
    containingFile.processDeclarations(new PsiScopeProcessor() {

        @Override
        public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) {
            if (element instanceof PsiNamedElement && !(element instanceof LightNamedElement)) {
                final String name = ((PsiNamedElement) element).getName();
                if (name != null && PyUtil.getInitialUnderscores(name) == 0 && !seenNames.contains(name)) {
                    seenNames.add(name);
                    result.add(LookupElementBuilder.createWithSmartPointer(name, element).withIcon(element.getIcon(0)));
                }
            } else if (element instanceof PyImportElement) {
                final String visibleName = ((PyImportElement) element).getVisibleName();
                if (visibleName != null && !seenNames.contains(visibleName)) {
                    seenNames.add(visibleName);
                    result.add(LookupElementBuilder.createWithSmartPointer(visibleName, element));
                }
            }
            return true;
        }

        @Override
        public <T> T getHint(@NotNull Key<T> hintKey) {
            return null;
        }

        @Override
        public void handleEvent(@NotNull Event event, @Nullable Object associated) {
        }
    }, ResolveState.initial(), null, containingFile);
    return ArrayUtil.toObjectArray(result);
}
Also used : PsiNamedElement(com.intellij.psi.PsiNamedElement) PyImportElement(com.jetbrains.python.psi.PyImportElement) ArrayList(java.util.ArrayList) PsiScopeProcessor(com.intellij.psi.scope.PsiScopeProcessor) PyFile(com.jetbrains.python.psi.PyFile) LookupElement(com.intellij.codeInsight.lookup.LookupElement) ResolveState(com.intellij.psi.ResolveState) LightNamedElement(com.jetbrains.python.psi.impl.LightNamedElement) PsiElement(com.intellij.psi.PsiElement) HashSet(java.util.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with PyImportElement

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

the class PyResolveProcessor method execute.

@Override
public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) {
    final PsiNamedElement namedElement = PyUtil.as(element, PsiNamedElement.class);
    if (namedElement != null && myName.equals(namedElement.getName())) {
        return tryAddResult(element, null);
    }
    final PyImportedNameDefiner importedNameDefiner = PyUtil.as(element, PyImportedNameDefiner.class);
    if (importedNameDefiner != null) {
        final List<RatedResolveResult> results = resolveInImportedNameDefiner(importedNameDefiner);
        if (!results.isEmpty()) {
            boolean cont = true;
            for (RatedResolveResult result : results) {
                final PsiElement resolved = result.getElement();
                if (resolved != null) {
                    cont = tryAddResult(resolved, importedNameDefiner) && cont;
                }
            }
            return cont;
        }
        final PyImportElement importElement = PyUtil.as(element, PyImportElement.class);
        if (importElement != null) {
            final String importName = importElement.getVisibleName();
            if (myName.equals(importName)) {
                return tryAddResult(null, importElement);
            }
        }
    }
    return myOwner == null || myOwner == ScopeUtil.getScopeOwner(element);
}
Also used : PsiNamedElement(com.intellij.psi.PsiNamedElement) PyImportElement(com.jetbrains.python.psi.PyImportElement) PyImportedNameDefiner(com.jetbrains.python.psi.PyImportedNameDefiner) PsiElement(com.intellij.psi.PsiElement)

Example 3 with PyImportElement

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

the class DocStringReferenceProvider method parseTypeReferences.

@NotNull
private static List<PsiReference> parseTypeReferences(@NotNull PsiElement anchor, @NotNull Substring s, int offset) {
    final List<PsiReference> result = new ArrayList<>();
    final PyTypeParser.ParseResult parseResult = PyTypeParser.parse(anchor, s.toString());
    final Map<TextRange, ? extends PyType> types = parseResult.getTypes();
    if (types.isEmpty()) {
        result.add(new DocStringTypeReference(anchor, s.getTextRange().shiftRight(offset), s.getTextRange().shiftRight(offset), null, null));
    }
    offset = s.getTextRange().getStartOffset() + offset;
    final Map<? extends PyType, TextRange> fullRanges = parseResult.getFullRanges();
    for (Map.Entry<TextRange, ? extends PyType> pair : types.entrySet()) {
        final PyType t = pair.getValue();
        final TextRange range = pair.getKey().shiftRight(offset);
        final TextRange fullRange = fullRanges.containsKey(t) ? fullRanges.get(t).shiftRight(offset) : range;
        final PyImportElement importElement = parseResult.getImports().get(t);
        result.add(new DocStringTypeReference(anchor, range, fullRange, t, importElement));
    }
    return result;
}
Also used : PyImportElement(com.jetbrains.python.psi.PyImportElement) ArrayList(java.util.ArrayList) PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange) PyTypeParser(com.jetbrains.python.psi.types.PyTypeParser) PyType(com.jetbrains.python.psi.types.PyType) Map(java.util.Map) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PyImportElement (com.jetbrains.python.psi.PyImportElement)3 PsiElement (com.intellij.psi.PsiElement)2 PsiNamedElement (com.intellij.psi.PsiNamedElement)2 ArrayList (java.util.ArrayList)2 NotNull (org.jetbrains.annotations.NotNull)2 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiReference (com.intellij.psi.PsiReference)1 ResolveState (com.intellij.psi.ResolveState)1 PsiScopeProcessor (com.intellij.psi.scope.PsiScopeProcessor)1 PyFile (com.jetbrains.python.psi.PyFile)1 PyImportedNameDefiner (com.jetbrains.python.psi.PyImportedNameDefiner)1 LightNamedElement (com.jetbrains.python.psi.impl.LightNamedElement)1 PyType (com.jetbrains.python.psi.types.PyType)1 PyTypeParser (com.jetbrains.python.psi.types.PyTypeParser)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1