List<TodoItem> (TodoItem @NotNull [] items, @Nullable TodoFilter filter) { record ByRange(@NotNull TextRange range, @NotNull TodoItem item) { @Override public boolean equals(Object obj) { return obj instanceof ByRange br && range.equals(br.range); } @Override public int hashCode() { return range.hashCode(); } } return Arrays.stream(items) .filter(next -> filter == null || filter.contains(next.getPattern())) .map(p->new ByRange(p.getTextRange(), p)) .distinct() .map(br -> br.item()) .sorted(TodoItem.BY_START_OFFSET) .toList(); }