from io import StringIO
import tokenize


def multiline_string_lines(source, include_docstrings=False):
    """Return line numbers that are within multiline strings.

    The line numbers are indexed at 1.

    Docstrings are ignored.

    """
    sio = StringIO(source)
    line_numbers = set()
    previous_token_type = ''
    try:
        for t in tokenize.generate_tokens(sio.readline):
            token_type = t[0]
            start_row = t[2][0]
            end_row = t[3][0]
            start_row = t[2][0]
            end_row = t[3][0]

            if (token_type == tokenize.STRING and start_row != end_row):
                if (include_docstrings or
                        previous_token_type != tokenize.INDENT):
                    # We increment by one since we want the contents of the
                    # string.
                    line_numbers |= set(range(1 + start_row, 1 + end_row))

            previous_token_type = token_type
    except (IndentationError, tokenize.TokenError):
        pass

    return line_numbers