Search in sources :

Example 1 with PsiTypeLookupItem

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;
    }
}
Also used : NamedArgumentDescriptor(org.jetbrains.plugins.groovy.extensions.NamedArgumentDescriptor) PsiTypeLookupItem(com.intellij.codeInsight.lookup.PsiTypeLookupItem) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)

Example 2 with PsiTypeLookupItem

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()]);
}
Also used : PsiTypeLookupItem(com.intellij.codeInsight.lookup.PsiTypeLookupItem) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) LookupElement(com.intellij.codeInsight.lookup.LookupElement)

Example 3 with PsiTypeLookupItem

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;
}
Also used : PsiTypeLookupItem(com.intellij.codeInsight.lookup.PsiTypeLookupItem) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PsiArrayType(com.intellij.psi.PsiArrayType) PsiType(com.intellij.psi.PsiType)

Example 4 with PsiTypeLookupItem

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);
}
Also used : PsiTypeLookupItem(com.intellij.codeInsight.lookup.PsiTypeLookupItem) PsiExpression(com.intellij.psi.PsiExpression) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PsiArrayType(com.intellij.psi.PsiArrayType) PsiType(com.intellij.psi.PsiType)

Example 5 with PsiTypeLookupItem

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;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) PsiTypeLookupItem(com.intellij.codeInsight.lookup.PsiTypeLookupItem) AfterNewClassInsertHandler(org.jetbrains.plugins.groovy.lang.completion.handlers.AfterNewClassInsertHandler) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PsiTypeLookupItem (com.intellij.codeInsight.lookup.PsiTypeLookupItem)8 LookupElement (com.intellij.codeInsight.lookup.LookupElement)5 PsiArrayType (com.intellij.psi.PsiArrayType)2 PsiType (com.intellij.psi.PsiType)2 CharTailType (com.intellij.codeInsight.CharTailType)1 TailType (com.intellij.codeInsight.TailType)1 LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)1 Document (com.intellij.openapi.editor.Document)1 Editor (com.intellij.openapi.editor.Editor)1 PsiExpression (com.intellij.psi.PsiExpression)1 SmartList (com.intellij.util.SmartList)1 Nullable (org.jetbrains.annotations.Nullable)1 NamedArgumentDescriptor (org.jetbrains.plugins.groovy.extensions.NamedArgumentDescriptor)1 AfterNewClassInsertHandler (org.jetbrains.plugins.groovy.lang.completion.handlers.AfterNewClassInsertHandler)1 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)1 GroovyFileBase (org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase)1 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)1 GrAnnotation (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation)1 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)1 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)1