Search in sources :

Example 21 with Processor

use of com.intellij.util.Processor in project intellij-community by JetBrains.

the class GroovyConstructorUsagesSearcher method processConstructorUsages.

static void processConstructorUsages(final PsiMethod constructor, final SearchScope searchScope, final Processor<PsiReference> consumer, final SearchRequestCollector collector, final boolean includeOverloads) {
    if (!constructor.isConstructor())
        return;
    final PsiClass clazz = constructor.getContainingClass();
    if (clazz == null)
        return;
    SearchScope onlyGroovy = GroovyScopeUtil.restrictScopeToGroovyFiles(searchScope, GroovyScopeUtil.getEffectiveScope(constructor));
    Set<PsiClass> processed = collector.getSearchSession().getUserData(LITERALLY_CONSTRUCTED_CLASSES);
    if (processed == null) {
        collector.getSearchSession().putUserData(LITERALLY_CONSTRUCTED_CLASSES, processed = ContainerUtil.newConcurrentSet());
    }
    if (!processed.add(clazz))
        return;
    if (clazz.isEnum() && clazz instanceof GroovyPsiElement) {
        for (PsiField field : clazz.getFields()) {
            if (field instanceof GrEnumConstant) {
                final PsiReference ref = field.getReference();
                if (ref != null && ref.isReferenceTo(constructor)) {
                    if (!consumer.process(ref))
                        return;
                }
            }
        }
    }
    final LiteralConstructorSearcher literalProcessor = new LiteralConstructorSearcher(constructor, consumer, includeOverloads);
    final Processor<GrNewExpression> newExpressionProcessor = grNewExpression -> {
        final PsiMethod resolvedConstructor = grNewExpression.resolveMethod();
        if (includeOverloads || constructor.getManager().areElementsEquivalent(resolvedConstructor, constructor)) {
            return consumer.process(grNewExpression.getReferenceElement());
        }
        return true;
    };
    processGroovyClassUsages(clazz, searchScope, collector, newExpressionProcessor, literalProcessor);
    //this()
    if (clazz instanceof GrTypeDefinition) {
        if (!processConstructors(constructor, consumer, clazz, true)) {
            return;
        }
    }
    //super()
    DirectClassInheritorsSearch.search(clazz, onlyGroovy).forEach(new ReadActionProcessor<PsiClass>() {

        @Override
        public boolean processInReadAction(PsiClass inheritor) {
            if (inheritor instanceof GrTypeDefinition) {
                if (!processConstructors(constructor, consumer, inheritor, false))
                    return false;
            }
            return true;
        }
    });
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) SearchScope(com.intellij.psi.search.SearchScope) ContainerUtil(com.intellij.util.containers.ContainerUtil) ControlFlowUtils(org.jetbrains.plugins.groovy.codeInspection.utils.ControlFlowUtils) GrConstructorInvocation(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrConstructorInvocation) PairProcessor(com.intellij.util.PairProcessor) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) ReadActionProcessor(com.intellij.openapi.application.ReadActionProcessor) MethodReferencesSearch(com.intellij.psi.search.searches.MethodReferencesSearch) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) LightMemberReference(com.intellij.psi.impl.light.LightMemberReference) SearchRequestCollector(com.intellij.psi.search.SearchRequestCollector) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) Key(com.intellij.openapi.util.Key) GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) Set(java.util.Set) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) TextRange(com.intellij.openapi.util.TextRange) GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrSafeCastExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrSafeCastExpression) QueryExecutorBase(com.intellij.openapi.application.QueryExecutorBase) GrTypeCastExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrTypeCastExpression) Nullable(org.jetbrains.annotations.Nullable) GrEnumConstant(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant) Processor(com.intellij.util.Processor) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) DirectClassInheritorsSearch(com.intellij.psi.search.searches.DirectClassInheritorsSearch) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrEnumConstant(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant) GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) SearchScope(com.intellij.psi.search.SearchScope)

Example 22 with Processor

use of com.intellij.util.Processor in project intellij-community by JetBrains.

the class GroovyFileImpl method processCachedDeclarations.

