Skip to content

Diff text

diff_text

Text renderer for ast_diff output.

Renders the :func:structural_diff payload as a compact changelog, grouped by file (## {file}) with + added / - removed / ~ changed lines, mirroring the header/glyph conventions of the sibling *_text renderers.

Every change is listed; modified symbols carry their before → after signatures. No change is masked. Compaction is lossless: the file is hoisted into a section header and the per-symbol kind/name is carried by the signature itself (def name(...) / class Name(...)), so the JSON key/quote/escape overhead is the only thing dropped.

render_diff_text(data)

Render a structural diff payload as a compact changelog.

Parameters:

Name Type Description Default
data dict[str, object]

Payload from :func:structural_diff.

required

Returns:

Type Description
str

Changelog text grouped by file: + added, - removed,

str

~ changed (with before → after signatures).

Source code in packages/axm-ast/src/axm_ast/tools/diff_text.py
Python
def render_diff_text(data: dict[str, object]) -> str:
    """Render a structural diff payload as a compact changelog.

    Args:
        data: Payload from :func:`structural_diff`.

    Returns:
        Changelog text grouped by file: ``+`` added, ``-`` removed,
        ``~`` changed (with before → after signatures).
    """
    typed = cast("_DiffData", data)
    added = typed.get("added") or []
    removed = typed.get("removed") or []
    modified = typed.get("modified") or []
    summary = typed.get("summary") or {}
    n_add = summary.get("added", len(added))
    n_rem = summary.get("removed", len(removed))
    n_mod = summary.get("modified", len(modified))

    lines: list[str] = [f"ast_diff | +{n_add} -{n_rem} ~{n_mod}"]
    for file_name, bucket in _group_by_file(added, removed, modified):
        lines.append(f"## {file_name}")
        lines.extend(f"+ {_symbol_sig(s)}" for s in bucket.added)
        lines.extend(f"- {_symbol_sig(s)}" for s in bucket.removed)
        lines.extend(_render_modified(s) for s in bucket.modified)
    return "\n".join(lines)