Search in sources :

Example 6 with PsiTypeLookupItem

use of com.intellij.codeInsight.lookup.PsiTypeLookupItem in project intellij-community by JetBrains.

the class GroovySmartCompletionContributor method generateInheritorVariants.

static void generateInheritorVariants(final CompletionParameters parameters, PrefixMatcher matcher, final Consumer<LookupElement> consumer) {
    final PsiElement place = parameters.getPosition();
    final GrExpression expression = PsiTreeUtil.getParentOfType(place, GrExpression.class);
    if (expression == null)
        return;
    GrExpression placeToInferType = expression;
    if (expression.getParent() instanceof GrApplicationStatement && expression.getParent().getParent() instanceof GrAssignmentExpression) {
        placeToInferType = (GrExpression) expression.getParent();
    }
    for (PsiType type : GroovyExpectedTypesProvider.getDefaultExpectedTypes(placeToInferType)) {
        if (type instanceof PsiArrayType) {
            final PsiType _type = GenericsUtil.eliminateWildcards(type);
            final PsiTypeLookupItem item = PsiTypeLookupItem.createLookupItem(_type, place, PsiTypeLookupItem.isDiamond(_type), ChooseTypeExpression.IMPORT_FIXER).setShowPackage();
            if (item.getObject() instanceof PsiClass) {
                item.setInsertHandler(new InsertHandler<LookupElement>() {

                    @Override
                    public void handleInsert(InsertionContext context, LookupElement item) {
                        GroovyCompletionUtil.addImportForItem(context.getFile(), context.getStartOffset(), item);
                    }
                });
            }
            consumer.consume(item);
        }
    }
    final List<PsiClassType> expectedClassTypes = new SmartList<>();
    for (PsiType psiType : GroovyExpectedTypesProvider.getDefaultExpectedTypes(placeToInferType)) {
        if (psiType instanceof PsiClassType) {
            PsiType type = GenericsUtil.eliminateWildcards(JavaCompletionUtil.originalize(psiType));
            final PsiClassType classType = (PsiClassType) type;
            if (classType.resolve() != null) {
                expectedClassTypes.add(classType);
            }
        }
    }
    final PsiType diamond = inferDiamond(place);
    JavaInheritorsGetter.processInheritors(parameters, expectedClassTypes, matcher, type -> {
        final LookupElement element = addExpectedType(type, place, parameters, diamond);
        if (element != null) {
            consumer.consume(element);
        }
    });
}
Also used : PsiTypeLookupItem(com.intellij.codeInsight.lookup.PsiTypeLookupItem) LookupElement(com.intellij.codeInsight.lookup.LookupElement) SmartList(com.intellij.util.SmartList)

Example 7 with PsiTypeLookupItem

use of com.intellij.codeInsight.lookup.PsiTypeLookupItem in project intellij-community by JetBrains.

the class ConstructorInsertHandler method handleInsert.