private static boolean processCachedDeclarations(@NotNull PsiScopeProcessor processor, @NotNull ResolveState state, MostlySingularMultiMap<String, ResultWithContext> cache) {
    for (PsiScopeProcessor each : GroovyResolverProcessor.allProcessors(processor)) {
        String name = ResolveUtil.getNameHint(each);
        Processor<ResultWithContext> cacheProcessor = res -> each.execute(res.getElement(), state.put(ClassHint.RESOLVE_CONTEXT, res.getFileContext()));
        boolean result = name != null ? cache.processForKey(name, cacheProcessor) : cache.processAllValues(cacheProcessor);
        if (!result)
            return false;
    }
    return true;
}
Also used : ResultWithContext(com.intellij.psi.impl.source.resolve.SymbolCollectingProcessor.ResultWithContext) GrFileStub(org.jetbrains.plugins.groovy.lang.psi.stubs.GrFileStub) ImportKind(org.jetbrains.plugins.groovy.lang.psi.impl.GroovyImportHelper.ImportKind) ElementClassHint(com.intellij.psi.scope.ElementClassHint) GrPackageDefinition(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition) GrPackageDefinitionStub(org.jetbrains.plugins.groovy.lang.psi.stubs.GrPackageDefinitionStub) GroovyLanguage(org.jetbrains.plugins.groovy.GroovyLanguage) PackageSkippingProcessor(org.jetbrains.plugins.groovy.lang.resolve.PackageSkippingProcessor) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) ResolveUtil(org.jetbrains.plugins.groovy.lang.resolve.ResolveUtil) ContainerUtil(com.intellij.util.containers.ContainerUtil) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) GrTopStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement) ConcurrentMap(java.util.concurrent.ConcurrentMap) MostlySingularMultiMap(com.intellij.util.containers.MostlySingularMultiMap) Logger(com.intellij.openapi.diagnostic.Logger) NameHint(com.intellij.psi.scope.NameHint) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GroovyTokenTypes(org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes) GroovyElementTypes(org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) PsiScopeProcessor(com.intellij.psi.scope.PsiScopeProcessor) GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GroovyResolverProcessor(org.jetbrains.plugins.groovy.lang.resolve.processors.GroovyResolverProcessor) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GrLightParameter(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightParameter) PsiFileEx(com.intellij.psi.impl.PsiFileEx) IncorrectOperationException(com.intellij.util.IncorrectOperationException) StringUtil(com.intellij.openapi.util.text.StringUtil) com.intellij.psi.util(com.intellij.psi.util) BaseScopeProcessor(com.intellij.psi.scope.BaseScopeProcessor) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) ASTNode(com.intellij.lang.ASTNode) Nullable(org.jetbrains.annotations.Nullable) ClassHint(org.jetbrains.plugins.groovy.lang.resolve.processors.ClassHint) MethodTypeInferencer(org.jetbrains.plugins.groovy.lang.resolve.MethodTypeInferencer) GrBindingVariable(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrBindingVariable) ImplicitImportsKt(org.jetbrains.plugins.groovy.lang.resolve.ImplicitImportsKt) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) Processor(com.intellij.util.Processor) ApplicationManager(com.intellij.openapi.application.ApplicationManager) com.intellij.psi(com.intellij.psi) StubElement(com.intellij.psi.stubs.StubElement) NotNull(org.jetbrains.annotations.NotNull) ResultWithContext(com.intellij.psi.impl.source.resolve.SymbolCollectingProcessor.ResultWithContext) PsiScopeProcessor(com.intellij.psi.scope.PsiScopeProcessor)

Example 23 with Processor

use of com.intellij.util.Processor in project intellij-community by JetBrains.

the class GroovyLanguageInjectionSupport method doInject.

