插入后选中新行并保留滚动位置、回车瞬间恢复原显示、把手±只动受影响行并防抖提速、编辑框文字与格子同位;备注格 .rmk 支持 pre-wrap 换行并改用 textarea(Enter=换行,Ctrl+Enter 保存),工料机备注同款、切回原数据保留视图;新增 test/代码地图生成.py 与 代码地图.md,AGENTS 数值速查收口术语表。
117 lines
4.4 KiB
Python
117 lines
4.4 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""代码地图生成 —— 省 token 铁律7 配套(2026-10-26 主人定)。
|
||
|
||
扫三大文件(common.js / workbench.html / api_server.py)的全部函数,
|
||
生成根目录 `代码地图.md`:文件 → 函数 → 行号 → 一句话说明。
|
||
定位先查地图、一次 sed 命中;增删函数后收尾重跑本脚本刷新:
|
||
python test/代码地图生成.py
|
||
只读源码、只写 代码地图.md,不碰其他任何文件。
|
||
"""
|
||
import io
|
||
import os
|
||
import re
|
||
import sys
|
||
import datetime
|
||
|
||
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||
OUT = os.path.join(ROOT, "代码地图.md")
|
||
FILES = [
|
||
("CostReview/app/ui/common.js", "js", "前台后台共用层(渲染/编辑/树/缓存)"),
|
||
("CostReview/app/ui/workbench.html", "js", "工作台页面(脚本块)"),
|
||
("CostReview/app/server/api_server.py", "py", "后台 API 服务(全部接口)"),
|
||
]
|
||
DESC_MAX = 60
|
||
|
||
RE_JS = re.compile(
|
||
r"^\s*(?:async\s+)?function\s+([A-Za-z_$][\w$]*)\s*\(" # function f( / async function f(
|
||
r"|^\s*([A-Za-z_$][\w$]*)\s*:\s*(?:async\s+)?function\s*\(" # f: function( / f: async function(
|
||
r"|^\s*window\.([A-Za-z_$][\w$]*)\s*=\s*(?:async\s+)?function" # window.f = function
|
||
)
|
||
RE_PY_DEF = re.compile(r"^(\s*)def\s+([A-Za-z_]\w*)\s*\(")
|
||
RE_PY_CLS = re.compile(r"^class\s+([A-Za-z_]\w*)")
|
||
|
||
|
||
def js_desc(lines, i):
|
||
"""向上找最近的注释行(// 或 块注),最多回看 4 行。"""
|
||
for j in range(i - 1, max(-1, i - 5), -1):
|
||
t = lines[j].strip()
|
||
if not t:
|
||
continue
|
||
if t.startswith("//") or t.startswith("/*") or t.startswith("*"):
|
||
return t.lstrip("/* ").rstrip("*/ \t")[:DESC_MAX]
|
||
break
|
||
return ""
|
||
|
||
|
||
def py_desc(lines, i):
|
||
for j in range(i - 1, max(-1, i - 5), -1):
|
||
t = lines[j].strip()
|
||
if not t:
|
||
continue
|
||
if t.startswith("#") and not t.startswith("#!"):
|
||
return t.lstrip("# ")[:DESC_MAX]
|
||
break
|
||
# 没有上行注释就取 docstring 第一句
|
||
m = re.search(r'"""([^"\n]{6,})', lines[i] + "".join(lines[i + 1:i + 3]))
|
||
return m.group(1).strip()[:DESC_MAX] if m else ""
|
||
|
||
|
||
def scan_js(path):
|
||
"""返回 [(行号, 函数名, 说明)];html 只扫 <script> 块内部。"""
|
||
raw = io.open(path, encoding="utf-8", errors="replace").read()
|
||
lines = raw.splitlines()
|
||
in_script = not path.endswith(".html")
|
||
out = []
|
||
for i, ln in enumerate(lines):
|
||
if path.endswith(".html"):
|
||
low = ln.lower()
|
||
if "<script" in low and "src=" not in low:
|
||
in_script = True
|
||
elif "</script" in low:
|
||
in_script = False
|
||
if not in_script:
|
||
continue
|
||
m = RE_JS.match(ln)
|
||
if m:
|
||
name = next(g for g in m.groups() if g)
|
||
out.append((i + 1, name + "()", js_desc(lines, i)))
|
||
return out
|
||
|
||
|
||
def scan_py(path):
|
||
lines = io.open(path, encoding="utf-8", errors="replace").read().splitlines()
|
||
out, cls, cls_indent = [], None, 99
|
||
for i, ln in enumerate(lines):
|
||
c = RE_PY_CLS.match(ln)
|
||
if c:
|
||
cls, cls_indent = c.group(1), len(ln) - len(ln.lstrip())
|
||
continue
|
||
m = RE_PY_DEF.match(ln)
|
||
if m:
|
||
indent, name = len(m.group(1)), m.group(2)
|
||
full = (cls + "." + name + "()") if (cls and indent > cls_indent) else (name + "()")
|
||
out.append((i + 1, full, py_desc(lines, i)))
|
||
return out
|
||
|
||
|
||
def main():
|
||
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
||
buf = [io.StringIO(), ]
|
||
w = buf[0]
|
||
w.write("# 代码地图 —— 三大文件 函数→行号→一句话(自动生成,勿手改)\n\n")
|
||
w.write("> 定位先查本地图、一次 sed 命中(省 token 铁律7)。增删函数后刷新:`python test/代码地图生成.py`\n\n")
|
||
total = 0
|
||
for path, kind, label in FILES:
|
||
fns = (scan_py if kind == "py" else scan_js)(os.path.join(ROOT, path))
|
||
total += len(fns)
|
||
w.write("## %s(%s,%d 个)\n\n| 行 | 函数 | 说明 |\n|---:|---|---|\n" % (path, label, len(fns)))
|
||
for no, name, desc in fns:
|
||
w.write("| %d | `%s` | %s |\n" % (no, name, desc.replace("|", "\\|")))
|
||
w.write("\n")
|
||
io.open(OUT, "w", encoding="utf-8", newline="\n").write(w.getvalue())
|
||
print("代码地图.md 已生成:%d 个函数,%d 字符" % (total, len(w.getvalue())))
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|