String (List<? extends TextRange> ranges) { StringBuilder b = new StringBuilder(getEditor().getDocument().getText()); int prevStart = Integer.MAX_VALUE; for (int i = ranges.size() - 1; i >= 0; i--) { TextRange r = ranges.get(i); assertTrue(r.getEndOffset() <= prevStart); b.insert(r.getEndOffset(), ']'); b.insert(prevStart = r.getStartOffset(), '['); } return b.toString(); }