@Override
public void handleInsert(InsertionContext context, LookupElementDecorator<LookupElement> item) {
    @SuppressWarnings({ "unchecked" }) final LookupElement delegate = item.getDelegate();
    PsiClass psiClass = (PsiClass) item.getObject();
    boolean isAbstract = psiClass.hasModifierProperty(PsiModifier.ABSTRACT);
    if (Lookup.REPLACE_SELECT_CHAR == context.getCompletionChar() && context.getOffsetMap().containsOffset(PARAM_LIST_START)) {
        final int plStart = context.getOffset(PARAM_LIST_START);
        final int plEnd = context.getOffset(PARAM_LIST_END);
        if (plStart >= 0 && plEnd >= 0) {
            context.getDocument().deleteString(plStart, plEnd);
        }
    }
    context.commitDocument();
    OffsetKey insideRef = context.trackOffset(context.getTailOffset(), false);
    final PsiElement position = SmartCompletionDecorator.getPosition(context, delegate);
    if (position == null)
        return;
    final PsiExpression enclosing = PsiTreeUtil.getContextOfType(position, PsiExpression.class, true);
    final PsiAnonymousClass anonymousClass = PsiTreeUtil.getParentOfType(position, PsiAnonymousClass.class);
    final boolean inAnonymous = anonymousClass != null && anonymousClass.getParent() == enclosing;
    if (delegate instanceof PsiTypeLookupItem) {
        if (context.getDocument().getTextLength() > context.getTailOffset() && context.getDocument().getCharsSequence().charAt(context.getTailOffset()) == '<') {
            PsiJavaCodeReferenceElement ref = PsiTreeUtil.findElementOfClassAtOffset(context.getFile(), context.getTailOffset(), PsiJavaCodeReferenceElement.class, false);
            if (ref != null) {
                PsiReferenceParameterList parameterList = ref.getParameterList();
                if (parameterList != null && context.getTailOffset() == parameterList.getTextRange().getStartOffset()) {
                    context.getDocument().deleteString(parameterList.getTextRange().getStartOffset(), parameterList.getTextRange().getEndOffset());
                    context.commitDocument();
                }
            }
        }
        delegate.handleInsert(context);
        PostprocessReformattingAspect.getInstance(context.getProject()).doPostponedFormatting(context.getFile().getViewProvider());
    }
    if (item.getDelegate() instanceof JavaPsiClassReferenceElement) {
        PsiTypeLookupItem.addImportForItem(context, psiClass);
    }
    insertParentheses(context, delegate, psiClass, !inAnonymous && isAbstract);
    if (inAnonymous) {
        return;
    }
    if (mySmart) {
        FeatureUsageTracker.getInstance().triggerFeatureUsed(JavaCompletionFeatures.AFTER_NEW);
    }
    if (isAbstract) {
        PostprocessReformattingAspect.getInstance(context.getProject()).doPostponedFormatting(context.getFile().getViewProvider());
        final Editor editor = context.getEditor();
        final Document document = editor.getDocument();
        final int offset = context.getTailOffset();
        document.insertString(offset, " {}");
        OffsetKey insideBraces = context.trackOffset(offset + 2, true);
        final PsiFile file = context.getFile();
        PsiDocumentManager.getInstance(file.getProject()).commitDocument(document);
        reformatEnclosingExpressionListAtOffset(file, offset);
        if (promptTypeOrConstructorArgs(context, delegate, insideRef, insideBraces))
            return;
        editor.getCaretModel().moveToOffset(context.getOffset(insideBraces));
        context.setLaterRunnable(generateAnonymousBody(editor, file));
    } else {
        PsiDocumentManager.getInstance(context.getProject()).commitAllDocuments();
        final PsiNewExpression newExpression = PsiTreeUtil.findElementOfClassAtOffset(context.getFile(), context.getStartOffset(), PsiNewExpression.class, false);
        if (newExpression != null) {
            final PsiJavaCodeReferenceElement classReference = newExpression.getClassOrAnonymousClassReference();
            if (classReference != null) {
                CodeStyleManager.getInstance(context.getProject()).reformat(classReference);
            }
        }
        if (mySmart) {
            FeatureUsageTracker.getInstance().triggerFeatureUsed(JavaCompletionFeatures.AFTER_NEW);
        }
        promptTypeOrConstructorArgs(context, delegate, insideRef, null);
    }
}
Also used : PsiTypeLookupItem(com.intellij.codeInsight.lookup.PsiTypeLookupItem) LookupElement(com.intellij.codeInsight.lookup.LookupElement) Document(com.intellij.openapi.editor.Document) Editor(com.intellij.openapi.editor.Editor)

Example 8 with PsiTypeLookupItem

use of com.intellij.codeInsight.lookup.PsiTypeLookupItem in project intellij-community by JetBrains.

the class TypeArgumentCompletionProvider method fillAllArgs.

private void fillAllArgs(CompletionResultSet resultSet, PsiElement context, ExpectedTypeInfo info, List<PsiType> expectedArgs, PsiTypeParameterListOwner paramOwner) {
    List<PsiTypeLookupItem> typeItems = ContainerUtil.map(expectedArgs, arg -> PsiTypeLookupItem.createLookupItem(arg, context));
    TailType globalTail = mySmart ? info.getTailType() : TailType.NONE;
    TypeArgsLookupElement element = new TypeArgsLookupElement(typeItems, globalTail, hasParameters(paramOwner, context));
    element.registerSingleClass(mySession);
    resultSet.addElement(element);
}
Also used : PsiTypeLookupItem(com.intellij.codeInsight.lookup.PsiTypeLookupItem) TailType(com.intellij.codeInsight.TailType) CharTailType(com.intellij.codeInsight.CharTailType)

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