private static boolean doInject(@NotNull String languageId, @NotNull PsiElement psiElement, @NotNull PsiLanguageInjectionHost host) {
    final PsiElement target = getTopLevelInjectionTarget(psiElement);
    final PsiElement parent = target.getParent();
    final Project project = psiElement.getProject();
    if (parent instanceof GrReturnStatement) {
        final GrControlFlowOwner owner = ControlFlowUtils.findControlFlowOwner(parent);
        if (owner instanceof GrOpenBlock && owner.getParent() instanceof GrMethod) {
            return JavaLanguageInjectionSupport.doInjectInJavaMethod(project, (PsiMethod) owner.getParent(), -1, host, languageId);
        }
    } else if (parent instanceof GrMethod) {
        return JavaLanguageInjectionSupport.doInjectInJavaMethod(project, (GrMethod) parent, -1, host, languageId);
    } else if (parent instanceof GrAnnotationNameValuePair) {
        final PsiReference ref = parent.getReference();
        if (ref != null) {
            final PsiElement resolved = ref.resolve();
            if (resolved instanceof PsiMethod) {
                return JavaLanguageInjectionSupport.doInjectInJavaMethod(project, (PsiMethod) resolved, -1, host, languageId);
            }
        }
    } else if (parent instanceof GrArgumentList && parent.getParent() instanceof GrMethodCall) {
        final PsiMethod method = ((GrMethodCall) parent.getParent()).resolveMethod();
        if (method != null) {
            final int index = GrInjectionUtil.findParameterIndex(target, ((GrMethodCall) parent.getParent()));
            if (index >= 0) {
                return JavaLanguageInjectionSupport.doInjectInJavaMethod(project, method, index, host, languageId);
            }
        }
    } else if (parent instanceof GrAssignmentExpression) {
        final GrExpression expr = ((GrAssignmentExpression) parent).getLValue();
        if (expr instanceof GrReferenceExpression) {
            final PsiElement element = ((GrReferenceExpression) expr).resolve();
            if (element != null) {
                return doInject(languageId, element, host);
            }
        }
    } else {
        if (parent instanceof PsiVariable) {
            Processor<PsiLanguageInjectionHost> fixer = getAnnotationFixer(project, languageId);
            if (JavaLanguageInjectionSupport.doAddLanguageAnnotation(project, (PsiModifierListOwner) parent, host, languageId, fixer))
                return true;
        } else if (target instanceof PsiVariable && !(target instanceof LightElement)) {
            Processor<PsiLanguageInjectionHost> fixer = getAnnotationFixer(project, languageId);
            if (JavaLanguageInjectionSupport.doAddLanguageAnnotation(project, (PsiModifierListOwner) target, host, languageId, fixer))
                return true;
        }
    }
    return false;
}
Also used : Processor(com.intellij.util.Processor) GrAnnotationNameValuePair(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationNameValuePair) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) LightElement(com.intellij.psi.impl.light.LightElement) Project(com.intellij.openapi.project.Project) GrControlFlowOwner(org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 24 with Processor

use of com.intellij.util.Processor in project intellij-community by JetBrains.

the class PatternEditorContextMembersProvider method processDynamicElements.

@Override
public void processDynamicElements(@NotNull PsiType qualifierType, @NotNull final PsiScopeProcessor scopeProcessor, @NotNull final PsiElement place, @NotNull final ResolveState state) {
    final PsiFile file = place.getContainingFile().getOriginalFile();
    final BaseInjection injection = file.getUserData(BaseInjection.INJECTION_KEY);
    Processor<PsiElement> processor = element -> element.processDeclarations(scopeProcessor, state, null, place);
    if (injection == null) {
        processDevContext(file, processor);
    } else {
        processPatternContext(injection, file, processor);
    }
}
Also used : ArrayUtil(com.intellij.util.ArrayUtil) PatternClassBean(com.intellij.patterns.compiler.PatternClassBean) ContainerUtil(com.intellij.util.containers.ContainerUtil) LowLevelSearchUtil(com.intellij.psi.impl.search.LowLevelSearchUtil) FileContextUtil(com.intellij.psi.impl.source.resolve.FileContextUtil) StringSearcher(com.intellij.util.text.StringSearcher) NonCodeMembersContributor(org.jetbrains.plugins.groovy.lang.resolve.NonCodeMembersContributor) BaseInjection(org.intellij.plugins.intelliLang.inject.config.BaseInjection) PatternCompilerFactory(com.intellij.patterns.compiler.PatternCompilerFactory) CacheManager(com.intellij.psi.impl.cache.CacheManager) Project(com.intellij.openapi.project.Project) TextOccurenceProcessor(com.intellij.psi.search.TextOccurenceProcessor) XmlTag(com.intellij.psi.xml.XmlTag) PsiScopeProcessor(com.intellij.psi.scope.PsiScopeProcessor) InjectorUtils(org.intellij.plugins.intelliLang.inject.InjectorUtils) GroovyFileType(org.jetbrains.plugins.groovy.GroovyFileType) StdFileTypes(com.intellij.openapi.fileTypes.StdFileTypes) StringUtil(com.intellij.openapi.util.text.StringUtil) com.intellij.psi.util(com.intellij.psi.util) Key(com.intellij.openapi.util.Key) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Set(java.util.Set) XmlText(com.intellij.psi.xml.XmlText) UsageSearchContext(com.intellij.psi.search.UsageSearchContext) EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Processor(com.intellij.util.Processor) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) SoftFactoryMap(com.intellij.util.containers.SoftFactoryMap) BaseInjection(org.intellij.plugins.intelliLang.inject.config.BaseInjection)

