Search in sources :

Example 1 with GrLabeledStatement

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

the class GroovyBlockGenerator method calculateAlignments.

private void calculateAlignments(List<ASTNode> children, boolean classLevel) {
    List<GrStatement> currentGroup = null;
    boolean spock = true;
    for (ASTNode child : children) {
        PsiElement psi = child.getPsi();
        if (psi instanceof GrLabeledStatement) {
            alignGroup(currentGroup, spock, classLevel);
            currentGroup = ContainerUtil.newArrayList();
            spock = true;
        } else if (currentGroup != null && spock && isTablePart(psi)) {
            currentGroup.add((GrStatement) psi);
        } else if (psi instanceof GrVariableDeclaration) {
            GrVariable[] variables = ((GrVariableDeclaration) psi).getVariables();
            if (variables.length > 0) {
                if (!classLevel || currentGroup == null || fieldGroupEnded(psi) || spock) {
                    alignGroup(currentGroup, spock, classLevel);
                    currentGroup = ContainerUtil.newArrayList();
                    spock = false;
                }
                currentGroup.add((GrStatement) psi);
            }
        } else {
            if (shouldSkip(classLevel, psi))
                continue;
            alignGroup(currentGroup, spock, classLevel);
            currentGroup = null;
        }
    }
    if (currentGroup != null) {
        alignGroup(currentGroup, spock, classLevel);
    }
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) ASTNode(com.intellij.lang.ASTNode) GrLabeledStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrLabeledStatement) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 2 with GrLabeledStatement

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

the class GroovyRefactoringSupportProvider method isInplaceRenameAvailable.

@Override
public boolean isInplaceRenameAvailable(@NotNull PsiElement elementToRename, PsiElement nameSuggestionContext) {
    if (nameSuggestionContext != null && nameSuggestionContext.getContainingFile() != elementToRename.getContainingFile())
        return false;
    if (!(elementToRename instanceof GrLabeledStatement)) {
        return false;
    }
    SearchScope useScope = PsiSearchHelper.SERVICE.getInstance(elementToRename.getProject()).getUseScope(elementToRename);
    if (!(useScope instanceof LocalSearchScope))
        return false;
    PsiElement[] scopeElements = ((LocalSearchScope) useScope).getScope();
    if (scopeElements.length > 1) {
        return false;
    }
    PsiFile containingFile = elementToRename.getContainingFile();
    return PsiTreeUtil.isAncestor(containingFile, scopeElements[0], false);
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) PsiFile(com.intellij.psi.PsiFile) GrLabeledStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrLabeledStatement) PsiElement(com.intellij.psi.PsiElement)

Example 3 with GrLabeledStatement

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

the class SpockUtils method createVariableMap.

