LookupElement (String keyword, String className) { LookupElement element; PsiElement nextElement = PsiTreeUtil.skipWhitespacesAndCommentsForward(PsiTreeUtil.nextLeaf(myPosition)); IElementType nextToken; if (nextElement instanceof PsiJavaToken) { nextToken = ((PsiJavaToken)nextElement).getTokenType(); } else { if (nextElement instanceof PsiParameterList l && l.getFirstChild() instanceof PsiJavaToken t) { nextToken = t.getTokenType(); } else if (nextElement instanceof PsiCodeBlock b && b.getFirstChild() instanceof PsiJavaToken t) { nextToken = t.getTokenType(); } else { nextToken = null; } } element = LookupElementBuilder.create(keyword + " " + className).withPresentableText(keyword).bold() .withTailText(" " + className, false) .withIcon(CreateClassKind.valueOf(keyword.toUpperCase(Locale.ROOT)).getKindIcon()) .withInsertHandler((context, item) -> { Document document = context.getDocument(); int offset = context.getTailOffset(); String suffix = " "; if (keyword.equals(PsiKeyword.RECORD)) { if (JavaTokenType.LPARENTH.equals(nextToken)) { suffix = ""; } else if (JavaTokenType.LBRACE.equals(nextToken)) { suffix = "() "; } else { suffix = "() {\n}"; } } else if (!JavaTokenType.LBRACE.equals(nextToken)) { suffix = " {\n}"; } if (offset < document.getTextLength() && document.getCharsSequence().charAt(offset) == ' ') { suffix = suffix.trim(); } document.insertString(offset, suffix); context.getEditor().getCaretModel().moveToOffset(offset + 1); }); return element; }