Search in sources :

Example 1 with GrCommandArgumentList

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

the class GroovyBlock method getChildAttributes.

@Override
@NotNull
public ChildAttributes getChildAttributes(final int newChildIndex) {
    ASTNode astNode = getNode();
    final PsiElement psiParent = astNode.getPsi();
    if (psiParent instanceof GroovyFileBase) {
        return new ChildAttributes(Indent.getNoneIndent(), null);
    }
    if (psiParent instanceof GrSwitchStatement) {
        List<Block> subBlocks = getSubBlocks();
        if (newChildIndex > 0) {
            Block block = subBlocks.get(newChildIndex - 1);
            if (block instanceof GroovyBlock) {
                PsiElement anchorPsi = ((GroovyBlock) block).getNode().getPsi();
                if (anchorPsi instanceof GrCaseSection) {
                    for (GrStatement statement : ((GrCaseSection) anchorPsi).getStatements()) {
                        if (statement instanceof GrBreakStatement || statement instanceof GrContinueStatement || statement instanceof GrReturnStatement || statement instanceof GrThrowStatement) {
                            final Indent indent = GroovyIndentProcessor.getSwitchCaseIndent(myContext.getSettings());
                            return new ChildAttributes(indent, null);
                        }
                    }
                    int indentSize = myContext.getSettings().getIndentOptions().INDENT_SIZE;
                    final int spaces = myContext.getSettings().INDENT_CASE_FROM_SWITCH ? 2 * indentSize : indentSize;
                    return new ChildAttributes(Indent.getSpaceIndent(spaces), null);
                }
            }
        }
    }
    if (psiParent instanceof GrCaseLabel) {
        return new ChildAttributes(GroovyIndentProcessor.getSwitchCaseIndent(getContext().getSettings()), null);
    }
    if (psiParent instanceof GrCaseSection) {
        return getSwitchIndent((GrCaseSection) psiParent, newChildIndex);
    }
    if (TokenSets.BLOCK_SET.contains(astNode.getElementType()) || GroovyElementTypes.SWITCH_STATEMENT.equals(astNode.getElementType())) {
        return new ChildAttributes(Indent.getNormalIndent(), null);
    }
    if (GroovyElementTypes.CASE_SECTION.equals(astNode.getElementType())) {
        return new ChildAttributes(Indent.getNormalIndent(), null);
    }
    if (psiParent instanceof GrBinaryExpression || psiParent instanceof GrConditionalExpression || psiParent instanceof GrCommandArgumentList || psiParent instanceof GrArgumentList || psiParent instanceof GrParameterList || psiParent instanceof GrListOrMap || psiParent instanceof GrAnnotationArgumentList || psiParent instanceof GrVariable || psiParent instanceof GrAssignmentExpression) {
        return new ChildAttributes(Indent.getContinuationWithoutFirstIndent(), null);
    }
    if (psiParent instanceof GrDocComment || psiParent instanceof GrDocTag) {
        return new ChildAttributes(Indent.getSpaceIndent(GroovyIndentProcessor.GDOC_COMMENT_INDENT), null);
    }
    if (psiParent instanceof GrIfStatement || psiParent instanceof GrLoopStatement) {
        return new ChildAttributes(Indent.getNormalIndent(), null);
    }
    if (psiParent instanceof GrLabeledStatement && newChildIndex == 2) {
        final Indent indent = getContext().getGroovySettings().INDENT_LABEL_BLOCKS ? Indent.getLabelIndent() : Indent.getNoneIndent();
        return new ChildAttributes(indent, null);
    }
    return new ChildAttributes(Indent.getNoneIndent(), null);
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) GrCommandArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrBinaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment) GrBreakStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrBreakStatement) GrContinueStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrContinueStatement) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection) ASTNode(com.intellij.lang.ASTNode) GrDocTag(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocTag) GrCaseLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseLabel) GrThrowStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrThrowStatement) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) GrAnnotationArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationArgumentList) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrConditionalExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrConditionalExpression) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with GrCommandArgumentList

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

the class GrMethodCallFixer method apply.

@Override
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
    final GrArgumentList argList = psiElement instanceof GrCall ? ((GrCall) psiElement).getArgumentList() : null;
    if (argList == null || argList instanceof GrCommandArgumentList)
        return;
    GrCall call = (GrCall) psiElement;
    PsiElement parenth = argList.getLastChild();
    if (parenth != null && ")".equals(parenth.getText()) || PsiImplUtil.hasClosureArguments(call))
        return;
    int endOffset = -1;
    for (PsiElement child = argList.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (!(child instanceof PsiErrorElement))
            continue;
        final PsiErrorElement errorElement = (PsiErrorElement) child;
        if (errorElement.getErrorDescription().contains("')'")) {
            endOffset = errorElement.getTextRange().getStartOffset();
            break;
        }
    }
    if (endOffset == -1) {
        endOffset = argList.getTextRange().getEndOffset();
    }
    final GrExpression[] params = argList.getExpressionArguments();
    if (params.length > 0 && GrForBodyFixer.startLine(editor.getDocument(), argList) != GrForBodyFixer.startLine(editor.getDocument(), params[0])) {
        endOffset = argList.getTextRange().getStartOffset() + 1;
    }
    endOffset = CharArrayUtil.shiftBackward(editor.getDocument().getCharsSequence(), endOffset - 1, " \t\n") + 1;
    editor.getDocument().insertString(endOffset, ")");
}
Also used : GrCommandArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList) PsiErrorElement(com.intellij.psi.PsiErrorElement) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiElement(com.intellij.psi.PsiElement)

