Search in sources :

Example 81 with GroovyPsiElement

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

the class GroovyPsiElementImpl method acceptGroovyChildren.

public static void acceptGroovyChildren(PsiElement parent, GroovyElementVisitor visitor) {
    PsiElement child = parent.getFirstChild();
    while (child != null) {
        ProgressManager.checkCanceled();
        if (child instanceof GroovyPsiElement) {
            ((GroovyPsiElement) child).accept(visitor);
        }
        child = child.getNextSibling();
    }
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) ASTDelegatePsiElement(com.intellij.extapi.psi.ASTDelegatePsiElement) PsiElement(com.intellij.psi.PsiElement) ASTWrapperPsiElement(com.intellij.extapi.psi.ASTWrapperPsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 82 with GroovyPsiElement

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

the class GrReassignedLocalVarsChecker method isReassignedVarImpl.

private static boolean isReassignedVarImpl(@NotNull final GrVariable resolved) {
    final GrControlFlowOwner variableScope = PsiTreeUtil.getParentOfType(resolved, GrCodeBlock.class, GroovyFile.class);
    if (variableScope == null)
        return false;
    final String name = resolved.getName();
    final Ref<Boolean> isReassigned = Ref.create(false);
    for (PsiElement scope = resolved.getParent().getNextSibling(); scope != null; scope = scope.getNextSibling()) {
        if (scope instanceof GroovyPsiElement) {
            ((GroovyPsiElement) scope).accept(new GroovyRecursiveElementVisitor() {

                @Override
                public void visitClosure(@NotNull GrClosableBlock closure) {
                    if (getUsedVarsInsideBlock(closure).contains(name)) {
                        isReassigned.set(true);
                    }
                }

                @Override
                public void visitElement(@NotNull GroovyPsiElement element) {
                    if (isReassigned.get())
                        return;
                    super.visitElement(element);
                }
            });
            if (isReassigned.get())
                break;
        }
    }
    return isReassigned.get();
}
Also used : GrControlFlowOwner(org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 83 with GroovyPsiElement

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

the class ConvertConcatenationToGstringIntention method getOperandText.

private static void getOperandText(@Nullable GrExpression operand, StringBuilder builder, boolean multiline) {
    if (operand instanceof GrRegex) {
        for (GroovyPsiElement element : ((GrRegex) operand).getAllContentParts()) {
            if (element instanceof GrStringInjection) {
                builder.append(element.getText());
            } else if (element instanceof GrStringContent) {
                if (GrStringUtil.isDollarSlashyString((GrLiteral) operand)) {
                    processDollarSlashyContent(builder, multiline, element.getText());
                } else {
                    processSlashyContent(builder, multiline, element.getText());
                }
            }
        }
    } else if (operand instanceof GrString) {
        boolean isMultiline = GrStringUtil.isMultilineStringLiteral((GrLiteral) operand);
        for (GroovyPsiElement element : ((GrString) operand).getAllContentParts()) {
            if (element instanceof GrStringInjection) {
                builder.append(element.getText());
            } else if (element instanceof GrStringContent) {
                if (isMultiline) {
                    processMultilineGString(builder, element.getText());
                } else {
                    processSinglelineGString(builder, element.getText());
                }
            }
        }
    } else if (operand instanceof GrLiteral) {
        String text = GrStringUtil.removeQuotes(operand.getText());
        GrLiteral literal = (GrLiteral) operand;
        if (GrStringUtil.isSingleQuoteString(literal)) {
            processSinglelineString(builder, text);
        } else if (GrStringUtil.isTripleQuoteString(literal)) {
            processMultilineString(builder, text);
        } else if (GrStringUtil.isDoubleQuoteString(literal)) {
            processSinglelineGString(builder, text);
        } else if (GrStringUtil.isTripleDoubleQuoteString(literal)) {
            processMultilineGString(builder, text);
        } else if (GrStringUtil.isSlashyString(literal)) {
            processSlashyContent(builder, multiline, text);
        } else if (GrStringUtil.isDollarSlashyString(literal)) {
            processDollarSlashyContent(builder, multiline, text);
        }
    } else if (MyPredicate.satisfied(operand)) {
        performIntention((GrBinaryExpression) operand, builder, multiline);
    } else if (isToStringMethod(operand, builder)) {
    //nothing to do
    } else {
        builder.append(START_BRACE).append(operand == null ? "" : operand.getText()).append(END_BRACE);
    }
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrBinaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression)

Example 84 with GroovyPsiElement

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

the class ConvertMultilineStringToSingleLineIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    String quote = element.getText().substring(0, 1);
    StringBuilder buffer = new StringBuilder();
    buffer.append(quote);
    GrExpression old;
    if (element instanceof GrLiteralImpl) {
        appendSimpleStringValue(element, buffer, quote);
        old = (GrExpression) element;
    } else {
        final GrStringImpl gstring = (GrStringImpl) element;
        for (GroovyPsiElement child : gstring.getAllContentParts()) {
            if (child instanceof GrStringContent) {
                appendSimpleStringValue(child, buffer, "\"");
            } else if (child instanceof GrStringInjection) {
                buffer.append(child.getText());
            }
        }
        old = gstring;
    }
    buffer.append(quote);
    try {
        final int offset = editor.getCaretModel().getOffset();
        final TextRange range = old.getTextRange();
        int shift;
        if (range.getStartOffset() == offset) {
            shift = 0;
        } else if (range.getStartOffset() == offset - 1) {
            shift = -1;
        } else if (range.getEndOffset() == offset) {
            shift = -4;
        } else if (range.getEndOffset() == offset + 1) {
            shift = -3;
        } else {
            shift = -2;
        }
        final GrExpression newLiteral = GroovyPsiElementFactory.getInstance(project).createExpressionFromText(buffer.toString());
        old.replaceWithExpression(newLiteral, true);
        if (shift != 0) {
            editor.getCaretModel().moveToOffset(offset + shift);
        }
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
}
Also used : GrStringContent(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringContent) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrLiteralImpl(org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.literals.GrLiteralImpl) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrStringImpl(org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.literals.GrStringImpl) TextRange(com.intellij.openapi.util.TextRange) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrStringInjection(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection)

Example 85 with GroovyPsiElement

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

the class RemoveParenthesesFromMethodPredicate method satisfiedBy.

@Override
public boolean satisfiedBy(PsiElement element) {
    if (!(element instanceof GrMethodCallExpression))
        return false;
    if (!PsiUtil.isExpressionStatement(element))
        return false;
    final GrMethodCallExpression expression = (GrMethodCallExpression) element;
    if (PsiImplUtil.hasClosureArguments(expression))
        return false;
    final StringBuilder newStatementText = new StringBuilder();
    newStatementText.append(expression.getInvokedExpression().getText()).append(' ');
    GrArgumentList argumentList = expression.getArgumentList();
    final GroovyPsiElement[] allArguments = argumentList.getAllArguments();
    argumentList = (GrArgumentList) argumentList.copy();
    final PsiElement leftParen = argumentList.getLeftParen();
    final PsiElement rightParen = argumentList.getRightParen();
    if (leftParen != null)
        leftParen.delete();
    if (rightParen != null)
        rightParen.delete();
    newStatementText.append(argumentList.getText());
    final GrStatement newStatement;
    try {
        newStatement = GroovyPsiElementFactory.getInstance(element.getProject()).createStatementFromText(newStatementText.toString());
    } catch (IncorrectOperationException e) {
        return false;
    }
    if (newStatement instanceof GrApplicationStatement) {
        final GrCommandArgumentList newArgList = ((GrApplicationStatement) newStatement).getArgumentList();
        if (newArgList.getAllArguments().length == allArguments.length) {
            return true;
        }
    }
    return false;
}
Also used : GrCommandArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrApplicationStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Aggregations

GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)98 PsiElement (com.intellij.psi.PsiElement)34 Nullable (org.jetbrains.annotations.Nullable)17 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)17 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)16 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)13 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)12 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)11 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)9 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)8 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)8 GrApplicationStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement)8 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)8 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)8 Project (com.intellij.openapi.project.Project)7 TextRange (com.intellij.openapi.util.TextRange)7 NotNull (org.jetbrains.annotations.NotNull)7 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)7 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)7 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)6