List<CaretState> (String text) { int x = 0; int y = 0; List<LogicalPosition[]> list = new ArrayList<>(); for (int i = 0; i < text.length(); i++) { char c = text.charAt(i); if (c == '\0') { int next = text.indexOf('\0', i + 1); if (next != -1) { String tag = text.substring(i + 1, next); Matcher matcher = TAG_PATTERN.matcher(tag); if (matcher.matches()) { String what = matcher.group(1); int idx = switch (what) { case "c" -> 0; // caret case "s" -> 1; // selection start case "e" -> 2; // selection end default -> throw new AssertionError("Unexpected value: " + what); }; int offset = Integer.parseInt(matcher.group(2)); while (list.size() <= offset) { list.add(new LogicalPosition[3]); } list.get(offset)[idx] = new LogicalPosition(y, x); } //noinspection AssignmentToForLoopParameter i = next; } continue; } if (c == '\n') { y++; x = 0; } else { x++; } } return ContainerUtil.map(list, arr -> new CaretState(arr[0], arr[1], arr[2])); }