Search in sources :

Example 11 with GrNewExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression in project intellij-community by JetBrains.

the class GroovyClassNameInsertHandler method findNewExpression.

@Nullable
private static GrNewExpression findNewExpression(@Nullable PsiElement position) {
    if (position == null)
        return null;
    final PsiElement reference = position.getParent();
    if (!(reference instanceof GrCodeReferenceElement))
        return null;
    PsiElement parent = reference.getParent();
    while (parent instanceof GrCodeReferenceElement) parent = parent.getParent();
    if (parent instanceof GrAnonymousClassDefinition)
        parent = parent.getParent();
    return parent instanceof GrNewExpression ? (GrNewExpression) parent : null;
}
Also used : GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 12 with GrNewExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression in project intellij-community by JetBrains.

the class GroovyTargetElementEvaluator method getElementByReference.

@Override
public PsiElement getElementByReference(@NotNull PsiReference ref, int flags) {
    PsiElement sourceElement = ref.getElement();
    if (sourceElement instanceof GrCodeReferenceElement) {
        GrNewExpression newExpr;
        if (sourceElement.getParent() instanceof GrNewExpression) {
            newExpr = (GrNewExpression) sourceElement.getParent();
        } else if (sourceElement.getParent().getParent() instanceof GrNewExpression) {
            //anonymous class declaration
            newExpr = (GrNewExpression) sourceElement.getParent().getParent();
        } else {
            return null;
        }
        final PsiMethod constructor = newExpr.resolveMethod();
        final GrArgumentList argumentList = newExpr.getArgumentList();
        if (constructor != null && argumentList != null && PsiImplUtil.hasNamedArguments(argumentList) && !PsiImplUtil.hasExpressionArguments(argumentList)) {
            if (constructor.getParameterList().getParametersCount() == 0)
                return constructor.getContainingClass();
        }
        return constructor;
    }
    if (sourceElement instanceof GrReferenceExpression) {
        PsiElement resolved = ((GrReferenceExpression) sourceElement).resolve();
        if (resolved instanceof GrGdkMethod || !(resolved instanceof GrRenameableLightElement)) {
            return correctSearchTargets(resolved);
        }
        return resolved;
    }
    return null;
}
Also used : GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GrGdkMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod) PsiMethod(com.intellij.psi.PsiMethod) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrRenameableLightElement(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrRenameableLightElement) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 13 with GrNewExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression in project intellij-community by JetBrains.

the class ConvertJavaStyleArrayIntention method getElementPredicate.

@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
    return new PsiElementPredicate() {

        @Override
        public boolean satisfiedBy(PsiElement element) {
            if (!(element instanceof GrMethodCallExpression))
                return false;
            final GrExpression expression = ((GrMethodCallExpression) element).getInvokedExpression();
            if (!(expression instanceof GrNewExpression))
                return false;
            if (((GrNewExpression) expression).getArrayCount() == 0)
                return false;
            if (!((GrMethodCallExpression) element).getArgumentList().getText().trim().isEmpty())
                return false;
            final GrClosableBlock[] closureArguments = ((GrMethodCallExpression) element).getClosureArguments();
            if (closureArguments.length != 1)
                return false;
            final GrClosableBlock block = closureArguments[0];
            if (block.getLBrace() == null || block.getRBrace() == null)
                return false;
            return true;
        }
    };
}
Also used : GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiElementPredicate(org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with GrNewExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression in project intellij-community by JetBrains.

the class GrTypeDefinitionRangeHandler method getDeclarationRange.

@NotNull
@Override
public TextRange getDeclarationRange(@NotNull GrTypeDefinition aClass) {
    if (aClass instanceof GrAnonymousClassDefinition) {
        GrNewExpression call = (GrNewExpression) aClass.getParent();
        int startOffset = call.getTextRange().getStartOffset();
        int endOffset = call.getArgumentList().getTextRange().getEndOffset();
        return new TextRange(startOffset, endOffset);
    } else {
        final PsiModifierList modifierList = aClass.getModifierList();
        int startOffset = modifierList == null ? aClass.getTextRange().getStartOffset() : modifierList.getTextRange().getStartOffset();
        final GrImplementsClause implementsList = aClass.getImplementsClause();
        int endOffset = implementsList == null ? aClass.getTextRange().getEndOffset() : implementsList.getTextRange().getEndOffset();
        return new TextRange(startOffset, endOffset);
    }
}
Also used : GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrImplementsClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrImplementsClause) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) TextRange(com.intellij.openapi.util.TextRange) PsiModifierList(com.intellij.psi.PsiModifierList) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GrNewExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression)14 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)9 GrAnonymousClassDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition)7 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)6 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)5 PsiElement (com.intellij.psi.PsiElement)4 NotNull (org.jetbrains.annotations.NotNull)4 Nullable (org.jetbrains.annotations.Nullable)4 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)4 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)3 TextRange (com.intellij.openapi.util.TextRange)2 CommonCodeStyleSettings (com.intellij.psi.codeStyle.CommonCodeStyleSettings)2 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)2 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)2 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)2 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)2 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)2 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)2 GrSafeCastExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrSafeCastExpression)2 GrTypeCastExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrTypeCastExpression)2