LookupElement (@NotNull LookupElement item, @NotNull PsiTypeLookupItem castTypeItem) { return new LookupElementDecorator<>(item) { @Override public void handleInsert(@NotNull InsertionContext context) { Document document = context.getEditor().getDocument(); context.commitDocument(); PsiFile file = context.getFile(); PsiJavaCodeReferenceElement ref = PsiTreeUtil.findElementOfClassAtOffset(file, context.getStartOffset(), PsiJavaCodeReferenceElement.class, false); if (ref != null) { PsiElement qualifier = ref.getQualifier(); if (qualifier != null) { CommonCodeStyleSettings settings = CompletionStyleUtil.getCodeStyleSettings(context); String parenSpace = settings.SPACE_WITHIN_PARENTHESES ? " " : ""; document.insertString(qualifier.getTextRange().getEndOffset(), parenSpace + ")"); String spaceWithin = settings.SPACE_WITHIN_CAST_PARENTHESES ? " " : ""; String prefix = "(" + parenSpace + "(" + spaceWithin; String spaceAfter = settings.SPACE_AFTER_TYPE_CAST ? " " : ""; int exprStart = qualifier.getTextRange().getStartOffset(); document.insertString(exprStart, prefix + spaceWithin + ")" + spaceAfter); CompletionUtil.emulateInsertion(context, exprStart + prefix.length(), castTypeItem); PsiDocumentManager.getInstance(file.getProject()).doPostponedOperationsAndUnblockDocument(document); context.getEditor().getCaretModel().moveToOffset(context.getTailOffset()); } } super.handleInsert(context); } @Override public void renderElement(@NotNull LookupElementPresentation presentation) { super.renderElement(presentation); presentation.appendTailText(" on " + castTypeItem.getType().getPresentableText(), true); } }; }