Repository navigation
Expand file tree
/
Copy pathmode-change-command.ts
More file actions
120 lines (105 loc) · 3.11 KB
/
Copy pathmode-change-command.ts
File metadata and controls
120 lines (105 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { spawn } from "node:child_process";
import type { ModeChangeSettings } from "./settings.js";
import type { Mode } from "./types.js";
const MODE_CHANGE_COMMAND_TIMEOUT_MS = 2000;
type ModeChangeCommandRunner = (command: string) => void;
type RunningModeChangeCommand = {
child: ReturnType<typeof spawn>;
timeout: ReturnType<typeof setTimeout>;
};
export type ModeChangeEvent = { mode: Mode; previousMode: Mode };
let activeModeChangeCommand: RunningModeChangeCommand | null = null;
let pendingModeChangeCommand: string | null = null;
let modeChangeCommandRunner: ModeChangeCommandRunner = spawnModeChangeCommand;
export function setModeChangeCommandRunnerForTests(
next: ModeChangeCommandRunner,
): () => void {
const prev = modeChangeCommandRunner;
modeChangeCommandRunner = next;
return () => {
modeChangeCommandRunner = prev;
};
}
function spawnModeChangeCommand(command: string): void {
if (!command) return;
if (activeModeChangeCommand) {
pendingModeChangeCommand = command;
return;
}
startModeChangeCommand(command);
}
function startModeChangeCommand(command: string): void {
let child: ReturnType<typeof spawn>;
try {
child = spawn(command, {
shell: true,
stdio: "ignore",
windowsHide: true,
});
} catch {
// spawn rejected synchronously (e.g., EMFILE) — never break the editor
runPendingModeChangeCommand();
return;
}
let finished = false;
const finish = () => {
if (finished) return;
finished = true;
clearTimeout(timeout);
if (activeModeChangeCommand?.child !== child) return;
activeModeChangeCommand = null;
runPendingModeChangeCommand();
};
const timeout = setTimeout(() => {
try {
child.kill();
} catch {
// best effort timeout cleanup
}
finish();
}, MODE_CHANGE_COMMAND_TIMEOUT_MS);
timeout.unref?.();
activeModeChangeCommand = { child, timeout };
child.once("error", finish);
child.once("close", finish);
}
function runPendingModeChangeCommand(): void {
const pending = pendingModeChangeCommand;
pendingModeChangeCommand = null;
if (pending) startModeChangeCommand(pending);
}
function clearPendingModeChangeCommand(): void {
pendingModeChangeCommand = null;
}
export function cancelModeChangeCommands(): void {
pendingModeChangeCommand = null;
const active = activeModeChangeCommand;
activeModeChangeCommand = null;
if (!active) return;
clearTimeout(active.timeout);
try {
active.child.kill();
} catch {
// best effort session cleanup
}
}
export function createModeChangeHandler(
modeChange: ModeChangeSettings | undefined,
emitModeChange: (event: ModeChangeEvent) => void,
): (mode: Mode, prevMode: Mode) => void {
const insert = modeChange?.insert;
const normal = modeChange?.normal;
return (mode, previousMode) => {
try {
emitModeChange({ mode, previousMode });
} catch {
// Subscribers must not break editing or configured mode-change commands.
}
const command = mode === "insert" ? insert : normal;
if (command) {
modeChangeCommandRunner(command);
} else {
clearPendingModeChangeCommand();
}
};
}