Store the previous and previous-previous items.

There's really no need to loop back through the line to get the previous
and previous-previous items. Just store them instead.
Move logic for adding a container into ReformattedLines object.
Use convenience function to add linebreak / indents at an index.
Remove unused variable.