PyPartitionScanner pyPartitionScanner = new PyPartitionScanner();
FastPartitioner fastPartitioner = new FastPartitioner(pyPartitionScanner, IPythonPartitions.types);
Document doc = new Document(result);
fastPartitioner.connect(doc);
TextPresentation textPresentation = new TextPresentation();
PyCodeScanner scanner = new PyCodeScanner(colorCache);
try {
ITypedRegion[] computePartitioning = fastPartitioner.computePartitioning(0, doc.getLength());
for (ITypedRegion region : computePartitioning) {
String type = region.getType();
int offset = region.getOffset();
int len = region.getLength();
if (IPythonPartitions.PY_DEFAULT.equals(type) || type == null) {
createDefaultRanges(textPresentation, scanner, doc, offset, len);
} else if (IPythonPartitions.PY_COMMENT.equals(type)) {
TextAttribute textAttribute = colorCache.getCommentTextAttribute();
textPresentation.addStyleRange(new StyleRange(offset, len, textAttribute.getForeground(), null,
textAttribute.getStyle()));
} else if (IPythonPartitions.PY_BACKQUOTES.equals(type)) {
TextAttribute textAttribute = colorCache.getBackquotesTextAttribute();
textPresentation.addStyleRange(new StyleRange(offset, len, textAttribute.getForeground(), null,
textAttribute.getStyle()));
} else if (IPythonPartitions.PY_MULTILINE_STRING1.equals(type)
|| IPythonPartitions.PY_MULTILINE_STRING2.equals(type)
|| IPythonPartitions.PY_SINGLELINE_STRING1.equals(type)
|| IPythonPartitions.PY_SINGLELINE_STRING2.equals(type)) {
TextAttribute textAttribute = colorCache.getStringTextAttribute();
textPresentation.addStyleRange(new StyleRange(offset, len, textAttribute.getForeground(), null,
textAttribute.getStyle()));
}
}
} finally {
fastPartitioner.disconnect();
}
if (showSpacesAndNewLines) {
for (int i = 0; i < result.length(); i++) {
char curr = result.charAt(i);
if (curr == '\\' && i + 1 < result.length() && result.charAt(i + 1) == 'n') {
textPresentation.mergeStyleRange(new StyleRange(i, 2, colorCache.getColor(new RGB(180, 180, 180)),
null));
i += 1;
} else if (curr == ' ') {
int finalI = i;
for (; finalI < result.length() && result.charAt(finalI) == ' '; finalI++) {
//just iterate (the finalI will have the right value at the end).
}
textPresentation.mergeStyleRange(new StyleRange(i, finalI - i, colorCache.getColor(new RGB(180,
180, 180)), null));
}
}
}
ArrayList<StyleRange> list = new ArrayList<StyleRange>();
Iterator<StyleRange> it = textPresentation.getAllStyleRangeIterator();
while (it.hasNext()) {
list.add(it.next());
}
StyleRange[] ranges = list.toArray(new StyleRange[list.size()]);
return new Tuple<String, StyleRange[]>(finalResult, ranges);