Search in sources :

Example 1 with PsiQualifiedNamedElement

use of com.intellij.psi.PsiQualifiedNamedElement in project kotlin by JetBrains.

the class ExpectedResolveData method checkResult.

public final void checkResult(BindingContext bindingContext) {
    Set<PsiElement> unresolvedReferences = Sets.newHashSet();
    for (Diagnostic diagnostic : bindingContext.getDiagnostics()) {
        if (Errors.UNRESOLVED_REFERENCE_DIAGNOSTICS.contains(diagnostic.getFactory())) {
            unresolvedReferences.add(diagnostic.getPsiElement());
        }
    }
    Map<String, PsiElement> nameToDeclaration = Maps.newHashMap();
    Map<PsiElement, String> declarationToName = Maps.newHashMap();
    for (Map.Entry<String, Position> entry : declarationToPosition.entrySet()) {
        String name = entry.getKey();
        Position position = entry.getValue();
        PsiElement element = position.getElement();
        PsiElement ancestorOfType;
        if (name.equals("file")) {
            ancestorOfType = element.getContainingFile();
        } else {
            ancestorOfType = getAncestorOfType(KtDeclaration.class, element);
            if (ancestorOfType == null) {
                KtPackageDirective directive = getAncestorOfType(KtPackageDirective.class, element);
                assert directive != null : "Not a declaration: " + name;
                ancestorOfType = element;
            }
        }
        nameToDeclaration.put(name, ancestorOfType);
        declarationToName.put(ancestorOfType, name);
    }
    for (Map.Entry<Position, String> entry : positionToReference.entrySet()) {
        Position position = entry.getKey();
        String name = entry.getValue();
        PsiElement element = position.getElement();
        KtReferenceExpression referenceExpression = PsiTreeUtil.getParentOfType(element, KtReferenceExpression.class);
        DeclarationDescriptor referenceTarget = bindingContext.get(REFERENCE_TARGET, referenceExpression);
        if ("!".equals(name)) {
            assertTrue("Must have been unresolved: " + renderReferenceInContext(referenceExpression) + " but was resolved to " + renderNullableDescriptor(referenceTarget), unresolvedReferences.contains(referenceExpression));
            assertTrue(String.format("Reference =%s= has a reference target =%s= but expected to be unresolved", renderReferenceInContext(referenceExpression), renderNullableDescriptor(referenceTarget)), referenceTarget == null);
            continue;
        }
        if ("!!".equals(name)) {
            assertTrue("Must have been resolved to multiple descriptors: " + renderReferenceInContext(referenceExpression) + " but was resolved to " + renderNullableDescriptor(referenceTarget), bindingContext.get(AMBIGUOUS_REFERENCE_TARGET, referenceExpression) != null);
            continue;
        } else if ("!null".equals(name)) {
            assertTrue("Must have been resolved to null: " + renderReferenceInContext(referenceExpression) + " but was resolved to " + renderNullableDescriptor(referenceTarget), referenceTarget == null);
            continue;
        } else if ("!error".equals(name)) {
            assertTrue("Must have been resolved to error: " + renderReferenceInContext(referenceExpression) + " but was resolved to " + renderNullableDescriptor(referenceTarget), ErrorUtils.isError(referenceTarget));
            continue;
        }
        PsiElement expected = nameToDeclaration.get(name);
        if (expected == null) {
            expected = nameToPsiElement.get(name);
        }
        KtReferenceExpression reference = getAncestorOfType(KtReferenceExpression.class, element);
        if (expected == null && name.startsWith(STANDARD_PREFIX)) {
            DeclarationDescriptor expectedDescriptor = nameToDescriptor.get(name);
            KtTypeReference typeReference = getAncestorOfType(KtTypeReference.class, element);
            if (expectedDescriptor != null) {
                DeclarationDescriptor actual = bindingContext.get(REFERENCE_TARGET, reference);
                assertDescriptorsEqual("Expected: " + name, expectedDescriptor.getOriginal(), actual == null ? null : actual.getOriginal());
                continue;
            }
            KotlinType actualType = bindingContext.get(BindingContext.TYPE, typeReference);
            assertNotNull("Type " + name + " not resolved for reference " + name, actualType);
            ClassifierDescriptor expectedClass = getBuiltinClass(name.substring(STANDARD_PREFIX.length()));
            assertTypeConstructorEquals("Type resolution mismatch: ", expectedClass.getTypeConstructor(), actualType.getConstructor());
            continue;
        }
        assert expected != null : "No declaration for " + name;
        if (referenceTarget instanceof PackageViewDescriptor) {
            KtPackageDirective expectedDirective = PsiTreeUtil.getParentOfType(expected, KtPackageDirective.class);
            FqName expectedFqName;
            if (expectedDirective != null) {
                expectedFqName = expectedDirective.getFqName();
            } else if (expected instanceof PsiQualifiedNamedElement) {
                String qualifiedName = ((PsiQualifiedNamedElement) expected).getQualifiedName();
                assert qualifiedName != null : "No qualified name for " + name;
                expectedFqName = new FqName(qualifiedName);
            } else {
                throw new IllegalStateException(expected.getClass().getName() + " name=" + name);
            }
            assertEquals(expectedFqName, ((PackageViewDescriptor) referenceTarget).getFqName());
            continue;
        }
        PsiElement actual = referenceTarget == null ? bindingContext.get(BindingContext.LABEL_TARGET, referenceExpression) : DescriptorToSourceUtils.descriptorToDeclaration(referenceTarget);
        if (actual instanceof KtSimpleNameExpression) {
            actual = ((KtSimpleNameExpression) actual).getIdentifier();
        }
        String actualName = null;
        if (actual != null) {
            actualName = declarationToName.get(actual);
            if (actualName == null) {
                actualName = actual.toString();
            }
        }
        assertNotNull(element.getText(), reference);
        assertEquals("Reference `" + name + "`" + renderReferenceInContext(reference) + " is resolved into " + actualName + ".", expected, actual);
    }
    for (Map.Entry<Position, String> entry : positionToType.entrySet()) {
        Position position = entry.getKey();
        String typeName = entry.getValue();
        PsiElement element = position.getElement();
        KtExpression expression = getAncestorOfType(KtExpression.class, element);
        KotlinType expressionType = bindingContext.getType(expression);
        TypeConstructor expectedTypeConstructor;
        if (typeName.startsWith(STANDARD_PREFIX)) {
            String name = typeName.substring(STANDARD_PREFIX.length());
            ClassifierDescriptor expectedClass = getBuiltinClass(name);
            expectedTypeConstructor = expectedClass.getTypeConstructor();
        } else {
            Position declarationPosition = declarationToPosition.get(typeName);
            assertNotNull("Undeclared: " + typeName, declarationPosition);
            PsiElement declElement = declarationPosition.getElement();
            assertNotNull(declarationPosition);
            KtDeclaration declaration = getAncestorOfType(KtDeclaration.class, declElement);
            assertNotNull(declaration);
            if (declaration instanceof KtClass) {
                ClassDescriptor classDescriptor = bindingContext.get(BindingContext.CLASS, declaration);
                expectedTypeConstructor = classDescriptor.getTypeConstructor();
            } else if (declaration instanceof KtTypeParameter) {
                TypeParameterDescriptor typeParameterDescriptor = bindingContext.get(BindingContext.TYPE_PARAMETER, (KtTypeParameter) declaration);
                expectedTypeConstructor = typeParameterDescriptor.getTypeConstructor();
            } else {
                fail("Unsupported declaration: " + declaration);
                return;
            }
        }
        assertNotNull(expression.getText() + " type is null", expressionType);
        assertTypeConstructorEquals("At " + position + ": ", expectedTypeConstructor, expressionType.getConstructor());
    }
}
Also used : FqName(org.jetbrains.kotlin.name.FqName) KotlinType(org.jetbrains.kotlin.types.KotlinType) Diagnostic(org.jetbrains.kotlin.diagnostics.Diagnostic) PsiQualifiedNamedElement(com.intellij.psi.PsiQualifiedNamedElement) PsiElement(com.intellij.psi.PsiElement) TypeConstructor(org.jetbrains.kotlin.types.TypeConstructor) Map(java.util.Map)

