Search in sources :

Example 1 with PsiNewExpression

use of com.intellij.psi.PsiNewExpression in project kotlin by JetBrains.

the class ConstantEvaluator method isArrayLiteral.

/**
     * Returns true if the node is pointing to a an array literal
     */
public static boolean isArrayLiteral(@Nullable PsiElement node) {
    if (node instanceof PsiReference) {
        PsiElement resolved = ((PsiReference) node).resolve();
        if (resolved instanceof PsiField) {
            PsiField field = (PsiField) resolved;
            if (field.getInitializer() != null) {
                return isArrayLiteral(field.getInitializer());
            }
        } else if (resolved instanceof PsiLocalVariable) {
            PsiLocalVariable variable = (PsiLocalVariable) resolved;
            PsiStatement statement = PsiTreeUtil.getParentOfType(node, PsiStatement.class, false);
            if (statement != null) {
                PsiStatement prev = PsiTreeUtil.getPrevSiblingOfType(statement, PsiStatement.class);
                String targetName = variable.getName();
                if (targetName == null) {
                    return false;
                }
                while (prev != null) {
                    if (prev instanceof PsiDeclarationStatement) {
                        for (PsiElement element : ((PsiDeclarationStatement) prev).getDeclaredElements()) {
                            if (variable.equals(element)) {
                                return isArrayLiteral(variable.getInitializer());
                            }
                        }
                    } else if (prev instanceof PsiExpressionStatement) {
                        PsiExpression expression = ((PsiExpressionStatement) prev).getExpression();
                        if (expression instanceof PsiAssignmentExpression) {
                            PsiAssignmentExpression assign = (PsiAssignmentExpression) expression;
                            PsiExpression lhs = assign.getLExpression();
                            if (lhs instanceof PsiReferenceExpression) {
                                PsiReferenceExpression reference = (PsiReferenceExpression) lhs;
                                if (targetName.equals(reference.getReferenceName()) && reference.getQualifier() == null) {
                                    return isArrayLiteral(assign.getRExpression());
                                }
                            }
                        }
                    }
                    prev = PsiTreeUtil.getPrevSiblingOfType(prev, PsiStatement.class);
                }
            }
        }
    } else if (node instanceof PsiNewExpression) {
        PsiNewExpression creation = (PsiNewExpression) node;
        if (creation.getArrayInitializer() != null) {
            return true;
        }
        PsiType type = creation.getType();
        if (type instanceof PsiArrayType) {
            return true;
        }
    } else if (node instanceof PsiParenthesizedExpression) {
        PsiParenthesizedExpression parenthesizedExpression = (PsiParenthesizedExpression) node;
        PsiExpression expression = parenthesizedExpression.getExpression();
        if (expression != null) {
            return isArrayLiteral(expression);
        }
    } else if (node instanceof PsiTypeCastExpression) {
        PsiTypeCastExpression castExpression = (PsiTypeCastExpression) node;
        PsiExpression operand = castExpression.getOperand();
        if (operand != null) {
            return isArrayLiteral(operand);
        }
    }
    return false;
}
Also used : PsiStatement(com.intellij.psi.PsiStatement) PsiDeclarationStatement(com.intellij.psi.PsiDeclarationStatement) PsiExpression(com.intellij.psi.PsiExpression) PsiReferenceExpression(com.intellij.psi.PsiReferenceExpression) PsiReference(com.intellij.psi.PsiReference) PsiAssignmentExpression(com.intellij.psi.PsiAssignmentExpression) PsiLocalVariable(com.intellij.psi.PsiLocalVariable) PsiNewExpression(com.intellij.psi.PsiNewExpression) PsiArrayType(com.intellij.psi.PsiArrayType) PsiTypeCastExpression(com.intellij.psi.PsiTypeCastExpression) PsiExpressionStatement(com.intellij.psi.PsiExpressionStatement) PsiField(com.intellij.psi.PsiField) PsiParenthesizedExpression(com.intellij.psi.PsiParenthesizedExpression) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType)

Example 2 with PsiNewExpression

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

the class MissingArrayConstructorBracketFixer method apply.

@Override
public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
    if (!(psiElement instanceof PsiNewExpression))
        return;
    PsiNewExpression expr = (PsiNewExpression) psiElement;
    int count = 0;
    for (PsiElement element : expr.getChildren()) {
        if (element.getNode().getElementType() == JavaTokenType.LBRACKET) {
            count++;
        } else if (element.getNode().getElementType() == JavaTokenType.RBRACKET) {
            count--;
        }
    }
    if (count > 0) {
        editor.getDocument().insertString(psiElement.getTextRange().getEndOffset(), "]");
    }
}
Also used : PsiNewExpression(com.intellij.psi.PsiNewExpression) PsiElement(com.intellij.psi.PsiElement)

Example 3 with PsiNewExpression

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

the class AnonymousToInnerAction method isAvailableOnElementInEditorAndFile.

protected boolean isAvailableOnElementInEditorAndFile(@NotNull final PsiElement element, @NotNull final Editor editor, @NotNull PsiFile file, @NotNull DataContext context) {
    final PsiElement targetElement = file.findElementAt(editor.getCaretModel().getOffset());
    if (PsiTreeUtil.getParentOfType(targetElement, PsiAnonymousClass.class) != null) {
        return true;
    }
    if (PsiTreeUtil.getParentOfType(element, PsiAnonymousClass.class) != null) {
        return true;
    }
    final PsiNewExpression newExpression = PsiTreeUtil.getParentOfType(element, PsiNewExpression.class);
    return newExpression != null && newExpression.getAnonymousClass() != null;
}
Also used : PsiAnonymousClass(com.intellij.psi.PsiAnonymousClass) PsiElement(com.intellij.psi.PsiElement) PsiNewExpression(com.intellij.psi.PsiNewExpression)

