Search in sources :

Example 21 with GrAssignmentExpression

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

the class GrCharConverter method isConvertibleEx.

@Nullable
@Override
public ConversionResult isConvertibleEx(@NotNull PsiType lType, @NotNull PsiType rType, @NotNull GroovyPsiElement context, @NotNull ApplicableTo currentPosition) {
    if (!PsiType.CHAR.equals(TypesUtil.unboxPrimitiveTypeWrapper(lType)))
        return null;
    if (PsiType.CHAR.equals(TypesUtil.unboxPrimitiveTypeWrapper(rType)))
        return ConversionResult.OK;
    // can assign numeric types to char
    if (TypesUtil.isNumericType(rType)) {
        if (rType instanceof PsiPrimitiveType || TypesUtil.unboxPrimitiveTypeWrapper(rType) instanceof PsiPrimitiveType) {
            return PsiType.CHAR.equals(lType) ? ConversionResult.OK : ConversionResult.ERROR;
        } else {
            // BigDecimal && BigInteger
            return ConversionResult.ERROR;
        }
    }
    {
        // special case 'c = []' will throw RuntimeError
        final GrExpression rValue;
        if (context instanceof GrAssignmentExpression) {
            final GrAssignmentExpression assignmentExpression = (GrAssignmentExpression) context;
            rValue = assignmentExpression.getRValue();
        } else if (context instanceof GrVariable) {
            final GrVariable assignmentExpression = (GrVariable) context;
            rValue = assignmentExpression.getInitializerGroovy();
        } else {
            rValue = null;
        }
        if (rValue instanceof GrListOrMap && ((GrListOrMap) rValue).isEmpty()) {
            return ConversionResult.WARNING;
        }
    }
    if (PsiType.BOOLEAN.equals(TypesUtil.unboxPrimitiveTypeWrapper(rType))) {
        switch(currentPosition) {
            case ASSIGNMENT:
            case RETURN_VALUE:
                return ConversionResult.WARNING;
            default:
                return null;
        }
    }
    // one-symbol string-to-char conversion doesn't work with return value
    if (currentPosition == ApplicableTo.RETURN_VALUE) {
        return null;
    }
    // can cast and assign one-symbol strings to char
    if (!TypesUtil.isClassType(rType, CommonClassNames.JAVA_LANG_STRING))
        return null;
    return checkSingleSymbolLiteral(context) ? ConversionResult.OK : ConversionResult.ERROR;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) PsiPrimitiveType(com.intellij.psi.PsiPrimitiveType) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) Nullable(org.jetbrains.annotations.Nullable)

Example 22 with GrAssignmentExpression

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

the class GroovyConfigSlurperCompletionProvider method getPrefix.

@Nullable
public static List<String> getPrefix(GrReferenceExpression ref) {
    List<String> res = new ArrayList<>();
    GrExpression qualifier = ref.getQualifierExpression();
    while (qualifier != null) {
        if (!(qualifier instanceof GrReferenceExpression))
            return null;
        GrReferenceExpression r = (GrReferenceExpression) qualifier;
        String name = r.getReferenceName();
        if (name == null)
            return null;
        res.add(name);
        qualifier = r.getQualifierExpression();
    }
    PsiElement e = ref.getParent();
    if (e instanceof GrAssignmentExpression) {
        GrAssignmentExpression assignmentExpression = (GrAssignmentExpression) e;
        if (assignmentExpression.getLValue() != ref)
            return null;
        e = assignmentExpression.getParent();
    }
    while (true) {
        if (e instanceof PsiFile) {
            break;
        } else if (e instanceof GrClosableBlock) {
            PsiElement eCall = e.getParent();
            if (!(eCall instanceof GrMethodCall))
                return null;
            GrMethodCall call = (GrMethodCall) eCall;
            if (!isPropertyCall(call))
                return null;
            String name = extractPropertyName(call);
            if (name == null)
                return null;
            res.add(name);
            e = call.getParent();
        } else if (e instanceof GrBlockStatement || e instanceof GrOpenBlock || e instanceof GrIfStatement || e instanceof GrForStatement || e instanceof GrWhileStatement || e instanceof GrTryCatchStatement) {
            e = e.getParent();
        } else {
            return null;
        }
    }
    Collections.reverse(res);
    return res;
}
Also used : GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) PsiFile(com.intellij.psi.PsiFile) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 23 with GrAssignmentExpression

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

the class GroovyConfigSlurperCompletionProvider method addCompletions.

