Search in sources :

Example 31 with GrClosableBlock

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

the class MultipleRepositoryUrlsFix method doFix.

@Override
protected void doFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) throws IncorrectOperationException {
    List<GrCallExpression> statements = MultipleRepositoryUrlsInspection.findUrlCallExpressions(myClosure);
    if (statements.size() <= 1)
        return;
    statements.remove(0);
    List<PsiElement> elements = new ArrayList<>(statements);
    for (GrCallExpression statement : statements) {
        PsiElement newLineCandidate = statement.getNextSibling();
        if (PsiUtil.isNewLine(newLineCandidate)) {
            elements.add(newLineCandidate);
        }
    }
    myClosure.removeElements(elements.toArray(new PsiElement[elements.size()]));
    GrClosableBlock closableBlock = PsiTreeUtil.getParentOfType(myClosure, GrClosableBlock.class);
    if (closableBlock == null)
        return;
    GroovyPsiElementFactory elementFactory = GroovyPsiElementFactory.getInstance(project);
    for (GrCallExpression statement : statements) {
        closableBlock.addStatementBefore(elementFactory.createStatementFromText(myRepoType + '{' + statement.getText() + '}'), null);
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrCallExpression) ArrayList(java.util.ArrayList) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) PsiElement(com.intellij.psi.PsiElement)

Example 32 with GrClosableBlock

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

the class CustomAnnotationChecker method checkAnnotationValueByType.

public static void checkAnnotationValueByType(@NotNull AnnotationHolder holder, @NotNull GrAnnotationMemberValue value, @Nullable PsiType ltype, boolean skipArrays) {
    final GlobalSearchScope resolveScope = value.getResolveScope();
    final PsiManager manager = value.getManager();
    if (value instanceof GrExpression) {
        final PsiType rtype;
        if (value instanceof GrClosableBlock) {
            rtype = PsiType.getJavaLangClass(manager, resolveScope);
        } else {
            rtype = ((GrExpression) value).getType();
        }
        if (rtype != null && !checkAnnoTypeAssignable(ltype, rtype, value, skipArrays)) {
            holder.createErrorAnnotation(value, GroovyBundle.message("cannot.assign", rtype.getPresentableText(), ltype.getPresentableText()));
        }
    } else if (value instanceof GrAnnotation) {
        final PsiElement resolved = ((GrAnnotation) value).getClassReference().resolve();
        if (resolved instanceof PsiClass) {
            final PsiClassType rtype = JavaPsiFacade.getElementFactory(value.getProject()).createType((PsiClass) resolved, PsiSubstitutor.EMPTY);
            if (!checkAnnoTypeAssignable(ltype, rtype, value, skipArrays)) {
                holder.createErrorAnnotation(value, GroovyBundle.message("cannot.assign", rtype.getPresentableText(), ltype.getPresentableText()));
            }
        }
    } else if (value instanceof GrAnnotationArrayInitializer) {
        if (ltype instanceof PsiArrayType) {
            final PsiType componentType = ((PsiArrayType) ltype).getComponentType();
            final GrAnnotationMemberValue[] initializers = ((GrAnnotationArrayInitializer) value).getInitializers();
            for (GrAnnotationMemberValue initializer : initializers) {
                checkAnnotationValueByType(holder, initializer, componentType, false);
            }
        } else {
            final PsiType rtype = TypesUtil.getTupleByAnnotationArrayInitializer((GrAnnotationArrayInitializer) value);
            if (!checkAnnoTypeAssignable(ltype, rtype, value, skipArrays)) {
                holder.createErrorAnnotation(value, GroovyBundle.message("cannot.assign", rtype.getPresentableText(), ltype.getPresentableText()));
            }
        }
    }
}
Also used : GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrAnnotationArrayInitializer(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationArrayInitializer) GrAnnotationMemberValue(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationMemberValue) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 33 with GrClosableBlock

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

the class GroovyAnnotator method checkAnnotationAttributeValue.

