shast.py¶
clew/shast.py
The shell dialect: function definitions for the parse-built front end.
harvest_definitions
dict[str, list[dict[str, Any]]] clew.shast.harvest_definitions(Any tree, bytes src_bytes)
Harvest one shell file's function definitions.
Source · 22 lines
def harvest_definitions(tree: Any, src_bytes: bytes) -> dict[str, list[dict[str, Any]]]:
"""Positions are 1-based lines and 0-based columns, as doxygen writes them, and the
payload is rowid-free JSON so it caches per file content like every other stage.
`classes` is present and empty: shell has no class-like construct, and the shape is
shared with the JS/TS payload the same writer consumes.
@brief Harvest shell definitions.
@return The definitions payload.
"""
out: dict[str, list[dict[str, Any]]] = {"functions": [], "classes": []}
if tree.root_node.type != SH_ROOT:
return out
lines = src_bytes.decode("utf-8", errors="replace").splitlines()
for node in walk_nodes(tree.root_node):
name = _definition_name(node)
if name:
out["functions"].append(_function_record(node, name, lines))
out["functions"].sort(key=lambda fn: (fn["line"], fn["name"]))
return out