@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
    PsiFile file = parameters.getOriginalFile();
    if (!(file instanceof GroovyFile))
        return;
    GroovyFile groovyFile = (GroovyFile) file;
    if (!groovyFile.isScript())
        return;
    GrReferenceExpression ref = (GrReferenceExpression) parameters.getPosition().getParent();
    if (ref == null)
        return;
    final Map<String, Boolean> variants = new HashMap<>();
    collectVariants((s, isFinal) -> variants.put(s, isFinal), ref, groovyFile);
    if (variants.isEmpty())
        return;
    // Remove existing variants.
    PsiElement parent = ref.getParent();
    if (parent instanceof GrAssignmentExpression) {
        parent = parent.getParent();
    }
    if (parent == null)
        return;
    Set<String> processedPrefixes = new HashSet<>();
    Set<String> prefixesInMethodCall = new HashSet<>();
    for (PsiElement e = parent.getFirstChild(); e != null; e = e.getNextSibling()) {
        if (e instanceof GrAssignmentExpression) {
            PsiElement left = ((GrAssignmentExpression) e).getLValue();
            if (left instanceof GrReferenceExpression) {
                String s = refToString((GrReferenceExpression) left);
                if (s == null)
                    continue;
                int dotIndex = s.indexOf('.');
                if (dotIndex > 0) {
                    processedPrefixes.add(s.substring(0, dotIndex));
                }
                variants.remove(s);
            }
        } else if (e instanceof GrMethodCall) {
            GrMethodCall call = (GrMethodCall) e;
            if (isPropertyCall(call)) {
                String name = extractPropertyName(call);
                if (name == null)
                    continue;
                processedPrefixes.add(name);
                prefixesInMethodCall.add(name);
                variants.remove(name);
            }
        }
    }
    // Process variants.
    for (Map.Entry<String, Boolean> entry : variants.entrySet()) {
        String variant = entry.getKey();
        int dotIndex = variant.indexOf('.');
        if (dotIndex > 0 && dotIndex < variant.length() - 1) {
            String p = variant.substring(0, dotIndex);
            if (prefixesInMethodCall.contains(p))
                continue;
            if (myAddPrefixes && processedPrefixes.add(p)) {
                result.addElement(LookupElementBuilder.create(p));
            }
        }
        LookupElement lookupElement = LookupElementBuilder.create(variant);
        if (entry.getValue()) {
            lookupElement = TailTypeDecorator.withTail(lookupElement, TailType.EQ);
        }
        result.addElement(lookupElement);
    }
}
Also used : GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) LookupElement(com.intellij.codeInsight.lookup.LookupElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) PsiFile(com.intellij.psi.PsiFile) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) PsiElement(com.intellij.psi.PsiElement)

Example 24 with GrAssignmentExpression

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

the class IndexedExpressionConversionIntention method processIntention.

@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final GrIndexProperty arrayIndexExpression = (GrIndexProperty) element;
    final GrArgumentList argList = (GrArgumentList) arrayIndexExpression.getLastChild();
    assert argList != null;
    final GrExpression[] arguments = argList.getExpressionArguments();
    final PsiElement parent = element.getParent();
    final GrExpression arrayExpression = arrayIndexExpression.getInvokedExpression();
    if (!(parent instanceof GrAssignmentExpression)) {
        rewriteAsGetAt(arrayIndexExpression, arrayExpression, arguments[0]);
        return;
    }
    final GrAssignmentExpression assignmentExpression = (GrAssignmentExpression) parent;
    final GrExpression rhs = assignmentExpression.getRValue();
    if (rhs.equals(element)) {
        rewriteAsGetAt(arrayIndexExpression, arrayExpression, arguments[0]);
    } else {
        rewriteAsSetAt(assignmentExpression, arrayExpression, arguments[0], rhs);
    }
}
Also used : GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrIndexProperty(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiElement(com.intellij.psi.PsiElement)

Example 25 with GrAssignmentExpression

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

the class IndexedExpressionConversionPredicate method satisfiedBy.

@Override
public boolean satisfiedBy(PsiElement element) {
    if (!(element instanceof GrIndexProperty))
        return false;
    if (ErrorUtil.containsError(element))
        return false;
    final GrIndexProperty arrayIndexExpression = (GrIndexProperty) element;
    final PsiElement lastChild = arrayIndexExpression.getLastChild();
    if (!(lastChild instanceof GrArgumentList))
        return false;
    final GrArgumentList argList = (GrArgumentList) lastChild;
    final GrExpression[] arguments = argList.getExpressionArguments();
    if (arguments.length != 1)
        return false;
    final PsiElement parent = element.getParent();
    if (!(parent instanceof GrAssignmentExpression)) {
        return true;
    }
    final GrAssignmentExpression assignmentExpression = (GrAssignmentExpression) parent;
    final GrExpression rvalue = assignmentExpression.getRValue();
    if (rvalue == null)
        return false;
    if (rvalue.equals(element))
        return true;
    final IElementType operator = assignmentExpression.getOperationTokenType();
    return GroovyTokenTypes.mASSIGN.equals(operator);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrIndexProperty(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiElement(com.intellij.psi.PsiElement)

Aggregations

GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)34 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)26 PsiElement (com.intellij.psi.PsiElement)16 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)12 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)8 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)7 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)6 IElementType (com.intellij.psi.tree.IElementType)5 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)5 NotNull (org.jetbrains.annotations.NotNull)4 Nullable (org.jetbrains.annotations.Nullable)4 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)4 PsiFile (com.intellij.psi.PsiFile)3 PsiType (com.intellij.psi.PsiType)3 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)3 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)3 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)3 GrApplicationStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement)3 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)3 ASTNode (com.intellij.lang.ASTNode)2