Search in sources :

Example 1 with TailType

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

the class TailTypeDecorator method handleInsert.

@Override
public void handleInsert(InsertionContext context) {
    final LookupElement delegate = getDelegate();
    final TailType tailType = computeTailType(context);
    final LookupItem lookupItem = delegate.as(LookupItem.CLASS_CONDITION_KEY);
    if (lookupItem != null && tailType != null) {
        lookupItem.setTailType(TailType.UNKNOWN);
    }
    delegate.handleInsert(context);
    if (tailType != null && tailType.isApplicable(context)) {
        PostprocessReformattingAspect.getInstance(context.getProject()).doPostponedFormatting();
        int tailOffset = context.getTailOffset();
        if (tailOffset < 0) {
            throw new AssertionError("tailOffset < 0: delegate=" + getDelegate() + "; this=" + this + "; tail=" + tailType);
        }
        tailType.processTail(context.getEditor(), tailOffset);
    }
}
Also used : TailType(com.intellij.codeInsight.TailType)

Example 2 with TailType

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

the class JavaKeywordCompletion method addBreakContinue.

private static void addBreakContinue(Consumer<LookupElement> result, PsiElement position) {
    PsiLoopStatement loop = PsiTreeUtil.getParentOfType(position, PsiLoopStatement.class, true, PsiLambdaExpression.class, PsiMember.class);
    LookupElement br = createKeyword(position, PsiKeyword.BREAK);
    LookupElement cont = createKeyword(position, PsiKeyword.CONTINUE);
    TailType tailType;
    if (psiElement().insideSequence(true, psiElement(PsiLabeledStatement.class), or(psiElement(PsiFile.class), psiElement(PsiMethod.class), psiElement(PsiClassInitializer.class))).accepts(position)) {
        tailType = TailType.HUMBLE_SPACE_BEFORE_WORD;
    } else {
        tailType = TailType.SEMICOLON;
    }
    br = TailTypeDecorator.withTail(br, tailType);
    cont = TailTypeDecorator.withTail(cont, tailType);
    if (loop != null && PsiTreeUtil.isAncestor(loop.getBody(), position, false)) {
        result.consume(br);
        result.consume(cont);
    }
    if (psiElement().inside(PsiSwitchStatement.class).accepts(position)) {
        result.consume(br);
    }
    for (PsiLabeledStatement labeled : psiApi().parents(position).takeWhile(notInstanceOf(PsiMember.class)).filter(PsiLabeledStatement.class)) {
        result.consume(TailTypeDecorator.withTail(LookupElementBuilder.create("break " + labeled.getName()).bold(), TailType.SEMICOLON));
    }
}
Also used : TailType(com.intellij.codeInsight.TailType)

Example 3 with TailType

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

the class JavaKeywordCompletion method addInstanceof.

private static void addInstanceof(Consumer<LookupElement> result, PsiElement position) {
    if (isInstanceofPlace(position)) {
        result.consume(LookupElementDecorator.withInsertHandler(createKeyword(position, PsiKeyword.INSTANCEOF), new InsertHandler<LookupElementDecorator<LookupElement>>() {

            @Override
            public void handleInsert(InsertionContext context, LookupElementDecorator<LookupElement> item) {
                TailType tailType = TailType.HUMBLE_SPACE_BEFORE_WORD;
                if (tailType.isApplicable(context)) {
                    tailType.processTail(context.getEditor(), context.getTailOffset());
                }
                if ('!' == context.getCompletionChar()) {
                    context.setAddCompletionChar(false);
                    context.commitDocument();
                    PsiInstanceOfExpression expr = PsiTreeUtil.findElementOfClassAtOffset(context.getFile(), context.getStartOffset(), PsiInstanceOfExpression.class, false);
                    if (expr != null) {
                        String space = context.getCodeStyleSettings().SPACE_WITHIN_PARENTHESES ? " " : "";
                        context.getDocument().insertString(expr.getTextRange().getStartOffset(), "!(" + space);
                        context.getDocument().insertString(context.getTailOffset(), space + ")");
                    }
                }
            }
        }));
    }
}
Also used : ParenthesesInsertHandler(com.intellij.codeInsight.completion.util.ParenthesesInsertHandler) TailType(com.intellij.codeInsight.TailType)

Example 4 with TailType

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

the class SmartCompletionDecorator method computeTailType.

