use of com.intellij.codeInsight.ExpectedTypeInfo in project intellij-community by JetBrains.
the class SmartCastProvider method addCastVariants.
static void addCastVariants(@NotNull CompletionParameters parameters, PrefixMatcher matcher, @NotNull Consumer<LookupElement> result) {
if (!shouldSuggestCast(parameters))
return;
PsiElement position = parameters.getPosition();
PsiElement parenthesisOwner = getParenthesisOwner(position);
final boolean insideCast = parenthesisOwner instanceof PsiTypeCastExpression;
if (insideCast) {
PsiElement parent = parenthesisOwner.getParent();
if (parent instanceof PsiParenthesizedExpression) {
if (parent.getParent() instanceof PsiReferenceExpression) {
for (ExpectedTypeInfo info : ExpectedTypesProvider.getExpectedTypes((PsiParenthesizedExpression) parent, false)) {
result.consume(PsiTypeLookupItem.createLookupItem(info.getType(), parent));
}
}
ExpectedTypeInfo info = getParenthesizedCastExpectationByOperandType(position);
if (info != null) {
addHierarchyTypes(parameters, matcher, info, type -> result.consume(PsiTypeLookupItem.createLookupItem(type, parent)));
}
return;
}
}
for (final ExpectedTypeInfo info : JavaSmartCompletionContributor.getExpectedTypes(parameters)) {
PsiType type = info.getDefaultType();
if (type instanceof PsiWildcardType) {
type = ((PsiWildcardType) type).getBound();
}
if (type == null || PsiType.VOID.equals(type)) {
continue;
}
if (type instanceof PsiPrimitiveType) {
final PsiType castedType = getCastedExpressionType(parenthesisOwner);
if (castedType != null && !(castedType instanceof PsiPrimitiveType)) {
final PsiClassType boxedType = ((PsiPrimitiveType) type).getBoxedType(position);
if (boxedType != null) {
type = boxedType;
}
}
}
result.consume(createSmartCastElement(parameters, insideCast, type));
}
}
use of com.intellij.codeInsight.ExpectedTypeInfo in project intellij-community by JetBrains.
the class ExpectedTypeMacro method getExpectedTypes.
@Nullable
private static PsiType[] getExpectedTypes(Expression[] params, final ExpressionContext context) {
if (params.length != 0)
return null;
final Project project = context.getProject();
PsiType[] types = null;
PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(context.getEditor().getDocument());
assert file != null;
final PsiFile fileCopy = (PsiFile) file.copy();
BlockSupport.getInstance(project).reparseRange(fileCopy, context.getTemplateStartOffset(), context.getTemplateEndOffset(), CompletionUtil.DUMMY_IDENTIFIER);
PsiElement element = fileCopy.findElementAt(context.getTemplateStartOffset());
if (element instanceof PsiIdentifier && element.getParent() instanceof PsiExpression) {
ExpectedTypeInfo[] infos = ExpectedTypesProvider.getExpectedTypes((PsiExpression) element.getParent(), true);
if (infos.length > 0) {
types = new PsiType[infos.length];
for (int i = 0; i < infos.length; i++) {
ExpectedTypeInfo info = infos[i];
types[i] = info.getType();
}
}
}
return types;
}
use of com.intellij.codeInsight.ExpectedTypeInfo in project intellij-community by JetBrains.
the class ExpectedTypesGetter method extractTypes.
@NotNull
public static PsiType[] extractTypes(ExpectedTypeInfo[] infos, boolean defaultTypes) {
Set<PsiType> result = new THashSet<>(infos.length);
for (ExpectedTypeInfo info : infos) {
final PsiType type = info.getType();
final PsiType defaultType = info.getDefaultType();
if (!defaultTypes && !defaultType.equals(type)) {
result.add(type);
}
result.add(defaultType);
}
return result.toArray(PsiType.createArray(result.size()));
}
use of com.intellij.codeInsight.ExpectedTypeInfo in project intellij-community by JetBrains.
the class MagicCompletionContributor method addCompletionVariants.
private static void addCompletionVariants(@NotNull final CompletionParameters parameters, @NotNull final CompletionResultSet result, PsiElement pos, MagicConstantInspection.AllowedValues allowedValues) {
final Set<PsiElement> allowed = new THashSet<>(new TObjectHashingStrategy<PsiElement>() {
@Override
public int computeHashCode(PsiElement object) {
return 0;
}
@Override
public boolean equals(PsiElement o1, PsiElement o2) {
return parameters.getOriginalFile().getManager().areElementsEquivalent(o1, o2);
}
});
if (allowedValues.canBeOred) {
PsiElementFactory factory = JavaPsiFacade.getElementFactory(pos.getProject());
PsiExpression zero = factory.createExpressionFromText("0", pos);
result.addElement(PrioritizedLookupElement.withPriority(LookupElementBuilder.create(zero, "0"), PRIORITY - 1));
PsiExpression minusOne = factory.createExpressionFromText("-1", pos);
result.addElement(PrioritizedLookupElement.withPriority(LookupElementBuilder.create(minusOne, "-1"), PRIORITY - 1));
allowed.add(zero);
allowed.add(minusOne);
}
List<ExpectedTypeInfo> types = Arrays.asList(JavaSmartCompletionContributor.getExpectedTypes(parameters));
for (PsiAnnotationMemberValue value : allowedValues.values) {
if (value instanceof PsiReference) {
PsiElement resolved = ((PsiReference) value).resolve();
if (resolved instanceof PsiNamedElement) {
LookupElement lookupElement = LookupItemUtil.objectToLookupItem(resolved);
if (lookupElement instanceof VariableLookupItem) {
((VariableLookupItem) lookupElement).setSubstitutor(PsiSubstitutor.EMPTY);
}
LookupElement element = PrioritizedLookupElement.withPriority(lookupElement, PRIORITY);
element = decorate(parameters, types, element);
result.addElement(element);
allowed.add(resolved);
continue;
}
}
LookupElement element = LookupElementBuilder.create(value, value.getText());
element = decorate(parameters, types, element);
result.addElement(element);
allowed.add(value);
}
result.runRemainingContributors(parameters, completionResult -> {
LookupElement element = completionResult.getLookupElement();
Object object = element.getObject();
if (object instanceof PsiElement && allowed.contains(object)) {
return;
}
result.passResult(completionResult);
});
}
use of com.intellij.codeInsight.ExpectedTypeInfo in project intellij-community by JetBrains.
the class CreateClassFromNewFix method setupInheritance.
private static void setupInheritance(PsiNewExpression element, PsiClass targetClass) throws IncorrectOperationException {
if (element.getParent() instanceof PsiReferenceExpression)
return;
ExpectedTypeInfo[] expectedTypes = ExpectedTypesProvider.getExpectedTypes(element, false);
for (ExpectedTypeInfo expectedType : expectedTypes) {
PsiType type = expectedType.getType();
if (!(type instanceof PsiClassType))
continue;
final PsiClassType classType = (PsiClassType) type;
PsiClass aClass = classType.resolve();
if (aClass == null)
continue;
if (aClass.equals(targetClass) || aClass.hasModifierProperty(PsiModifier.FINAL))
continue;
PsiElementFactory factory = JavaPsiFacade.getInstance(aClass.getProject()).getElementFactory();
if (aClass.isInterface()) {
PsiReferenceList implementsList = targetClass.getImplementsList();
assert implementsList != null : targetClass;
implementsList.add(factory.createReferenceElementByType(classType));
} else {
PsiReferenceList extendsList = targetClass.getExtendsList();
assert extendsList != null : targetClass;
if (extendsList.getReferencedTypes().length == 0 && !CommonClassNames.JAVA_LANG_OBJECT.equals(classType.getCanonicalText())) {
extendsList.add(factory.createReferenceElementByType(classType));
}
}
}
}
Aggregations