fix: decode repeated source layout escapes

This commit is contained in:
wuyang
2026-07-26 14:37:56 +08:00
parent 2dd308cf3b
commit 1216e96038
2 changed files with 25 additions and 8 deletions
+13 -8
View File
@@ -257,19 +257,22 @@ def decode_code_layout_escapes(content: str) -> str:
index += 1
continue
if content.startswith("\\r\\n", index):
escaped_crlf = re.match(r"\\+r\\+n", content[index:])
if escaped_crlf:
output.append("\n")
index += 4
index += len(escaped_crlf.group(0))
line_comment = False
continue
if content.startswith("\\n", index):
escaped_newline = re.match(r"\\+n", content[index:])
if escaped_newline:
output.append("\n")
index += 2
index += len(escaped_newline.group(0))
line_comment = False
continue
if content.startswith("\\t", index):
escaped_tab = re.match(r"\\+t", content[index:])
if escaped_tab:
output.append("\t")
index += 2
index += len(escaped_tab.group(0))
continue
if line_comment:
if content[index] == "\n":
@@ -297,13 +300,15 @@ def decode_code_layout_escapes(content: str) -> str:
def normalize_write_file_content(path: str, content: str) -> str:
literal_newlines = content.count("\\n") + content.count("\\r\\n")
actual_newlines = content.count("\n")
if literal_newlines < 2 or literal_newlines <= max(2, actual_newlines * 2):
if literal_newlines < 2:
return content
suffix = PurePosixPath(path).suffix.lower()
if suffix in CODE_FILE_SUFFIXES:
return decode_code_layout_escapes(content)
if suffix in PLAIN_TEXT_FILE_SUFFIXES:
actual_newlines = content.count("\n")
if literal_newlines <= max(2, actual_newlines * 2):
return content
return content.replace("\\r\\n", "\n").replace("\\n", "\n").replace("\\t", "\t")
return content