Search in sources :

Example 11 with PsiScopeProcessor

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

the class FlexResolveHelper method importClass.

public boolean importClass(final PsiScopeProcessor processor, final PsiNamedElement file) {
    // there is no need to process package stuff at function level
    if (file instanceof JSFunction)
        return true;
    if (file instanceof XmlBackedJSClassImpl) {
        if (!processInlineComponentsInScope((XmlBackedJSClassImpl) file, inlineComponent -> processor.execute(inlineComponent, ResolveState.initial()))) {
            return false;
        }
    }
    final String packageQualifierText = JSResolveUtil.findPackageStatementQualifier(file);
    final Project project = file.getProject();
    GlobalSearchScope scope = JSResolveUtil.getResolveScope(file);
    final MxmlAndFxgFilesProcessor filesProcessor = new MxmlAndFxgFilesProcessor() {

        final PsiManager manager = PsiManager.getInstance(project);

        public void addDependency(final PsiDirectory directory) {
        }

        public boolean processFile(final VirtualFile file, final VirtualFile root) {
            final PsiFile xmlFile = manager.findFile(file);
            if (!(xmlFile instanceof XmlFile))
                return true;
            return processor.execute(XmlBackedJSClassFactory.getXmlBackedClass((XmlFile) xmlFile), ResolveState.initial());
        }
    };
    PsiFile containingFile = file.getContainingFile();
    boolean completion = containingFile.getOriginalFile() != containingFile;
    if (completion) {
        return processAllMxmlAndFxgFiles(scope, project, filesProcessor, null);
    } else {
        if (packageQualifierText != null && packageQualifierText.length() > 0) {
            if (!processMxmlAndFxgFilesInPackage(scope, project, packageQualifierText, filesProcessor))
                return false;
        }
        return processMxmlAndFxgFilesInPackage(scope, project, "", filesProcessor);
    }
}
Also used : XmlBackedJSClassImpl(com.intellij.lang.javascript.flex.XmlBackedJSClassImpl) JavaScriptSupportLoader(com.intellij.lang.javascript.JavaScriptSupportLoader) XmlFile(com.intellij.psi.xml.XmlFile) JSChangeUtil(com.intellij.lang.javascript.psi.impl.JSChangeUtil) VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) FilenameIndex(com.intellij.psi.search.FilenameIndex) ContainerUtil(com.intellij.util.containers.ContainerUtil) ResolveProcessor(com.intellij.lang.javascript.psi.resolve.ResolveProcessor) DirectoryIndex(com.intellij.openapi.roots.impl.DirectoryIndex) Query(com.intellij.util.Query) Project(com.intellij.openapi.project.Project) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) JSResolveHelper(com.intellij.lang.javascript.flex.JSResolveHelper) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager) CssString(com.intellij.psi.css.CssString) JSFile(com.intellij.lang.javascript.psi.JSFile) XmlTag(com.intellij.psi.xml.XmlTag) PsiScopeProcessor(com.intellij.psi.scope.PsiScopeProcessor) com.intellij.lang.javascript.psi.ecmal4(com.intellij.lang.javascript.psi.ecmal4) JSImportHandlingUtil(com.intellij.lang.javascript.psi.resolve.JSImportHandlingUtil) JSReferenceExpression(com.intellij.lang.javascript.psi.JSReferenceExpression) VfsUtilCore(com.intellij.openapi.vfs.VfsUtilCore) JSResolveUtil(com.intellij.lang.javascript.psi.resolve.JSResolveUtil) StringUtil(com.intellij.openapi.util.text.StringUtil) Collection(java.util.Collection) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) JSPsiImplUtils(com.intellij.lang.javascript.psi.impl.JSPsiImplUtils) ImportUtils(com.intellij.lang.javascript.flex.ImportUtils) ASTNode(com.intellij.lang.ASTNode) Nullable(org.jetbrains.annotations.Nullable) JSFunction(com.intellij.lang.javascript.psi.JSFunction) JarFileSystem(com.intellij.openapi.vfs.JarFileSystem) Processor(com.intellij.util.Processor) MxmlJSClassProvider(com.intellij.javascript.flex.mxml.MxmlJSClassProvider) com.intellij.psi(com.intellij.psi) Ref(com.intellij.openapi.util.Ref) VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlBackedJSClassImpl(com.intellij.lang.javascript.flex.XmlBackedJSClassImpl) XmlFile(com.intellij.psi.xml.XmlFile) CssString(com.intellij.psi.css.CssString) Project(com.intellij.openapi.project.Project) JSFunction(com.intellij.lang.javascript.psi.JSFunction) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope)

Example 12 with PsiScopeProcessor

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

the class PsiDocParamRef method getReference.