Example 25 with Processor

use of com.intellij.util.Processor in project intellij-community by JetBrains.

the class PsiSearchHelperImpl method isCheapEnoughToSearch.

@NotNull
@Override
public SearchCostResult isCheapEnoughToSearch(@NotNull String name, @NotNull final GlobalSearchScope scope, @Nullable final PsiFile fileToIgnoreOccurrencesIn, @Nullable final ProgressIndicator progress) {
    final AtomicInteger count = new AtomicInteger();
    final ProgressIndicator indicator = progress == null ? new EmptyProgressIndicator() : progress;
    final Processor<VirtualFile> processor = new Processor<VirtualFile>() {

        private final VirtualFile virtualFileToIgnoreOccurrencesIn = fileToIgnoreOccurrencesIn == null ? null : fileToIgnoreOccurrencesIn.getVirtualFile();

        @Override
        public boolean process(VirtualFile file) {
            indicator.checkCanceled();
            if (Comparing.equal(file, virtualFileToIgnoreOccurrencesIn))
                return true;
            final int value = count.incrementAndGet();
            return value < 10;
        }
    };
    List<IdIndexEntry> keys = getWordEntries(name, true);
    boolean cheap = keys.isEmpty() || processFilesContainingAllKeys(myManager.getProject(), scope, null, keys, processor);
    if (!cheap) {
        return SearchCostResult.TOO_MANY_OCCURRENCES;
    }
    return count.get() == 0 ? SearchCostResult.ZERO_OCCURRENCES : SearchCostResult.FEW_OCCURRENCES;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) ReadActionProcessor(com.intellij.openapi.application.ReadActionProcessor) Processor(com.intellij.util.Processor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) IdIndexEntry(com.intellij.psi.impl.cache.impl.id.IdIndexEntry) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Processor (com.intellij.util.Processor)83 NotNull (org.jetbrains.annotations.NotNull)65 Project (com.intellij.openapi.project.Project)49 Nullable (org.jetbrains.annotations.Nullable)49 ContainerUtil (com.intellij.util.containers.ContainerUtil)42 com.intellij.psi (com.intellij.psi)31 List (java.util.List)28 ApplicationManager (com.intellij.openapi.application.ApplicationManager)25 StringUtil (com.intellij.openapi.util.text.StringUtil)25 VirtualFile (com.intellij.openapi.vfs.VirtualFile)25 java.util (java.util)25 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)24 ProgressManager (com.intellij.openapi.progress.ProgressManager)21 Logger (com.intellij.openapi.diagnostic.Logger)20 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)20 NonNls (org.jetbrains.annotations.NonNls)18 Ref (com.intellij.openapi.util.Ref)16 Collection (java.util.Collection)16 SmartList (com.intellij.util.SmartList)14 Document (com.intellij.openapi.editor.Document)13