Handle potential incorrect newline representations in FileWriter.write()

This commit is contained in:
shenchucheng 2024-06-18 14:38:40 +08:00
parent 351cf18c3a
commit 9d06f9f4f8

View file

@ -1,5 +1,4 @@
import os
import re
import shutil
import subprocess
@ -27,8 +26,9 @@ class Editor:
def write(self, path: str, content: str):
"""Write the whole content to a file. When used, make sure content arg contains the full content of the file."""
if len(re.findall(r"\\n", content)) >= 5:
# A very raw rule to correct the content: Many \\n suggests all new line characters are mistaken as \\n whereas the correct one should be \n
if "\n" not in content and "\\n" in content:
# A very raw rule to correct the content: If 'content' lacks actual newlines ('\n') but includes '\\n', consider
# replacing them with '\n' to potentially correct mistaken representations of newline characters.
content = content.replace("\\n", "\n")
directory = os.path.dirname(path)
if directory and not os.path.exists(directory):