int start = s.getStartLine();
int end = s.getEndLine();
IDocument doc = ed.getDocument();
int soff = doc.getLineOffset(start);
int eoff = lineEndOffset(end, doc);
IntRange editedRegion = new IntRange(soff, eoff);
MarkdownPage page = ed.getMarkdownPage();
StringBuilder sb = new StringBuilder(s.getLength());
for(int i=start; i<=end; i++) {
IRegion para = getParagraph(page, i, ed.getDocument());
if (para==null) {
sb.append(page.getText().get(i));
continue;
}
String paragraph = ed.getDocument().get(para.getOffset(), para.getLength());
// int lines = StrUtils.splitLines(paragraph).length;
String formatted = MarkdownFormatter.format(paragraph, cols);
// append formatted and move forward
sb.append(formatted);
CharSequence le = lineEnd(i, doc);
sb.append(le);
int pEnd = doc.getLineOfOffset(para.getOffset()+para.getLength());
i = pEnd;
// Adjust edited region?
IntRange pr = new IntRange(para.getOffset(),
para.getOffset()+para.getLength()+le.length());
editedRegion = new IntRange(Math.min(pr.low, editedRegion.low),
Math.max(pr.high, editedRegion.high));
}
// Replace the unformatted region with the new formatted one
String old = doc.get(editedRegion.low, editedRegion.size());
String newText = sb.toString();