@Override
public PsiReference getReference() {
    final PsiDocComment comment = PsiTreeUtil.getParentOfType(this, PsiDocComment.class);
    if (comment == null)
        return null;
    final PsiJavaDocumentedElement owner = comment.getOwner();
    if (!(owner instanceof PsiMethod) && !(owner instanceof PsiClass))
        return null;
    final ASTNode valueToken = findChildByType(JavaDocTokenType.DOC_TAG_VALUE_TOKEN);
    if (valueToken == null)
        return null;
    final String name = valueToken.getText();
    PsiElement reference = null;
    final PsiElement firstChild = getFirstChild();
    if (firstChild instanceof PsiDocToken && ((PsiDocToken) firstChild).getTokenType().equals(JavaDocTokenType.DOC_TAG_VALUE_LT)) {
        final PsiTypeParameter[] typeParameters = ((PsiTypeParameterListOwner) owner).getTypeParameters();
        for (PsiTypeParameter typeParameter : typeParameters) {
            if (typeParameter.getName().equals(name)) {
                reference = typeParameter;
            }
        }
    } else if (owner instanceof PsiMethod) {
        final PsiParameter[] parameters = ((PsiMethod) owner).getParameterList().getParameters();
        for (PsiParameter parameter : parameters) {
            if (parameter.getName().equals(name)) {
                reference = parameter;
            }
        }
    }
    final PsiElement resultReference = reference;
    return new PsiJavaReference() {

        @Override
        public PsiElement resolve() {
            return resultReference;
        }

        @Override
        @NotNull
        public String getCanonicalText() {
            return valueToken.getText();
        }

        @Override
        public PsiElement handleElementRename(String newElementName) {
            final CharTable charTableByTree = SharedImplUtil.findCharTableByTree(getNode());
            LeafElement newElement = Factory.createSingleLeafElement(JavaDocTokenType.DOC_TAG_VALUE_TOKEN, newElementName, charTableByTree, getManager());
            replaceChild(valueToken, newElement);
            return PsiDocParamRef.this;
        }

        @Override
        public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException {
            if (isReferenceTo(element))
                return PsiDocParamRef.this;
            if (!(element instanceof PsiParameter)) {
                throw new IncorrectOperationException("Unsupported operation");
            }
            return handleElementRename(((PsiParameter) element).getName());
        }

        @Override
        public boolean isReferenceTo(PsiElement element) {
            if (!(element instanceof PsiNamedElement))
                return false;
            PsiNamedElement namedElement = (PsiNamedElement) element;
            if (!getCanonicalText().equals(namedElement.getName()))
                return false;
            return getManager().areElementsEquivalent(resolve(), element);
        }

        @Override
        @NotNull
        public PsiElement[] getVariants() {
            final PsiElement firstChild = getFirstChild();
            Set<String> usedNames = new HashSet<>();
            for (PsiDocTag tag : comment.getTags()) {
                if (tag.getName().equals("param")) {
                    PsiDocTagValue valueElement = tag.getValueElement();
                    if (valueElement != null) {
                        usedNames.add(valueElement.getText());
                    }
                }
            }
            PsiNamedElement[] result = PsiNamedElement.EMPTY_ARRAY;
            if (firstChild instanceof PsiDocToken && ((PsiDocToken) firstChild).getTokenType().equals(JavaDocTokenType.DOC_TAG_VALUE_LT)) {
                result = ((PsiTypeParameterListOwner) owner).getTypeParameters();
            } else if (owner instanceof PsiMethod) {
                result = ((PsiMethod) owner).getParameterList().getParameters();
            }
            List<PsiElement> filtered = new ArrayList<>();
            for (PsiNamedElement namedElement : result) {
                if (!usedNames.contains(namedElement.getName())) {
                    filtered.add(namedElement);
                }
            }
            return filtered.toArray(new PsiElement[filtered.size()]);
        }

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

        @Override
        public TextRange getRangeInElement() {
            final int startOffsetInParent = valueToken.getPsi().getStartOffsetInParent();
            return new TextRange(startOffsetInParent, startOffsetInParent + valueToken.getTextLength());
        }

        @Override
        public PsiElement getElement() {
            return PsiDocParamRef.this;
        }

        @Override
        public void processVariants(@NotNull PsiScopeProcessor processor) {
            for (final PsiElement element : getVariants()) {
                if (!processor.execute(element, ResolveState.initial())) {
                    return;
                }
            }
        }

        @Override
        @NotNull
        public JavaResolveResult advancedResolve(boolean incompleteCode) {
            return resultReference == null ? JavaResolveResult.EMPTY : new CandidateInfo(resultReference, PsiSubstitutor.EMPTY);
        }

        @Override
        @NotNull
        public JavaResolveResult[] multiResolve(boolean incompleteCode) {
            return resultReference == null ? JavaResolveResult.EMPTY_ARRAY : new JavaResolveResult[] { new CandidateInfo(resultReference, PsiSubstitutor.EMPTY) };
        }
    };
}
Also used : PsiDocTag(com.intellij.psi.javadoc.PsiDocTag) CandidateInfo(com.intellij.psi.infos.CandidateInfo) ArrayList(java.util.ArrayList) PsiDocToken(com.intellij.psi.javadoc.PsiDocToken) PsiScopeProcessor(com.intellij.psi.scope.PsiScopeProcessor) CharTable(com.intellij.util.CharTable) NotNull(org.jetbrains.annotations.NotNull) ASTNode(com.intellij.lang.ASTNode) HashSet(java.util.HashSet) PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) TextRange(com.intellij.openapi.util.TextRange) PsiDocTagValue(com.intellij.psi.javadoc.PsiDocTagValue) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 13 with PsiScopeProcessor

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

