Search in sources :

Example 6 with GrForClause

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

the class GrForStatementImpl method processDeclarations.

@Override
public boolean processDeclarations(@NotNull PsiScopeProcessor processor, @NotNull ResolveState state, @Nullable PsiElement lastParent, @NotNull PsiElement place) {
    if (!ResolveUtil.shouldProcessProperties(processor.getHint(ElementClassHint.KEY)))
        return true;
    GrForClause forClause = getClause();
    final GrVariable varScope = PsiTreeUtil.getParentOfType(place, GrVariable.class);
    if (forClause == null)
        return true;
    if (lastParent == null || lastParent instanceof GrForInClause)
        return true;
    GrVariable var = forClause.getDeclaredVariable();
    if (var == null || var.equals(varScope))
        return true;
    if (!ResolveUtil.processElement(processor, var, state))
        return false;
    return true;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrForInClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause) GrForClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForClause)

Example 7 with GrForClause

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

the class GroovyValidationUtil method validateVariableOccurrencesUp.

private static void validateVariableOccurrencesUp(PsiElement parent, PsiElement lastParent, MultiMap<PsiElement, String> conflicts, @NotNull String varName, final boolean containerIsFile) {
    if (!containerIsFile && (parent instanceof PsiFile) || parent == null)
        return;
    PsiElement child = parent.getFirstChild();
    while (child != null && child != lastParent) {
        // Upper variable declarations
        if (child instanceof GrVariableDeclaration) {
            for (GrVariable variable : ((GrVariableDeclaration) child).getVariables()) {
                if (varName.equals(variable.getName())) {
                    addConflict(varName, variable, conflicts);
                }
            }
        }
        child = child.getNextSibling();
    }
    if (parent instanceof GrParametersOwner) {
        //method or closure parameters
        GrParametersOwner owner = (GrParametersOwner) parent;
        for (GrParameter parameter : owner.getParameters()) {
            if (varName.equals(parameter.getName())) {
                addConflict(varName, parameter, conflicts);
            }
        }
    } else if (parent instanceof GrForStatement) {
        // For statement binding
        GrForStatement statement = (GrForStatement) parent;
        GrForClause clause = statement.getClause();
        if (clause != null) {
            final GrVariable variable = clause.getDeclaredVariable();
            if (variable != null && varName.equals(variable.getName())) {
                addConflict(varName, variable, conflicts);
            }
        }
    }
    if (parent instanceof PsiFile)
        return;
    validateVariableOccurrencesUp(parent.getParent(), parent, conflicts, varName, false);
}
Also used : GrForClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForClause) PsiFile(com.intellij.psi.PsiFile) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) PsiElement(com.intellij.psi.PsiElement)

Example 8 with GrForClause

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

the class EachToForIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final GrMethodCallExpression expression = (GrMethodCallExpression) element;
    final GrClosableBlock block = expression.getClosureArguments()[0];
    final GrParameterList parameterList = block.getParameterList();
    final GrParameter[] parameters = parameterList.getParameters();
    String var;
    if (parameters.length == 1) {
        var = parameters[0].getText();
        var = StringUtil.replace(var, GrModifier.DEF, "");
    } else {
        var = "it";
    }
    final GrExpression invokedExpression = expression.getInvokedExpression();
    GrExpression qualifier = ((GrReferenceExpression) invokedExpression).getQualifierExpression();
    final GroovyPsiElementFactory elementFactory = GroovyPsiElementFactory.getInstance(element.getProject());
    if (qualifier == null) {
        qualifier = elementFactory.createExpressionFromText("this");
    }
    StringBuilder builder = new StringBuilder();
    builder.append("for (").append(var).append(" in ").append(qualifier.getText()).append(") {\n");
    String text = block.getText();
    final PsiElement blockArrow = block.getArrow();
    int index;
    if (blockArrow != null) {
        index = blockArrow.getStartOffsetInParent() + blockArrow.getTextLength();
    } else {
        index = 1;
    }
    while (index < text.length() && Character.isWhitespace(text.charAt(index))) index++;
    text = text.substring(index, text.length() - 1);
    builder.append(text);
    builder.append("}");
    final GrStatement statement = elementFactory.createStatementFromText(builder.toString());
    GrForStatement forStatement = (GrForStatement) expression.replaceWithStatement(statement);
    final GrForClause clause = forStatement.getClause();
    GrVariable variable = clause.getDeclaredVariable();
    forStatement = updateReturnStatements(forStatement);
    if (variable == null)
        return;
    if (ApplicationManager.getApplication().isUnitTestMode())
        return;
    final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
    final Document doc = documentManager.getDocument(element.getContainingFile());
    if (doc == null)
        return;
    documentManager.doPostponedOperationsAndUnblockDocument(doc);
    editor.getCaretModel().moveToOffset(variable.getTextOffset());
    new VariableInplaceRenamer(variable, editor).performInplaceRename();
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) Document(com.intellij.openapi.editor.Document) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrForClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForClause) VariableInplaceRenamer(com.intellij.refactoring.rename.inplace.VariableInplaceRenamer) PsiElement(com.intellij.psi.PsiElement) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 9 with GrForClause

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

