void (@NotNull InsertionContext context, @NotNull LookupElement item) { final CompletionItem completionItem = (CompletionItem)item.getObject(); final Document document = context.getDocument(); final Editor editor = context.getEditor(); context.commitDocument(); context.setAddCompletionChar(false); final String prefix = completionItem.getPrefix(); final String suffix = completionItem.getSuffix(); String text = document.getText(); int offset = context.getStartOffset(); // skip possible spaces after '{', e.g. "{ My Project <caret>" if (prefix.endsWith("{")) { while (offset > prefix.length() && Character.isWhitespace(text.charAt(offset - 1))) { offset--; } } if (!prefix.isEmpty() && !hasPrefixAt(document.getText(), offset - prefix.length(), prefix)) { document.insertString(offset, prefix); } offset = context.getTailOffset(); text = document.getText(); if (suffix.startsWith("} ")) { while (offset < text.length() - suffix.length() && Character.isWhitespace(text.charAt(offset))) { offset++; } } if (!suffix.isEmpty() && !hasPrefixAt(text, offset, suffix)) { document.insertString(offset, suffix); } editor.getCaretModel().moveToOffset(context.getTailOffset()); } } static boolean hasPrefixAt(String text, int offset, String prefix) { if (offset < 0 || offset >= text.length()) { return false; } return text.regionMatches(true, offset, prefix, 0, prefix.length()); } }