String (@NotNull CharSequence text, int selectionStart, int selectionEnd) { int maxBacktickSequenceSeen = 0; int curBacktickSequence = 0; for (int i = selectionStart; i < selectionEnd; ++i) { if (text.charAt(i) != '`') { curBacktickSequence = 0; } else { curBacktickSequence++; maxBacktickSequenceSeen = Math.max(maxBacktickSequenceSeen, curBacktickSequence); } } return StringUtil.repeat("`", maxBacktickSequenceSeen + 1); }