feat(mma): Implement ASTParser in file_cache.py and refactor mcp_client.py
This commit is contained in:
@@ -242,57 +242,10 @@ def get_python_skeleton(path: str) -> str:
|
||||
return f"ERROR: not a python file: {path}"
|
||||
|
||||
try:
|
||||
# Use mma_exec's generator if possible, or a local simplified version
|
||||
# For now, we will use a dedicated script or just inline logic here.
|
||||
# Given we have tree-sitter already installed in the env...
|
||||
import tree_sitter
|
||||
import tree_sitter_python
|
||||
|
||||
from file_cache import ASTParser
|
||||
code = p.read_text(encoding="utf-8")
|
||||
PY_LANGUAGE = tree_sitter.Language(tree_sitter_python.language())
|
||||
parser = tree_sitter.Parser(PY_LANGUAGE)
|
||||
tree = parser.parse(bytes(code, "utf8"))
|
||||
|
||||
edits = []
|
||||
|
||||
def is_docstring(node):
|
||||
if node.type == "expression_statement" and node.child_count > 0:
|
||||
if node.children[0].type == "string":
|
||||
return True
|
||||
return False
|
||||
|
||||
def walk(node):
|
||||
if node.type == "function_definition":
|
||||
body = node.child_by_field_name("body")
|
||||
if body and body.type == "block":
|
||||
indent = " " * body.start_point.column
|
||||
first_stmt = None
|
||||
for child in body.children:
|
||||
if child.type != "comment":
|
||||
first_stmt = child
|
||||
break
|
||||
|
||||
if first_stmt and is_docstring(first_stmt):
|
||||
start_byte = first_stmt.end_byte
|
||||
end_byte = body.end_byte
|
||||
if end_byte > start_byte:
|
||||
edits.append((start_byte, end_byte, f"\\n{indent}..."))
|
||||
else:
|
||||
start_byte = body.start_byte
|
||||
end_byte = body.end_byte
|
||||
edits.append((start_byte, end_byte, "..."))
|
||||
|
||||
for child in node.children:
|
||||
walk(child)
|
||||
|
||||
walk(tree.root_node)
|
||||
|
||||
edits.sort(key=lambda x: x[0], reverse=True)
|
||||
code_bytes = bytearray(code, "utf8")
|
||||
for start, end, replacement in edits:
|
||||
code_bytes[start:end] = bytes(replacement, "utf8")
|
||||
|
||||
return code_bytes.decode("utf8")
|
||||
parser = ASTParser("python")
|
||||
return parser.get_skeleton(code)
|
||||
except Exception as e:
|
||||
return f"ERROR generating skeleton for '{path}': {e}"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user