'Helps cleaning up the tree nodes that get inserted.'

def _remove_endmarker(self, tree_nodes):

    last_leaf = tree_nodes[(-1)].get_last_leaf()
    is_endmarker = (last_leaf.type == self.endmarker_type)
    self._last_prefix = ''
    if is_endmarker:
        try:
            separation = last_leaf.prefix.rindex('\n')
        except ValueError:
            pass
        else:
            (last_leaf.prefix, self._last_prefix) = (last_leaf.prefix[:(separation + 1)], last_leaf.prefix[(separation + 1):])
    first_leaf = tree_nodes[0].get_first_leaf()
    first_leaf.prefix = (self.prefix + first_leaf.prefix)
    self.prefix = ''
    if is_endmarker:
        self.prefix = last_leaf.prefix
        tree_nodes = tree_nodes[:(-1)]
    return tree_nodes
