Files
manual_slop/scripts/force_1space.py
T
ed f6feab9243 fix(gui): Correct indentation bug in _render_mma_dashboard that caused crash
The code after the 'prior session' return block was incorrectly indented
at 1 space, placing it inside the 'if is_viewing_prior_session' block
instead of after it. This caused 'total_cost' and 'perc' to be undefined
when viewing an active session, triggering an IM_ASSERT error.

Fix: Moved 'track_name', 'track_stats', and 'total_cost' to the
correct 2-space indentation (method body level).
2026-05-06 19:41:22 -04:00

66 lines
1.5 KiB
Python

import tokenize
import io
import sys
import os
def force_1space(path):
try:
with open(path, 'rb') as f:
content = f.read()
tokens = list(tokenize.tokenize(io.BytesIO(content).readline))
except Exception:
return
col_to_level = {0: 0}
level = 0
for tok in tokens:
if tok.type == tokenize.INDENT:
level += 1
col_to_level[tok.end[1]] = level
elif tok.type == tokenize.DEDENT:
level -= 1
new_content = []
level = 0
last_line = -1
last_end = (1, 0)
for tok in tokens:
if tok.type == tokenize.ENCODING:
continue
if tok.type == tokenize.ENDMARKER:
break
if tok.type == tokenize.INDENT:
level += 1
continue
if tok.type == tokenize.DEDENT:
level -= 1
continue
if tok.start[0] > last_line:
if tok.type not in (tokenize.NEWLINE, tokenize.NL):
if tok.type == tokenize.COMMENT:
actual_level = col_to_level.get(tok.start[1], level)
else:
actual_level = level
new_content.append(" " * actual_level)
new_content.append(tok.string)
else:
new_content.append(tok.string)
last_line = tok.start[0]
last_end = tok.end
else:
if tok.start[1] > last_end[1]:
new_content.append(" " * (tok.start[1] - last_end[1]))
new_content.append(tok.string)
last_end = tok.end
with open(path, 'w', encoding='utf-8') as f:
f.write("".join(new_content))
if __name__ == "__main__":
for arg in sys.argv[1:]:
if os.path.isfile(arg):
force_1space(arg)