@Override
protected TailType computeTailType(InsertionContext context) {
    if (context.getCompletionChar() == Lookup.COMPLETE_STATEMENT_SELECT_CHAR) {
        return TailType.NONE;
    }
    if (LookupItem.getDefaultTailType(context.getCompletionChar()) != null) {
        return null;
    }
    LookupElement delegate = getDelegate();
    LookupItem item = as(LookupItem.CLASS_CONDITION_KEY);
    Object object = delegate.getObject();
    if (!CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET && (object instanceof PsiMethod || object instanceof PsiClass)) {
        return TailType.NONE;
    }
    final PsiExpression enclosing = PsiTreeUtil.getContextOfType(myPosition, PsiExpression.class, true);
    if (enclosing != null) {
        final PsiType type = JavaCompletionUtil.getLookupElementType(delegate);
        final TailType itemType = item != null ? item.getTailType() : TailType.NONE;
        if (type != null && type.isValid()) {
            Set<TailType> voidTyped = new HashSet<>();
            Set<TailType> sameTyped = new HashSet<>();
            Set<TailType> assignableTyped = new HashSet<>();
            for (ExpectedTypeInfo info : myExpectedTypeInfos) {
                final PsiType infoType = info.getType();
                final PsiType originalInfoType = JavaCompletionUtil.originalize(infoType);
                if (PsiType.VOID.equals(infoType)) {
                    voidTyped.add(info.getTailType());
                } else if (infoType.equals(type) || originalInfoType.equals(type)) {
                    sameTyped.add(info.getTailType());
                } else if ((info.getKind() == ExpectedTypeInfo.TYPE_OR_SUBTYPE && (infoType.isAssignableFrom(type) || originalInfoType.isAssignableFrom(type))) || (info.getKind() == ExpectedTypeInfo.TYPE_OR_SUPERTYPE && (type.isAssignableFrom(infoType) || type.isAssignableFrom(originalInfoType)))) {
                    assignableTyped.add(info.getTailType());
                }
            }
            if (!sameTyped.isEmpty()) {
                return sameTyped.size() == 1 ? sameTyped.iterator().next() : itemType;
            }
            if (!assignableTyped.isEmpty()) {
                return assignableTyped.size() == 1 ? assignableTyped.iterator().next() : itemType;
            }
            if (!voidTyped.isEmpty()) {
                return voidTyped.size() == 1 ? voidTyped.iterator().next() : itemType;
            }
        } else {
            if (myExpectedTypeInfos.size() == 1) {
                return myExpectedTypeInfos.iterator().next().getTailType();
            }
        }
        return itemType;
    }
    return null;
}
Also used : ExpectedTypeInfo(com.intellij.codeInsight.ExpectedTypeInfo) LookupItem(com.intellij.codeInsight.lookup.LookupItem) LookupElement(com.intellij.codeInsight.lookup.LookupElement) TailType(com.intellij.codeInsight.TailType) THashSet(gnu.trove.THashSet) HashSet(java.util.HashSet)

Example 5 with TailType

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

the class LookupItemUtil method objectToLookupItem.

/**
   * @deprecated
   * @see LookupElementBuilder
  */
@NotNull
public static LookupElement objectToLookupItem(Object object) {
    if (object instanceof LookupElement)
        return (LookupElement) object;
    if (object instanceof PsiClass) {
        return JavaClassNameCompletionContributor.createClassLookupItem((PsiClass) object, true);
    }
    if (object instanceof PsiMethod) {
        return new JavaMethodCallElement((PsiMethod) object);
    }
    if (object instanceof PsiVariable) {
        return new VariableLookupItem((PsiVariable) object);
    }
    if (object instanceof PsiExpression) {
        return new ExpressionLookupItem((PsiExpression) object);
    }
    if (object instanceof PsiType) {
        return PsiTypeLookupItem.createLookupItem((PsiType) object, null);
    }
    if (object instanceof PsiPackage) {
        return new PackageLookupItem((PsiPackage) object);
    }
    String s = null;
    LookupItem item = new LookupItem(object, "");
    if (object instanceof PsiElement) {
        s = PsiUtilCore.getName((PsiElement) object);
    }
    TailType tailType = TailType.NONE;
    if (object instanceof PsiMetaData) {
        s = ((PsiMetaData) object).getName();
    } else if (object instanceof String) {
        s = (String) object;
    } else if (object instanceof Template) {
        s = ((Template) object).getKey();
    } else if (object instanceof PresentableLookupValue) {
        s = ((PresentableLookupValue) object).getPresentation();
    }
    if (object instanceof LookupValueWithUIHint && ((LookupValueWithUIHint) object).isBold()) {
        item.setBold();
    }
    if (s == null) {
        LOG.error("Null string for object: " + object + " of class " + (object != null ? object.getClass() : null));
    }
    item.setLookupString(s);
    item.setTailType(tailType);
    return item;
}
Also used : JavaMethodCallElement(com.intellij.codeInsight.completion.JavaMethodCallElement) TailType(com.intellij.codeInsight.TailType) Template(com.intellij.codeInsight.template.Template) PsiMetaData(com.intellij.psi.meta.PsiMetaData) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

TailType (com.intellij.codeInsight.TailType)15 CharTailType (com.intellij.codeInsight.CharTailType)4 Editor (com.intellij.openapi.editor.Editor)3 LookupElement (com.intellij.codeInsight.lookup.LookupElement)2 LookupItem (com.intellij.codeInsight.lookup.LookupItem)2 Template (com.intellij.codeInsight.template.Template)2 PsiElement (com.intellij.psi.PsiElement)2 PsiMetaData (com.intellij.psi.meta.PsiMetaData)2 NotNull (org.jetbrains.annotations.NotNull)2 ExpectedTypeInfo (com.intellij.codeInsight.ExpectedTypeInfo)1 InsertHandler (com.intellij.codeInsight.completion.InsertHandler)1 JavaMethodCallElement (com.intellij.codeInsight.completion.JavaMethodCallElement)1 ParenthesesInsertHandler (com.intellij.codeInsight.completion.util.ParenthesesInsertHandler)1 LookupValueWithUIHint (com.intellij.codeInsight.lookup.LookupValueWithUIHint)1 PresentableLookupValue (com.intellij.codeInsight.lookup.PresentableLookupValue)1 PsiTypeLookupItem (com.intellij.codeInsight.lookup.PsiTypeLookupItem)1 CfmlAttributeDescription (com.intellij.coldFusion.model.info.CfmlAttributeDescription)1 CfmlComponent (com.intellij.coldFusion.model.psi.CfmlComponent)1 CfmlTag (com.intellij.coldFusion.model.psi.CfmlTag)1 CfmlAttributeImpl (com.intellij.coldFusion.model.psi.impl.CfmlAttributeImpl)1