Search in sources :

Example 11 with BindingContext

use of org.jetbrains.kotlin.resolve.BindingContext in project kotlin by JetBrains.

the class AbstractDiagnosticMessageTest method doTest.

public void doTest(String filePath) throws Exception {
    File file = new File(filePath);
    String fileName = file.getName();
    String fileData = KotlinTestUtils.doLoadFile(file);
    Map<String, String> directives = KotlinTestUtils.parseDirectives(fileData);
    int diagnosticNumber = getDiagnosticNumber(directives);
    final Set<DiagnosticFactory<?>> diagnosticFactories = getDiagnosticFactories(directives);
    MessageType messageType = getMessageTypeDirective(directives);
    String explicitLanguageVersion = InTextDirectivesUtils.findStringWithPrefixes(fileData, "// LANGUAGE_VERSION:");
    LanguageVersion version = explicitLanguageVersion == null ? null : LanguageVersion.fromVersionString(explicitLanguageVersion);
    KtFile psiFile = KotlinTestUtils.createFile(fileName, KotlinTestUtils.doLoadFile(getTestDataPath(), fileName), getProject());
    AnalysisResult analysisResult = analyze(psiFile, version);
    BindingContext bindingContext = analysisResult.getBindingContext();
    List<Diagnostic> diagnostics = ContainerUtil.filter(bindingContext.getDiagnostics().all(), new Condition<Diagnostic>() {

        @Override
        public boolean value(Diagnostic diagnostic) {
            return diagnosticFactories.contains(diagnostic.getFactory());
        }
    });
    assertEquals("Expected diagnostics number mismatch:", diagnosticNumber, diagnostics.size());
    int index = 1;
    String name = FileUtil.getNameWithoutExtension(fileName);
    for (Diagnostic diagnostic : diagnostics) {
        String readableDiagnosticText;
        String extension;
        if (messageType != MessageType.TEXT && IdeErrorMessages.hasIdeSpecificMessage(diagnostic)) {
            readableDiagnosticText = FormatHtmlUtilKt.formatHtml(IdeErrorMessages.render(diagnostic));
            extension = MessageType.HTML.extension;
        } else {
            readableDiagnosticText = DefaultErrorMessages.render(diagnostic);
            extension = MessageType.TEXT.extension;
        }
        String errorMessageFileName = name + index;
        String path = getTestDataPath() + "/" + errorMessageFileName + "." + extension;
        String actualText = "<!-- " + errorMessageFileName + " -->\n" + readableDiagnosticText;
        assertSameLinesWithFile(path, actualText);
        index++;
    }
}
Also used : Diagnostic(org.jetbrains.kotlin.diagnostics.Diagnostic) BindingContext(org.jetbrains.kotlin.resolve.BindingContext) AnalysisResult(org.jetbrains.kotlin.analyzer.AnalysisResult) DiagnosticFactory(org.jetbrains.kotlin.diagnostics.DiagnosticFactory) LanguageVersion(org.jetbrains.kotlin.config.LanguageVersion) KtFile(org.jetbrains.kotlin.psi.KtFile) KtFile(org.jetbrains.kotlin.psi.KtFile) File(java.io.File)

Example 12 with BindingContext

use of org.jetbrains.kotlin.resolve.BindingContext 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 13 with BindingContext

use of org.jetbrains.kotlin.resolve.BindingContext 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)

Example 14 with BindingContext

use of org.jetbrains.kotlin.resolve.BindingContext in project kotlin by JetBrains.

the class MoveDeclarationsOutHelper method getPropertyType.

@NotNull
private static KotlinType getPropertyType(@NotNull KtProperty property) {
    BindingContext bindingContext = ResolutionUtils.analyze(property, BodyResolveMode.PARTIAL);
    VariableDescriptor propertyDescriptor = bindingContext.get(BindingContext.VARIABLE, property);
    assert propertyDescriptor != null : "Couldn't resolve property to property descriptor " + property.getText();
    return propertyDescriptor.getType();
}
Also used : BindingContext(org.jetbrains.kotlin.resolve.BindingContext) VariableDescriptor(org.jetbrains.kotlin.descriptors.VariableDescriptor) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with BindingContext

use of org.jetbrains.kotlin.resolve.BindingContext in project kotlin by JetBrains.

the class CheckerTestUtil method getDiagnosticsIncludingSyntaxErrors.

@NotNull
public static List<ActualDiagnostic> getDiagnosticsIncludingSyntaxErrors(@NotNull BindingContext bindingContext, @NotNull List<Pair<MultiTargetPlatform, BindingContext>> implementingModulesBindings, @NotNull PsiElement root, boolean markDynamicCalls, @Nullable List<DeclarationDescriptor> dynamicCallDescriptors) {
    List<ActualDiagnostic> result = getDiagnosticsIncludingSyntaxErrors(bindingContext, root, markDynamicCalls, dynamicCallDescriptors, null);
    List<Pair<MultiTargetPlatform, BindingContext>> sortedBindings = CollectionsKt.sortedWith(implementingModulesBindings, new Comparator<Pair<MultiTargetPlatform, BindingContext>>() {

        @Override
        public int compare(Pair<MultiTargetPlatform, BindingContext> o1, Pair<MultiTargetPlatform, BindingContext> o2) {
            return o1.getFirst().compareTo(o2.getFirst());
        }
    });
    for (Pair<MultiTargetPlatform, BindingContext> binding : sortedBindings) {
        MultiTargetPlatform platform = binding.getFirst();
        assert platform instanceof MultiTargetPlatform.Specific : "Implementing module must have a specific platform: " + platform;
        result.addAll(getDiagnosticsIncludingSyntaxErrors(binding.getSecond(), root, markDynamicCalls, dynamicCallDescriptors, ((MultiTargetPlatform.Specific) platform).getPlatform()));
    }
    return result;
}
Also used : MultiTargetPlatform(org.jetbrains.kotlin.resolve.MultiTargetPlatform) BindingContext(org.jetbrains.kotlin.resolve.BindingContext) Pair(kotlin.Pair) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

BindingContext (org.jetbrains.kotlin.resolve.BindingContext)20 KtFile (org.jetbrains.kotlin.psi.KtFile)6 File (java.io.File)5 NotNull (org.jetbrains.annotations.NotNull)5 Nullable (org.jetbrains.annotations.Nullable)3 AnalysisResult (org.jetbrains.kotlin.analyzer.AnalysisResult)3 IOException (java.io.IOException)2 KotlinCoreEnvironment (org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment)2 ResolutionFacade (org.jetbrains.kotlin.idea.resolve.ResolutionFacade)2 KtNamedFunction (org.jetbrains.kotlin.psi.KtNamedFunction)2 KotlinType (org.jetbrains.kotlin.types.KotlinType)2 Type (org.jetbrains.org.objectweb.asm.Type)2 HierarchyNodeDescriptor (com.intellij.ide.hierarchy.HierarchyNodeDescriptor)1 Editor (com.intellij.openapi.editor.Editor)1 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 PsiMethod (com.intellij.psi.PsiMethod)1 SearchScope (com.intellij.psi.search.SearchScope)1 HashMap (com.intellij.util.containers.HashMap)1