Example 2 with PsiQualifiedNamedElement

use of com.intellij.psi.PsiQualifiedNamedElement in project intellij-community by JetBrains.

the class RefactoringScopeElementListenerProvider method getListener.

@Override
public RefactoringElementListener getListener(PsiElement element) {
    if (!(element instanceof PsiQualifiedNamedElement) && !(element instanceof PsiDirectory))
        return null;
    final PsiFile containingFile = element.getContainingFile();
    RefactoringElementListenerComposite composite = null;
    String oldName = getQualifiedName(element, false);
    if (oldName != null) {
        composite = getComposite(element, containingFile, null, oldName);
    }
    if (element instanceof PsiQualifiedNamedElement) {
        oldName = getQualifiedName(element, true);
        if (oldName != null) {
            composite = getComposite(element, containingFile, composite, oldName);
        }
    }
    return composite;
}
Also used : PsiQualifiedNamedElement(com.intellij.psi.PsiQualifiedNamedElement) PsiDirectory(com.intellij.psi.PsiDirectory) RefactoringElementListenerComposite(com.intellij.refactoring.listeners.RefactoringElementListenerComposite) PsiFile(com.intellij.psi.PsiFile)

Example 3 with PsiQualifiedNamedElement

use of com.intellij.psi.PsiQualifiedNamedElement in project intellij-community by JetBrains.

the class RefactoringScopeElementListenerProvider method getQualifiedName.

private static String getQualifiedName(PsiElement element, boolean acceptQNames) {
    if (element instanceof PsiQualifiedNamedElement && acceptQNames) {
        return ((PsiQualifiedNamedElement) element).getQualifiedName();
    } else {
        final Project project = element.getProject();
        final VirtualFile virtualFile = PsiUtilCore.getVirtualFile(element);
        if (virtualFile == null) {
            return null;
        }
        return FilePatternPackageSet.getRelativePath(virtualFile, ProjectRootManager.getInstance(project).getFileIndex(), true, project.getBaseDir());
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) PsiQualifiedNamedElement(com.intellij.psi.PsiQualifiedNamedElement)

Aggregations

PsiQualifiedNamedElement (com.intellij.psi.PsiQualifiedNamedElement)3 Project (com.intellij.openapi.project.Project)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiDirectory (com.intellij.psi.PsiDirectory)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 RefactoringElementListenerComposite (com.intellij.refactoring.listeners.RefactoringElementListenerComposite)1 Map (java.util.Map)1 Diagnostic (org.jetbrains.kotlin.diagnostics.Diagnostic)1 FqName (org.jetbrains.kotlin.name.FqName)1 KotlinType (org.jetbrains.kotlin.types.KotlinType)1 TypeConstructor (org.jetbrains.kotlin.types.TypeConstructor)1