Search in sources :

Example 1 with JBTreeTraverser

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

the class PsiViewerDialog method initMap.

private void initMap(BlockTreeNode rootBlockNode, PsiElement psiEl) {
    JBTreeTraverser<BlockTreeNode> traverser = new JBTreeTraverser<>(o -> JBIterable.of(o.getChildren()));
    for (BlockTreeNode block : traverser.withRoot(rootBlockNode)) {
        PsiElement currentElem = null;
        if (block.getBlock() instanceof ASTBlock) {
            ASTNode node = ((ASTBlock) block.getBlock()).getNode();
            if (node != null) {
                currentElem = node.getPsi();
            }
        }
        if (currentElem == null) {
            currentElem = InjectedLanguageUtil.findElementAtNoCommit(psiEl.getContainingFile(), block.getBlock().getTextRange().getStartOffset());
        }
        myPsiToBlockMap.put(currentElem, block);
        //nested PSI elements with same ranges will be mapped to one blockNode
        //    assert currentElem != null;      //for Scala-language plugin etc it can be null, because formatterBlocks is not instance of ASTBlock
        TextRange curTextRange = currentElem.getTextRange();
        PsiElement parentElem = currentElem.getParent();
        while (parentElem != null && parentElem.getTextRange().equals(curTextRange)) {
            myPsiToBlockMap.put(parentElem, block);
            parentElem = parentElem.getParent();
        }
    }
}
Also used : BlockTreeNode(com.intellij.internal.psiView.formattingblocks.BlockTreeNode) ASTBlock(com.intellij.formatting.ASTBlock) ASTNode(com.intellij.lang.ASTNode) JBTreeTraverser(com.intellij.util.containers.JBTreeTraverser) TextRange(com.intellij.openapi.util.TextRange)

Example 2 with JBTreeTraverser

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

the class CodeInsightUtil method processSubTypes.

public static void processSubTypes(PsiType psiType, final PsiElement context, boolean getRawSubtypes, @NotNull final PrefixMatcher matcher, Consumer<PsiType> consumer) {
    int arrayDim = psiType.getArrayDimensions();
    psiType = psiType.getDeepComponentType();
    if (!(psiType instanceof PsiClassType))
        return;
    PsiClassType baseType = JavaCompletionUtil.originalize((PsiClassType) psiType);
    PsiClassType.ClassResolveResult baseResult = baseType.resolveGenerics();
    PsiClass baseClass = baseResult.getElement();
    PsiSubstitutor baseSubstitutor = baseResult.getSubstitutor();
    if (baseClass == null)
        return;
    GlobalSearchScope scope = context.getResolveScope();
    Processor<PsiClass> inheritorsProcessor = createInheritorsProcessor(context, baseType, arrayDim, getRawSubtypes, consumer, baseClass, baseSubstitutor);
    addContextTypeArguments(context, baseType, inheritorsProcessor);
    if (baseClass.hasModifierProperty(PsiModifier.FINAL))
        return;
    if (matcher.getPrefix().length() > 2) {
        JBTreeTraverser<PsiClass> traverser = new JBTreeTraverser<>(c -> Arrays.asList(c.getInnerClasses()));
        AllClassesGetter.processJavaClasses(matcher, context.getProject(), scope, psiClass -> {
            Iterable<PsiClass> inheritors = traverser.withRoot(psiClass).filter(c -> c.isInheritor(baseClass, true));
            return ContainerUtil.process(inheritors, inheritorsProcessor);
        });
    } else {
        Query<PsiClass> baseQuery = ClassInheritorsSearch.search(baseClass, scope, true, true, false);
        Query<PsiClass> query = new FilteredQuery<>(baseQuery, psiClass -> !(psiClass instanceof PsiTypeParameter) && ContainerUtil.exists(JavaCompletionUtil.getAllLookupStrings(psiClass), matcher::prefixMatches));
        query.forEach(inheritorsProcessor);
    }
}
Also used : JBTreeTraverser(com.intellij.util.containers.JBTreeTraverser) FilteredQuery(com.intellij.util.FilteredQuery) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope)

Aggregations

JBTreeTraverser (com.intellij.util.containers.JBTreeTraverser)2 ASTBlock (com.intellij.formatting.ASTBlock)1 BlockTreeNode (com.intellij.internal.psiView.formattingblocks.BlockTreeNode)1 ASTNode (com.intellij.lang.ASTNode)1 TextRange (com.intellij.openapi.util.TextRange)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 FilteredQuery (com.intellij.util.FilteredQuery)1