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);
}
}
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));
}
}
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 + ")");
}
}
}
}));
}
}
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 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;
}
Aggregations