Java Code Examples for com.intellij.psi.util.PsiTreeUtil#nextVisibleLeaf()

The following examples show how to use com.intellij.psi.util.PsiTreeUtil#nextVisibleLeaf() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Override
public boolean isAlwaysLeaf(StructureViewTreeElement element) {
    if (element instanceof GraphQLStructureViewTreeElement) {
        GraphQLStructureViewTreeElement treeElement = (GraphQLStructureViewTreeElement) element;
        if (treeElement.childrenBase instanceof LeafPsiElement) {
            return true;
        }
        if (treeElement.childrenBase instanceof GraphQLField) {
            final PsiElement[] children = treeElement.childrenBase.getChildren();
            if (children.length == 0) {
                // field with no sub selections, but we have to check if there's attributes
                final PsiElement nextVisible = PsiTreeUtil.nextVisibleLeaf(treeElement.childrenBase);
                if (nextVisible != null && nextVisible.getNode().getElementType() == GraphQLElementTypes.PAREN_L) {
                    return false;
                }
                return true;
            }
            if (children.length == 1 && children[0] instanceof LeafPsiElement) {
                return true;
            }
        }
    }
    return false;
}
 
Example 2
Source Project: idea-php-symfony2-plugin   File: VoterUtil.java    License: MIT License 5 votes vote down vote up
private static void visitAttributeForeach(@NotNull Method method, @NotNull Consumer<Pair<String, PsiElement>> consumer) {
    Parameter[] parameters = method.getParameters();
    if(parameters.length < 3) {
        return;
    }

    for (Variable variable : PhpElementsUtil.getVariablesInScope(method, parameters[2])) {
        // foreach ($attributes as $attribute)
        PsiElement psiElement = PsiTreeUtil.nextVisibleLeaf(variable);
        if(psiElement != null && psiElement.getNode().getElementType() == PhpTokenTypes.kwAS) {
            PsiElement parent = variable.getParent();
            if(!(parent instanceof ForeachStatement)) {
                continue;
            }

            PhpPsiElement variableDecl = variable.getNextPsiSibling();
            if(variableDecl instanceof Variable) {
                for (Variable variable1 : PhpElementsUtil.getVariablesInScope(parent, (Variable) variableDecl)) {
                    visitVariable(variable1, consumer);
                }
            }
        }

        // in_array('foobar', $attributes)
        PsiElement parameterList = variable.getParent();
        if(parameterList instanceof ParameterList && PsiElementUtils.getParameterIndexValue(variable) == 1) {
            PsiElement functionCall = parameterList.getParent();
            if(functionCall instanceof FunctionReference && "in_array".equalsIgnoreCase(((FunctionReference) functionCall).getName())) {
                PsiElement[] functionParameter = ((ParameterList) parameterList).getParameters();
                if(functionParameter.length > 0) {
                    String stringValue = PhpElementsUtil.getStringValue(functionParameter[0]);
                    if(stringValue != null && StringUtils.isNotBlank(stringValue)) {
                        consumer.accept(Pair.create(stringValue, functionParameter[0]));
                    }
                }
            }
        }
    }
}
 
Example 3
@Override
public String generateDoc(PsiElement element, @Nullable PsiElement originalElement) {
    if (element instanceof PsiFakeModule) {
        PsiElement child = element.getContainingFile().getFirstChild();
        String text = "";

        PsiElement nextSibling = child;
        while (nextSibling instanceof PsiComment) {
            if (isSpecialComment(nextSibling)) {
                text = nextSibling.getText();
                nextSibling = null;
            } else {
                // Not a special comment, try with next child until no more comments found
                nextSibling = PsiTreeUtil.nextVisibleLeaf(nextSibling);
            }
        }

        if (!text.isEmpty()) {
            return DocFormatter.format(element.getContainingFile(), element, text);
        }
    } else if (element instanceof PsiUpperSymbol || element instanceof PsiLowerSymbol) {
        element = element.getParent();
        if (element instanceof PsiTypeConstrName) {
            element = element.getParent();
        }

        // If it's an alias, resolve to the alias
        if (element instanceof PsiLet) {
            String alias = ((PsiLet) element).getAlias();
            if (alias != null) {
                Project project = element.getProject();
                PsiFinder psiFinder = PsiFinder.getInstance(project);
                PsiVal valFromAlias = psiFinder.findValFromQn(alias);
                if (valFromAlias == null) {
                    PsiLet letFromAlias = psiFinder.findLetFromQn(alias);
                    if (letFromAlias != null) {
                        element = letFromAlias;
                    }
                } else {
                    element = valFromAlias;
                }
            }
        }

        // Try to find a comment just below (OCaml only)
        if (element.getLanguage() == OclLanguage.INSTANCE) {
            PsiElement belowComment = findBelowComment(element);
            if (belowComment != null) {
                return isSpecialComment(belowComment) ? DocFormatter.format(element.getContainingFile(), element, belowComment.getText()) :
                        belowComment.getText();
            }
        }

        // Else try to find a comment just above
        PsiElement aboveComment = findAboveComment(element);
        if (aboveComment != null) {
            return isSpecialComment(aboveComment) ? DocFormatter.format(element.getContainingFile(), element, aboveComment.getText()) :
                    aboveComment.getText();
        }
    }

    return super.generateDoc(element, originalElement);
}