boolean (@NotNull Editor editor, @NotNull Set<? extends Pair<Integer, ElementPattern<String>>> restartingPrefixConditions, @NotNull String toAppend) { int caretOffset = editor.getCaretModel().getOffset(); CharSequence text = editor.getDocument().getCharsSequence(); for (Pair<Integer, ElementPattern<String>> pair : restartingPrefixConditions) { int start = pair.first; if (caretOffset >= start && start >= 0 && caretOffset <= text.length()) { String newPrefix = text.subSequence(start, caretOffset).toString() + toAppend; if (pair.second.accepts(newPrefix)) { return true; } } } return false; }