from __future__ import annotations
import re
from pathlib import Path
ROOT = Path("/mnt/c/Users/RyanJ/spectre-rs")
TARGETS = [
*(ROOT / "src").glob("*.rs"),
*(ROOT / "crates/spectre-parse/src").glob("*.rs"),
]
def normalize(text: str) -> str:
lines = text.splitlines(keepends=True)
out: list[str] = []
for i, line in enumerate(lines):
m = re.match(r"^( )(//[/!]?[ ]?)(.*)$", line)
if not m:
out.append(line)
continue
_, marker, rest = m.groups()
target_indent = ""
for j in range(i + 1, len(lines)):
nxt = lines[j].rstrip("\n")
if not nxt.strip():
continue
nxt_indent = re.match(r"^(\s*)", nxt).group(1)
target_indent = nxt_indent
break
new = target_indent + marker + rest
if not new.endswith("\n"):
new += "\n"
out.append(new)
return "".join(out)
for path in TARGETS:
if "target/" in str(path):
continue
original = path.read_text(encoding="utf-8")
fixed = normalize(original)
if fixed != original:
path.write_text(fixed, encoding="utf-8")
print(f" re-indented {path.relative_to(ROOT)}")