Example 3 with GrCommandArgumentList

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

the class GroovyShellCompletionContributor method fillCompletionVariants.

@Override
public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) {
    PsiFile file = parameters.getOriginalFile();
    if (!(file instanceof GroovyShellCodeFragment))
        return;
    PsiElement position = parameters.getPosition();
    PsiElement parent = position.getParent();
    if (!(parent instanceof GrReferenceExpression && !((GrReferenceExpression) parent).isQualified()))
        return;
    if (PsiUtil.isExpressionStatement(parent)) {
        addAllCommands(result);
    } else if (parent.getParent() instanceof GrCommandArgumentList) {
        PsiElement ppparent = parent.getParent().getParent();
        if (ppparent instanceof GrMethodCall && isFirstArg((GrMethodCall) ppparent, parent)) {
            GrExpression invokedExpression = ((GrMethodCall) ppparent).getInvokedExpression();
            if (invokedExpression instanceof GrReferenceExpression && !((GrReferenceExpression) invokedExpression).isQualified()) {
                String name = ((GrReferenceExpression) invokedExpression).getReferenceName();
                if ("help".equals(name)) {
                    addAllCommands(result);
                } else if ("show".equals(name)) {
                    add(result, "classes");
                    add(result, "imports");
                    add(result, "preferences");
                    add(result, "all");
                } else if ("purge".equals(name)) {
                    add(result, "variables");
                    add(result, "classes");
                    add(result, "imports");
                    add(result, "preferences");
                    add(result, "all");
                } else if ("record".equals(name)) {
                    add(result, "start");
                    add(result, "stop");
                    add(result, "status");
                } else if ("history".equals(name)) {
                    add(result, "show");
                    add(result, "recall");
                    add(result, "flush");
                    add(result, "clear");
                }
            }
        }
    }
}
Also used : GrCommandArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList) GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) PsiFile(com.intellij.psi.PsiFile) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 4 with GrCommandArgumentList

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList in project android by JetBrains.

the class AndroidGradleSpellcheckingStrategy method isPrint.

private static boolean isPrint(PsiElement element) {
    PsiElement parent0 = element.getParent();
    if (parent0 == null) {
        return false;
    }
    PsiElement parent1 = parent0.getParent();
    if (parent1 == null) {
        return false;
    }
    PsiElement parent2 = parent1.getParent();
    if (parent2 == null) {
        return false;
    }
    if (parent2 instanceof GrCommandArgumentList) {
        parent2 = parent2.getParent();
    }
    if (parent2 instanceof GrApplicationStatement) {
        GrApplicationStatement call = (GrApplicationStatement) parent2;
        GrExpression propertyExpression = call.getInvokedExpression();
        if (propertyExpression instanceof GrReferenceExpression) {
            GrReferenceExpression propertyRef = (GrReferenceExpression) propertyExpression;
            String property = propertyRef.getReferenceName();
            if ("print".equals(property) || "println".equals(property)) {
                return true;
            }
        }
    }
    return false;
}
Also used : GrCommandArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrApplicationStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 5 with GrCommandArgumentList

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList in project android by JetBrains.

the class GradleDslLiteral method apply.

@Override
protected void apply() {
    GroovyPsiElement psiElement = getPsiElement();
    if (psiElement == null) {
        return;
    }
    GrLiteral newLiteral = createLiteral();
    if (newLiteral == null) {
        return;
    }
    if (myExpression != null) {
        PsiElement replace = myExpression.replace(newLiteral);
        if (replace instanceof GrLiteral) {
            myExpression = (GrLiteral) replace;
        }
    } else {
        PsiElement added;
        if (// Entries in [].
        psiElement instanceof GrListOrMap || (psiElement instanceof GrArgumentList && !(psiElement instanceof GrCommandArgumentList))) {
            // Method call arguments in ().
            // add before ) or ]
            added = psiElement.addBefore(newLiteral, psiElement.getLastChild());
        } else {
            added = psiElement.addAfter(newLiteral, psiElement.getLastChild());
        }
        if (added instanceof GrLiteral) {
            myExpression = (GrLiteral) added;
        }
        if (myUnsavedConfigBlock != null) {
            addConfigBlock();
        }
    }
}
Also used : GrCommandArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Aggregations

GrCommandArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList)8 PsiElement (com.intellij.psi.PsiElement)7 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)6 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)5 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)3 GrApplicationStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement)3 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)3 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)3 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)2 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)2 GrParameterList (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList)2 ASTNode (com.intellij.lang.ASTNode)1 PsiErrorElement (com.intellij.psi.PsiErrorElement)1 PsiFile (com.intellij.psi.PsiFile)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 NotNull (org.jetbrains.annotations.NotNull)1 GrDocComment (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment)1 GrDocTag (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocTag)1 GroovyElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor)1 GroovyFileBase (org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase)1