private static List<BidiRun> createRuns(EditorImpl editor, char[] text, int startOffsetInEditor) { int textLength = text.length; if (editor.myDisableRtl) return Collections.singletonList(new BidiRun((byte) 0, 0, textLength)); List<BidiRun> runs = new ArrayList<BidiRun>(); if (startOffsetInEditor >= 0) { // running bidi algorithm separately for text fragments corresponding to different lexer // tokens int lastOffset = startOffsetInEditor; IElementType lastToken = null; HighlighterIterator iterator = editor.getHighlighter().createIterator(startOffsetInEditor); int endOffsetInEditor = startOffsetInEditor + textLength; while (!iterator.atEnd() && iterator.getStart() < endOffsetInEditor) { IElementType currentToken = iterator.getTokenType(); if (distinctTokens(lastToken, currentToken)) { int tokenStart = Math.max(iterator.getStart(), startOffsetInEditor); addRuns(runs, text, lastOffset - startOffsetInEditor, tokenStart - startOffsetInEditor); lastToken = currentToken; lastOffset = tokenStart; } iterator.advance(); } addRuns( runs, text, lastOffset - startOffsetInEditor, endOffsetInEditor - startOffsetInEditor); } else { addRuns(runs, text, 0, textLength); } return runs; }