Search in sources :

Example 1 with GrForInClause

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

the class ForToEachIntention method processIntention.

@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final GrForStatement parentStatement = (GrForStatement) element;
    final GrForInClause clause = (GrForInClause) parentStatement.getClause();
    final GrVariable var = clause.getDeclaredVariable();
    final GrStatement body = parentStatement.getBody();
    final String bodyText;
    if (body instanceof GrBlockStatement) {
        final String text = body.getText();
        bodyText = text.substring(1, text.length() - 1);
    } else {
        bodyText = body.getText();
    }
    GrExpression collection = clause.getIteratedExpression();
    assert collection != null;
    @NonNls final String statement = "x.each{" + var.getText() + " -> " + bodyText + " }";
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(parentStatement.getProject());
    final GrMethodCallExpression eachExpression = (GrMethodCallExpression) factory.createTopElementFromText(statement);
    ((GrReferenceExpression) eachExpression.getInvokedExpression()).getQualifierExpression().replaceWithExpression(collection, true);
    parentStatement.replaceWithStatement(eachExpression);
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) NonNls(org.jetbrains.annotations.NonNls) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrForInClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause) GrForStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrForStatement) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrBlockStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 2 with GrForInClause

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

the class GrSetStrongTypeIntention method getElementPredicate.

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

        @Override
        public boolean satisfiedBy(PsiElement element) {
            PsiElement parent = element.getParent();
            PsiElement pparent;
            if (isNameIdentifierOfVariable(element, parent) || isModifierListOfVar(element, parent)) {
                pparent = parent.getParent();
            } else if (isModifierListOfVarDecl(element, parent)) {
                pparent = parent;
            } else {
                return false;
            }
            if (pparent instanceof GrVariableDeclaration) {
                if (((GrVariableDeclaration) pparent).getTypeElementGroovy() != null)
                    return false;
                GrVariable[] variables = ((GrVariableDeclaration) pparent).getVariables();
                for (GrVariable variable : variables) {
                    if (isVarDeclaredWithInitializer(variable))
                        return true;
                }
            } else if (pparent instanceof GrForInClause) {
                final GrVariable variable = ((GrForInClause) pparent).getDeclaredVariable();
                return variable != null && variable.getTypeElementGroovy() == null && PsiUtil.extractIteratedType((GrForInClause) pparent) != null;
            } else if (parent instanceof GrParameter && pparent instanceof GrParameterList) {
                return ((GrParameter) parent).getTypeElementGroovy() == null && getClosureParameterType((PsiParameter) parent) != null;
            } else {
                final GrVariable variable = (GrVariable) parent;
                return variable.getTypeElementGroovy() == null && isVarDeclaredWithInitializer(variable);
            }
            return false;
        }

        private boolean isModifierListOfVarDecl(PsiElement element, PsiElement parent) {
            return parent instanceof GrVariableDeclaration && ((GrVariableDeclaration) parent).getModifierList() == element;
        }

        private boolean isModifierListOfVar(PsiElement element, PsiElement parent) {
            return parent instanceof GrVariable && ((GrVariable) parent).getModifierList() == element;
        }

        private boolean isNameIdentifierOfVariable(PsiElement element, PsiElement parent) {
            return parent instanceof GrVariable && ((GrVariable) parent).getTypeElementGroovy() == null && element == ((GrVariable) parent).getNameIdentifierGroovy();
        }
    };
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrForInClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause) PsiElementPredicate(org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with GrForInClause

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

the class GroovyAnnotator method visitVariable.

@Override
public void visitVariable(@NotNull GrVariable variable) {
    checkName(variable);
    PsiElement parent = variable.getParent();
    if (parent instanceof GrForInClause) {
        PsiElement delimiter = ((GrForInClause) parent).getDelimiter();
        if (delimiter.getNode().getElementType() == GroovyTokenTypes.mCOLON) {
            GrTypeElement typeElement = variable.getTypeElementGroovy();
            GrModifierList modifierList = variable.getModifierList();
            if (typeElement == null && StringUtil.isEmptyOrSpaces(modifierList.getText())) {
                Annotation annotation = myHolder.createErrorAnnotation(variable.getNameIdentifierGroovy(), GroovyBundle.message("java.style.for.each.statement.requires.a.type.declaration"));
                annotation.registerFix(new ReplaceDelimiterFix());
            }
        }
    }
    PsiNamedElement duplicate = ResolveUtil.findDuplicate(variable);
    if (duplicate instanceof GrVariable && (variable instanceof GrField || ResolveUtil.isScriptField(variable) || !(duplicate instanceof GrField))) {
        final String key = duplicate instanceof GrField ? "field.already.defined" : "variable.already.defined";
        myHolder.createErrorAnnotation(variable.getNameIdentifierGroovy(), GroovyBundle.message(key, variable.getName()));
    }
    PsiType type = variable.getDeclaredType();
    if (type instanceof PsiEllipsisType && !isLastParameter(variable)) {
        TextRange range = getEllipsisRange(variable);
        if (range == null) {
            range = getTypeRange(variable);
        }
        if (range != null) {
            myHolder.createErrorAnnotation(range, GroovyBundle.message("ellipsis.type.is.not.allowed.here"));
        }
    }
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) TextRange(com.intellij.openapi.util.TextRange) Annotation(com.intellij.lang.annotation.Annotation) GrForInClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause)

