Search in sources :

Example 1 with AfterNewClassInsertHandler

use of org.jetbrains.plugins.groovy.lang.completion.handlers.AfterNewClassInsertHandler 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)1 Nullable (org.jetbrains.annotations.Nullable)1 AfterNewClassInsertHandler (org.jetbrains.plugins.groovy.lang.completion.handlers.AfterNewClassInsertHandler)1 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)1 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)1 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)1