Search in sources :

Example 1 with ClassQualifier

use of org.jetbrains.kotlin.resolve.scopes.receivers.ClassQualifier in project kotlin by JetBrains.

the class CodeInsightUtils method findElement.

@Nullable
public static PsiElement findElement(@NotNull PsiFile file, int startOffset, int endOffset, @NotNull CodeInsightUtils.ElementKind elementKind) {
    Class<? extends KtElement> elementClass;
    switch(elementKind) {
        case EXPRESSION:
            elementClass = KtExpression.class;
            break;
        case TYPE_ELEMENT:
            elementClass = KtTypeElement.class;
            break;
        case TYPE_CONSTRUCTOR:
            elementClass = KtSimpleNameExpression.class;
            break;
        default:
            throw new IllegalArgumentException(elementKind.name());
    }
    PsiElement element = findElementOfClassAtRange(file, startOffset, endOffset, elementClass);
    if (elementKind == ElementKind.TYPE_ELEMENT)
        return element;
    if (elementKind == ElementKind.TYPE_CONSTRUCTOR) {
        return element != null && KtPsiUtilKt.isTypeConstructorReference(element) ? element : null;
    }
    if (element instanceof KtScriptInitializer) {
        element = ((KtScriptInitializer) element).getBody();
    }
    if (element == null)
        return null;
    // TODO: Support binary operations in "Introduce..." refactorings
    if (element instanceof KtOperationReferenceExpression && ((KtOperationReferenceExpression) element).getReferencedNameElementType() != KtTokens.IDENTIFIER && element.getParent() instanceof KtBinaryExpression) {
        return null;
    }
    // For cases like 'this@outerClass', don't return the label part
    if (KtPsiUtil.isLabelIdentifierExpression(element)) {
        element = PsiTreeUtil.getParentOfType(element, KtExpression.class);
    }
    if (element instanceof KtBlockExpression) {
        List<KtExpression> statements = ((KtBlockExpression) element).getStatements();
        if (statements.size() == 1) {
            KtExpression statement = statements.get(0);
            if (statement.getText().equals(element.getText())) {
                return statement;
            }
        }
    }
    KtExpression expression = (KtExpression) element;
    BindingContext context = ResolutionUtils.analyze(expression);
    Qualifier qualifier = context.get(BindingContext.QUALIFIER, expression);
    if (qualifier != null) {
        if (!(qualifier instanceof ClassQualifier))
            return null;
        if (((ClassQualifier) qualifier).getDescriptor().getKind() != ClassKind.OBJECT)
            return null;
    }
    return expression;
}
Also used : ClassQualifier(org.jetbrains.kotlin.resolve.scopes.receivers.ClassQualifier) Qualifier(org.jetbrains.kotlin.resolve.scopes.receivers.Qualifier) BindingContext(org.jetbrains.kotlin.resolve.BindingContext) ClassQualifier(org.jetbrains.kotlin.resolve.scopes.receivers.ClassQualifier) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Nullable (org.jetbrains.annotations.Nullable)1 BindingContext (org.jetbrains.kotlin.resolve.BindingContext)1 ClassQualifier (org.jetbrains.kotlin.resolve.scopes.receivers.ClassQualifier)1 Qualifier (org.jetbrains.kotlin.resolve.scopes.receivers.Qualifier)1