def highlight_source(linenumber, index, lines, offset=None, text_range=None):
    """Extracts a few relevant lines from a file content given as a list
    of lines, adding line number information and identifying
    a particular line.

    When dealing with a ``SyntaxError`` or its subclasses, offset is an
    integer normally used by Python to indicate the position of
    the error with a ``^``, like::

        if True
              ^

    which, in this case, points to a missing colon. We use the same
    representation in this case.
    """

    # The weird index arithmetic below is based on the information returned
    # by Python's inspect.getinnerframes()
    new_lines = []
    problem_line = ""
    nb_digits = len(str(linenumber + index))
    no_mark = "       {:%d}: " % nb_digits
    with_mark = "    -->{:%d}: " % nb_digits

    offset_mark = None
    if offset is not None:
        offset_mark = " " * (8 + nb_digits + offset) + "^"

    text_range_mark = None
    if text_range is not None:
        begin, end = text_range
        text_range_mark = " " * (8 + nb_digits + begin + 1) + "^" * (end - begin)

    marked = False
    for i, line in enumerate(lines, linenumber - index):
        if i == linenumber:
            num = with_mark.format(i)
            problem_line = line
            new_lines.append(num + line.rstrip())
            if offset_mark is not None:
                new_lines.append(offset_mark)
            elif text_range_mark is not None:
                new_lines.append(text_range_mark)
            marked = True
        elif marked:
            if not line.strip():  # do not add empty line if last line
                break
            num = no_mark.format(i)
            new_lines.append(num + line.rstrip())
        else:
            num = no_mark.format(i)
            new_lines.append(num + line.rstrip())
    return "\n".join(new_lines), problem_line