use of com.intellij.codeInsight.lookup.PsiTypeLookupItem in project intellij-community by JetBrains.
the class GrKindWeigher method weigh.
@Override
public Comparable weigh(@NotNull LookupElement element, @NotNull CompletionLocation location) {
final PsiElement position = location.getCompletionParameters().getPosition();
if (!(position.getContainingFile() instanceof GroovyFileBase))
return null;
Object o = element.getObject();
if (o instanceof ResolveResult) {
o = ((ResolveResult) o).getElement();
}
final PsiElement parent = position.getParent();
final PsiElement qualifier = parent instanceof GrReferenceElement ? ((GrReferenceElement) parent).getQualifier() : null;
if (qualifier == null) {
if (o instanceof NamedArgumentDescriptor) {
switch(((NamedArgumentDescriptor) o).getPriority()) {
case ALWAYS_ON_TOP:
return NotQualifiedKind.onTop;
case AS_LOCAL_VARIABLE:
return NotQualifiedKind.local;
default:
return NotQualifiedKind.unknown;
}
}
if (o instanceof PsiVariable && !(o instanceof PsiField)) {
return NotQualifiedKind.local;
}
PsiTypeLookupItem item = element.as(PsiTypeLookupItem.CLASS_CONDITION_KEY);
if (item != null && item.getBracketsCount() > 0) {
return NotQualifiedKind.arrayType;
}
if (isPriorityKeyword(o))
return NotQualifiedKind.local;
if (isLightElement(o))
return NotQualifiedKind.unknown;
if (o instanceof PsiClass) {
if (((PsiClass) o).isAnnotationType() && GrMainCompletionProvider.AFTER_AT.accepts(position)) {
final GrAnnotation annotation = PsiTreeUtil.getParentOfType(position, GrAnnotation.class);
if (annotation != null) {
PsiElement annoParent = annotation.getParent();
PsiElement ownerToUse = annoParent instanceof PsiModifierList ? annoParent.getParent() : annoParent;
PsiAnnotation.TargetType[] elementTypeFields = GrAnnotationImpl.getApplicableElementTypeFields(ownerToUse);
if (AnnotationTargetUtil.findAnnotationTarget((PsiClass) o, elementTypeFields) != null) {
return NotQualifiedKind.restrictedClass;
}
}
}
if (GrMainCompletionProvider.IN_CATCH_TYPE.accepts(position) && InheritanceUtil.isInheritor((PsiClass) o, CommonClassNames.JAVA_LANG_THROWABLE)) {
return NotQualifiedKind.restrictedClass;
}
}
if (o instanceof PsiMember) {
final PsiClass containingClass = ((PsiMember) o).getContainingClass();
if (isAccessor((PsiMember) o))
return NotQualifiedKind.accessor;
if (o instanceof PsiClass && ((PsiClass) o).getContainingClass() == null || o instanceof PsiPackage)
return NotQualifiedKind.unknown;
if (o instanceof PsiClass)
return NotQualifiedKind.innerClass;
if (PsiTreeUtil.isContextAncestor(containingClass, position, false))
return NotQualifiedKind.currentClassMember;
return NotQualifiedKind.member;
}
return NotQualifiedKind.unknown;
} else {
if (o instanceof PsiEnumConstant)
return QualifiedKind.enumConstant;
if (isLightElement(o))
return QualifiedKind.unknown;
if (o instanceof PsiMember) {
if (isTrashMethod((PsiMember) o))
return QualifiedKind.unknown;
if (isAccessor((PsiMember) o))
return QualifiedKind.accessor;
if (isQualifierClassMember((PsiMember) o, qualifier)) {
return QualifiedKind.currentClassMember;
}
if (o instanceof PsiClass && ((PsiClass) o).getContainingClass() == null || o instanceof PsiPackage)
return QualifiedKind.unknown;
if (o instanceof PsiClass)
return QualifiedKind.innerClass;
return QualifiedKind.member;
}
return QualifiedKind.unknown;
}
}
use of com.intellij.codeInsight.lookup.PsiTypeLookupItem in project intellij-community by JetBrains.
the class ChooseTypeExpression method calculateLookupItems.
@Override
public LookupElement[] calculateLookupItems(ExpressionContext context) {
List<LookupElement> result = ContainerUtil.newArrayList();
for (SmartTypePointer item : myItems) {
PsiType type = TypesUtil.unboxPrimitiveTypeWrapper(item.getType());
if (type == null)
continue;
PsiTypeLookupItem lookupItem = PsiTypeLookupItem.createLookupItem(type, null, PsiTypeLookupItem.isDiamond(type), IMPORT_FIXER);
result.add(lookupItem);
}
if (myForGroovy) {
LookupElementBuilder def = LookupElementBuilder.create(GrModifier.DEF).bold();
if (mySelectDef) {
result.add(0, def);
} else {
result.add(def);
}
}
return result.toArray(new LookupElement[result.size()]);
}
use of com.intellij.codeInsight.lookup.PsiTypeLookupItem in project intellij-community by JetBrains.
the class ComponentTypeOfMacro method calculateLookupItems.
@Override
public LookupElement[] calculateLookupItems(@NotNull Expression[] params, ExpressionContext context) {
if (params.length != 1)
return null;
LookupElement[] lookupItems = params[0].calculateLookupItems(context);
if (lookupItems == null)
return null;
List<LookupElement> result = ContainerUtil.newArrayList();
for (LookupElement element : lookupItems) {
PsiTypeLookupItem lookupItem = element.as(PsiTypeLookupItem.CLASS_CONDITION_KEY);
if (lookupItem != null) {
PsiType psiType = lookupItem.getType();
if (psiType instanceof PsiArrayType) {
result.add(PsiTypeLookupItem.createLookupItem(((PsiArrayType) psiType).getComponentType(), null));
}
}
}
return lookupItems;
}
use of com.intellij.codeInsight.lookup.PsiTypeLookupItem in project intellij-community by JetBrains.
the class ComponentTypeOfMacro method calculateResult.
@Override
public Result calculateResult(@NotNull Expression[] params, final ExpressionContext context) {
if (params.length != 1)
return null;
final Result result = params[0].calculateResult(context);
if (result == null)
return null;
if (result instanceof PsiTypeResult) {
PsiType type = ((PsiTypeResult) result).getType();
if (type instanceof PsiArrayType) {
return new PsiTypeResult(((PsiArrayType) type).getComponentType(), context.getProject());
}
}
PsiExpression expr = MacroUtil.resultToPsiExpression(result, context);
PsiType type = expr == null ? MacroUtil.resultToPsiType(result, context) : expr.getType();
if (type instanceof PsiArrayType) {
return new PsiTypeResult(((PsiArrayType) type).getComponentType(), context.getProject());
}
LookupElement[] elements = params[0].calculateLookupItems(context);
if (elements != null) {
for (LookupElement element : elements) {
PsiTypeLookupItem typeLookupItem = element.as(PsiTypeLookupItem.CLASS_CONDITION_KEY);
if (typeLookupItem != null) {
PsiType psiType = typeLookupItem.getType();
if (psiType instanceof PsiArrayType) {
return new PsiTypeResult(((PsiArrayType) psiType).getComponentType(), context.getProject());
}
}
}
}
return new PsiElementResult(null);
}
use of com.intellij.codeInsight.lookup.PsiTypeLookupItem in project intellij-community by JetBrains.
the class GroovySmartCompletionContributor method addExpectedType.
@Nullable
private static LookupElement addExpectedType(PsiType type, final PsiElement place, CompletionParameters parameters, @Nullable PsiType diamond) {
if (!JavaCompletionUtil.hasAccessibleConstructor(type))
return null;
final PsiClass psiClass = com.intellij.psi.util.PsiUtil.resolveClassInType(type);
if (psiClass == null)
return null;
if (!checkForInnerClass(psiClass, place))
return null;
boolean isDiamond = false;
if (diamond != null && psiClass.hasTypeParameters() && !((PsiClassType) type).isRaw() && !psiClass.hasModifierProperty(PsiModifier.ABSTRACT)) {
final String canonicalText = TypeConversionUtil.erasure(type).getCanonicalText();
final GroovyPsiElementFactory elementFactory = GroovyPsiElementFactory.getInstance(place.getProject());
final String text = diamond.getCanonicalText() + " v = new " + canonicalText + "<>()";
final GrStatement statement = elementFactory.createStatementFromText(text, parameters.getOriginalFile());
final GrVariable declaredVar = ((GrVariableDeclaration) statement).getVariables()[0];
final GrNewExpression initializer = (GrNewExpression) declaredVar.getInitializerGroovy();
assert initializer != null;
final boolean hasDefaultConstructorOrNoGenericsOne = PsiDiamondTypeImpl.hasDefaultConstructor(psiClass) || !PsiDiamondTypeImpl.haveConstructorsGenericsParameters(psiClass);
final PsiType initializerType = initializer.getType();
if (hasDefaultConstructorOrNoGenericsOne && initializerType != null && initializerType instanceof PsiClassType && ((PsiClassType) initializerType).getParameters().length > 0) {
type = initializerType;
isDiamond = true;
}
}
final PsiTypeLookupItem item = PsiTypeLookupItem.createLookupItem(GenericsUtil.eliminateWildcards(type), place, isDiamond, ChooseTypeExpression.IMPORT_FIXER).setShowPackage();
Object object = item.getObject();
if (object instanceof PsiClass && ((PsiClass) object).hasModifierProperty(PsiModifier.ABSTRACT)) {
item.setIndicateAnonymous(true);
}
item.setInsertHandler(new AfterNewClassInsertHandler((PsiClassType) type, true));
return item;
}
Aggregations