Example 4 with GrForInClause

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

the class ReplaceDelimiterFix method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final PsiFile file = element.getContainingFile();
    PsiElement at = file.findElementAt(editor.getCaretModel().getOffset());
    GrForStatement forStatement = PsiTreeUtil.getParentOfType(at, GrForStatement.class);
    if (forStatement == null)
        return;
    GrForClause clause = forStatement.getClause();
    if (clause instanceof GrForInClause) {
        GrForStatement stubFor = (GrForStatement) GroovyPsiElementFactory.getInstance(project).createStatementFromText("for (x in y){}");
        PsiElement newDelimiter = ((GrForInClause) stubFor.getClause()).getDelimiter();
        PsiElement delimiter = ((GrForInClause) clause).getDelimiter();
        delimiter.replace(newDelimiter);
    }
}
Also used : GrForInClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause) GrForStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrForStatement) GrForClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForClause) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement)

Example 5 with GrForInClause

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

the class GroovyTypeCheckVisitor method visitVariable.

@Override
public void visitVariable(@NotNull GrVariable variable) {
    super.visitVariable(variable);
    final PsiType varType = variable.getType();
    final PsiElement parent = variable.getParent();
    if (variable instanceof GrParameter && ((GrParameter) variable).getDeclarationScope() instanceof GrMethod || parent instanceof GrForInClause) {
        return;
    } else if (parent instanceof GrVariableDeclaration && ((GrVariableDeclaration) parent).isTuple()) {
        //check tuple assignment:  def (int x, int y) = foo()
        final GrVariableDeclaration tuple = (GrVariableDeclaration) parent;
        final GrExpression initializer = tuple.getTupleInitializer();
        if (initializer == null)
            return;
        if (!(initializer instanceof GrListOrMap)) {
            PsiType type = initializer.getType();
            if (type == null)
                return;
            PsiType valueType = extractIterableTypeParameter(type, false);
            processAssignment(varType, valueType, tuple, variable.getNameIdentifierGroovy());
            return;
        }
    }
    GrExpression initializer = variable.getInitializerGroovy();
    if (initializer == null)
        return;
    processAssignment(varType, initializer, variable.getNameIdentifierGroovy(), "cannot.assign", variable, ApplicableTo.ASSIGNMENT);
}
Also used : GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrForInClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Aggregations

GrForInClause (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause)11 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)5 GrForClause (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForClause)4 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)4 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)4 PsiElement (com.intellij.psi.PsiElement)3 GrForStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrForStatement)3 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)3 TextRange (com.intellij.openapi.util.TextRange)2 GrTraditionalForClause (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrTraditionalForClause)2 GrParameterList (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList)2 Template (com.intellij.codeInsight.template.Template)1 TemplateBuilderImpl (com.intellij.codeInsight.template.TemplateBuilderImpl)1 TemplateEditingAdapter (com.intellij.codeInsight.template.TemplateEditingAdapter)1 TemplateManager (com.intellij.codeInsight.template.TemplateManager)1 Annotation (com.intellij.lang.annotation.Annotation)1 Document (com.intellij.openapi.editor.Document)1 RangeMarker (com.intellij.openapi.editor.RangeMarker)1 PsiFile (com.intellij.psi.PsiFile)1 PsiParameter (com.intellij.psi.PsiParameter)1