Search in sources :

Example 1 with PsiScopeProcessor

use of com.intellij.psi.scope.PsiScopeProcessor 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 PsiScopeProcessor

use of com.intellij.psi.scope.PsiScopeProcessor in project intellij-community by JetBrains.

the class StaticImportInsertHandler method importAlreadyExists.

private static boolean importAlreadyExists(final PsiMember member, final GroovyFile file, final PsiElement place) {
    final PsiManager manager = file.getManager();
    PsiScopeProcessor processor = new PsiScopeProcessor() {

        @Override
        public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) {
            return !manager.areElementsEquivalent(element, member);
        }

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

        @Override
        public void handleEvent(@NotNull Event event, Object associated) {
        }
    };
    boolean skipStaticImports = member instanceof PsiClass;
    final GrImportStatement[] imports = file.getImportStatements();
    final ResolveState initial = ResolveState.initial();
    for (GrImportStatement anImport : imports) {
        if (skipStaticImports == anImport.isStatic())
            continue;
        if (!anImport.processDeclarations(processor, initial, null, place))
            return true;
    }
    return false;
}
Also used : PsiScopeProcessor(com.intellij.psi.scope.PsiScopeProcessor) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) NotNull(org.jetbrains.annotations.NotNull) Key(com.intellij.openapi.util.Key)

Example 3 with PsiScopeProcessor

use of com.intellij.psi.scope.PsiScopeProcessor in project intellij-community by JetBrains.

the class PyFileImpl method processDeclarations.

@Override
public boolean processDeclarations(@NotNull final PsiScopeProcessor processor, @NotNull ResolveState resolveState, PsiElement lastParent, @NotNull PsiElement place) {
    final List<String> dunderAll = getDunderAll();
    final List<String> remainingDunderAll = dunderAll == null ? null : new ArrayList<>(dunderAll);
    PsiScopeProcessor wrapper = new DelegatingScopeProcessor(processor) {

        @Override
        public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) {
            if (!super.execute(element, state))
                return false;
            if (remainingDunderAll != null && element instanceof PyElement) {
                remainingDunderAll.remove(((PyElement) element).getName());
            }
            return true;
        }
    };
    Set<PyFile> pyFiles = resolveState.get(PROCESSED_FILES);
    if (pyFiles == null) {
        pyFiles = new HashSet<>();
        resolveState = resolveState.put(PROCESSED_FILES, pyFiles);
    }
    if (pyFiles.contains(this))
        return true;
    pyFiles.add(this);
    for (PyClass c : getTopLevelClasses()) {
        if (c == lastParent)
            continue;
        if (!wrapper.execute(c, resolveState))
            return false;
    }
    for (PyFunction f : getTopLevelFunctions()) {
        if (f == lastParent)
            continue;
        if (!wrapper.execute(f, resolveState))
            return false;
    }
    for (PyTargetExpression e : getTopLevelAttributes()) {
        if (e == lastParent)
            continue;
        if (!wrapper.execute(e, resolveState))
            return false;
    }
    for (PyImportElement e : getImportTargets()) {
        if (e == lastParent)
            continue;
        if (!wrapper.execute(e, resolveState))
            return false;
    }
    for (PyFromImportStatement e : getFromImports()) {
        if (e == lastParent)
            continue;
        if (!e.processDeclarations(wrapper, resolveState, null, this))
            return false;
    }
    if (remainingDunderAll != null) {
        for (String s : remainingDunderAll) {
            if (!PyNames.isIdentifier(s)) {
                continue;
            }
            if (!processor.execute(new LightNamedElement(myManager, PythonLanguage.getInstance(), s), resolveState))
                return false;
        }
    }
    return true;
}
Also used : PsiScopeProcessor(com.intellij.psi.scope.PsiScopeProcessor) NotNull(org.jetbrains.annotations.NotNull) DelegatingScopeProcessor(com.intellij.psi.scope.DelegatingScopeProcessor)

Example 4 with PsiScopeProcessor

use of com.intellij.psi.scope.PsiScopeProcessor in project intellij-community by JetBrains.

the class CanonicalPsiTypeConverterImpl method getReferences.