the class ForToEachPredicate method satisfiedBy.

@Override
public boolean satisfiedBy(PsiElement element) {
    if (!(element instanceof GrForStatement)) {
        return false;
    }
    final GrForStatement statement = (GrForStatement) element;
    final GrForClause clause = statement.getClause();
    if (!(clause instanceof GrForInClause) || ((GrForInClause) clause).getIteratedExpression() == null) {
        return false;
    }
    return !ErrorUtil.containsError(element);
}
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)

Example 10 with GrForClause

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

the class CodeBlockGenerator method visitForStatement.

@Override
public void visitForStatement(@NotNull GrForStatement forStatement) {
    //final StringBuilder builder = new StringBuilder();
    builder.append("for(");
    final GrForClause clause = forStatement.getClause();
    ExpressionContext forContext = context.extend();
    if (clause instanceof GrForInClause) {
        final GrExpression expression = ((GrForInClause) clause).getIteratedExpression();
        final GrVariable declaredVariable = clause.getDeclaredVariable();
        LOG.assertTrue(declaredVariable != null);
        writeVariableWithoutSemicolonAndInitializer(builder, declaredVariable, context);
        builder.append(" : ");
        if (expression != null) {
            final ExpressionContext context = forContext.copy();
            writeExpression(expression, builder, context);
        }
    } else if (clause instanceof GrTraditionalForClause) {
        final GrTraditionalForClause cl = (GrTraditionalForClause) clause;
        final GrCondition initialization = cl.getInitialization();
        final GrExpression condition = cl.getCondition();
        final GrExpression update = cl.getUpdate();
        if (initialization instanceof GrParameter) {
            StringBuilder partBuilder = new StringBuilder();
            writeVariableWithoutSemicolonAndInitializer(partBuilder, (GrParameter) initialization, context);
            final GrExpression initializer = ((GrParameter) initialization).getInitializerGroovy();
            if (initializer != null) {
                final ExpressionContext partContext = forContext.copy();
                partBuilder.append(" = ");
                writeExpression(initializer, partBuilder, partContext);
                for (String statement : partContext.myStatements) {
                    builder.append(statement).append(", ");
                }
                builder.append(partBuilder);
            }
        } else if (initialization != null) {
            StringBuilder partBuilder = new StringBuilder();
            final ExpressionContext partContext = forContext.copy();
            genForPart(builder, initialization, new CodeBlockGenerator(partBuilder, partContext, null));
        }
        builder.append(';');
        if (condition != null) {
            //todo???
            genForPart(builder, condition, forContext.copy());
        }
        builder.append(';');
        if (update != null) {
            genForPart(builder, update, forContext.copy());
        }
    }
    builder.append(')');
    final GrStatement body = forStatement.getBody();
    if (body != null) {
        body.accept(new CodeBlockGenerator(builder, forContext, null));
    }
}
Also used : GrForInClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause) GrForClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForClause) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GrCondition(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrCondition) GrTraditionalForClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrTraditionalForClause)

Aggregations

GrForClause (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForClause)10 GrForInClause (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause)4 PsiElement (com.intellij.psi.PsiElement)3 GrForStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrForStatement)3 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)3 PsiFile (com.intellij.psi.PsiFile)2 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)2 Document (com.intellij.openapi.editor.Document)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)1 VariableInplaceRenamer (com.intellij.refactoring.rename.inplace.VariableInplaceRenamer)1 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)1 GrCondition (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrCondition)1 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)1 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)1 GrTraditionalForClause (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrTraditionalForClause)1 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)1 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)1 GrParameterList (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList)1