Search in sources :

Example 1 with KtReferenceExpression

use of org.jetbrains.kotlin.psi.KtReferenceExpression in project kotlin by JetBrains.

the class DebugInfoAnnotator method annotate.

@Override
public void annotate(@NotNull PsiElement element, @NotNull final AnnotationHolder holder) {
    if (!isDebugInfoEnabled() || !ProjectRootsUtil.isInProjectOrLibSource(element)) {
        return;
    }
    if (element instanceof KtFile && !(element instanceof KtCodeFragment)) {
        KtFile file = (KtFile) element;
        try {
            BindingContext bindingContext = ResolutionUtils.analyzeFully(file);
            DebugInfoUtil.markDebugAnnotations(file, bindingContext, new DebugInfoUtil.DebugInfoReporter() {

                @Override
                public void reportElementWithErrorType(@NotNull KtReferenceExpression expression) {
                    holder.createErrorAnnotation(expression, "[DEBUG] Resolved to error element").setTextAttributes(KotlinHighlightingColors.RESOLVED_TO_ERROR);
                }

                @Override
                public void reportMissingUnresolved(@NotNull KtReferenceExpression expression) {
                    holder.createErrorAnnotation(expression, "[DEBUG] Reference is not resolved to anything, but is not marked unresolved").setTextAttributes(KotlinHighlightingColors.DEBUG_INFO);
                }

                @Override
                public void reportUnresolvedWithTarget(@NotNull KtReferenceExpression expression, @NotNull String target) {
                    holder.createErrorAnnotation(expression, "[DEBUG] Reference marked as unresolved is actually resolved to " + target).setTextAttributes(KotlinHighlightingColors.DEBUG_INFO);
                }
            });
        } catch (ProcessCanceledException e) {
            throw e;
        } catch (Throwable e) {
            // TODO
            holder.createErrorAnnotation(element, e.getClass().getCanonicalName() + ": " + e.getMessage());
            e.printStackTrace();
        }
    }
}
Also used : KtReferenceExpression(org.jetbrains.kotlin.psi.KtReferenceExpression) KtCodeFragment(org.jetbrains.kotlin.psi.KtCodeFragment) KtFile(org.jetbrains.kotlin.psi.KtFile) BindingContext(org.jetbrains.kotlin.resolve.BindingContext) DebugInfoUtil(org.jetbrains.kotlin.checkers.DebugInfoUtil) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 2 with KtReferenceExpression

use of org.jetbrains.kotlin.psi.KtReferenceExpression in project kotlin by JetBrains.

the class CheckerTestUtil method getDebugInfoDiagnostics.

@SuppressWarnings("TestOnlyProblems")
@NotNull
private static List<ActualDiagnostic> getDebugInfoDiagnostics(@NotNull PsiElement root, @NotNull BindingContext bindingContext, final boolean markDynamicCalls, @Nullable final List<DeclarationDescriptor> dynamicCallDescriptors, @Nullable final String platform) {
    final List<ActualDiagnostic> debugAnnotations = new ArrayList<ActualDiagnostic>();
    DebugInfoUtil.markDebugAnnotations(root, bindingContext, new DebugInfoUtil.DebugInfoReporter() {

        @Override
        public void reportElementWithErrorType(@NotNull KtReferenceExpression expression) {
            newDiagnostic(expression, DebugInfoDiagnosticFactory.ELEMENT_WITH_ERROR_TYPE);
        }

        @Override
        public void reportMissingUnresolved(@NotNull KtReferenceExpression expression) {
            newDiagnostic(expression, DebugInfoDiagnosticFactory.MISSING_UNRESOLVED);
        }

        @Override
        public void reportUnresolvedWithTarget(@NotNull KtReferenceExpression expression, @NotNull String target) {
            newDiagnostic(expression, DebugInfoDiagnosticFactory.UNRESOLVED_WITH_TARGET);
        }

        @Override
        public void reportDynamicCall(@NotNull KtElement element, DeclarationDescriptor declarationDescriptor) {
            if (dynamicCallDescriptors != null) {
                dynamicCallDescriptors.add(declarationDescriptor);
            }
            if (markDynamicCalls) {
                newDiagnostic(element, DebugInfoDiagnosticFactory.DYNAMIC);
            }
        }

        private void newDiagnostic(KtElement element, DebugInfoDiagnosticFactory factory) {
            debugAnnotations.add(new ActualDiagnostic(new DebugInfoDiagnostic(element, factory), platform));
        }
    });
    //noinspection unchecked
    for (Pair<? extends WritableSlice<? extends KtExpression, ?>, DebugInfoDiagnosticFactory> factory : Arrays.asList(TuplesKt.to(BindingContext.SMARTCAST, DebugInfoDiagnosticFactory.SMARTCAST), TuplesKt.to(BindingContext.IMPLICIT_RECEIVER_SMARTCAST, DebugInfoDiagnosticFactory.IMPLICIT_RECEIVER_SMARTCAST), TuplesKt.to(BindingContext.SMARTCAST_NULL, DebugInfoDiagnosticFactory.CONSTANT), TuplesKt.to(BindingContext.LEAKING_THIS, DebugInfoDiagnosticFactory.LEAKING_THIS), TuplesKt.to(BindingContext.IMPLICIT_EXHAUSTIVE_WHEN, DebugInfoDiagnosticFactory.IMPLICIT_EXHAUSTIVE))) {
        for (KtExpression expression : bindingContext.getSliceContents(factory.getFirst()).keySet()) {
            if (PsiTreeUtil.isAncestor(root, expression, false)) {
                debugAnnotations.add(new ActualDiagnostic(new DebugInfoDiagnostic(expression, factory.getSecond()), platform));
            }
        }
    }
    return debugAnnotations;
}
Also used : KtElement(org.jetbrains.kotlin.psi.KtElement) KtReferenceExpression(org.jetbrains.kotlin.psi.KtReferenceExpression) DeclarationDescriptor(org.jetbrains.kotlin.descriptors.DeclarationDescriptor) KtExpression(org.jetbrains.kotlin.psi.KtExpression) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

KtReferenceExpression (org.jetbrains.kotlin.psi.KtReferenceExpression)2 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)1 NotNull (org.jetbrains.annotations.NotNull)1 DebugInfoUtil (org.jetbrains.kotlin.checkers.DebugInfoUtil)1 DeclarationDescriptor (org.jetbrains.kotlin.descriptors.DeclarationDescriptor)1 KtCodeFragment (org.jetbrains.kotlin.psi.KtCodeFragment)1 KtElement (org.jetbrains.kotlin.psi.KtElement)1 KtExpression (org.jetbrains.kotlin.psi.KtExpression)1 KtFile (org.jetbrains.kotlin.psi.KtFile)1 BindingContext (org.jetbrains.kotlin.resolve.BindingContext)1