tmux-ide 2.9.17 → 2.9.18

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.
Files changed (26) hide show
  1. package/bin/cli.js +629 -320
  2. package/package.json +1 -1
  3. package/packages/daemon/dist/doctor.js +101 -2
  4. package/packages/daemon/dist/lib/daemon-embed.js +4 -1
  5. package/packages/daemon/dist/lib/native-split-capability.js +20 -0
  6. package/packages/daemon/dist/lib/tmux-server-owner.js +2 -1
  7. package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/Info.plist +2 -2
  8. package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/MacOS/tmux-ide-notifier +0 -0
  9. package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/Resources/Assets.car +0 -0
  10. package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/_CodeSignature/CodeResources +2 -2
  11. package/packages/daemon/dist/terminal/mirror/control-channel.js +3 -2
  12. package/packages/daemon/dist/terminal/mirror/mirror-service.js +47 -11
  13. package/packages/daemon/dist/terminal/mirror/session-channel.js +19 -4
  14. package/packages/daemon/dist/terminal/mirror/window-split-authority.js +9 -2
  15. package/packages/daemon/dist/terminal/protocol/native-split-resize.js +16 -16
  16. package/packages/daemon/dist/terminal/protocol/stock-split-resize.js +194 -0
  17. package/packages/daemon/src/doctor.ts +104 -2
  18. package/packages/daemon/src/lib/daemon-embed.ts +4 -0
  19. package/packages/daemon/src/lib/native-split-capability.ts +21 -0
  20. package/packages/daemon/src/lib/tmux-server-owner.ts +2 -0
  21. package/packages/daemon/src/terminal/mirror/control-channel.ts +3 -2
  22. package/packages/daemon/src/terminal/mirror/mirror-service.ts +51 -12
  23. package/packages/daemon/src/terminal/mirror/session-channel.ts +23 -4
  24. package/packages/daemon/src/terminal/mirror/window-split-authority.ts +15 -4
  25. package/packages/daemon/src/terminal/protocol/native-split-resize.ts +16 -16
  26. package/packages/daemon/src/terminal/protocol/stock-split-resize.ts +268 -0
@@ -32,7 +32,7 @@ function json(text: string, limit: number): unknown {
32
32
  throw new Error("Bounded native response required");
33
33
  return JSON.parse(text);
34
34
  }
