}
//offset can be changed after text trimming after insert due to buffer constraints
appendToHistoryDocument(history, text);
int offset = history.getTextLength() - text.length();
final HighlighterIterator iterator = highlighter.createIterator(localStartOffset);
final int localEndOffset = textRange.getEndOffset();
while (!iterator.atEnd()) {
final int itStart = iterator.getStart();
if (itStart > localEndOffset) break;
final int itEnd = iterator.getEnd();
if (itEnd >= localStartOffset) {
final int start = Math.max(itStart, localStartOffset) - localStartOffset + offset;
final int end = Math.min(itEnd, localEndOffset) - localStartOffset + offset;
markupModel.addRangeHighlighter(start, end, HighlighterLayer.SYNTAX, iterator.getTextAttributes(),
HighlighterTargetArea.EXACT_RANGE);
}
iterator.advance();
}
if (!text.endsWith("\n")) {
appendToHistoryDocument(history, "\n");
}
return text;