code: def split_lines(tokenlist):
      
      line = []
  
      for item in tokenlist:
          # For (token, text) tuples.
          if len(item) == 2:
              token, string = item
              parts = string.split('\n')
  
              for part in parts[:-1]:
                  if part:
                      line.append((token, part))
                  yield line
                  line = []
  
              line.append((token, parts[-1]))
                  # Note that parts[-1] can be empty, and that's fine. It happens
                  # in the case of [(Token.SetCursorPosition, '')].
  
          # For (token, text, mouse_handler) tuples.
          #     I know, partly copy/paste, but understandable and more efficient
          #     than many tests.
          else:
              token, string, mouse_handler = item
              parts = string.split('\n')
  
              for part in parts[:-1]:
                  if part:
                      line.append((token, part, mouse_handler))
                  yield line
                  line = []
  
              line.append((token, parts[-1], mouse_handler))
  
      # Always yield the last line, even when this is an empty line. This ensures
      # that when `tokenlist` ends with a newline character, an additional empty
      # line is yielded. (Otherwise, there's no way to differentiate between the
      # cases where `tokenlist` does and doesn't end with a newline.)
      yield line
docs: Take a single list of (Token, text) tuples and yield one such list for each
      line. Just like str.split, this will yield at least one item.
  
      :param tokenlist: List of (token, text) or (token, text, mouse_handler)
                        tuples.