the class GdkMethodUtil method processMixinToMetaclass.

public static boolean processMixinToMetaclass(GrStatementOwner run, final PsiScopeProcessor processor, ResolveState state, PsiElement lastParent, PsiElement place) {
    GrStatement[] statements = run.getStatements();
    for (GrStatement statement : statements) {
        if (statement == lastParent)
            break;
        final Trinity<PsiClassType, GrReferenceExpression, PsiClass> result = getMixinTypes(statement);
        if (result != null) {
            final PsiClassType subjectType = result.first;
            final GrReferenceExpression qualifier = result.second;
            final PsiClass mixin = result.third;
            for (PsiScopeProcessor each : GroovyResolverProcessor.allProcessors(processor)) {
                if (!mixin.processDeclarations(new MixinMemberContributor.MixinProcessor(each, subjectType, qualifier), state, null, place)) {
                    return false;
                }
            }
        } else {
            Trinity<PsiClassType, GrReferenceExpression, List<GrMethod>> closureResult = getClosureMixins(statement);
            if (closureResult != null) {
                final PsiClassType subjectType = closureResult.first;
                final GrReferenceExpression qualifier = closureResult.second;
                final List<GrMethod> methods = closureResult.third;
                final DelegatingScopeProcessor delegate = new MixinMemberContributor.MixinProcessor(processor, subjectType, qualifier);
                for (GrMethod method : methods) {
                    ResolveUtil.processElement(delegate, method, state);
                }
            }
        }
    }
    return true;
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) PsiScopeProcessor(com.intellij.psi.scope.PsiScopeProcessor) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) List(java.util.List) DelegatingScopeProcessor(com.intellij.psi.scope.DelegatingScopeProcessor)

Example 14 with PsiScopeProcessor

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

the class GrCollectionTypeMembersProvider method processDynamicElements.

@Override
public void processDynamicElements(@NotNull final PsiType qualifierType, PsiClass aClass, @NotNull final PsiScopeProcessor processor, @NotNull final PsiElement place, @NotNull final ResolveState state) {
    final PsiType collectionType = PsiUtil.extractIterableTypeParameter(qualifierType, true);
    if (collectionType == null)
        return;
    PsiClass collectionClass = JavaPsiFacade.getInstance(place.getProject()).findClass(CommonClassNames.JAVA_UTIL_COLLECTION, place.getResolveScope());
    final PsiScopeProcessor fieldSearcher = new FieldSearcher(processor, collectionClass);
    ResolveUtil.processAllDeclarations(collectionType, fieldSearcher, state, place);
}
Also used : PsiScopeProcessor(com.intellij.psi.scope.PsiScopeProcessor)

Example 15 with PsiScopeProcessor

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

the class GdkMethodDslProvider method processCategoryMethods.

private static void processCategoryMethods(final String className, final GdslMembersHolderConsumer consumer, final boolean isStatic) {
    final GlobalSearchScope scope = consumer.getResolveScope();
    final PsiClass categoryClass = JavaPsiFacade.getInstance(consumer.getProject()).findClass(className, scope);
    if (categoryClass == null) {
        return;
    }
    final VolatileNotNullLazyValue<GdkMethodHolder> methodsMap = new VolatileNotNullLazyValue<GdkMethodHolder>() {

        @NotNull
        @Override
        protected GdkMethodHolder compute() {
            return GdkMethodHolder.getHolderForClass(categoryClass, isStatic, scope);
        }
    };
    consumer.addMemberHolder(new CustomMembersHolder() {

        @Override
        public boolean processMembers(GroovyClassDescriptor descriptor, PsiScopeProcessor _processor, ResolveState state) {
            for (PsiScopeProcessor each : GroovyResolverProcessor.allProcessors(_processor)) {
                if (ResolveUtil.shouldProcessMethods(each.getHint(ElementClassHint.KEY)) && !methodsMap.getValue().processMethods(each, state, descriptor.getPsiType(), descriptor.getProject())) {
                    return false;
                }
            }
            return true;
        }
    });
}
Also used : GdkMethodHolder(org.jetbrains.plugins.groovy.dgm.GdkMethodHolder) CustomMembersHolder(org.jetbrains.plugins.groovy.dsl.holders.CustomMembersHolder) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiClass(com.intellij.psi.PsiClass) GroovyClassDescriptor(org.jetbrains.plugins.groovy.dsl.GroovyClassDescriptor) VolatileNotNullLazyValue(com.intellij.openapi.util.VolatileNotNullLazyValue) PsiScopeProcessor(com.intellij.psi.scope.PsiScopeProcessor) ResolveState(com.intellij.psi.ResolveState)

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