scripts/dev/split_module.py (lexer-based, with 16 self-tests) moves the top-level fn and impl items of 59 oversize module files in control-plane, desktop-core, model-format, gateway-text, leleka-memory, experimental-ref, context, serving-policy, tokenizer, backend-api, quant-core, harmony, igpu, libc-shim, npu-service and runtime-client into foo/<name>.rs groups of about 250 lines; types, constants, macros, nested modules and tests stay, every path stays valid through pub use re-exports, restricted visibilities keep their scope one level down, include paths and super:: paths are shifted. Not touched: leleka-runtime, cuda-driver, native-cpu-expert, sampler-ref and cpu-* (hot path, next wave) and the files kar-off-ab edits. The baseline drops from 311 to 263 oversize files and from 273,600 to about 233,000 lines; the path-include ratchet ignores dir/name.rs child declarations.
84 lines
3.1 KiB
Python
84 lines
3.1 KiB
Python
"""Edits a function or impl chunk needs when it moves one module down: visibility and `super::` / `self::` paths."""
|
|
import re
|
|
|
|
from .items import CLOSE, OPEN, _keyword, _skip_attrs, _skip_visibility, significant
|
|
from .lexer import tokenize
|
|
|
|
VIS = "pub(super) "
|
|
INCLUDE = re.compile(r'(include_(?:str|bytes)!\(\s*")(?=[^/])')
|
|
|
|
|
|
def _is_double_colon(sig, k):
|
|
return k >= 1 and sig[k].text == ":" and sig[k - 1].text == ":"
|
|
|
|
|
|
def _restricted_visibility_edits(sig):
|
|
"""`pub(super)` and `pub(self)` name a scope relative to the module; one level down they need `pub(in ...)`."""
|
|
edits = []
|
|
for k, tok in enumerate(sig[:-3]):
|
|
if tok.text == "pub" and sig[k + 1].text == "(" and sig[k + 3].text == ")":
|
|
inner = sig[k + 2]
|
|
replacement = {"super": "in super::super", "self": "in super"}.get(inner.text)
|
|
if replacement:
|
|
edits.append((inner.start, len(inner.text), replacement))
|
|
return edits
|
|
|
|
|
|
def _path_edits(sig):
|
|
"""(position, length, replacement) for every path that starts with `super` or `self` (not after `::`)."""
|
|
edits = []
|
|
for k, tok in enumerate(sig[:-2]):
|
|
follows_path = k >= 2 and _is_double_colon(sig, k - 1)
|
|
if tok.kind != "ident" or follows_path or not _is_double_colon(sig, k + 2):
|
|
continue
|
|
if tok.text == "super":
|
|
edits.append((tok.start, 5, "super::super"))
|
|
elif tok.text == "self":
|
|
edits.append((tok.start, 4, "super"))
|
|
return edits
|
|
|
|
|
|
def _assoc_item_starts(sig):
|
|
"""Indexes in `sig` where an associated item of an impl body (brace depth 1) begins."""
|
|
starts, depth = [], 0
|
|
for k, tok in enumerate(sig):
|
|
depth += (tok.text in OPEN) - (tok.text in CLOSE)
|
|
if k > 0 and sig[k - 1].text in ("{", "}", ";") and depth == 1 and tok.text != "}":
|
|
starts.append(k)
|
|
return starts
|
|
|
|
|
|
def _assoc_visibility_edits(sig):
|
|
"""`pub(super)` before every private fn or const of an inherent impl body."""
|
|
edits = []
|
|
for k in _assoc_item_starts(sig):
|
|
first = _skip_attrs(sig, k)
|
|
if first < len(sig) and sig[first].text != "pub" and _keyword(sig, first)[0] in ("fn", "const"):
|
|
edits.append((sig[first].start, 0, VIS))
|
|
return edits
|
|
|
|
|
|
def _fn_visibility_edits(sig):
|
|
"""`pub(super)` for a free fn that has no visibility."""
|
|
first = _skip_attrs(sig, 0)
|
|
if sig[first].text == "pub" or _keyword(sig, first)[0] != "fn":
|
|
return []
|
|
return [(sig[first].start, 0, VIS)]
|
|
|
|
|
|
def apply_edits(text, edits):
|
|
for pos, length, replacement in sorted(edits, reverse=True):
|
|
text = text[:pos] + replacement + text[pos + length:]
|
|
return text
|
|
|
|
|
|
def retarget(chunk, kind, inherent):
|
|
"""The chunk (one fn or impl, from its leading comments on) as it must read inside a child module."""
|
|
sig = significant(tokenize(chunk))
|
|
edits = _path_edits(sig) + _restricted_visibility_edits(sig)
|
|
if kind == "fn":
|
|
edits += _fn_visibility_edits(sig)
|
|
elif inherent:
|
|
edits += _assoc_visibility_edits(sig)
|
|
return INCLUDE.sub(lambda m: m.group(1) + "../", apply_edits(chunk, edits))
|