private boolean checkAnnotationAttributeValue(@Nullable GrAnnotationMemberValue value, @NotNull PsiElement toHighlight) {
    if (value == null)
        return false;
    if (value instanceof GrLiteral)
        return false;
    if (value instanceof GrClosableBlock)
        return false;
    if (value instanceof GrAnnotation)
        return false;
    if (value instanceof GrReferenceExpression) {
        PsiElement resolved = ((GrReferenceExpression) value).resolve();
        if (resolved instanceof PsiClass)
            return false;
        if (resolved instanceof PsiEnumConstant)
            return false;
        if (resolved == null && isClassReference(value))
            return false;
        if (resolved instanceof GrAccessorMethod)
            resolved = ((GrAccessorMethod) resolved).getProperty();
        if (resolved instanceof PsiField) {
            GrExpression initializer;
            try {
                if (resolved instanceof GrField) {
                    initializer = ((GrField) resolved).getInitializerGroovy();
                } else {
                    final PsiExpression _initializer = ((PsiField) resolved).getInitializer();
                    initializer = _initializer != null ? (GrExpression) ExpressionConverter.getExpression(_initializer, GroovyLanguage.INSTANCE, value.getProject()) : null;
                }
            } catch (IncorrectOperationException e) {
                initializer = null;
            }
            if (initializer != null) {
                return checkAnnotationAttributeValue(initializer, toHighlight);
            }
        }
    }
    if (value instanceof GrAnnotationArrayInitializer) {
        for (GrAnnotationMemberValue expression : ((GrAnnotationArrayInitializer) value).getInitializers()) {
            if (checkAnnotationAttributeValue(expression, toHighlight))
                return true;
        }
        return false;
    }
    if (value instanceof GrUnaryExpression) {
        final IElementType tokenType = ((GrUnaryExpression) value).getOperationTokenType();
        if (tokenType == GroovyTokenTypes.mMINUS || tokenType == GroovyTokenTypes.mPLUS) {
            return checkAnnotationAttributeValue(((GrUnaryExpression) value).getOperand(), toHighlight);
        }
    }
    myHolder.createErrorAnnotation(toHighlight, GroovyBundle.message("expected.0.to.be.inline.constant", value.getText()));
    return true;
}
Also used : GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) IElementType(com.intellij.psi.tree.IElementType) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 34 with GrClosableBlock

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

the class GrUnnecessarySemicolonInspection method isSemicolonNecessary.

private static boolean isSemicolonNecessary(@NotNull PsiElement semicolon) {
    if (semicolon.getParent() instanceof GrTraditionalForClause)
        return true;
    PsiElement prevSibling = PsiUtil.skipSet(semicolon, false, BACKWARD_SET);
    PsiElement nextSibling = PsiUtil.skipSet(semicolon, true, FORWARD_SET);
    return prevSibling instanceof GrStatement && (nextSibling instanceof GrStatement || nextSibling != null && nextSibling.getNextSibling() instanceof GrClosableBlock);
}
Also used : GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) PsiElement(com.intellij.psi.PsiElement) GrTraditionalForClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrTraditionalForClause) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 35 with GrClosableBlock

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

the class UnassignedVariableAccessInspection method check.

@Override
protected void check(@NotNull GrControlFlowOwner owner, @NotNull ProblemsHolder problemsHolder) {
    Instruction[] flow = owner.getControlFlow();
    ReadWriteVariableInstruction[] reads = ControlFlowBuilderUtil.getReadsWithoutPriorWrites(flow, true);
    for (ReadWriteVariableInstruction read : reads) {
        PsiElement element = read.getElement();
        if (element instanceof GroovyPsiElement && !(element instanceof GrClosableBlock)) {
            String name = read.getVariableName();
            GroovyPsiElement property = ResolveUtil.resolveProperty((GroovyPsiElement) element, name);
            if (property != null && !(property instanceof PsiParameter) && !(property instanceof PsiField) && PsiTreeUtil.isAncestor(owner, property, false) && !(myIgnoreBooleanExpressions && isBooleanCheck(element))) {
                problemsHolder.registerProblem(element, GroovyInspectionBundle.message("unassigned.access.tooltip", name));
            }
        }
    }
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) PsiParameter(com.intellij.psi.PsiParameter) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) PsiField(com.intellij.psi.PsiField) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Aggregations

GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)116 PsiElement (com.intellij.psi.PsiElement)32 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)31 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)26 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)26 Nullable (org.jetbrains.annotations.Nullable)23 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)23 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)18 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)17 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)15 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)15 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)14 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)13 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)12 ArrayList (java.util.ArrayList)11 NotNull (org.jetbrains.annotations.NotNull)10 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)10 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)10 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)10 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)9