// See org.spockframework.compiler.WhereBlockRewriter
public static Map<String, SpockVariableDescriptor> createVariableMap(GrMethod method) {
    GrOpenBlock block = method.getBlock();
    if (block == null)
        return Collections.emptyMap();
    PsiElement elementUnderLabel = null;
    PsiElement elementAfterLabel = null;
    main: for (PsiElement e = block.getFirstChild(); e != null; e = e.getNextSibling()) {
        if (e instanceof GrLabeledStatement) {
            GrLabeledStatement l = (GrLabeledStatement) e;
            elementAfterLabel = l.getNextSibling();
            while (true) {
                GrStatement statement = l.getStatement();
                if ("where".equals(l.getName())) {
                    elementUnderLabel = statement;
                    break main;
                }
                if (statement instanceof GrLabeledStatement) {
                    l = (GrLabeledStatement) statement;
                    continue;
                }
                break;
            }
        }
    }
    if (elementUnderLabel == null)
        return Collections.emptyMap();
    Map<String, SpockVariableDescriptor> res = new HashMap<>();
    PsiElement e = elementUnderLabel;
    while (e != null) {
        if (e instanceof GrBinaryExpression && ((GrBinaryExpression) e).getOperationTokenType() == GroovyElementTypes.COMPOSITE_LSHIFT_SIGN) {
            GrBinaryExpression shift = (GrBinaryExpression) e;
            GrExpression leftOperand = shift.getLeftOperand();
            GrExpression rightOperand = shift.getRightOperand();
            if (leftOperand instanceof GrReferenceExpression) {
                String name = getNameByReference(leftOperand);
                if (name != null) {
                    SpockVariableDescriptor descriptor = new SpockVariableDescriptor(leftOperand, name);
                    descriptor.addExpressionOfCollection(rightOperand);
                    res.put(name, descriptor);
                }
            } else if (leftOperand instanceof GrListOrMap) {
                GrExpression[] variableDefinitions = ((GrListOrMap) leftOperand).getInitializers();
                SpockVariableDescriptor[] variables = createVariables(res, Arrays.asList(variableDefinitions));
                if (rightOperand instanceof GrListOrMap) {
                    for (GrExpression expression : ((GrListOrMap) rightOperand).getInitializers()) {
                        if (expression instanceof GrListOrMap) {
                            add(variables, Arrays.asList(((GrListOrMap) expression).getInitializers()));
                        } else {
                            for (SpockVariableDescriptor variable : variables) {
                                if (variable != null) {
                                    variable.addExpressionOfCollection(expression);
                                }
                            }
                        }
                    }
                }
            }
        } else if (e instanceof GrAssignmentExpression) {
            GrAssignmentExpression assExpr = (GrAssignmentExpression) e;
            GrExpression lValue = assExpr.getLValue();
            String name = getNameByReference(lValue);
            if (name != null) {
                res.put(name, new SpockVariableDescriptor(lValue, name).addExpression(assExpr.getRValue()));
            }
        } else if (isOrStatement(e)) {
            // See org.spockframework.compiler.WhereBlockRewriter#rewriteTableLikeParameterization()
            List<GrExpression> variableDefinitions = new ArrayList<>();
            splitOr(variableDefinitions, (GrExpression) e);
            SpockVariableDescriptor[] variables = createVariables(res, variableDefinitions);
            List<GrExpression> row = new ArrayList<>();
            PsiElement rowElement = getNext(e, elementUnderLabel, elementAfterLabel);
            while (isOrStatement(rowElement)) {
                row.clear();
                splitOr(row, (GrExpression) rowElement);
                add(variables, row);
                rowElement = getNext(rowElement, elementUnderLabel, elementAfterLabel);
            }
            e = rowElement;
            continue;
        }
        e = getNext(e, elementUnderLabel, elementAfterLabel);
    }
    return res;
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrBinaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression) GrLabeledStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrLabeledStatement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) PsiElement(com.intellij.psi.PsiElement)

Example 4 with GrLabeledStatement

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

the class GrLabelReference method getVariants.

@Override
@NotNull
public Object[] getVariants() {
    final List<PsiElement> result = new ArrayList<>();
    PsiElement context = myStatement;
    while (context != null) {
        if (context instanceof GrLabeledStatement) {
            result.add(context);
        }
        context = context.getContext();
    }
    return ArrayUtil.toObjectArray(result);
}
Also used : ArrayList(java.util.ArrayList) GrLabeledStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrLabeledStatement) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with GrLabeledStatement

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

the class GroovyCompletionUtil method isNewStatement.

/**
   * Shows whether keyword may be placed as a new statement beginning
   *
   * @param element
   * @param canBeAfterBrace May be after '{' symbol or not
   * @return
   */
public static boolean isNewStatement(PsiElement element, boolean canBeAfterBrace) {
    PsiElement previousLeaf = getLeafByOffset(element.getTextRange().getStartOffset() - 1, element);
    previousLeaf = PsiImplUtil.realPrevious(previousLeaf);
    if (previousLeaf != null) {
        if (canBeAfterBrace && GroovyTokenTypes.mLCURLY.equals(previousLeaf.getNode().getElementType())) {
            return true;
        }
        if (GroovyTokenTypes.mCOLON.equals(previousLeaf.getNode().getElementType()) && previousLeaf.getParent() instanceof GrLabeledStatement) {
            return true;
        }
    }
    return (previousLeaf == null || SEPARATORS.contains(previousLeaf.getNode().getElementType()));
}
Also used : GrLabeledStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrLabeledStatement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

Aggregations

GrLabeledStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrLabeledStatement)5 PsiElement (com.intellij.psi.PsiElement)4 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)2 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)2 ASTNode (com.intellij.lang.ASTNode)1 PsiFile (com.intellij.psi.PsiFile)1 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)1 SearchScope (com.intellij.psi.search.SearchScope)1 ArrayList (java.util.ArrayList)1 NotNull (org.jetbrains.annotations.NotNull)1 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)1 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)1 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)1 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)1 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)1 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)1 GrBinaryExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression)1 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)1 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)1