Search in sources :

Example 76 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 77 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 78 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)

Example 79 with GroovyPsiElement

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

the class CompleteReferencesWithSameQualifier method addVariantsWithSameQualifier.

private void addVariantsWithSameQualifier(@NotNull PsiElement element, @NotNull Set<String> result) {
    if (element instanceof GrReferenceExpression && element != myRefExpr && !PsiUtil.isLValue((GroovyPsiElement) element)) {
        final GrReferenceExpression refExpr = (GrReferenceExpression) element;
        final String refName = refExpr.getReferenceName();
        if (refName != null && !result.contains(refName) && myMatcher.prefixMatches(refName)) {
            final GrExpression hisQualifier = refExpr.getQualifierExpression();
            if (hisQualifier != null && myQualifier != null) {
                if (PsiEquivalenceUtil.areElementsEquivalent(hisQualifier, myQualifier)) {
                    if (refExpr.resolve() == null) {
                        result.add(refName);
                    }
                }
            } else if (hisQualifier == null && myQualifier == null) {
                if (refExpr.resolve() == null) {
                    result.add(refName);
                }
            }
        }
    }
    for (PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
        addVariantsWithSameQualifier(child, result);
    }
}
Also used : 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 80 with GroovyPsiElement

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

the class GroovyPositionManager method getAllClasses.

@Override
@NotNull
public List<ReferenceType> getAllClasses(@NotNull final SourcePosition position) throws NoDataException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("getAllClasses: " + position);
    }
    checkGroovyFile(position);
    List<ReferenceType> result = ApplicationManager.getApplication().runReadAction(new Computable<List<ReferenceType>>() {

        @Override
        public List<ReferenceType> compute() {
            GroovyPsiElement sourceImage = findReferenceTypeSourceImage(position);
            if (sourceImage instanceof GrTypeDefinition && !((GrTypeDefinition) sourceImage).isAnonymous()) {
                String qName = getClassNameForJvm((GrTypeDefinition) sourceImage);
                if (qName != null)
                    return myDebugProcess.getVirtualMachineProxy().classesByName(qName);
            } else if (sourceImage == null) {
                final String scriptName = getScriptQualifiedName(position);
                if (scriptName != null)
                    return myDebugProcess.getVirtualMachineProxy().classesByName(scriptName);
            } else {
                String enclosingName = findEnclosingName(position);
                if (enclosingName == null)
                    return null;
                final List<ReferenceType> outers = myDebugProcess.getVirtualMachineProxy().classesByName(enclosingName);
                final List<ReferenceType> result = new ArrayList<>(outers.size());
                for (ReferenceType outer : outers) {
                    final ReferenceType nested = findNested(outer, sourceImage, position);
                    if (nested != null) {
                        result.add(nested);
                    }
                }
                return result;
            }
            return null;
        }
    });
    if (LOG.isDebugEnabled()) {
        LOG.debug("getAllClasses = " + result);
    }
    if (result == null)
        throw NoDataException.INSTANCE;
    return result;
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ReferenceType(com.sun.jdi.ReferenceType) NotNull(org.jetbrains.annotations.NotNull)

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