public PsiReference[] getReferences(@Nullable PsiType type, String typeText, int startOffsetInText, @NotNull final PsiElement element) {
    final ElementManipulator<PsiElement> manipulator = ElementManipulators.getManipulator(element);
    assert manipulator != null;
    String trimmed = typeText.trim();
    int offset = manipulator.getRangeInElement(element).getStartOffset() + startOffsetInText + typeText.indexOf(trimmed);
    if (trimmed.startsWith(ARRAY_PREFIX)) {
        offset += ARRAY_PREFIX.length();
        if (trimmed.endsWith(";")) {
            trimmed = trimmed.substring(ARRAY_PREFIX.length(), trimmed.length() - 1);
        } else {
            trimmed = trimmed.substring(ARRAY_PREFIX.length());
        }
    }
    if (type != null) {
        type = type.getDeepComponentType();
    }
    final boolean isPrimitiveType = type instanceof PsiPrimitiveType;
    return new JavaClassReferenceSet(trimmed, element, offset, false, CLASS_REFERENCE_PROVIDER) {

        @Override
        @NotNull
        protected JavaClassReference createReference(int refIndex, @NotNull String subRefText, @NotNull TextRange textRange, boolean staticImport) {
            return new JavaClassReference(this, textRange, refIndex, subRefText, staticImport) {

                @Override
                public boolean isSoft() {
                    return true;
                }

                @Override
                @NotNull
                public JavaResolveResult advancedResolve(final boolean incompleteCode) {
                    if (isPrimitiveType) {
                        return new CandidateInfo(element, PsiSubstitutor.EMPTY, false, false, element);
                    }
                    return super.advancedResolve(incompleteCode);
                }

                @Override
                public void processVariants(@NotNull final PsiScopeProcessor processor) {
                    if (processor instanceof JavaCompletionProcessor) {
                        ((JavaCompletionProcessor) processor).setCompletionElements(getVariants());
                    } else {
                        super.processVariants(processor);
                    }
                }

                @Override
                @NotNull
                public Object[] getVariants() {
                    final Object[] variants = super.getVariants();
                    if (myIndex == 0) {
                        return ArrayUtil.mergeArrays(variants, PRIMITIVES, ArrayUtil.OBJECT_ARRAY_FACTORY);
                    }
                    return variants;
                }
            };
        }
    }.getAllReferences();
}
Also used : JavaClassReferenceSet(com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReferenceSet) JavaClassReference(com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReference) CandidateInfo(com.intellij.psi.infos.CandidateInfo) TextRange(com.intellij.openapi.util.TextRange) PsiScopeProcessor(com.intellij.psi.scope.PsiScopeProcessor) NotNull(org.jetbrains.annotations.NotNull) JavaCompletionProcessor(com.intellij.codeInsight.completion.scope.JavaCompletionProcessor)

Example 5 with PsiScopeProcessor

use of com.intellij.psi.scope.PsiScopeProcessor in project intellij-community by JetBrains.

the class JavaClassReference method processVariants.

@Override
public void processVariants(@NotNull final PsiScopeProcessor processor) {
    if (processor instanceof JavaCompletionProcessor) {
        final Map<CustomizableReferenceProvider.CustomizationKey, Object> options = getOptions();
        if (options != null && (JavaClassReferenceProvider.EXTEND_CLASS_NAMES.getValue(options) != null || JavaClassReferenceProvider.NOT_INTERFACE.getBooleanValue(options) || JavaClassReferenceProvider.CONCRETE.getBooleanValue(options)) || JavaClassReferenceProvider.CLASS_KIND.getValue(options) != null) {
            ((JavaCompletionProcessor) processor).setCompletionElements(getVariants());
            return;
        }
    }
    PsiScopeProcessor processorToUse = processor;
    if (myInStaticImport) {
        // allows to complete members
        processor.handleEvent(JavaScopeProcessorEvent.CHANGE_LEVEL, null);
    } else {
        if (isDefinitelyStatic()) {
            processor.handleEvent(JavaScopeProcessorEvent.START_STATIC, null);
        }
        processorToUse = new PsiScopeProcessor() {

            @Override
            public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) {
                return !(element instanceof PsiClass || element instanceof PsiPackage) || processor.execute(element, state);
            }

            @Override
            public <V> V getHint(@NotNull Key<V> hintKey) {
                return processor.getHint(hintKey);
            }

            @Override
            public void handleEvent(@NotNull Event event, Object associated) {
                processor.handleEvent(event, associated);
            }
        };
    }
    super.processVariants(processorToUse);
}
Also used : PsiScopeProcessor(com.intellij.psi.scope.PsiScopeProcessor) JavaScopeProcessorEvent(com.intellij.psi.scope.JavaScopeProcessorEvent) JavaCompletionProcessor(com.intellij.codeInsight.completion.scope.JavaCompletionProcessor)

Aggregations

PsiScopeProcessor (com.intellij.psi.scope.PsiScopeProcessor)17 NotNull (org.jetbrains.annotations.NotNull)11 Nullable (org.jetbrains.annotations.Nullable)5 ASTNode (com.intellij.lang.ASTNode)3 StringUtil (com.intellij.openapi.util.text.StringUtil)3 com.intellij.psi (com.intellij.psi)3 DelegatingScopeProcessor (com.intellij.psi.scope.DelegatingScopeProcessor)3 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)3 Processor (com.intellij.util.Processor)3 ContainerUtil (com.intellij.util.containers.ContainerUtil)3 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)3 JavaCompletionProcessor (com.intellij.codeInsight.completion.scope.JavaCompletionProcessor)2 Project (com.intellij.openapi.project.Project)2 Key (com.intellij.openapi.util.Key)2 Ref (com.intellij.openapi.util.Ref)2 ResolveState (com.intellij.psi.ResolveState)2 com.intellij.psi.util (com.intellij.psi.util)2 XmlTag (com.intellij.psi.xml.XmlTag)2 ArrayList (java.util.ArrayList)2 GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)2