35
- function capability(text: string): boolean {
35
+ export function nativeSplitCapability(text: string): boolean {
36
36
  const value = json(text, 1024);
37
37
  return (
38
38
  object(value, [
@@ -54,13 +54,13 @@ function capability(text: string): boolean {
54
54
  /** Read-only, uncached probe; the caller pins and bounds the selected server. */
55
55
  export async function supportsNativeSplitResize(runTmux: NativeSplitRunner): Promise<boolean> {
56
56
  try {
57
- return capability(await runTmux(["tmux-ide-resize-split", "-V"]));
57
+ return nativeSplitCapability(await runTmux(["tmux-ide-resize-split", "-V"]));
58
58
  } catch {
59
59
  return false;
60
60
  }
61
61
  }
62
62
 
63
- function validTree(tree: LayoutTreeNode): boolean {
63
+ export function validSplitTree(tree: LayoutTreeNode): boolean {
64
64
  let budget = 5;
65
65
  const visit = (node: LayoutTreeNode): boolean => {
66
66
  if (node.left + node.width > 4096 || node.top + node.height > 4096) return false;
@@ -74,7 +74,7 @@ function validTree(tree: LayoutTreeNode): boolean {
74
74
  };
75
75
  return visit(tree);
76
76
  }
77
- function locate(tree: LayoutTreeNode, path: readonly number[]) {
77
+ export function locateSplit(tree: LayoutTreeNode, path: readonly number[]) {
78
78
  let node = tree;
79
79
  let parent: Extract<LayoutTreeNode, { kind: "split" }> | undefined;
80
80
  for (const index of path) {
@@ -84,7 +84,7 @@ function locate(tree: LayoutTreeNode, path: readonly number[]) {
84
84
  }
85
85
  return parent && path.at(-1)! < parent.children.length - 1 ? { parent, node } : null;
86
86
  }
87
- function edge(node: LayoutTreeNode, axis: "cols" | "rows") {
87
+ export function splitEdge(node: LayoutTreeNode, axis: "cols" | "rows") {
88
88
  return axis === "cols" ? node.left + node.width : node.top + node.height;
89
89
  }
90
90
  function sameRect(a: LayoutTreeNode, b: LayoutTreeNode) {
@@ -93,7 +93,7 @@ function sameRect(a: LayoutTreeNode, b: LayoutTreeNode) {
93
93
  /** Only geometry inside the exact parent may change, along the requested axis.
94
94
  * Native redistribution can affect multiple siblings. This verifies convergence,
95
95
  * not exclusive causation or authentication of the returned layout. */
96
- function preservesTree(
96
+ export function preservesSplitTree(
97
97
  before: LayoutTreeNode,
98
98
  after: LayoutTreeNode,
99
99
  affected: LayoutTreeNode,
@@ -116,7 +116,7 @@ function preservesTree(
116
116
  before.axis === after.axis &&
117
117
  before.children.length === after.children.length &&
118
118
  before.children.every((child, index) =>
119
- preservesTree(child, after.children[index]!, affected, axis, inside),
119
+ preservesSplitTree(child, after.children[index]!, affected, axis, inside),
120
120
  )
121
121
  );
122
122
  }
@@ -151,8 +151,8 @@ export async function resizeNativeSplit(
151
151
  )
152
152
  return { status: "refused", reason: "invalid-request" };
153
153
  const tree = parseLayoutTree(request.expectedLayout);
154
- if (!tree || !validTree(tree)) return { status: "refused", reason: "invalid-request" };
155
- const selected = locate(tree, request.path);
154
+ if (!tree || !validSplitTree(tree)) return { status: "refused", reason: "invalid-request" };
155
+ const selected = locateSplit(tree, request.path);
156
156
  if (!selected || selected.parent.axis !== request.axis)
157
157
  return { status: "refused", reason: "invalid-request" };
158
158
  before = tree;
@@ -162,7 +162,7 @@ export async function resizeNativeSplit(
162
162
  return { status: "refused", reason: "invalid-request" };
163
163
  }
164
164
  try {
165
- if (!capability(await runTmux(["tmux-ide-resize-split", "-V"])))
165
+ if (!nativeSplitCapability(await runTmux(["tmux-ide-resize-split", "-V"])))
166
166
  return { status: "refused", reason: "unsupported" };
167
167
  } catch {
168
168
  return { status: "refused", reason: "unsupported" };
@@ -200,15 +200,15 @@ export async function resizeNativeSplit(
200
200
  )
201
201
  return { status: "uncertain", reason: "invalid-receipt" };
202
202
  const after = parseLayoutTree(receipt.layout);
203
- const original = locate(before, captured.path)!;
204
- const actual = after && locate(after, captured.path);
205
- const previous = edge(original.node, captured.axis);
203
+ const original = locateSplit(before, captured.path)!;
204
+ const actual = after && locateSplit(after, captured.path);
205
+ const previous = splitEdge(original.node, captured.axis);
206
206
  if (
207
207
  !after ||
208
- !validTree(after) ||
208
+ !validSplitTree(after) ||
209
209
  !actual ||
210
- !preservesTree(before, after, original.parent, captured.axis) ||
211
- receipt.boundary !== edge(actual.node, captured.axis) ||
210
+ !preservesSplitTree(before, after, original.parent, captured.axis) ||
211
+ receipt.boundary !== splitEdge(actual.node, captured.axis) ||
212
212
  receipt.boundary < Math.min(previous, captured.boundary) ||
213
213
  receipt.boundary > Math.max(previous, captured.boundary)
214
214
  )
@@ -0,0 +1,268 @@
1
+ /**
2
+ * Moves one split boundary on a stock tmux server, which has neither
3
+ * `tmux-ide-resize-split` nor the operation journal.
4
+ *
5
+ * `resize-pane` names a pane, and tmux applies it to that pane's nearest
6
+ * same-axis ancestor, so the target is a leaf that reaches the split's parent
7
+ * through opposite-axis splits only. The boundary is clamped to its two
8
+ * neighbours, because tmux takes space from further siblings once they run out.
9
+ * The move is relative, because an absolute -y adds one on the status line's edge.
10
+ *
11
+ * The guard and the resize are one `if-shell -F`: tmux evaluates the format
12
+ * inline and runs the commands it queues before any other client's command, so
13
+ * a change from outside cannot land between the comparison and the resize. Send
14
+ * it on a control connection, where if-shell and each branch command answer in
15
+ * their own block, three in all, the last holding the answer. On tmux 3.8 only control clients read
16
+ * `#{window_layout}` in the format the mirror holds; other clients read JSON.
17
+ */
18
+ import { randomUUID } from "node:crypto";
19
+ import { parseLayoutTree, type LayoutTreeNode } from "./layout-parse.ts";
20
+ import {
21
+ locateSplit,
22
+ preservesSplitTree,
23
+ splitEdge,
24
+ validSplitTree,
25
+ type NativeSplitResizeRequest,
26
+ type NativeSplitResizeResult,
27
+ } from "./native-split-resize.ts";
28
+
29
+ type Axis = "cols" | "rows";
30
+ const span = (node: LayoutTreeNode, axis: Axis) => (axis === "cols" ? node.width : node.height);
31
+ const start = (node: LayoutTreeNode, axis: Axis) => (axis === "cols" ? node.left : node.top);
32
+
33
+ /** The window options that decide how small tmux lets a pane get. */
34
+ export interface StockSplitSizes {
35
+ readonly borderStatus: string;
36
+ /** Columns a pane scrollbar takes, or 0 with scrollbars off. */
37
+ readonly scrollbar: number;
38
+ }
39
+ /** tmux's own minimum (`layout_resize_check`): a leaf holds one cell, plus the pane
40
+ * status line on the window's top or bottom edge, and any scrollbar. */
41
+ function minimum(
42
+ node: LayoutTreeNode,
43
+ axis: Axis,
44
+ sizes: StockSplitSizes,
45
+ root: LayoutTreeNode,
46
+ ): number {
47
+ if (node.kind === "leaf") {
48
+ if (axis === "cols") return 1 + sizes.scrollbar;
49
+ const onEdge =
50
+ (sizes.borderStatus === "top" && node.top === root.top) ||
51
+ (sizes.borderStatus === "bottom" && node.top + node.height === root.top + root.height);
52
+ return onEdge ? 2 : 1;
53
+ }
54
+ const mins = node.children.map((child) => minimum(child, axis, sizes, root));
55
+ return node.axis === axis
56
+ ? mins.reduce((sum, size) => sum + size, node.children.length - 1)
57
+ : Math.max(...mins);
58
+ }
59
+ /** A pane whose nearest ancestor on `axis` is the cell directly above `node`. */
60
+ function reachingLeaf(node: LayoutTreeNode, axis: Axis): string | null {
61
+ if (node.kind === "leaf") return node.id;
62
+ if (node.axis === axis) return null;
63
+ for (const child of node.children) {
64
+ const leaf = reachingLeaf(child, axis);
65
+ if (leaf) return leaf;
66
+ }
67
+ return null;
68
+ }
69
+ /** The pane to name in `resize-pane` for the boundary after the cell at `path`.
70
+ * Null means tmux has no way to address that boundary. In its last cell a pane
71
+ * acts on the boundary before it, so either side's pane moves it the same way. */
72
+ export function stockSplitTarget(
73
+ tree: LayoutTreeNode,
74
+ path: readonly number[],
75
+ axis: Axis,
76
+ ): string | null {
77
+ const located = locateSplit(tree, path);
78
+ if (!located || located.parent.axis !== axis) return null;
79
+ const leading = reachingLeaf(located.node, axis);
80
+ if (leading) return leading;
81
+ const index = path.at(-1)!;
82
+ if (index + 1 !== located.parent.children.length - 1) return null;
83
+ return reachingLeaf(located.parent.children[index + 1]!, axis);
84
+ }
85
+ /** Where the boundary may go without tmux taking space from further siblings. */
86
+ export function stockSplitRange(
87
+ tree: LayoutTreeNode,
88
+ path: readonly number[],
89
+ axis: Axis,
90
+ sizes: StockSplitSizes,
91
+ ): { readonly low: number; readonly high: number } | null {
92
+ const located = locateSplit(tree, path);
93
+ if (!located) return null;
94
+ const after = located.parent.children[path.at(-1)! + 1]!;
95
+ const low = start(located.node, axis) + minimum(located.node, axis, sizes, tree);
96
+ const high = start(after, axis) + span(after, axis) - minimum(after, axis, sizes, tree) - 1;
97
+ return { low, high };
98
+ }
99
+ /** Whether a handle on this boundary could move it at all. */
100
+ export function stockSplitMovable(
101
+ tree: LayoutTreeNode,
102
+ path: readonly number[],
103
+ axis: Axis,
104
+ sizes: StockSplitSizes,
105
+ ): boolean {
106
+ const range = stockSplitRange(tree, path, axis, sizes);
107
+ return stockSplitTarget(tree, path, axis) !== null && range !== null && range.high > range.low;
108
+ }
109
+ export interface StockSplitPlan {
110
+ readonly paneId: string;
111
+ readonly boundary: number;
112
+ /** How far the boundary moves; negative is left or up. */
113
+ readonly delta: number;
114
+ }
115
+ export function planStockSplitResize(
116
+ tree: LayoutTreeNode,
117
+ path: readonly number[],
118
+ axis: Axis,
119
+ boundary: number,
120
+ sizes: StockSplitSizes,
121
+ ): StockSplitPlan | null {
122
+ const paneId = stockSplitTarget(tree, path, axis);
123
+ const range = stockSplitRange(tree, path, axis, sizes);
124
+ const located = locateSplit(tree, path);
125
+ if (!paneId || !range || !located || range.high < range.low) return null;
126
+ const clamped = Math.min(range.high, Math.max(range.low, boundary));
127
+ return { paneId, boundary: clamped, delta: clamped - splitEdge(located.node, axis) };
128
+ }
129
+ /** Escapes a layout for the right side of a format comparison. */
130
+ function formatLiteral(layout: string): string {
131
+ return layout.replaceAll(",", "#,").replaceAll("}", "#}");
132
+ }
133
+ function leafCount(node: LayoutTreeNode): number {
134
+ return node.kind === "leaf" ? 1 : node.children.reduce((sum, child) => sum + leafCount(child), 0);
135
+ }
136
+ export function stockSplitCommand(
137
+ request: NativeSplitResizeRequest,
138
+ sessionCreated: string,
139
+ plan: StockSplitPlan,
140
+ panes: number,
141
+ options: StockSplitOptions,
142
+ marker: string,
143
+ ): string {
144
+ const equal = (name: string, value: string | number) => `#{==:#{${name}},${value}}`;
145
+ const condition = [
146
+ equal("window_id", request.windowId),
147
+ equal("session_id", request.sessionId),
148
+ equal("session_created", sessionCreated),
149
+ equal("window_zoomed_flag", 0),
150
+ // Control clients' layouts leave out floating panes; this count includes them.
151
+ equal("window_panes", panes),
152
+ equal("pane-border-status", options.borderStatus),
153
+ equal("pane-scrollbars", options.scrollbars),
154
+ equal("window_layout", formatLiteral(request.expectedLayout)),
155
+ ].reduceRight((rest, next) => `#{&&:${next},${rest}}`);
156
+ // Relative, because tmux adds one to an absolute -y on the status line's edge.
157
+ const flag =
158
+ request.axis === "cols" ? (plan.delta > 0 ? "-R" : "-L") : plan.delta > 0 ? "-D" : "-U";
159
+ const move =
160
+ plan.delta === 0
161
+ ? `display-message -p ""`
162
+ : `resize-pane -t ${plan.paneId} ${flag} ${Math.abs(plan.delta)}`;
163
+ const readback = `display-message -p -t ${request.windowId} "${marker} #{window_layout}"`;
164
+ return `if-shell -F -t '${request.sessionId}:${request.windowId}' '${condition}' '${move} ; ${readback}' 'display-message -p ${marker} ; display-message -p ${marker}'`;
165
+ }
166
+ export type StockSplitResizeResult =
167
+ | NativeSplitResizeResult
168
+ | { status: "refused"; reason: "stale-layout" };
169
+
170
+ /** The format that reads a window's sizing options, and its parser. */
171
+ export const STOCK_SPLIT_OPTIONS_FORMAT =
172
+ "#{pane-border-status}\t#{pane-scrollbars}\t#{pane-scrollbars-style}";
173
+ export interface StockSplitOptions extends StockSplitSizes {
174
+ readonly scrollbars: string;
175
+ }
176
+ export function parseStockSplitOptions(line: string | undefined): StockSplitOptions | null {
177
+ const [borderStatus, scrollbars, style] = (line ?? "").split("\t");
178
+ if (!/^(off|top|bottom)$/u.test(borderStatus ?? "") || !/^[a-z]+$/u.test(scrollbars ?? ""))
179
+ return null;
180
+ const field = (name: string) =>
181
+ Number(new RegExp(`\\b${name}=(\\d+)`, "u").exec(style ?? "")?.[1] ?? 0);
182
+ return {
183
+ borderStatus: borderStatus!,
184
+ scrollbars: scrollbars!,
185
+ scrollbar: scrollbars === "off" ? 0 : Math.max(field("width"), 1) + field("pad"),
186
+ };
187
+ }
188
+ export interface StockSplitIo {
189
+ readonly readOptions: (windowId: string) => Promise<string[]>;
190
+ readonly send: (command: string) => Promise<string[]>;
191
+ }
192
+
193
+ /** Authorizes, then sends the command without awaiting in between. A sent command
194
+ * that does not answer as expected is uncertain: the caller must not retry it. */
195
+ export async function resizeStockSplit(
196
+ request: NativeSplitResizeRequest,
197
+ sessionCreated: string,
198
+ authorizeBeforeEffect: () => void,
199
+ io: StockSplitIo,
200
+ ): Promise<StockSplitResizeResult> {
201
+ const before = parseLayoutTree(request.expectedLayout);
202
+ if (
203
+ !before ||
204
+ !validSplitTree(before) ||
205
+ !/^\$\d+$/u.test(request.sessionId) ||
206
+ !/^@\d+$/u.test(request.windowId) ||
207
+ !/^[1-9]\d*$/u.test(sessionCreated)
208
+ )
209
+ return { status: "refused", reason: "invalid-request" };
210
+ let options: StockSplitOptions | null;
211
+ try {
212
+ options = parseStockSplitOptions((await io.readOptions(request.windowId))[0]);
213
+ } catch {
214
+ return { status: "refused", reason: "unsupported" };
215
+ }
216
+ if (!options) return { status: "refused", reason: "unsupported" };
217
+ const plan = planStockSplitResize(before, request.path, request.axis, request.boundary, options);
218
+ if (!plan) return { status: "refused", reason: "invalid-request" };
219
+ const marker = `tmux-ide-split-${randomUUID()}`;
220
+ const command = stockSplitCommand(
221
+ request,
222
+ sessionCreated,
223
+ plan,
224
+ leafCount(before),
225
+ options,
226
+ marker,
227
+ );
228
+ authorizeBeforeEffect();
229
+ let reply: string[];
230
+ try {
231
+ reply = await io.send(command);
232
+ } catch {
233
+ return { status: "uncertain", reason: "command-failed" };
234
+ }
235
+ const answer = reply.join("\n");
236
+ if (answer === marker) return { status: "refused", reason: "stale-layout" };
237
+ if (reply.length !== 1 || !answer.startsWith(`${marker} `))
238
+ return { status: "uncertain", reason: "invalid-receipt" };
239
+ const layout = answer.slice(marker.length + 1);
240
+ const after = parseLayoutTree(layout);
241
+ const original = locateSplit(before, request.path)!;
242
+ const actual = after && locateSplit(after, request.path);
243
+ if (
244
+ !after ||
245
+ !validSplitTree(after) ||
246
+ !actual ||
247
+ !preservesSplitTree(before, after, original.parent, request.axis)
248
+ )
249
+ return { status: "uncertain", reason: "invalid-receipt" };
250
+ // Only this boundary may have moved: its parent's other children keep their edges.
251
+ const edges = (parent: typeof original.parent) =>
252
+ parent.children.map((child) => splitEdge(child, request.axis));
253
+ const moved = request.path.at(-1)!;
254
+ const others = edges(actual.parent);
255
+ if (edges(original.parent).some((edge, index) => index !== moved && edge !== others[index]))
256
+ return { status: "uncertain", reason: "invalid-receipt" };
257
+ const boundary = splitEdge(actual.node, request.axis);
258
+ const previous = splitEdge(original.node, request.axis);
259
+ if (boundary < Math.min(previous, plan.boundary) || boundary > Math.max(previous, plan.boundary))
260
+ return { status: "uncertain", reason: "invalid-receipt" };
261
+ return {
262
+ status: "applied",
263
+ boundary,
264
+ layout,
265
+ tree: after,
266
+ changed: layout !== request.expectedLayout,
267
+ };
268
+ }