feat(mcp): finalize Python structural tools with security checks and indentation normalization

This commit is contained in:
2026-05-13 22:03:37 -04:00
parent 46a415c9a0
commit b9e4050175
7 changed files with 203 additions and 26 deletions
+25 -2
View File
@@ -39,12 +39,15 @@ def find_definition_range(source: str, symbol_path: str) -> tuple[int, int] | No
def shift_indentation(content: str, target_depth: int) -> str:
"""
Shifts the indentation of a code block to the target depth using 1-space units.
Shifts and normalizes the indentation of a code block to 1-space units.
Detects the base indentation and scales relative indentation to 1-space.
[C: scripts/py_struct_tools.py:py_add_def]
"""
lines = content.splitlines()
if not lines:
return ""
# 1. Find min indent of non-empty lines
min_indent = sys.maxsize
for line in lines:
if line.strip():
@@ -53,12 +56,32 @@ def shift_indentation(content: str, target_depth: int) -> str:
min_indent = indent
if min_indent == sys.maxsize:
min_indent = 0
# 2. Try to detect indentation unit (width)
indent_unit = 0
for line in lines:
if line.strip():
indent = len(line) - len(line.lstrip())
rel_indent = indent - min_indent
if rel_indent > 0:
if indent_unit == 0:
indent_unit = rel_indent
else:
import math
indent_unit = math.gcd(indent_unit, rel_indent)
if indent_unit == 0:
indent_unit = 1
shifted_lines = []
for line in lines:
if line.strip():
shifted_lines.append(" " * target_depth + line[min_indent:])
indent = len(line) - len(line.lstrip())
rel_level = (indent - min_indent) // indent_unit
shifted_lines.append(" " * (target_depth + rel_level) + line.lstrip())
else:
shifted_lines.append("")
return "\n".join(shifted_lines) + ("\n" if content.endswith("\n") else "")
def py_remove_def(filepath: str, symbol_path: str) -> str: