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;
}
use of com.intellij.codeInsight.TailType in project intellij-community by JetBrains.
the class CompletionData method objectToLookupItem.
public static LookupElement objectToLookupItem(@NotNull final Object object) {
if (object instanceof LookupElement)
return (LookupElement) object;
String s = null;
TailType tailType = TailType.NONE;
if (object instanceof PsiElement) {
s = PsiUtilCore.getName((PsiElement) object);
} else 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 (s == null) {
throw new AssertionError("Null string for object: " + object + " of class " + object.getClass());
}
LookupItem item = new LookupItem(object, s);
if (object instanceof LookupValueWithUIHint && ((LookupValueWithUIHint) object).isBold()) {
item.setBold();
}
item.setAttribute(LookupItem.TAIL_TYPE_ATTR, tailType);
return item;
}
use of com.intellij.codeInsight.TailType in project intellij-community by JetBrains.
the class LookupItem method handleInsert.
@Override
public void handleInsert(final InsertionContext context) {
final InsertHandler<? extends LookupElement> handler = getInsertHandler();
if (handler != null) {
//noinspection unchecked
((InsertHandler) handler).handleInsert(context, this);
}
if (getTailType() != TailType.UNKNOWN && myInsertHandler == null) {
context.setAddCompletionChar(false);
final TailType type = handleCompletionChar(context.getEditor(), this, context.getCompletionChar());
type.processTail(context.getEditor(), context.getTailOffset());
}
}
use of com.intellij.codeInsight.TailType in project intellij-community by JetBrains.
the class LookupItem method handleCompletionChar.
@NotNull
public static TailType handleCompletionChar(@NotNull final Editor editor, @NotNull final LookupElement lookupElement, final char completionChar) {
final TailType type = getDefaultTailType(completionChar);
if (type != null) {
return type;
}
if (lookupElement instanceof LookupItem) {
final LookupItem<?> item = (LookupItem) lookupElement;
final TailType attr = item.getAttribute(TAIL_TYPE_ATTR);
if (attr != null) {
return attr;
}
}
return TailType.NONE;
}
use of com.intellij.codeInsight.TailType in project intellij-community by JetBrains.
the class DefaultInsertHandler method handleInsert.
@Override
public void handleInsert(final InsertionContext context, LookupElement lookupElement) {
context.commitDocument();
TailType tailType = getTailType(context.getCompletionChar(), (LookupItem) lookupElement);
final Editor editor = context.getEditor();
editor.getCaretModel().moveToOffset(context.getSelectionEndOffset());
tailType.processTail(editor, context.getSelectionEndOffset());
editor.getSelectionModel().removeSelection();
if (tailType == TailType.DOT || context.getCompletionChar() == '.') {
AutoPopupController.getInstance(context.getProject()).autoPopupMemberLookup(editor, null);
}
}
Aggregations