Example 4 with PsiNewExpression

use of com.intellij.psi.PsiNewExpression in project oxy-template-support-plugin by mutant-industries.

the class LiteralJsMacroReferenceContributor method registerReferenceProviders.

@Override
public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar) {
    registrar.registerReferenceProvider(PlatformPatterns.psiElement(PsiLiteralExpression.class), new PsiReferenceProvider() {

        @NotNull
        @Override
        public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
            PsiLiteralExpression literalExpression = (PsiLiteralExpression) element;
            PsiMethodCallExpression callExpression;
            PsiNewExpression newMacroCallExpression;
            if (!(literalExpression.getValue() instanceof String)) {
                return PsiReference.EMPTY_ARRAY;
            }
            if ((newMacroCallExpression = PsiTreeUtil.getParentOfType(literalExpression, PsiNewExpressionImpl.class)) != null && newMacroCallExpression.getClassReference() != null) {
                if (MacroCall.class.getName().equals(newMacroCallExpression.getClassReference().getQualifiedName())) {
                    return new MacroReferenceSet(literalExpression).getAllReferences();
                }
            } else if ((callExpression = PsiTreeUtil.getParentOfType(literalExpression, PsiMethodCallExpression.class)) != null) {
                PsiExpression[] parameters = callExpression.getArgumentList().getExpressions();
                PsiReferenceExpression expression = callExpression.getMethodExpression();
                String callText = expression.getText();
                if (// TODO type check...
                callText.contains("ageUpdater.update")) {
                    if (parameters.length > 0 && literalExpression.isEquivalentTo(parameters[0])) {
                    // TODO template path reference
                    } else if (parameters.length > 1 && literalExpression.isEquivalentTo(parameters[1])) {
                        return new MacroReferenceSet(literalExpression).getAllReferences();
                    }
                }
            }
            return PsiReference.EMPTY_ARRAY;
        }
    });
}
Also used : ProcessingContext(com.intellij.util.ProcessingContext) MacroReferenceSet(ool.intellij.plugin.psi.reference.MacroReferenceSet) PsiExpression(com.intellij.psi.PsiExpression) PsiReferenceExpression(com.intellij.psi.PsiReferenceExpression) NotNull(org.jetbrains.annotations.NotNull) PsiNewExpression(com.intellij.psi.PsiNewExpression) PsiMethodCallExpression(com.intellij.psi.PsiMethodCallExpression) PsiLiteralExpression(com.intellij.psi.PsiLiteralExpression) PsiReferenceProvider(com.intellij.psi.PsiReferenceProvider) PsiNewExpressionImpl(com.intellij.psi.impl.source.tree.java.PsiNewExpressionImpl) PsiElement(com.intellij.psi.PsiElement)

Example 5 with PsiNewExpression

use of com.intellij.psi.PsiNewExpression in project oxy-template-support-plugin by mutant-industries.

the class ExtenderProvider method getExtenderFromProvider.

@Nullable
private static PsiClass getExtenderFromProvider(@NotNull PsiClass extenderProvider) {
    PsiMethod[] provideMethods = extenderProvider.findMethodsByName(EXTENDER_GETTER_METHOD_MANE, true);
    if (provideMethods.length < 2) {
        return null;
    }
    /**
     * return is always followed by a new expression, which contains class reference or anonymous class
     */
    PsiNewExpression newExpression = PsiTreeUtil.findChildOfType(provideMethods[0], PsiNewExpression.class);
    if (newExpression == null) {
        return null;
    }
    PsiJavaCodeReferenceElement extenderClassReference = PsiTreeUtil.getNextSiblingOfType(newExpression.getFirstChild(), PsiJavaCodeReferenceElement.class);
    PsiElement result;
    if (extenderClassReference != null && (result = extenderClassReference.resolve()) instanceof PsiClass) {
        return (PsiClass) result;
    }
    PsiAnonymousClass anonymousClass = PsiTreeUtil.getNextSiblingOfType(newExpression.getFirstChild(), PsiAnonymousClass.class);
    if (anonymousClass != null) {
        return anonymousClass;
    }
    return null;
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) PsiJavaCodeReferenceElement(com.intellij.psi.PsiJavaCodeReferenceElement) PsiClass(com.intellij.psi.PsiClass) PsiAnonymousClass(com.intellij.psi.PsiAnonymousClass) PsiNewExpression(com.intellij.psi.PsiNewExpression) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PsiNewExpression (com.intellij.psi.PsiNewExpression)10 PsiElement (com.intellij.psi.PsiElement)8 PsiExpression (com.intellij.psi.PsiExpression)6 PsiReferenceExpression (com.intellij.psi.PsiReferenceExpression)6 PsiClass (com.intellij.psi.PsiClass)4 PsiMethodCallExpression (com.intellij.psi.PsiMethodCallExpression)4 PsiJavaCodeReferenceElement (com.intellij.psi.PsiJavaCodeReferenceElement)3 PsiReference (com.intellij.psi.PsiReference)3 PsiType (com.intellij.psi.PsiType)3 Project (com.intellij.openapi.project.Project)2 PsiAnonymousClass (com.intellij.psi.PsiAnonymousClass)2 PsiArrayType (com.intellij.psi.PsiArrayType)2 PsiAssignmentExpression (com.intellij.psi.PsiAssignmentExpression)2 PsiDeclarationStatement (com.intellij.psi.PsiDeclarationStatement)2 PsiExpressionStatement (com.intellij.psi.PsiExpressionStatement)2 PsiField (com.intellij.psi.PsiField)2 PsiLocalVariable (com.intellij.psi.PsiLocalVariable)2 PsiMethod (com.intellij.psi.PsiMethod)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2