Files
jspg/reorder.py
2026-03-18 02:41:56 -04:00

70 lines
1.7 KiB
Python

import sys
with open("src/queryer/compiler.rs", "r") as f:
text = f.read()
def find_function(text, name):
pos = text.find(f" fn {name}(")
if pos == -1:
pos = text.find(f" pub fn {name}(")
if pos == -1:
return None, None, None
# Capture documentation comments immediately above the function
doc_pos = pos
while True:
prev_newline = text.rfind("\n", 0, doc_pos)
if prev_newline == -1: break
line = text[prev_newline+1:doc_pos].strip()
if line.startswith("///") or line == "":
doc_pos = prev_newline
else:
break
start_brace = text.find("{", pos)
depth = 1
i = start_brace + 1
while depth > 0 and i < len(text):
if text[i] == '{': depth += 1
elif text[i] == '}': depth -= 1
i += 1
return doc_pos + 1, i, text[doc_pos+1:i]
# Desired order
funcs = [
"compile",
"compile_node",
"compile_entity",
"compile_object",
"compile_one_of",
"compile_from_clause",
"compile_select_clause",
"compile_where_clause",
"get_merged_properties"
]
blocks = {}
for f in funcs:
s, e, block = find_function(text, f)
if block:
blocks[f] = block.strip()
else:
print(f"Failed to find {f}")
sys.exit(1)
impl_start = text.find("impl<'a> Compiler<'a> {")
header = text[:impl_start] + "impl<'a> Compiler<'a> {\n"
footer = "}\n\n"
new_text = header
for f in funcs:
new_text += " " + blocks[f].replace("\n", "\n ") + "\n\n"
new_text = new_text.rstrip() + "\n}\n"
# Remove extra indents injected back to the root level
new_text = new_text.replace(" \n", "\n").replace("\n }\n", "\n}\n")
with open("src/queryer/compiler.rs", "w") as f:
f.write(new_text)