pi-model-manager 0.2.0 → 0.3.1

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.
@@ -6,28 +6,42 @@
6
6
  // 设计:列表页保留 KISS 的键盘模型,同时支持摘要、列头、详情区、底部快捷键。
7
7
  // 表单页另有 Ctrl+S 保存;列表页可注册单键快捷操作,并用 / 做轻量过滤。
8
8
 
9
- import type { ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
9
+ import type { ExtensionCommandContext, Theme } from "@earendil-works/pi-coding-agent";
10
10
  import { CURSOR_MARKER, Key, matchesKey, truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
11
+ import { t } from "../i18n.ts";
11
12
 
12
13
  export interface MenuRow {
13
14
  id: string;
14
15
  label: string;
15
- description?: string | readonly string[];
16
+ // [喵喵喵]: label 是按列宽截断后的表格文本,长 ID 的后半段不在其中;
17
+ // 需要按真实标识搜索的调用方必须显式提供未截断的 searchText。
18
+ searchText?: string;
19
+ // [喵喵喵]: 可调整性属于行自身;开关切换会让字段集变化(如开 Thinking 后才出现 Adaptive 行),
20
+ // 放在外部集合里就会在行重建后与实际行脱节,新出现的开关行按 ←→ 没反应。
21
+ adjustable?: boolean;
22
+ }
23
+
24
+ // 底部快捷键提示;key 与说明分开才能分别着色,并在窄终端按项折行而不是被截掉。
25
+ export interface MenuHint {
26
+ key: string;
27
+ label: string;
16
28
  }
17
29
 
18
30
  export interface PersistentMenuOptions {
19
31
  summaryLines?: readonly string[];
20
- tableHeader?: string | ((width: number) => string);
21
- formatRow?: (row: MenuRow, width: number) => string;
22
- getDetailLines?: (selectedRow: MenuRow | undefined) => readonly string[];
23
- footer?: string;
32
+ // [喵喵喵]: 渲染回调拿到 theme 才能给列与详情上语义色;theme 只存在于 custom 回调作用域。
33
+ tableHeader?: string | ((width: number, theme: Theme) => string);
34
+ formatRow?: (row: MenuRow, width: number, theme: Theme) => string;
35
+ getDetailLines?: (selectedRow: MenuRow | undefined, theme: Theme) => readonly string[];
36
+ hints?: readonly MenuHint[];
24
37
  emptyLabel?: string;
25
38
  visibleRows?: number;
26
39
  searchable?: boolean;
27
40
  }
28
41
 
29
42
  export interface PersistentFormMenuOptions extends PersistentMenuOptions {
30
- adjustableRowIds?: readonly string[];
43
+ // 就地切换该字段并返回重建后的行;返回 undefined 则忽略本次按键。
44
+ onAdjust?: (id: string, direction: HorizontalDirection) => MenuRow[] | undefined;
31
45
  }
32
46
 
33
47
  export type MenuAction =
@@ -35,7 +49,8 @@ export type MenuAction =
35
49
  | { type: "cancel" };
36
50
 
37
51
  export type HorizontalDirection = "left" | "right";
38
- export type FormMenuAction = MenuAction | { type: "save" } | { type: "adjust"; id: string; direction: HorizontalDirection };
52
+ // 横向切换已改为组件内处理,不再作为外部动作返回。
53
+ export type FormMenuAction = MenuAction | { type: "save" };
39
54
 
40
55
  export type ShortcutMenuAction<TShortcut extends string = string> = MenuAction | { type: "shortcut"; shortcut: TShortcut };
41
56
 
@@ -62,13 +77,52 @@ export function padLabel(label: string, columns: number): string {
62
77
  return padToVisibleWidth(label, columns);
63
78
  }
64
79
 
65
- function getDescriptionLines(row: MenuRow): string[] {
66
- const source = row.description;
67
- return typeof source === "string" ? source.split("\n") : [...(source ?? [])];
80
+ function getSearchText(row: MenuRow): string {
81
+ return (row.searchText ?? `${row.id}\n${row.label}`).toLocaleLowerCase();
68
82
  }
69
83
 
70
- function getSearchText(row: MenuRow): string {
71
- return [row.id, row.label, ...getDescriptionLines(row)].join("\n").toLocaleLowerCase();
84
+ const HINT_GAP = " ";
85
+
86
+ // 按项贪心排版:单行装不下就换行,保证窄终端下 Esc 等尾部提示不会被 truncate 丢失。
87
+ function layoutHintLines(hints: readonly MenuHint[], theme: Theme, width: number): string[] {
88
+ const lines: string[] = [];
89
+ let currentText = "";
90
+ let currentWidth = 0;
91
+ for (const hint of hints) {
92
+ const hintWidth = visibleWidth(`${hint.key} ${hint.label}`);
93
+ const styled = `${theme.fg("accent", hint.key)} ${theme.fg("dim", hint.label)}`;
94
+ if (!currentText) {
95
+ currentText = styled;
96
+ currentWidth = hintWidth;
97
+ continue;
98
+ }
99
+ if (currentWidth + HINT_GAP.length + hintWidth > width) {
100
+ lines.push(currentText);
101
+ currentText = styled;
102
+ currentWidth = hintWidth;
103
+ continue;
104
+ }
105
+ currentText += `${HINT_GAP}${styled}`;
106
+ currentWidth += HINT_GAP.length + hintWidth;
107
+ }
108
+ if (currentText) lines.push(currentText);
109
+ return lines;
110
+ }
111
+
112
+ // 滚动条字符在不同终端的宽度属于 ambiguous,因此固定预留 2 列,宁可多一个空格也不让行溢出。
113
+ const SCROLLBAR_WIDTH = 2;
114
+
115
+ // pi 在菜单下方还要渲染输入框与状态行,不预留就会把菜单底部的快捷键提示顶出屏幕。
116
+ const RESERVED_TERMINAL_ROWS = 3;
117
+ // 小于这个高度就无法同时容纳边框、标题、提示和列表,此时宁可溢出也不再继续压缩。
118
+ const MIN_MENU_ROWS = 8;
119
+ const MIN_LIST_ROWS = 1;
120
+
121
+ function getScrollbarGlyph(rowOffset: number, viewportRows: number, windowStart: number, totalRows: number): string {
122
+ const thumbRows = Math.max(1, Math.round((viewportRows * viewportRows) / totalRows));
123
+ const maxWindowStart = Math.max(1, totalRows - viewportRows);
124
+ const thumbStart = Math.round((windowStart / maxWindowStart) * (viewportRows - thumbRows));
125
+ return rowOffset >= thumbStart && rowOffset < thumbStart + thumbRows ? "█" : "│";
72
126
  }
73
127
 
74
128
  function filterRows(rows: MenuRow[], query: string): MenuRow[] {
@@ -115,8 +169,10 @@ function createPersistentMenu<TAction extends MenuAction | FormMenuAction | Shor
115
169
  cursor: MenuCursor,
116
170
  createSaveAction: (() => TAction) | undefined,
117
171
  shortcuts: MenuShortcut[],
118
- createAdjustAction: ((id: string, direction: HorizontalDirection) => TAction) | undefined = undefined,
119
- adjustableRowIds: ReadonlySet<string> = new Set(),
172
+ // [喵喵喵]: 横向切换是纯本地状态变更,必须在组件内完成。早期实现用 done() 结束组件再由
173
+ // 调用方重开,每按一次方向键都会销毁重建整个 TUI,在终端上表现为闪烁。
174
+ // 回调就地改写草稿并返回新行,返回 undefined 表示本次不可调整。
175
+ onAdjust: ((id: string, direction: HorizontalDirection) => MenuRow[] | undefined) | undefined = undefined,
120
176
  options: PersistentMenuOptions = {},
121
177
  ): Promise<TAction> {
122
178
  cursor.index = clampIndex(cursor.index, rows.length);
@@ -126,14 +182,19 @@ function createPersistentMenu<TAction extends MenuAction | FormMenuAction | Shor
126
182
  let searchQuery = "";
127
183
  let searchCursor = 0;
128
184
  let focused = false;
129
- const visibleRows = options.visibleRows ?? 18;
185
+ const configuredVisibleRows = options.visibleRows ?? 18;
186
+ // [喵喵喵]: 真实可见行数要减去本帧的标题/摘要/详情/提示,每帧在 render 里重算,
187
+ // 翻页也必须用同一个值,否则矮终端上 PgUp/PgDn 会跳过看不见的行。
188
+ let viewportRows = configuredVisibleRows;
130
189
  const searchable = options.searchable ?? false;
131
190
 
132
- const getActiveRows = (): MenuRow[] => searchable ? filterRows(rows, searchQuery) : rows;
191
+ // onAdjust 会整批替换行数据,因此不能直接闭包参数 rows。
192
+ let currentRows = rows;
193
+ const getActiveRows = (): MenuRow[] => searchable ? filterRows(currentRows, searchQuery) : currentRows;
133
194
 
134
195
  const syncCursor = (activeRows: MenuRow[]): void => {
135
196
  const row = activeRows[selectedIndex];
136
- if (row) cursor.index = rows.indexOf(row);
197
+ if (row) cursor.index = currentRows.indexOf(row);
137
198
  };
138
199
 
139
200
  const requestRender = (): void => {
@@ -148,7 +209,7 @@ function createPersistentMenu<TAction extends MenuAction | FormMenuAction | Shor
148
209
  searchActive = false;
149
210
  searchQuery = "";
150
211
  searchCursor = 0;
151
- selectedIndex = clampIndex(cursor.index, rows.length);
212
+ selectedIndex = clampIndex(cursor.index, currentRows.length);
152
213
  requestRender();
153
214
  return true;
154
215
  };
@@ -175,6 +236,35 @@ function createPersistentMenu<TAction extends MenuAction | FormMenuAction | Shor
175
236
  selectedIndex = nextIndex;
176
237
  requestRender();
177
238
  };
239
+ // 输入态显示带光标的搜索行;Tab 退出后仍需告知用户过滤仍生效。
240
+ const renderQueryLine = (width: number): string => {
241
+ if (!searchActive) {
242
+ return truncateToWidth(`${theme.fg("dim", t("过滤:"))}${searchQuery}`, width, "");
243
+ }
244
+ const cursorGlyph = focused ? `${CURSOR_MARKER}${theme.fg("accent", "▌")}` : "";
245
+ const searchPrefix = theme.fg("accent", t("搜索:"));
246
+ const queryWidth = Math.max(0, width - visibleWidth(searchPrefix));
247
+ const queryDisplay = searchQuery
248
+ ? fitSearchQueryAroundCursor(searchQuery, searchCursor, cursorGlyph, queryWidth)
249
+ : `${cursorGlyph}${theme.fg("dim", t("<输入关键词>"))}`;
250
+ return truncateToWidth(`${searchPrefix}${queryDisplay}`, width, "");
251
+ };
252
+
253
+ const getHints = (): MenuHint[] => {
254
+ const hints: MenuHint[] = [...(options.hints ?? [])];
255
+ if (!searchable) return hints;
256
+ if (searchActive) hints.push({ key: "Tab", label: t("保留过滤") }, { key: "Esc", label: t("清空搜索") });
257
+ else if (searchQuery) hints.push({ key: "Tab", label: t("继续输入") }, { key: "Esc", label: t("清空过滤") });
258
+ else hints.push({ key: "/", label: t("搜索") });
259
+ return hints;
260
+ };
261
+
262
+ // 终端高度未知(如测试环境)时不限制总行数,交由调用方配置的 visibleRows 控制。
263
+ const getRowBudget = (): number => {
264
+ const terminalRows = tui.terminal?.rows ?? 0;
265
+ if (terminalRows <= 0) return Number.POSITIVE_INFINITY;
266
+ return Math.max(MIN_MENU_ROWS, terminalRows - RESERVED_TERMINAL_ROWS);
267
+ };
178
268
 
179
269
  return {
180
270
  get focused(): boolean {
@@ -195,11 +285,15 @@ function createPersistentMenu<TAction extends MenuAction | FormMenuAction | Shor
195
285
  : matchesKey(data, Key.right)
196
286
  ? "right"
197
287
  : undefined;
198
- if (horizontalDirection && createAdjustAction) {
288
+ if (horizontalDirection && onAdjust) {
199
289
  const row = getActiveRows()[selectedIndex];
200
- if (row && adjustableRowIds.has(row.id)) {
290
+ if (row?.adjustable) {
201
291
  syncCursor(getActiveRows());
202
- done(createAdjustAction(row.id, horizontalDirection));
292
+ const nextRows = onAdjust(row.id, horizontalDirection);
293
+ if (nextRows) {
294
+ currentRows = nextRows;
295
+ requestRender();
296
+ }
203
297
  }
204
298
  return;
205
299
  }
@@ -246,6 +340,12 @@ function createPersistentMenu<TAction extends MenuAction | FormMenuAction | Shor
246
340
  requestRender();
247
341
  return;
248
342
  }
343
+ // [喵喵喵]: Tab 只退出输入态并保留过滤结果,让用户搜到目标后还能按单字母快捷键;Esc 才清空。
344
+ if (matchesKey(data, Key.tab)) {
345
+ searchActive = false;
346
+ requestRender();
347
+ return;
348
+ }
249
349
  if (matchesKey(data, Key.enter)) {
250
350
  pickSelected();
251
351
  return;
@@ -257,12 +357,19 @@ function createPersistentMenu<TAction extends MenuAction | FormMenuAction | Shor
257
357
  }
258
358
  }
259
359
 
260
- const shortcut = shortcuts.find((candidate) => candidate.input === data);
360
+ // [喵喵喵]: 提示里快捷键显示为大写,因此 Shift 组合也必须命中同一个动作。
361
+ const shortcut = shortcuts.find((candidate) => candidate.input.toLowerCase() === data.toLowerCase());
261
362
  if (shortcut) {
262
363
  syncCursor(getActiveRows());
263
364
  done({ type: "shortcut", shortcut: shortcut.shortcut } as TAction);
264
365
  return;
265
366
  }
367
+ if (searchable && searchQuery && matchesKey(data, Key.tab)) {
368
+ searchActive = true;
369
+ searchCursor = Array.from(searchQuery).length;
370
+ requestRender();
371
+ return;
372
+ }
266
373
  if (searchable && data === "/") {
267
374
  searchActive = true;
268
375
  searchCursor = Array.from(searchQuery).length;
@@ -289,11 +396,11 @@ function createPersistentMenu<TAction extends MenuAction | FormMenuAction | Shor
289
396
  return;
290
397
  }
291
398
  if (matchesKey(data, Key.pageUp)) {
292
- moveSelection(Math.max(0, selectedIndex - visibleRows));
399
+ moveSelection(Math.max(0, selectedIndex - viewportRows));
293
400
  return;
294
401
  }
295
402
  if (matchesKey(data, Key.pageDown)) {
296
- moveSelection(Math.min(activeRows.length - 1, selectedIndex + visibleRows));
403
+ moveSelection(Math.min(activeRows.length - 1, selectedIndex + viewportRows));
297
404
  return;
298
405
  }
299
406
  if (matchesKey(data, Key.home)) {
@@ -309,83 +416,93 @@ function createPersistentMenu<TAction extends MenuAction | FormMenuAction | Shor
309
416
  const activeRows = getActiveRows();
310
417
  selectedIndex = clampIndex(selectedIndex, activeRows.length);
311
418
  syncCursor(activeRows);
312
- const windowStart = Math.max(
313
- 0,
314
- Math.min(selectedIndex - Math.floor(visibleRows / 2), Math.max(0, activeRows.length - visibleRows)),
315
- );
316
- const shownRows = activeRows.slice(windowStart, windowStart + visibleRows);
317
419
 
318
- const border = theme.fg("borderMuted", "─".repeat(Math.max(0, Math.min(width, 100))));
319
- const summaryLines = options.summaryLines ?? (help ? help.split("\n") : []);
320
- const lines: string[] = [
420
+ const border = theme.fg("borderMuted", "─".repeat(Math.max(0, width)));
421
+ const hintLines = layoutHintLines(getHints(), theme, width);
422
+ const summaryLines = (options.summaryLines ?? (help ? help.split("\n") : []))
423
+ .map((line) => truncateToWidth(theme.fg("dim", line), width));
424
+ const searchLine = searchable && (searchActive || searchQuery) ? renderQueryLine(width) : undefined;
425
+ const tableHeaderText = typeof options.tableHeader === "function"
426
+ ? options.tableHeader(width, theme)
427
+ : options.tableHeader;
428
+ // [喵喵喵]: 详情文本由调用方按语义着色,这里只负责裁剪,避免外层样式与内层 reset 互相打断。
429
+ const detailLines = (options.getDetailLines?.(activeRows[selectedIndex], theme) ?? [])
430
+ .map((line) => truncateToWidth(line, width));
431
+
432
+ // [喵喵喵]: 矮终端下按优先级降级:上下边框、标题、快捷键提示和最小列表必须保留,
433
+ // 剩余空间才依次发给搜索行、表头、摘要、详情;否则总行数超过终端高度时底部提示会被裁掉。
434
+ const budget = getRowBudget();
435
+ const essentialRows = 2 + 1 + 1 + (hintLines.length > 0 ? hintLines.length + 1 : 0);
436
+ // [喵喵喵]: 列表装不下时底部会多一行位置提示,先按最坏情况预留;
437
+ // 放到算出 viewportRows 之后再扣,在列表已压到最小时就无处可扣了。
438
+ const scrollHintRows = activeRows.length > MIN_LIST_ROWS ? 1 : 0;
439
+ let spare = budget - essentialRows - MIN_LIST_ROWS - scrollHintRows;
440
+
441
+ const showSearchLine = searchLine !== undefined && spare >= 1;
442
+ if (showSearchLine) spare -= 1;
443
+ const showTableHeader = Boolean(tableHeaderText) && spare >= 1;
444
+ if (showTableHeader) spare -= 1;
445
+ const showSummary = summaryLines.length > 0 && spare >= summaryLines.length;
446
+ if (showSummary) spare -= summaryLines.length;
447
+ const detailBlockRows = detailLines.length > 0 ? detailLines.length + 2 : 0;
448
+ const showDetail = detailBlockRows > 0 && spare >= detailBlockRows;
449
+ if (showDetail) spare -= detailBlockRows;
450
+
451
+ viewportRows = Math.min(configuredVisibleRows, MIN_LIST_ROWS + Math.max(0, spare));
452
+
453
+ const headLines: string[] = [
321
454
  border,
322
455
  truncateToWidth(theme.fg("accent", theme.bold(title)), width),
323
456
  ];
457
+ if (showSummary) headLines.push(...summaryLines);
458
+ if (showSearchLine) headLines.push(searchLine!);
459
+ headLines.push("");
460
+ if (showTableHeader) headLines.push(truncateToWidth(theme.fg("dim", tableHeaderText!), width));
324
461
 
325
- for (const line of summaryLines) {
326
- lines.push(truncateToWidth(theme.fg("dim", line), width));
327
- }
328
- if (searchable && searchActive) {
329
- const cursorGlyph = focused ? `${CURSOR_MARKER}${theme.fg("accent", "▌")}` : "";
330
- const searchPrefix = theme.fg("accent", "搜索:");
331
- const queryWidth = Math.max(0, width - visibleWidth(searchPrefix));
332
- const queryDisplay = searchQuery
333
- ? fitSearchQueryAroundCursor(searchQuery, searchCursor, cursorGlyph, queryWidth)
334
- : `${cursorGlyph}${theme.fg("dim", "<输入关键词>")}`;
335
- lines.push(truncateToWidth(`${searchPrefix}${queryDisplay}`, width, ""));
336
- }
337
- lines.push("");
462
+ const tailLines: string[] = [];
463
+ if (showDetail) tailLines.push("", border, ...detailLines);
464
+ if (hintLines.length > 0) tailLines.push("", ...hintLines.map((line) => truncateToWidth(line, width)));
465
+ tailLines.push(border);
338
466
 
339
- const tableHeader = typeof options.tableHeader === "function"
340
- ? options.tableHeader(width)
341
- : options.tableHeader;
342
- if (tableHeader) lines.push(truncateToWidth(theme.fg("dim", tableHeader), width));
467
+ const windowStart = Math.max(
468
+ 0,
469
+ Math.min(selectedIndex - Math.floor(viewportRows / 2), Math.max(0, activeRows.length - viewportRows)),
470
+ );
471
+ const shownRows = activeRows.slice(windowStart, windowStart + viewportRows);
472
+ const scrolling = activeRows.length > viewportRows;
473
+ const contentWidth = Math.max(0, scrolling ? width - SCROLLBAR_WIDTH : width);
343
474
 
475
+ const bodyLines: string[] = [];
344
476
  if (shownRows.length === 0) {
345
- const emptyLabel = searchQuery ? `无匹配项:${searchQuery}` : options.emptyLabel ?? "暂无条目";
346
- lines.push(truncateToWidth(theme.fg("dim", ` ${emptyLabel}`), width));
477
+ const emptyLabel = searchQuery ? t("无匹配项:{query}", { query: searchQuery }) : options.emptyLabel ?? t("暂无条目");
478
+ bodyLines.push(truncateToWidth(theme.fg("dim", ` ${emptyLabel}`), width));
347
479
  } else {
348
- for (let i = 0; i < shownRows.length; i += 1) {
349
- const absoluteIndex = windowStart + i;
350
- const row = shownRows[i]!;
351
- const selected = absoluteIndex === selectedIndex;
480
+ for (let offset = 0; offset < shownRows.length; offset += 1) {
481
+ const row = shownRows[offset]!;
482
+ const selected = windowStart + offset === selectedIndex;
352
483
  const prefix = selected ? "❯ " : " ";
353
- const rowLabel = options.formatRow?.(row, Math.max(0, width - visibleWidth(prefix))) ?? row.label;
354
- const line = `${prefix}${rowLabel}`;
355
- lines.push(truncateToWidth(selected ? theme.fg("accent", line) : line, width));
356
- for (const description of getDescriptionLines(row)) {
357
- lines.push(truncateToWidth(theme.fg("dim", ` ${description}`), width));
358
- }
359
- }
360
- }
361
-
362
- const detailLines = options.getDetailLines?.(activeRows[selectedIndex]) ?? [];
363
- if (detailLines.length > 0) {
364
- lines.push("", border);
365
- for (const line of detailLines) {
366
- lines.push(truncateToWidth(theme.fg("dim", line), width));
484
+ const rowLabel = options.formatRow?.(row, Math.max(0, contentWidth - visibleWidth(prefix)), theme) ?? row.label;
485
+ const rowText = truncateToWidth(`${prefix}${rowLabel}`, contentWidth);
486
+ // [喵喵喵]: 选中行用背景色而不是整行前景色,列内的语义色才不会被抹平。
487
+ const styledRow = selected ? theme.bg("selectedBg", padToVisibleWidth(rowText, contentWidth)) : rowText;
488
+ bodyLines.push(scrolling
489
+ ? `${styledRow} ${theme.fg("borderMuted", getScrollbarGlyph(offset, viewportRows, windowStart, activeRows.length))}`
490
+ : styledRow);
367
491
  }
368
- }
369
-
370
- const footerLines = options.footer ? options.footer.split("\n") : [];
371
- if (searchable) {
372
- footerLines.push(searchActive ? `/ 搜索:${searchQuery || "<输入关键词>"} Backspace 删除 Esc 清空` : "/ 搜索");
373
- }
374
- if (footerLines.length > 0) {
375
- lines.push("");
376
- for (const line of footerLines) {
377
- lines.push(truncateToWidth(theme.fg("dim", line), width));
492
+ if (scrolling) {
493
+ const position = `${windowStart + 1}-${windowStart + shownRows.length} / ${activeRows.length}`;
494
+ bodyLines.push(theme.fg("dim", padToVisibleWidth("", Math.max(0, contentWidth - visibleWidth(position))) + position));
378
495
  }
379
496
  }
380
497
 
381
- lines.push(border);
382
- return lines.map((line) => truncateToWidth(line, width));
498
+ return [...headLines, ...bodyLines, ...tailLines].map((line) => truncateToWidth(line, width));
383
499
  },
384
500
  };
385
501
  });
386
502
  }
387
503
 
388
- export async function showPersistentMenu(
504
+ // 内部基础菜单:对外只暴露 showOptionPicker / showPersistentFormMenu / showPersistentShortcutMenu 三个语义入口。
505
+ async function showPersistentMenu(
389
506
  ctx: ExtensionCommandContext,
390
507
  title: string,
391
508
  help: string,
@@ -393,7 +510,35 @@ export async function showPersistentMenu(
393
510
  cursor: MenuCursor,
394
511
  options: PersistentMenuOptions = {},
395
512
  ): Promise<MenuAction> {
396
- return createPersistentMenu<MenuAction>(ctx, title, help, rows, cursor, undefined, [], undefined, undefined, options);
513
+ return createPersistentMenu<MenuAction>(ctx, title, help, rows, cursor, undefined, [], undefined, options);
514
+ }
515
+ // 单选弹窗:两个编辑器的所有枚举字段都走这里,避免同一表单里出现两种选择器外观。
516
+ export async function showOptionPicker<TChoice extends { id: string; label: string }>(
517
+ ctx: ExtensionCommandContext,
518
+ title: string,
519
+ choices: readonly TChoice[],
520
+ currentId: string,
521
+ ): Promise<TChoice | undefined> {
522
+ const cursor: MenuCursor = { index: Math.max(0, choices.findIndex((choice) => choice.id === currentId)) };
523
+ const action = await showPersistentMenu(
524
+ ctx,
525
+ title,
526
+ "",
527
+ choices.map((choice) => ({
528
+ id: choice.id,
529
+ label: choice.id === currentId ? `${choice.label} ${t("← 当前")}` : choice.label,
530
+ })),
531
+ cursor,
532
+ {
533
+ hints: [
534
+ { key: "↑↓", label: t("移动") },
535
+ { key: "Enter", label: t("选择") },
536
+ { key: "Esc", label: t("返回") },
537
+ ],
538
+ },
539
+ );
540
+ if (action.type === "cancel") return undefined;
541
+ return choices.find((choice) => choice.id === action.id);
397
542
  }
398
543
 
399
544
  export async function showPersistentFormMenu(
@@ -412,8 +557,7 @@ export async function showPersistentFormMenu(
412
557
  cursor,
413
558
  () => ({ type: "save" }),
414
559
  [],
415
- (id, direction) => ({ type: "adjust", id, direction }),
416
- new Set(options.adjustableRowIds ?? []),
560
+ options.onAdjust,
417
561
  options,
418
562
  );
419
563
  }
@@ -427,5 +571,5 @@ export async function showPersistentShortcutMenu<TShortcut extends string>(
427
571
  shortcuts: MenuShortcut<TShortcut>[],
428
572
  options: PersistentMenuOptions = {},
429
573
  ): Promise<ShortcutMenuAction<TShortcut>> {
430
- return createPersistentMenu<ShortcutMenuAction<TShortcut>>(ctx, title, help, rows, cursor, undefined, shortcuts, undefined, undefined, { searchable: true, ...options });
574
+ return createPersistentMenu<ShortcutMenuAction<TShortcut>>(ctx, title, help, rows, cursor, undefined, shortcuts, undefined, { searchable: true, ...options });
431
575
  }