Search in sources :

Example 1 with GrMapType

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

the class MapKeysCompletionProvider method addCompletions.

@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
    PsiElement element = parameters.getPosition();
    GrReferenceExpression expression = (GrReferenceExpression) element.getParent();
    GrExpression qualifierExpression = expression.getQualifierExpression();
    if (qualifierExpression == null)
        return;
    PsiType mapType = qualifierExpression.getType();
    if (!InheritanceUtil.isInheritor(mapType, CommonClassNames.JAVA_UTIL_MAP)) {
        return;
    }
    PsiElement resolve = null;
    if (qualifierExpression instanceof GrMethodCall) {
        resolve = ((GrMethodCall) qualifierExpression).resolveMethod();
    } else if (qualifierExpression instanceof GrReferenceExpression) {
        resolve = ((GrReferenceExpression) qualifierExpression).resolve();
    }
    for (GroovyMapContentProvider provider : GroovyMapContentProvider.EP_NAME.getExtensions()) {
        GroovyMapCompletionUtil.addKeyVariants(provider, qualifierExpression, resolve, result);
    }
    if (mapType instanceof GrMapType) {
        for (String key : ((GrMapType) mapType).getStringKeys()) {
            LookupElement lookup = LookupElementBuilder.create(key);
            lookup = PrioritizedLookupElement.withPriority(lookup, 1);
            result.addElement(lookup);
        }
    }
}
Also used : GrMapType(org.jetbrains.plugins.groovy.lang.psi.impl.GrMapType) GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyMapContentProvider(org.jetbrains.plugins.groovy.extensions.GroovyMapContentProvider) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) PsiType(com.intellij.psi.PsiType)

Example 2 with GrMapType

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

the class GroovyMapValueTypeEnhancer method getReferenceType.

@Override
public PsiType getReferenceType(GrReferenceExpression ref, @Nullable PsiElement resolved) {
    if (resolved != null)
        return null;
    GrExpression qualifierExpression = ref.getQualifierExpression();
    if (qualifierExpression == null)
        return null;
    PsiType mapType = qualifierExpression.getType();
    if (!InheritanceUtil.isInheritor(mapType, CommonClassNames.JAVA_UTIL_MAP)) {
        return null;
    }
    PsiElement qResolved;
    if (qualifierExpression instanceof GrReferenceExpression) {
        qResolved = ((GrReferenceExpression) qualifierExpression).resolve();
    } else if (qualifierExpression instanceof GrMethodCall) {
        qResolved = ((GrMethodCall) qualifierExpression).resolveMethod();
    } else {
        return null;
    }
    String key = ref.getReferenceName();
    if (key == null)
        return null;
    for (GroovyMapContentProvider provider : GroovyMapContentProvider.EP_NAME.getExtensions()) {
        PsiType type = provider.getValueType(qualifierExpression, qResolved, key);
        if (type != null) {
            return type;
        }
    }
    if (mapType instanceof GrMapType) {
        return ((GrMapType) mapType).getTypeByStringKey(key);
    }
    return null;
}
Also used : GrMapType(org.jetbrains.plugins.groovy.lang.psi.impl.GrMapType) GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyMapContentProvider(org.jetbrains.plugins.groovy.extensions.GroovyMapContentProvider) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Aggregations

PsiElement (com.intellij.psi.PsiElement)2 PsiType (com.intellij.psi.PsiType)2 GroovyMapContentProvider (org.jetbrains.plugins.groovy.extensions.GroovyMapContentProvider)2 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)2 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)2 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)2 GrMapType (org.jetbrains.plugins.groovy.lang.psi.impl.GrMapType)2 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1