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++;
}
}
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();
}
}
}
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;
}
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();
}
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;
}
Aggregations