List<TextRange> (@NotNull String text, @Nullable Set<Character> separatorsSet) { List<TextRange> result = new SmartList<>(); int start = -1; for (int i = 0; i < text.length(); i++) { char c = text.charAt(i); boolean isIdentifierPart = separatorsSet == null ? Character.isJavaIdentifierPart(c) : !separatorsSet.contains(c); if (isIdentifierPart && start == -1) { start = i; } if (isIdentifierPart && i == text.length() - 1) { result.add(new TextRange(start, i + 1)); } else if (!isIdentifierPart && start != -1) { result.add(new TextRange(start, i)); start = -1; } } return result; }