Search in sources :

Example 16 with LighterASTNode

use of com.intellij.lang.LighterASTNode in project intellij-community by JetBrains.

the class JavaNullMethodArgumentIndex method findCallsWithNulls.

@NotNull
private static Set<LighterASTNode> findCallsWithNulls(LighterAST lighterAst, int[] nullOffsets) {
    Set<LighterASTNode> calls = new HashSet<>();
    for (int offset : nullOffsets) {
        LighterASTNode leaf = LightTreeUtil.findLeafElementAt(lighterAst, offset);
        LighterASTNode literal = leaf == null ? null : lighterAst.getParent(leaf);
        if (isNullLiteral(lighterAst, literal)) {
            LighterASTNode exprList = lighterAst.getParent(literal);
            if (exprList != null && exprList.getTokenType() == EXPRESSION_LIST) {
                ContainerUtil.addIfNotNull(calls, LightTreeUtil.getParentOfType(lighterAst, exprList, CALL_TYPES, ElementType.MEMBER_BIT_SET));
            }
        }
    }
    return calls;
}
Also used : LighterASTNode(com.intellij.lang.LighterASTNode) NotNull(org.jetbrains.annotations.NotNull)

Example 17 with LighterASTNode

use of com.intellij.lang.LighterASTNode in project intellij-community by JetBrains.

the class JavaFunctionalExpressionIndex method getCalledMethodName.

@Nullable
private static String getCalledMethodName(LighterAST tree, LighterASTNode call) {
    if (call.getTokenType() == NEW_EXPRESSION) {
        LighterASTNode anonClass = LightTreeUtil.firstChildOfType(tree, call, ANONYMOUS_CLASS);
        LighterASTNode ref = LightTreeUtil.firstChildOfType(tree, anonClass != null ? anonClass : call, JAVA_CODE_REFERENCE);
        return ref == null ? null : JavaLightTreeUtil.getNameIdentifierText(tree, ref);
    }
    LighterASTNode methodExpr = tree.getChildren(call).get(0);
    if (LightTreeUtil.firstChildOfType(tree, methodExpr, JavaTokenType.SUPER_KEYWORD) != null) {
        return getSuperClassName(tree, call);
    }
    if (LightTreeUtil.firstChildOfType(tree, methodExpr, JavaTokenType.THIS_KEYWORD) != null) {
        return JavaLightTreeUtil.getNameIdentifierText(tree, findClass(tree, call));
    }
    return JavaLightTreeUtil.getNameIdentifierText(tree, methodExpr);
}
Also used : LighterASTNode(com.intellij.lang.LighterASTNode) Nullable(org.jetbrains.annotations.Nullable)

Example 18 with LighterASTNode

use of com.intellij.lang.LighterASTNode in project intellij-community by JetBrains.

the class JavaFunctionalExpressionIndex method createCallChain.

@NotNull
private static List<ReferenceChainLink> createCallChain(FileLocalResolver resolver, @Nullable LighterASTNode expr) {
    List<ReferenceChainLink> chain = new ArrayList<>();
    while (true) {
        if (expr == null)
            return reversedChain(chain);
        if (expr.getTokenType() == PARENTH_EXPRESSION) {
            expr = LightTreeUtil.firstChildOfType(resolver.getLightTree(), expr, ElementType.EXPRESSION_BIT_SET);
            continue;
        }
        if (expr.getTokenType() == TYPE_CAST_EXPRESSION) {
            String typeName = resolver.getShortClassTypeName(expr);
            ContainerUtil.addIfNotNull(chain, typeName != null ? new ReferenceChainLink(typeName, false, -1) : null);
            return reversedChain(chain);
        }
        boolean isCall = expr.getTokenType() == METHOD_CALL_EXPRESSION || expr.getTokenType() == NEW_EXPRESSION;
        String referenceName = getReferencedMemberName(resolver.getLightTree(), expr, isCall);
        if (referenceName == null)
            return reversedChain(chain);
        LighterASTNode qualifier = getQualifier(resolver.getLightTree(), expr, isCall);
        if (qualifier == null) {
            ContainerUtil.addIfNotNull(chain, createChainStart(resolver, expr, isCall, referenceName));
            return reversedChain(chain);
        }
        chain.add(new ReferenceChainLink(referenceName, isCall, getArgCount(resolver.getLightTree(), expr)));
        expr = qualifier;
    }
}
Also used : LighterASTNode(com.intellij.lang.LighterASTNode) NotNull(org.jetbrains.annotations.NotNull)

Example 19 with LighterASTNode

use of com.intellij.lang.LighterASTNode in project intellij-community by JetBrains.

the class JavaFunctionalExpressionIndex method calcReturnType.

