Summary: Turns out there was also a problem with how this function deals with final newlines... Reviewed By: jeremydubreil Differential Revision: D4191154 fbshipit-source-id: fc10517
@ -73,7 +73,18 @@ def syntax_highlighting(source_name, mode, s):
while (i < len(s) and s[i] == '\n'):
initial_newlines += '\n'
i += 1
return initial_newlines + pygments.highlight(s, lexer, formatter)
# pygments.highlight() also insists that all string end with exactly one
# newline character regardless of the input string!
final_newlines = ''
i = 1
while (i <= len(s) and s[-i] == '\n'):
final_newlines += '\n'
colorized_string = pygments.highlight(s, lexer, formatter)
# strip the result from pygments.highlight() to get rid of the
# potentially spurious final newline, and also to continue to
# work in case the bugs in pygments.highlight() gets fixed.
return initial_newlines + colorized_string.strip('\n') + final_newlines
def color(s, color, mode):
@ -72,7 +72,7 @@ def build_source_context(source_name, mode, report_line):
# avoid going past the end of the file
for line in source_file:
last_line = line_number
if start_line <= line_number <= end_line:
if start_line <= line_number < end_line:
excerpt += line
elif line_number > end_line:
# OPTIM: no need to read past the last line of the excerpt