Fix invalid iterator usage in TextDocument::appendLine()