private static FunctionalExpressionKey.CoarseType calcReturnType(final LighterAST tree, LighterASTNode funExpr) {
    if (funExpr.getTokenType() == METHOD_REF_EXPRESSION)
        return FunctionalExpressionKey.CoarseType.UNKNOWN;
    LighterASTNode block = LightTreeUtil.firstChildOfType(tree, funExpr, CODE_BLOCK);
    if (block == null) {
        LighterASTNode expr = findExpressionChild(funExpr, tree);
        return isBooleanExpression(tree, expr) ? FunctionalExpressionKey.CoarseType.BOOLEAN : FunctionalExpressionKey.CoarseType.UNKNOWN;
    }
    final Ref<Boolean> returnsSomething = Ref.create(null);
    final AtomicBoolean isBoolean = new AtomicBoolean();
    final AtomicBoolean hasStatements = new AtomicBoolean();
    new RecursiveLighterASTNodeWalkingVisitor(tree) {

        @Override
        public void visitNode(@NotNull LighterASTNode element) {
            IElementType type = element.getTokenType();
            if (type == LAMBDA_EXPRESSION || ElementType.MEMBER_BIT_SET.contains(type)) {
                return;
            }
            if (type == RETURN_STATEMENT) {
                LighterASTNode expr = findExpressionChild(element, tree);
                returnsSomething.set(expr != null);
                if (isBooleanExpression(tree, expr)) {
                    isBoolean.set(true);
                }
                return;
            }
            if (type == EXPRESSION_STATEMENT) {
                hasStatements.set(true);
            }
            super.visitNode(element);
        }
    }.visitNode(block);
    if (isBoolean.get()) {
        return FunctionalExpressionKey.CoarseType.BOOLEAN;
    }
    if (returnsSomething.isNull()) {
        return hasStatements.get() ? FunctionalExpressionKey.CoarseType.VOID : FunctionalExpressionKey.CoarseType.UNKNOWN;
    }
    return returnsSomething.get() ? FunctionalExpressionKey.CoarseType.NON_VOID : FunctionalExpressionKey.CoarseType.VOID;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) LighterASTNode(com.intellij.lang.LighterASTNode) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RecursiveLighterASTNodeWalkingVisitor(com.intellij.psi.impl.source.tree.RecursiveLighterASTNodeWalkingVisitor)

Example 20 with LighterASTNode

use of com.intellij.lang.LighterASTNode in project Perl5-IDEA by Camelcade.

the class TemplateToolkitParserUtil method parseTryCatchBlock.

public static boolean parseTryCatchBlock(PsiBuilder b, int l, PsiBuilder.Marker branchMarker, IElementType branchTokenType) {
    while (!b.eof()) {
        PsiBuilder.Marker currentMarker = b.mark();
        if (TemplateToolkitParser.element(b, l)) {
            LighterASTNode latestDoneMarker = b.getLatestDoneMarker();
            if (latestDoneMarker != null) {
                if (latestDoneMarker.getTokenType() == END_DIRECTIVE) {
                    branchMarker.doneBefore(branchTokenType, currentMarker);
                    branchMarker.setCustomEdgeTokenBinders(WhitespacesBinders.GREEDY_LEFT_BINDER, WhitespacesBinders.GREEDY_RIGHT_BINDER);
                    currentMarker.drop();
                    branchMarker = null;
                    break;
                } else if (latestDoneMarker.getTokenType() == CATCH_DIRECTIVE) {
                    branchMarker.doneBefore(branchTokenType, currentMarker);
                    branchMarker.setCustomEdgeTokenBinders(WhitespacesBinders.GREEDY_LEFT_BINDER, WhitespacesBinders.GREEDY_RIGHT_BINDER);
                    branchMarker = currentMarker.precede();
                    branchTokenType = CATCH_BRANCH;
                } else if (latestDoneMarker.getTokenType() == FINAL_DIRECTIVE) {
                    branchMarker.doneBefore(branchTokenType, currentMarker);
                    branchMarker.setCustomEdgeTokenBinders(WhitespacesBinders.GREEDY_LEFT_BINDER, WhitespacesBinders.GREEDY_RIGHT_BINDER);
                    branchMarker = currentMarker.precede();
                    branchTokenType = FINAL_BRANCH;
                } else {
                    currentMarker.error(PerlBundle.message("ttk2.catch.final.end.expected"));
                }
                currentMarker.drop();
            }
        } else {
            b.advanceLexer();
            currentMarker.error(PerlBundle.message("ttk2.unexpected.token"));
        }
    }
    if (branchMarker != null) {
        branchMarker.done(branchTokenType);
        branchMarker.precede().error(PerlBundle.message("ttk2.error.unclosed.block.directive"));
        branchMarker.setCustomEdgeTokenBinders(WhitespacesBinders.GREEDY_LEFT_BINDER, WhitespacesBinders.GREEDY_RIGHT_BINDER);
    }
    return true;
}
Also used : LighterASTNode(com.intellij.lang.LighterASTNode) PsiBuilder(com.intellij.lang.PsiBuilder)

Aggregations

LighterASTNode (com.intellij.lang.LighterASTNode)48 IElementType (com.intellij.psi.tree.IElementType)14 Nullable (org.jetbrains.annotations.Nullable)12 NotNull (org.jetbrains.annotations.NotNull)11 PsiBuilder (com.intellij.lang.PsiBuilder)6 LighterAST (com.intellij.lang.LighterAST)4 ValueConstraint (com.intellij.codeInspection.dataFlow.MethodContract.ValueConstraint)3 TypeInfo (com.intellij.psi.impl.cache.TypeInfo)3 SmartList (com.intellij.util.SmartList)3 JavaTokenType (com.intellij.psi.JavaTokenType)2 JavaLightTreeUtil (com.intellij.psi.impl.source.JavaLightTreeUtil)2 JavaLightTreeUtil.findExpressionChild (com.intellij.psi.impl.source.JavaLightTreeUtil.findExpressionChild)2 JavaLightTreeUtil.getExpressionChildren (com.intellij.psi.impl.source.JavaLightTreeUtil.getExpressionChildren)2 ElementType (com.intellij.psi.impl.source.tree.ElementType)2 JavaElementType (com.intellij.psi.impl.source.tree.JavaElementType)2 LightTreeUtil.firstChildOfType (com.intellij.psi.impl.source.tree.LightTreeUtil.firstChildOfType)2 LightTreeUtil.getChildrenOfType (com.intellij.psi.impl.source.tree.LightTreeUtil.getChildrenOfType)2 ContainerUtil (com.intellij.util.containers.ContainerUtil)2 DuplicatesProfile (com.intellij.dupLocator.DuplicatesProfile)1 DuplocatorState (com.intellij.dupLocator.DuplocatorState)1