@uncarved/jade 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/jade.js +5 -4
- package/dist/api.d.ts +266 -0
- package/dist/api.js +38 -0
- package/dist/api.js.map +1 -0
- package/dist/config.d.ts +13 -32
- package/dist/config.js +39 -121
- package/dist/config.js.map +1 -1
- package/dist/fetch.d.ts +20 -0
- package/dist/fetch.js +190 -0
- package/dist/fetch.js.map +1 -0
- package/dist/hash.d.ts +2 -0
- package/dist/hash.js +42 -0
- package/dist/hash.js.map +1 -0
- package/dist/host.d.ts +71 -0
- package/dist/host.js +400 -0
- package/dist/host.js.map +1 -0
- package/dist/index.d.ts +11 -28
- package/dist/index.js +11 -28
- package/dist/index.js.map +1 -1
- package/dist/install.d.ts +78 -0
- package/dist/install.js +292 -0
- package/dist/install.js.map +1 -0
- package/dist/main.d.ts +1 -2
- package/dist/main.js +156 -1223
- package/dist/main.js.map +1 -1
- package/dist/manifest.d.ts +23 -0
- package/dist/manifest.js +159 -0
- package/dist/manifest.js.map +1 -0
- package/dist/membrane.d.ts +33 -0
- package/dist/membrane.js +140 -0
- package/dist/membrane.js.map +1 -0
- package/dist/remote.d.ts +10 -0
- package/dist/remote.js +292 -0
- package/dist/remote.js.map +1 -0
- package/dist/rescue.d.ts +10 -0
- package/dist/rescue.js +216 -0
- package/dist/rescue.js.map +1 -0
- package/dist/resolve.d.ts +34 -0
- package/dist/resolve.js +178 -0
- package/dist/resolve.js.map +1 -0
- package/dist/rpc.d.ts +26 -0
- package/dist/rpc.js +144 -0
- package/dist/rpc.js.map +1 -0
- package/dist/secrets.d.ts +12 -0
- package/dist/secrets.js +125 -0
- package/dist/secrets.js.map +1 -0
- package/dist/session.d.ts +45 -0
- package/dist/session.js +401 -0
- package/dist/session.js.map +1 -0
- package/dist/source.d.ts +12 -0
- package/dist/source.js +72 -0
- package/dist/source.js.map +1 -0
- package/dist/state.d.ts +37 -0
- package/dist/state.js +93 -0
- package/dist/state.js.map +1 -0
- package/dist/update.d.ts +55 -0
- package/dist/update.js +258 -0
- package/dist/update.js.map +1 -0
- package/package.json +12 -9
- package/plugins/approval/index.ts +297 -0
- package/plugins/approval/plugin.json +18 -0
- package/{src/plugins → plugins/approval}/policy.ts +21 -84
- package/plugins/approval/review.ts +192 -0
- package/plugins/ask/index.ts +159 -0
- package/plugins/ask/plugin.json +8 -0
- package/plugins/budget/index.ts +38 -0
- package/plugins/budget/plugin.json +13 -0
- package/{src/plugins → plugins/checks}/checks.ts +68 -39
- package/plugins/checks/index.ts +158 -0
- package/plugins/checks/plugin.json +14 -0
- package/plugins/checks/project.ts +81 -0
- package/plugins/commands/index.ts +76 -0
- package/plugins/commands/plugin.json +7 -0
- package/{src/plugins → plugins/compact}/compact.ts +112 -38
- package/plugins/compact/index.ts +115 -0
- package/plugins/compact/plugin.json +17 -0
- package/plugins/compact/style.ts +17 -0
- package/plugins/continue/index.ts +58 -0
- package/plugins/continue/plugin.json +13 -0
- package/{src/ui → plugins/fs}/diff.ts +3 -4
- package/{src/tools → plugins/fs}/fs.ts +64 -46
- package/{src/tools → plugins/fs}/grep.ts +4 -3
- package/plugins/fs/index.ts +34 -0
- package/plugins/fs/plugin.json +7 -0
- package/plugins/fs/renderers.ts +138 -0
- package/plugins/fs/style.ts +18 -0
- package/plugins/fs/sys.ts +81 -0
- package/plugins/keys/index.ts +53 -0
- package/plugins/keys/plugin.json +7 -0
- package/plugins/mcp/index.ts +192 -0
- package/{src → plugins/mcp}/mcp.ts +32 -32
- package/plugins/mcp/plugin.json +7 -0
- package/plugins/mcp/project.ts +93 -0
- package/plugins/memory/index.ts +105 -0
- package/{src → plugins/memory}/memory.ts +20 -2
- package/plugins/memory/notes.ts +43 -0
- package/plugins/memory/plugin.json +7 -0
- package/plugins/memory/style.ts +17 -0
- package/plugins/mentions/attach.ts +127 -0
- package/plugins/mentions/index.ts +79 -0
- package/plugins/mentions/plugin.json +7 -0
- package/plugins/mentions/style.ts +17 -0
- package/plugins/models/config.ts +53 -0
- package/plugins/models/configure.ts +455 -0
- package/plugins/models/context.ts +32 -0
- package/plugins/models/index.ts +198 -0
- package/plugins/models/plugin.json +8 -0
- package/{src → plugins/models}/provider.ts +23 -27
- package/plugins/models/saved.ts +94 -0
- package/plugins/modes/index.ts +222 -0
- package/plugins/modes/plugin.json +9 -0
- package/plugins/plugins/index.ts +38 -0
- package/plugins/plugins/plugin.json +7 -0
- package/plugins/print/index.ts +142 -0
- package/plugins/print/plugin.json +8 -0
- package/plugins/prompts/index.ts +83 -0
- package/plugins/prompts/plugin.json +8 -0
- package/plugins/prompts/prompts.ts +253 -0
- package/plugins/prompts/sys.ts +72 -0
- package/plugins/renderers/index.ts +78 -0
- package/plugins/renderers/plugin.json +7 -0
- package/plugins/repeats/index.ts +165 -0
- package/plugins/repeats/plugin.json +6 -0
- package/plugins/schedule/cron.ts +74 -0
- package/plugins/schedule/index.ts +292 -0
- package/plugins/schedule/plugin.json +13 -0
- package/plugins/schedule/store.ts +149 -0
- package/plugins/scratch/index.ts +31 -0
- package/plugins/scratch/plugin.json +7 -0
- package/plugins/screenshot/index.ts +40 -0
- package/plugins/screenshot/plugin.json +7 -0
- package/plugins/screenshot/sys.ts +84 -0
- package/{src/tools/screenshot.ts → plugins/screenshot/tool.ts} +6 -6
- package/plugins/sessions/index.ts +148 -0
- package/plugins/sessions/plugin.json +8 -0
- package/plugins/shell/index.ts +33 -0
- package/plugins/shell/plugin.json +17 -0
- package/plugins/shell/renderers.ts +45 -0
- package/plugins/shell/service.ts +44 -0
- package/{src/tools → plugins/shell}/shell.ts +36 -21
- package/{src → plugins/shell}/sys.ts +19 -43
- package/plugins/skills/index.ts +96 -0
- package/plugins/skills/plugin.json +7 -0
- package/plugins/skills/project.ts +49 -0
- package/{src → plugins/skills}/skills.ts +4 -11
- package/plugins/suggest/index.ts +286 -0
- package/plugins/suggest/plugin.json +18 -0
- package/plugins/task/index.ts +167 -0
- package/plugins/task/plugin.json +15 -0
- package/plugins/task/style.ts +17 -0
- package/plugins/task/subagent.ts +257 -0
- package/{src/tools/todo.ts → plugins/todo/index.ts} +33 -7
- package/plugins/todo/plugin.json +7 -0
- package/{src/ui → plugins/tui}/editor.ts +53 -1
- package/plugins/tui/frontend.ts +596 -0
- package/plugins/tui/index.ts +37 -0
- package/{src/ui → plugins/tui}/markdown.ts +258 -219
- package/plugins/tui/mentions.ts +113 -0
- package/plugins/tui/plugin.json +10 -0
- package/plugins/tui/render.ts +67 -0
- package/plugins/tui/screen.ts +268 -0
- package/{src/ui → plugins/tui}/transcript.ts +179 -111
- package/{src/ui/tui.tsx → plugins/tui/tui.ts} +684 -418
- package/{src/tools → plugins/undo}/checkpoint.ts +21 -60
- package/plugins/undo/index.ts +139 -0
- package/plugins/undo/notes.ts +43 -0
- package/plugins/undo/plugin.json +7 -0
- package/plugins/undo/style.ts +17 -0
- package/plugins/update/index.ts +40 -0
- package/plugins/update/plugin.json +13 -0
- package/plugins/web/env.ts +78 -0
- package/plugins/web/index.ts +133 -0
- package/plugins/web/plugin.json +15 -0
- package/plugins/web/renderers.ts +11 -0
- package/plugins/web/web.ts +713 -0
- package/plugins-dist/approval/index.js +271 -0
- package/plugins-dist/approval/plugin.json +24 -0
- package/{dist/plugins → plugins-dist/approval}/policy.js +15 -65
- package/plugins-dist/approval/review.js +176 -0
- package/{dist/plugins → plugins-dist/approval}/schema.js +0 -1
- package/plugins-dist/ask/index.js +145 -0
- package/plugins-dist/ask/plugin.json +12 -0
- package/plugins-dist/budget/index.js +37 -0
- package/plugins-dist/budget/plugin.json +15 -0
- package/{dist/plugins → plugins-dist/checks}/checks.js +65 -34
- package/plugins-dist/checks/index.js +150 -0
- package/plugins-dist/checks/plugin.json +21 -0
- package/plugins-dist/checks/project.js +76 -0
- package/plugins-dist/commands/index.js +33 -0
- package/plugins-dist/commands/plugin.json +9 -0
- package/{dist/plugins → plugins-dist/compact}/compact.js +95 -33
- package/plugins-dist/compact/index.js +100 -0
- package/plugins-dist/compact/plugin.json +25 -0
- package/plugins-dist/compact/style.js +14 -0
- package/plugins-dist/continue/index.js +57 -0
- package/plugins-dist/continue/plugin.json +15 -0
- package/{dist/ui → plugins-dist/fs}/diff.js +3 -5
- package/{dist/tools → plugins-dist/fs}/fs.js +62 -46
- package/{dist/tools → plugins-dist/fs}/grep.js +4 -4
- package/plugins-dist/fs/index.js +36 -0
- package/plugins-dist/fs/plugin.json +10 -0
- package/plugins-dist/fs/renderers.js +132 -0
- package/plugins-dist/fs/style.js +15 -0
- package/plugins-dist/fs/sys.js +54 -0
- package/plugins-dist/keys/index.js +36 -0
- package/plugins-dist/keys/plugin.json +9 -0
- package/plugins-dist/mcp/index.js +187 -0
- package/{dist → plugins-dist/mcp}/mcp.js +34 -30
- package/plugins-dist/mcp/plugin.json +12 -0
- package/plugins-dist/mcp/project.js +90 -0
- package/plugins-dist/memory/index.js +94 -0
- package/{dist → plugins-dist/memory}/memory.js +21 -3
- package/plugins-dist/memory/notes.js +36 -0
- package/plugins-dist/memory/plugin.json +11 -0
- package/plugins-dist/memory/style.js +14 -0
- package/plugins-dist/mentions/attach.js +120 -0
- package/plugins-dist/mentions/index.js +69 -0
- package/plugins-dist/mentions/plugin.json +9 -0
- package/plugins-dist/mentions/style.js +14 -0
- package/plugins-dist/models/config.js +53 -0
- package/plugins-dist/models/configure.js +415 -0
- package/plugins-dist/models/context.js +27 -0
- package/plugins-dist/models/index.js +167 -0
- package/plugins-dist/models/plugin.json +14 -0
- package/{dist → plugins-dist/models}/provider.js +22 -19
- package/plugins-dist/models/saved.js +80 -0
- package/plugins-dist/modes/index.js +190 -0
- package/plugins-dist/modes/plugin.json +18 -0
- package/plugins-dist/plugins/index.js +37 -0
- package/plugins-dist/plugins/plugin.json +10 -0
- package/plugins-dist/print/index.js +153 -0
- package/plugins-dist/print/plugin.json +14 -0
- package/plugins-dist/prompts/index.js +57 -0
- package/plugins-dist/prompts/plugin.json +13 -0
- package/plugins-dist/prompts/prompts.js +227 -0
- package/plugins-dist/prompts/sys.js +46 -0
- package/plugins-dist/renderers/index.js +36 -0
- package/plugins-dist/renderers/plugin.json +9 -0
- package/plugins-dist/repeats/index.js +165 -0
- package/plugins-dist/repeats/plugin.json +6 -0
- package/plugins-dist/schedule/cron.js +67 -0
- package/plugins-dist/schedule/index.js +281 -0
- package/plugins-dist/schedule/plugin.json +16 -0
- package/plugins-dist/schedule/store.js +125 -0
- package/plugins-dist/scratch/index.js +26 -0
- package/plugins-dist/scratch/plugin.json +10 -0
- package/plugins-dist/screenshot/index.js +35 -0
- package/plugins-dist/screenshot/plugin.json +11 -0
- package/plugins-dist/screenshot/sys.js +76 -0
- package/{dist/tools/screenshot.js → plugins-dist/screenshot/tool.js} +5 -4
- package/plugins-dist/sessions/index.js +136 -0
- package/plugins-dist/sessions/plugin.json +12 -0
- package/plugins-dist/shell/index.js +33 -0
- package/plugins-dist/shell/plugin.json +19 -0
- package/plugins-dist/shell/renderers.js +40 -0
- package/plugins-dist/shell/service.js +29 -0
- package/{dist/tools → plugins-dist/shell}/shell.js +36 -23
- package/{dist → plugins-dist/shell}/sys.js +20 -44
- package/plugins-dist/skills/index.js +90 -0
- package/plugins-dist/skills/plugin.json +12 -0
- package/plugins-dist/skills/project.js +47 -0
- package/{dist → plugins-dist/skills}/skills.js +3 -7
- package/plugins-dist/suggest/index.js +282 -0
- package/plugins-dist/suggest/plugin.json +23 -0
- package/plugins-dist/task/index.js +151 -0
- package/plugins-dist/task/plugin.json +23 -0
- package/plugins-dist/task/style.js +14 -0
- package/plugins-dist/task/subagent.js +208 -0
- package/{dist/tools/todo.js → plugins-dist/todo/index.js} +29 -7
- package/plugins-dist/todo/plugin.json +9 -0
- package/{dist/ui → plugins-dist/tui}/editor.js +53 -2
- package/plugins-dist/tui/frontend.js +586 -0
- package/{dist → plugins-dist/tui}/history.js +0 -1
- package/plugins-dist/tui/index.js +21 -0
- package/{dist/ui → plugins-dist/tui}/markdown.js +57 -20
- package/plugins-dist/tui/mentions.js +115 -0
- package/plugins-dist/tui/plugin.json +25 -0
- package/plugins-dist/tui/render.js +61 -0
- package/plugins-dist/tui/screen.js +201 -0
- package/{dist/ui → plugins-dist/tui}/transcript.js +179 -111
- package/{dist/ui → plugins-dist/tui}/tui.js +484 -101
- package/{dist/tools → plugins-dist/undo}/checkpoint.js +21 -55
- package/plugins-dist/undo/index.js +140 -0
- package/plugins-dist/undo/notes.js +36 -0
- package/plugins-dist/undo/plugin.json +9 -0
- package/plugins-dist/undo/style.js +14 -0
- package/plugins-dist/update/index.js +34 -0
- package/plugins-dist/update/plugin.json +15 -0
- package/plugins-dist/web/env.js +80 -0
- package/plugins-dist/web/index.js +124 -0
- package/plugins-dist/web/plugin.json +21 -0
- package/plugins-dist/web/renderers.js +7 -0
- package/plugins-dist/web/web.js +660 -0
- package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
- package/sdk/python/jade_plugin.py +380 -0
- package/src/api.ts +298 -0
- package/src/config.ts +32 -134
- package/src/fetch.ts +216 -0
- package/src/hash.ts +39 -0
- package/src/host.ts +429 -0
- package/src/index.ts +23 -28
- package/src/install.ts +355 -0
- package/src/main.ts +152 -1263
- package/src/manifest.ts +160 -0
- package/src/membrane.ts +142 -0
- package/src/remote.ts +288 -0
- package/src/rescue.ts +217 -0
- package/src/resolve.ts +199 -0
- package/src/rpc.ts +144 -0
- package/src/secrets.ts +122 -0
- package/src/session.ts +458 -0
- package/src/source.ts +83 -0
- package/src/state.ts +120 -0
- package/src/update.ts +265 -0
- package/LICENSE +0 -21
- package/README.md +0 -198
- package/dist/commands.d.ts +0 -30
- package/dist/commands.js +0 -106
- package/dist/commands.js.map +0 -1
- package/dist/context.d.ts +0 -17
- package/dist/context.js +0 -53
- package/dist/context.js.map +0 -1
- package/dist/distro.d.ts +0 -104
- package/dist/distro.js +0 -312
- package/dist/distro.js.map +0 -1
- package/dist/history.d.ts +0 -10
- package/dist/history.js.map +0 -1
- package/dist/mcp.d.ts +0 -114
- package/dist/mcp.js.map +0 -1
- package/dist/memory.d.ts +0 -19
- package/dist/memory.js.map +0 -1
- package/dist/mentions.d.ts +0 -35
- package/dist/mentions.js +0 -130
- package/dist/mentions.js.map +0 -1
- package/dist/plugin-cli.d.ts +0 -16
- package/dist/plugin-cli.js +0 -251
- package/dist/plugin-cli.js.map +0 -1
- package/dist/plugins/budget.d.ts +0 -9
- package/dist/plugins/budget.js +0 -30
- package/dist/plugins/budget.js.map +0 -1
- package/dist/plugins/builtins.d.ts +0 -10
- package/dist/plugins/builtins.js +0 -22
- package/dist/plugins/builtins.js.map +0 -1
- package/dist/plugins/checks.d.ts +0 -70
- package/dist/plugins/checks.js.map +0 -1
- package/dist/plugins/compact.d.ts +0 -26
- package/dist/plugins/compact.js.map +0 -1
- package/dist/plugins/core.d.ts +0 -42
- package/dist/plugins/core.js +0 -190
- package/dist/plugins/core.js.map +0 -1
- package/dist/plugins/install.d.ts +0 -68
- package/dist/plugins/install.js +0 -377
- package/dist/plugins/install.js.map +0 -1
- package/dist/plugins/loader.d.ts +0 -182
- package/dist/plugins/loader.js +0 -253
- package/dist/plugins/loader.js.map +0 -1
- package/dist/plugins/policy.d.ts +0 -96
- package/dist/plugins/policy.js.map +0 -1
- package/dist/plugins/prompts.d.ts +0 -38
- package/dist/plugins/prompts.js +0 -196
- package/dist/plugins/prompts.js.map +0 -1
- package/dist/plugins/repeats.d.ts +0 -9
- package/dist/plugins/repeats.js +0 -90
- package/dist/plugins/repeats.js.map +0 -1
- package/dist/plugins/review.d.ts +0 -16
- package/dist/plugins/review.js +0 -92
- package/dist/plugins/review.js.map +0 -1
- package/dist/plugins/schema.d.ts +0 -2
- package/dist/plugins/schema.js.map +0 -1
- package/dist/provider.d.ts +0 -111
- package/dist/provider.js.map +0 -1
- package/dist/sessions.d.ts +0 -22
- package/dist/sessions.js +0 -53
- package/dist/sessions.js.map +0 -1
- package/dist/skills.d.ts +0 -37
- package/dist/skills.js.map +0 -1
- package/dist/sys.d.ts +0 -48
- package/dist/sys.js.map +0 -1
- package/dist/tools/checkpoint.d.ts +0 -43
- package/dist/tools/checkpoint.js.map +0 -1
- package/dist/tools/fs.d.ts +0 -35
- package/dist/tools/fs.js.map +0 -1
- package/dist/tools/grep.d.ts +0 -6
- package/dist/tools/grep.js.map +0 -1
- package/dist/tools/screenshot.d.ts +0 -15
- package/dist/tools/screenshot.js.map +0 -1
- package/dist/tools/shell.d.ts +0 -55
- package/dist/tools/shell.js.map +0 -1
- package/dist/tools/task.d.ts +0 -35
- package/dist/tools/task.js +0 -89
- package/dist/tools/task.js.map +0 -1
- package/dist/tools/todo.d.ts +0 -10
- package/dist/tools/todo.js.map +0 -1
- package/dist/tools/web.d.ts +0 -23
- package/dist/tools/web.js +0 -426
- package/dist/tools/web.js.map +0 -1
- package/dist/ui/configure.d.ts +0 -20
- package/dist/ui/configure.js +0 -378
- package/dist/ui/configure.js.map +0 -1
- package/dist/ui/diff.d.ts +0 -20
- package/dist/ui/diff.js.map +0 -1
- package/dist/ui/editor.d.ts +0 -33
- package/dist/ui/editor.js.map +0 -1
- package/dist/ui/markdown.d.ts +0 -17
- package/dist/ui/markdown.js.map +0 -1
- package/dist/ui/render.d.ts +0 -15
- package/dist/ui/render.js +0 -94
- package/dist/ui/render.js.map +0 -1
- package/dist/ui/screen.d.ts +0 -98
- package/dist/ui/screen.js +0 -108
- package/dist/ui/screen.js.map +0 -1
- package/dist/ui/transcript.d.ts +0 -33
- package/dist/ui/transcript.js.map +0 -1
- package/dist/ui/tui.d.ts +0 -45
- package/dist/ui/tui.js.map +0 -1
- package/src/commands.ts +0 -120
- package/src/context.ts +0 -64
- package/src/distro.ts +0 -400
- package/src/mentions.ts +0 -135
- package/src/plugin-cli.ts +0 -239
- package/src/plugins/budget.ts +0 -37
- package/src/plugins/builtins.ts +0 -31
- package/src/plugins/core.ts +0 -217
- package/src/plugins/install.ts +0 -429
- package/src/plugins/loader.ts +0 -386
- package/src/plugins/prompts.ts +0 -234
- package/src/plugins/repeats.ts +0 -96
- package/src/plugins/review.ts +0 -109
- package/src/sessions.ts +0 -61
- package/src/tools/task.ts +0 -109
- package/src/tools/web.ts +0 -451
- package/src/ui/configure.ts +0 -408
- package/src/ui/render.ts +0 -83
- package/src/ui/screen.ts +0 -169
- /package/{src/plugins → plugins/approval}/schema.ts +0 -0
- /package/{src → plugins/tui}/history.ts +0 -0
|
@@ -1,219 +1,258 @@
|
|
|
1
|
-
// 终端里显示模型回复的 Markdown:标题、粗体、斜体、行内代码、链接、列表、引用、分隔线、代码块和表格。
|
|
2
|
-
// 回复是流式到达的,按完整的行逐行处理:代码块要记住是否在块里,表格要攒齐所有行才能算列宽,
|
|
3
|
-
// 所以做成有状态的 markdownStream。表格和分隔线跟终端宽度有关,交出去的是按宽度现排的函数。
|
|
4
|
-
|
|
5
|
-
import stringWidth from "string-width";
|
|
6
|
-
import wrapAnsi from "wrap-ansi";
|
|
7
|
-
import { bold, cyan, dim, italic, strike, underline } from "./screen.ts";
|
|
8
|
-
|
|
9
|
-
/** 排好版的一行,或者按显示宽度现排的若干行(表格、分隔线) */
|
|
10
|
-
export type MdLine = string | { render(width: number): string[] };
|
|
11
|
-
|
|
12
|
-
export interface MarkdownStream {
|
|
13
|
-
/** 送进一个完整的行,返回可以显示的行;表格的行先攒着,等表格结束 */
|
|
14
|
-
push(line: string): MdLine[];
|
|
15
|
-
/** 回复结束:交出攒着的内容,恢复初始状态 */
|
|
16
|
-
flush(): MdLine[];
|
|
17
|
-
/** 攒着还没交出的内容,流式输出时先显示在底部 */
|
|
18
|
-
pending(): MdLine[];
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
type Align = "left" | "center" | "right";
|
|
22
|
-
|
|
23
|
-
const FENCE = /^\s*(`{3,}|~{3,})\s*([^`\s]*)/;
|
|
24
|
-
const RULE = /^\s{0,3}([-*_])(\s*\1){2,}\s*$/;
|
|
25
|
-
const DELIMITER = /^\s*\|?\s*:?-+:?\s*(\|\s*:?-+:?\s*)*\|?\s*$/;
|
|
26
|
-
|
|
27
|
-
export function markdownStream(): MarkdownStream {
|
|
28
|
-
/** 所在代码块的开头标记(``` 或 ~~~),不在代码块里时为 undefined */
|
|
29
|
-
let fence: string | undefined;
|
|
30
|
-
/** 带 | 的一行:下一行是分隔行时它是表头,否则是普通的一行 */
|
|
31
|
-
let candidate: string | undefined;
|
|
32
|
-
let table: { align: Align[]; rows: string[][] } | undefined;
|
|
33
|
-
|
|
34
|
-
const endTable = (): MdLine[] => {
|
|
35
|
-
const out: MdLine[] = [];
|
|
36
|
-
if (table) out.push(tableLine(table.rows, table.align));
|
|
37
|
-
else if (candidate !== undefined) out.push(renderLine(candidate));
|
|
38
|
-
table = undefined;
|
|
39
|
-
candidate = undefined;
|
|
40
|
-
return out;
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
function push(line: string): MdLine[] {
|
|
44
|
-
if (fence) {
|
|
45
|
-
const m = line.match(FENCE);
|
|
46
|
-
if (m && m[1]![0] === fence[0] && m[1]!.length >= fence.length && !m[2]) {
|
|
47
|
-
fence = undefined;
|
|
48
|
-
return [];
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
.
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
const
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
const
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
const
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
const
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
1
|
+
// 终端里显示模型回复的 Markdown:标题、粗体、斜体、行内代码、链接、列表、引用、分隔线、代码块和表格。
|
|
2
|
+
// 回复是流式到达的,按完整的行逐行处理:代码块要记住是否在块里,表格要攒齐所有行才能算列宽,
|
|
3
|
+
// 所以做成有状态的 markdownStream。表格和分隔线跟终端宽度有关,交出去的是按宽度现排的函数。
|
|
4
|
+
|
|
5
|
+
import stringWidth from "string-width";
|
|
6
|
+
import wrapAnsi from "wrap-ansi";
|
|
7
|
+
import { bold, cyan, dim, italic, strike, underline } from "./screen.ts";
|
|
8
|
+
|
|
9
|
+
/** 排好版的一行,或者按显示宽度现排的若干行(表格、分隔线) */
|
|
10
|
+
export type MdLine = string | { render(width: number): string[] };
|
|
11
|
+
|
|
12
|
+
export interface MarkdownStream {
|
|
13
|
+
/** 送进一个完整的行,返回可以显示的行;表格的行先攒着,等表格结束 */
|
|
14
|
+
push(line: string): MdLine[];
|
|
15
|
+
/** 回复结束:交出攒着的内容,恢复初始状态 */
|
|
16
|
+
flush(): MdLine[];
|
|
17
|
+
/** 攒着还没交出的内容,流式输出时先显示在底部 */
|
|
18
|
+
pending(): MdLine[];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
type Align = "left" | "center" | "right";
|
|
22
|
+
|
|
23
|
+
const FENCE = /^\s*(`{3,}|~{3,})\s*([^`\s]*)/;
|
|
24
|
+
const RULE = /^\s{0,3}([-*_])(\s*\1){2,}\s*$/;
|
|
25
|
+
const DELIMITER = /^\s*\|?\s*:?-+:?\s*(\|\s*:?-+:?\s*)*\|?\s*$/;
|
|
26
|
+
|
|
27
|
+
export function markdownStream(): MarkdownStream {
|
|
28
|
+
/** 所在代码块的开头标记(``` 或 ~~~),不在代码块里时为 undefined */
|
|
29
|
+
let fence: string | undefined;
|
|
30
|
+
/** 带 | 的一行:下一行是分隔行时它是表头,否则是普通的一行 */
|
|
31
|
+
let candidate: string | undefined;
|
|
32
|
+
let table: { align: Align[]; rows: string[][] } | undefined;
|
|
33
|
+
|
|
34
|
+
const endTable = (): MdLine[] => {
|
|
35
|
+
const out: MdLine[] = [];
|
|
36
|
+
if (table) out.push(tableLine(table.rows, table.align));
|
|
37
|
+
else if (candidate !== undefined) out.push(renderLine(candidate));
|
|
38
|
+
table = undefined;
|
|
39
|
+
candidate = undefined;
|
|
40
|
+
return out;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
function push(line: string): MdLine[] {
|
|
44
|
+
if (fence) {
|
|
45
|
+
const m = line.match(FENCE);
|
|
46
|
+
if (m && m[1]![0] === fence[0] && m[1]!.length >= fence.length && !m[2]) {
|
|
47
|
+
fence = undefined;
|
|
48
|
+
return [];
|
|
49
|
+
}
|
|
50
|
+
// 左边一条竖线和正文分开;长行折行后续行也带竖线
|
|
51
|
+
return [hanging(dim("┃ "), line, dim("┃ "))];
|
|
52
|
+
}
|
|
53
|
+
if (table) {
|
|
54
|
+
if (line.includes("|") && line.trim()) {
|
|
55
|
+
table.rows.push(cells(line));
|
|
56
|
+
return [];
|
|
57
|
+
}
|
|
58
|
+
return [...endTable(), ...push(line)];
|
|
59
|
+
}
|
|
60
|
+
if (candidate !== undefined) {
|
|
61
|
+
if (DELIMITER.test(line) && line.includes("-")) {
|
|
62
|
+
const header = cells(candidate);
|
|
63
|
+
const align = cells(line).map((c): Align => (c.startsWith(":") && c.endsWith(":") ? "center" : c.endsWith(":") ? "right" : "left"));
|
|
64
|
+
while (align.length < header.length) align.push("left");
|
|
65
|
+
table = { align, rows: [header] };
|
|
66
|
+
candidate = undefined;
|
|
67
|
+
return [];
|
|
68
|
+
}
|
|
69
|
+
return [...endTable(), ...push(line)];
|
|
70
|
+
}
|
|
71
|
+
const m = line.match(FENCE);
|
|
72
|
+
if (m) {
|
|
73
|
+
fence = m[1]!;
|
|
74
|
+
return m[2] ? [dim(`┃ ${m[2]}`)] : [];
|
|
75
|
+
}
|
|
76
|
+
if (splitCells(line).length > 1) {
|
|
77
|
+
candidate = line;
|
|
78
|
+
return [];
|
|
79
|
+
}
|
|
80
|
+
return [renderLine(line)];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return {
|
|
84
|
+
push,
|
|
85
|
+
flush() {
|
|
86
|
+
const out = endTable();
|
|
87
|
+
fence = undefined;
|
|
88
|
+
return out;
|
|
89
|
+
},
|
|
90
|
+
pending: () => (table ? [tableLine(table.rows, table.align)] : candidate !== undefined ? [renderLine(candidate)] : []),
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** 整段 Markdown 排好版(测试和一次性显示用) */
|
|
95
|
+
export function renderMarkdown(text: string, width: number): string[] {
|
|
96
|
+
const md = markdownStream();
|
|
97
|
+
const out = [...text.split("\n").flatMap((l) => md.push(l)), ...md.flush()];
|
|
98
|
+
return out.flatMap((l) => (typeof l === "string" ? [l] : l.render(width)));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** 代码块和表格以外的一行 */
|
|
102
|
+
function renderLine(line: string): MdLine {
|
|
103
|
+
if (RULE.test(line)) return { render: (width) => [dim("─".repeat(Math.max(1, Math.min(width, 80))))] };
|
|
104
|
+
const heading = line.match(/^\s{0,3}(#{1,6})\s+(.*?)(\s+#+)?\s*$/);
|
|
105
|
+
if (heading) {
|
|
106
|
+
const text = inline(heading[2]!);
|
|
107
|
+
return heading[1]!.length === 1 ? bold(underline(text)) : bold(text);
|
|
108
|
+
}
|
|
109
|
+
const quote = line.match(/^\s*>\s?(.*)$/);
|
|
110
|
+
if (quote) return hanging(dim("│ "), inline(quote[1]!), dim("│ "));
|
|
111
|
+
const item = line.match(/^(\s*)([-*+])\s+(\[[ xX]\]\s+)?(.*)$/);
|
|
112
|
+
// 列表项折行时续行和正文对齐
|
|
113
|
+
if (item) {
|
|
114
|
+
const box = item[3] ? (item[3][1] === " " ? "☐ " : "☑ ") : "";
|
|
115
|
+
return hanging(`${item[1]}${box ? "" : cyan("• ")}${box}`, inline(item[4]!));
|
|
116
|
+
}
|
|
117
|
+
const ordered = line.match(/^(\s*)(\d+[.)])\s+(.*)$/);
|
|
118
|
+
if (ordered) return hanging(`${ordered[1]}${cyan(ordered[2]!)} `, inline(ordered[3]!));
|
|
119
|
+
return inline(line);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* 行内格式:`代码`、**粗体**、*斜体*、~~删除线~~、[链接](url)、反斜杠转义。代码里的内容原样显示。
|
|
124
|
+
* 行内代码先换成占位符,粗体、斜体处理完再换回来,"**用 `foo()` 实现**" 这样跨过代码的粗体才认得出
|
|
125
|
+
*/
|
|
126
|
+
export function inline(text: string): string {
|
|
127
|
+
const codes: string[] = [];
|
|
128
|
+
return text
|
|
129
|
+
.replace(/`+[^`]*?`+/g, (code) => `\x01${codes.push(cyan(code.replace(/^`+ ?|`+$/g, "").replace(/ $/, ""))) - 1}\x01`)
|
|
130
|
+
.replace(/\\([\\`*_{}[\]()#+\-.!|~<>])/g, (_, c: string) => `\0${c.charCodeAt(0)}\0`)
|
|
131
|
+
.replace(/\[([^\]]+)\]\(([^)\s]+)(?:\s+"[^"]*")?\)/g, (_, label: string, url: string) =>
|
|
132
|
+
label === url ? underline(url) : `${underline(label)}${dim(` (${url})`)}`,
|
|
133
|
+
)
|
|
134
|
+
.replace(/<(https?:\/\/[^>\s]+)>/g, (_, url: string) => underline(url))
|
|
135
|
+
.replace(/\*\*(?=\S)(.+?)(?<=\S)\*\*|(?<![\w])__(?=\S)(.+?)(?<=\S)__(?![\w])/g, (_, a?: string, b?: string) => bold(a ?? b!))
|
|
136
|
+
.replace(/(?<![*\w])\*(?=[^\s*])(.+?)(?<=[^\s*])\*(?![*\w])|(?<![_\w])_(?=[^\s_])(.+?)(?<=[^\s_])_(?![_\w])/g, (_, a?: string, b?: string) => italic(a ?? b!))
|
|
137
|
+
.replace(/~~(?=\S)(.+?)(?<=\S)~~/g, (_, s: string) => strike(s))
|
|
138
|
+
.replace(/\0(\d+)\0/g, (_, c: string) => String.fromCharCode(Number(c)))
|
|
139
|
+
.replace(/\x01(\d+)\x01/g, (_, i: string) => codes[Number(i)]!);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** 制表符换成空格,按 stop 列一个制表位对齐(按显示宽度算,忽略颜色)。终端按 8 列显示 tab,按 0 列算宽度会对不齐、超宽 */
|
|
143
|
+
export function expandTabs(text: string, stop = 4): string {
|
|
144
|
+
if (!text.includes("\t")) return text;
|
|
145
|
+
return text
|
|
146
|
+
.split("\n")
|
|
147
|
+
.map((line) => {
|
|
148
|
+
if (!line.includes("\t")) return line;
|
|
149
|
+
let out = "";
|
|
150
|
+
let col = 0;
|
|
151
|
+
const parts = line.split("\t");
|
|
152
|
+
parts.forEach((p, i) => {
|
|
153
|
+
out += p;
|
|
154
|
+
col += stringWidth(p);
|
|
155
|
+
if (i < parts.length - 1) {
|
|
156
|
+
const n = stop - (col % stop);
|
|
157
|
+
out += " ".repeat(n);
|
|
158
|
+
col += n;
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
return out;
|
|
162
|
+
})
|
|
163
|
+
.join("\n");
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** 折行时续行缩进到 head 之后(列表项的正文、代码块的竖线);rest 是续行开头显示的内容,宽度和 head 相同 */
|
|
167
|
+
function hanging(head: string, body: string, rest = " ".repeat(stringWidth(head))): MdLine {
|
|
168
|
+
return {
|
|
169
|
+
render: (width) => {
|
|
170
|
+
const room = Math.max(1, width - stringWidth(head));
|
|
171
|
+
return wrapAnsi(expandTabs(body), room, { hard: true, trim: false })
|
|
172
|
+
.split("\n")
|
|
173
|
+
// 折行处留下的行尾空格看不见,去掉免得多占一列
|
|
174
|
+
.map((r, i) => (i === 0 ? head : rest) + r.trimEnd());
|
|
175
|
+
},
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** 按没转义、不在行内代码里的 | 拆开一行 */
|
|
180
|
+
function splitCells(line: string): string[] {
|
|
181
|
+
const out: string[] = [];
|
|
182
|
+
let cell = "";
|
|
183
|
+
let code = false;
|
|
184
|
+
for (let i = 0; i < line.length; i++) {
|
|
185
|
+
const c = line[i]!;
|
|
186
|
+
if (c === "\\" && line[i + 1] === "|") {
|
|
187
|
+
cell += "|";
|
|
188
|
+
i++;
|
|
189
|
+
} else if (c === "`") {
|
|
190
|
+
code = !code;
|
|
191
|
+
cell += c;
|
|
192
|
+
} else if (c === "|" && !code) {
|
|
193
|
+
out.push(cell);
|
|
194
|
+
cell = "";
|
|
195
|
+
} else cell += c;
|
|
196
|
+
}
|
|
197
|
+
out.push(cell);
|
|
198
|
+
return out;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** 表格一行的各个单元格,去掉两端的 | */
|
|
202
|
+
function cells(line: string): string[] {
|
|
203
|
+
const parts = splitCells(line.trim());
|
|
204
|
+
if (parts.length > 1 && parts[0]!.trim() === "") parts.shift();
|
|
205
|
+
if (parts.length > 1 && parts.at(-1)!.trim() === "") parts.pop();
|
|
206
|
+
return parts.map((c) => c.trim());
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function tableLine(rows: string[][], align: Align[]): MdLine {
|
|
210
|
+
const shown = rows.map((r, i) => r.map((c) => (i === 0 ? bold(inline(c)) : inline(c))));
|
|
211
|
+
return { render: (width) => renderTable(shown, align, width) };
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* 画带边框的表格。放不下时按比例收窄较宽的列、单元格内折行;
|
|
216
|
+
* 窄到每列连 3 列都分不到时,改成每行一段「表头: 值」
|
|
217
|
+
*/
|
|
218
|
+
function renderTable(rows: string[][], align: Align[], width: number): string[] {
|
|
219
|
+
const n = Math.max(...rows.map((r) => r.length));
|
|
220
|
+
const grid = rows.map((r) => Array.from({ length: n }, (_, i) => r[i] ?? ""));
|
|
221
|
+
const natural = Array.from({ length: n }, (_, i) => Math.max(1, ...grid.map((r) => stringWidth(r[i]!))));
|
|
222
|
+
const room = width - (3 * n + 1);
|
|
223
|
+
if (room < 3 * n) {
|
|
224
|
+
const [head, ...body] = grid;
|
|
225
|
+
return body.flatMap((r, k) => [
|
|
226
|
+
...(k ? [""] : []),
|
|
227
|
+
...r.flatMap((c, i) => wrapAnsi(`${head![i]}${dim(":")} ${c}`, Math.max(1, width), { hard: true }).split("\n")),
|
|
228
|
+
]);
|
|
229
|
+
}
|
|
230
|
+
// 从最窄的列开始,能放下的列按原宽,放不下的平分剩下的宽度
|
|
231
|
+
const widths = [...natural];
|
|
232
|
+
if (natural.reduce((a, b) => a + b, 0) > room) {
|
|
233
|
+
let left = room;
|
|
234
|
+
let open = n;
|
|
235
|
+
for (const i of [...natural.keys()].sort((a, b) => natural[a]! - natural[b]!)) {
|
|
236
|
+
const share = Math.floor(left / open);
|
|
237
|
+
widths[i] = Math.min(natural[i]!, share);
|
|
238
|
+
left -= widths[i]!;
|
|
239
|
+
open--;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
const pad = (s: string, w: number, a: Align) => {
|
|
243
|
+
const gap = w - stringWidth(s);
|
|
244
|
+
if (a === "right") return " ".repeat(gap) + s;
|
|
245
|
+
if (a === "center") return " ".repeat(Math.floor(gap / 2)) + s + " ".repeat(Math.ceil(gap / 2));
|
|
246
|
+
return s + " ".repeat(gap);
|
|
247
|
+
};
|
|
248
|
+
const border = (l: string, m: string, r: string) => dim(l + widths.map((w) => "─".repeat(w + 2)).join(m) + r);
|
|
249
|
+
const row = (r: string[]) => {
|
|
250
|
+
const parts = r.map((c, i) => wrapAnsi(c, widths[i]!, { hard: true, trim: true }).split("\n"));
|
|
251
|
+
const height = Math.max(...parts.map((p) => p.length));
|
|
252
|
+
return Array.from({ length: height }, (_, k) =>
|
|
253
|
+
dim("│ ") + parts.map((p, i) => pad(p[k] ?? "", widths[i]!, align[i] ?? "left")).join(dim(" │ ")) + dim(" │"),
|
|
254
|
+
);
|
|
255
|
+
};
|
|
256
|
+
const [head, ...body] = grid;
|
|
257
|
+
return [border("┌", "┬", "┐"), ...row(head!), border("├", "┼", "┤"), ...body.flatMap(row), border("└", "┴", "┘")];
|
|
258
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// @ 引用文件的补全:输入框里打 @ 时找出光标处正在输入的路径、给候选路径排序。
|
|
2
|
+
// 候选从工作目录来(git 仓库里只看版本控制的,和 glob 一样),发送时附上文件内容由 jade/mentions 负责。
|
|
3
|
+
|
|
4
|
+
import { execFile } from "node:child_process";
|
|
5
|
+
import { readdir } from "node:fs/promises";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
|
|
8
|
+
/** 候选(文件加目录)最多这么多,再多也只是拖慢补全 */
|
|
9
|
+
const MAX_CANDIDATES = 20_000;
|
|
10
|
+
|
|
11
|
+
/** 光标正在输入的 @ 引用:@ 的下标和已经输入的部分;不在引用里时返回 undefined */
|
|
12
|
+
export function mentionAt(value: string, cursor: number): { start: number; query: string } | undefined {
|
|
13
|
+
const m = /(?:^|\s)@("?[^\s"]*)$/.exec(value.slice(0, cursor));
|
|
14
|
+
if (!m) return undefined;
|
|
15
|
+
return { start: cursor - m[1]!.length - 1, query: m[1]!.replace(/^"/, "") };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** 补全后的文本:@ 引用加一个空格,含空白的路径加引号 */
|
|
19
|
+
export const mentionText = (p: string) => (/\s/.test(p) ? `@"${p}"` : `@${p}`);
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 按 query 给候选路径排序:文件名以 query 开头的最前,其次文件名包含、路径包含,
|
|
23
|
+
* 最后是按顺序出现 query 每个字符的模糊匹配;同一档里短的在前
|
|
24
|
+
*/
|
|
25
|
+
export function rankPaths(paths: string[], query: string, limit: number): string[] {
|
|
26
|
+
const q = query.toLowerCase().replaceAll("\\", "/");
|
|
27
|
+
if (!q) return paths.filter((p) => !p.slice(0, -1).includes("/")).slice(0, limit);
|
|
28
|
+
const scored: { p: string; score: number }[] = [];
|
|
29
|
+
for (const p of paths) {
|
|
30
|
+
const lower = p.toLowerCase();
|
|
31
|
+
const base = lower.replace(/\/$/, "").split("/").pop()!;
|
|
32
|
+
let score: number;
|
|
33
|
+
if (lower.startsWith(q)) score = 0;
|
|
34
|
+
else if (base.startsWith(q)) score = 1;
|
|
35
|
+
else if (base.includes(q)) score = 2;
|
|
36
|
+
else if (lower.includes(q)) score = 3;
|
|
37
|
+
else if (subsequence(lower, q)) score = 4;
|
|
38
|
+
else continue;
|
|
39
|
+
scored.push({ p, score });
|
|
40
|
+
}
|
|
41
|
+
scored.sort((a, b) => a.score - b.score || a.p.length - b.p.length || a.p.localeCompare(b.p));
|
|
42
|
+
return scored.slice(0, limit).map((s) => s.p);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function subsequence(text: string, q: string): boolean {
|
|
46
|
+
let i = 0;
|
|
47
|
+
for (const ch of text) if (ch === q[i] && ++i === q.length) return true;
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* @ 补全的候选:工作目录里的文件(git 仓库里只看版本控制的)和它们所在的目录,目录以 / 结尾。
|
|
53
|
+
* 5 秒还没列完就先给已经找到的(大仓库第一次列得慢,不该让输入框卡住)
|
|
54
|
+
*/
|
|
55
|
+
export async function mentionCandidates(cwd: string, signal: AbortSignal = AbortSignal.timeout(5000)): Promise<string[]> {
|
|
56
|
+
const files: string[] = [];
|
|
57
|
+
const dirs = new Set<string>();
|
|
58
|
+
try {
|
|
59
|
+
for await (const full of await listFiles(cwd, signal)) {
|
|
60
|
+
const rel = display(cwd, full);
|
|
61
|
+
files.push(rel);
|
|
62
|
+
for (let i = rel.lastIndexOf("/"); i > 0; i = rel.lastIndexOf("/", i - 1)) {
|
|
63
|
+
const dir = rel.slice(0, i + 1);
|
|
64
|
+
if (dirs.has(dir)) break;
|
|
65
|
+
dirs.add(dir);
|
|
66
|
+
}
|
|
67
|
+
if (files.length + dirs.size >= MAX_CANDIDATES) break;
|
|
68
|
+
}
|
|
69
|
+
} catch {
|
|
70
|
+
// 列到一半被中止(超时):用已经找到的
|
|
71
|
+
}
|
|
72
|
+
return [...dirs, ...files];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// 以下和 jade/fs 的 listFiles、display 相同:插件之间不共享代码(只能共享类型),所以这里留一份
|
|
76
|
+
|
|
77
|
+
const SKIP_DIRS = new Set([".git", "node_modules"]);
|
|
78
|
+
|
|
79
|
+
/** 显示用:工作目录内的路径用相对形式,统一为正斜杠 */
|
|
80
|
+
function display(cwd: string, full: string): string {
|
|
81
|
+
const rel = path.relative(cwd, full);
|
|
82
|
+
const shown = rel && rel !== ".." && !rel.startsWith(`..${path.sep}`) && !path.isAbsolute(rel) ? rel : full;
|
|
83
|
+
return shown.split(path.sep).join("/");
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** git 仓库里的文件(版本控制的和没忽略的新文件);不是仓库、没有 git 时 undefined */
|
|
87
|
+
function gitFiles(root: string): Promise<string[] | undefined> {
|
|
88
|
+
return new Promise((resolve) => {
|
|
89
|
+
execFile("git", ["ls-files", "-z", "--cached", "--others", "--exclude-standard"], { cwd: root, maxBuffer: 256 * 1024 * 1024, windowsHide: true }, (err, out) =>
|
|
90
|
+
// 合并冲突中的文件会出现多次
|
|
91
|
+
resolve(err ? undefined : [...new Set(out.split("\0").filter(Boolean))]),
|
|
92
|
+
);
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async function listFiles(root: string, signal: AbortSignal): Promise<Iterable<string> | AsyncIterable<string>> {
|
|
97
|
+
const tracked = await gitFiles(root);
|
|
98
|
+
if (!tracked) return walk(root, signal);
|
|
99
|
+
return tracked.filter((rel) => !rel.split("/").some((seg) => SKIP_DIRS.has(seg))).map((rel) => path.join(root, rel));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async function* walk(dir: string, signal: AbortSignal): AsyncIterable<string> {
|
|
103
|
+
signal.throwIfAborted();
|
|
104
|
+
const entries = (await readdir(dir, { withFileTypes: true })).sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
105
|
+
for (const e of entries) {
|
|
106
|
+
const full = path.join(dir, e.name);
|
|
107
|
+
if (e.isDirectory()) {
|
|
108
|
+
if (!SKIP_DIRS.has(e.name)) yield* walk(full, signal);
|
|
109
|
+
} else if (e.isFile()) {
|
|
110
|
+
yield full;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "jade/tui",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"api": "^1",
|
|
5
|
+
"entry": "./index.ts",
|
|
6
|
+
"provides": ["frontend@1", "ui@1"],
|
|
7
|
+
"requires": ["commands@1", "keys@1", "renderers@1"],
|
|
8
|
+
"optional": ["modes@1", "models@1", "compact@1", "sessions@1"],
|
|
9
|
+
"frontend": { "name": "tui", "default": true }
|
|
10
|
+
}
|