in scripts/convert_addSourceLines_textblock.py [0:0]
def transform_args_text(args_text: str) -> Optional[str]:
lines = args_text.splitlines(keepends=True)
if not lines:
return None
items: List[Tuple[str, str, str, Optional[str]]] = []
arg_line_indices: List[int] = []
filename_line_idx = None
filename_line = None
arg_indent = None
for idx, line in enumerate(lines):
if not line.strip():
continue
indent_len = len(line) - len(line.lstrip())
indent = line[:indent_len]
body = line[indent_len:]
if body.startswith('"""'):
return None
if body.startswith('"'):
parsed = split_string_literal(body)
if parsed is None:
return None
content, rest = parsed
rest_no_nl = rest.rstrip("\r\n")
comment_idx = rest_no_nl.find("//")
block_idx = rest_no_nl.find("/*")
idx_comment = min(
[i for i in (comment_idx, block_idx) if i != -1],
default=-1,
)
comment = None
spacing = ""
rest_prefix = rest_no_nl
if idx_comment != -1:
comment = rest_no_nl[idx_comment:]
spacing = rest_no_nl[:idx_comment].replace(",", "")
rest_prefix = rest_no_nl[:idx_comment]
if rest_prefix.strip().strip(","):
return None
if filename_line_idx is None:
filename_line_idx = idx
filename_line = line
arg_indent = indent
else:
items.append(("string", content, spacing, comment))
arg_line_indices.append(idx)
elif body.lstrip().startswith("//") or body.lstrip().startswith("/*"):
if filename_line_idx is None:
return None
if arg_indent is None:
arg_indent = indent
if line.startswith(arg_indent):
comment_body = line[len(arg_indent) :].rstrip("\r\n")
else:
comment_body = line.lstrip().rstrip("\r\n")
items.append(("comment", comment_body, "", None))
arg_line_indices.append(idx)
else:
return None
if filename_line_idx is None or filename_line is None:
return None
if not items:
return None
if arg_indent is None:
arg_indent = ""
last_arg_line_idx = max(arg_line_indices) if arg_line_indices else filename_line_idx
line_ending = "\n"
for line in lines:
if line.endswith("\r\n"):
line_ending = "\r\n"
break
content_lines: List[str] = []
for kind, content, spacing, comment in items:
if kind == "string":
line = unescape_for_text_block(content)
if comment:
line += f"{spacing}{comment}"
content_lines.append(line)
else:
content_lines.append(content)
prefix_lines = lines[:filename_line_idx]
suffix_lines = lines[last_arg_line_idx + 1 :]
new_lines: List[str] = []
new_lines.extend(prefix_lines)
if filename_line.endswith(("\n", "\r\n")):
new_lines.append(filename_line)
else:
new_lines.append(filename_line + line_ending)
new_lines.append(f'{arg_indent}"""{line_ending}')
for content_line in content_lines:
new_lines.append(f"{arg_indent}{content_line}{line_ending}")
new_lines.append(f'{arg_indent}"""{line_ending}')
new_lines.extend(suffix_lines)
return "".join(new_lines)