Collects the set of lines that should be visible in a diff with a certain number of context lines

def _visible_in_diff(merge_result, context_lines=3):
    """Collects the set of lines that should be visible in a diff with a certain number of context lines"""
    i = old_line = new_line = 0
    while i < len(merge_result):
        line_or_conflict = merge_result[i]
        if isinstance(line_or_conflict, tuple):
            yield old_line, new_line, line_or_conflict
            old_line += len(line_or_conflict[0])
            new_line += len(line_or_conflict[1])
        else:
            for j in (list(range(max(0, i-context_lines), i                                        )) +  # look behind for nearby conflicts
                      list(range(i+1                    , min(len(merge_result), i+1+context_lines)))):  # look ahead for nearby conflicts
                if isinstance(merge_result[j], tuple):
                    yield old_line, new_line, line_or_conflict
                    break
            else:
                yield None  # sentinel to mark boundaries between diff section groups
            old_line += 1
            new_line += 1
        i += 1
    yield None