{"version":3,"file":"external-editor.d.ts","sourceRoot":"","sources":["../../../src/modes/interactive/external-editor.ts"],"names":[],"mappings":"AAOA,MAAM,WAAW,qBAAqB;IACrC,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,MAAM,oBAAoB,GAAG;IAAE,MAAM,EAAE,UAAU,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,MAAM,EAAE,QAAQ,CAAA;CAAE,CAAC;AAElG;;;;;;;;GAQG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,EAAE,CAoC5D;AAED,wBAAsB,oBAAoB,CAAC,OAAO,EAAE,qBAAqB,GAAG,OAAO,CAAC,oBAAoB,CAAC,CAyCxG","sourcesContent":["import { spawn } from \"node:child_process\";\nimport { mkdtempSync, readFileSync, rmSync, writeFileSync } from \"node:fs\";\nimport { tmpdir } from \"node:os\";\nimport { join } from \"node:path\";\nimport { APP_NAME } from \"../../config.ts\";\nimport { stripBom } from \"../../utils/text.ts\";\n\nexport interface ExternalEditorOptions {\n\tcommand: string;\n\tcontent: string;\n}\n\nexport type ExternalEditorResult = { status: \"complete\"; content: string } | { status: \"failed\" };\n\n/**\n * Split an editor command into executable + arguments, honouring quotes.\n *\n * A plain `command.split(\" \")` breaks every Windows editor installed under a path\n * with a space — `C:\\Program Files\\Microsoft VS Code\\Code.exe --wait` launched\n * `C:\\Program` and failed silently. Quoting the executable is the portable way to\n * express that, so both `\"C:\\Program Files\\...\\Code.exe\" --wait` and\n * `'/usr/bin/my editor' -w` now work, and `\\\"` escapes a literal quote.\n */\nexport function parseEditorCommand(command: string): string[] {\n\tconst tokens: string[] = [];\n\tlet current = \"\";\n\tlet quote: '\"' | \"'\" | undefined;\n\tlet started = false;\n\n\tfor (let index = 0; index < command.length; index++) {\n\t\tconst char = command[index];\n\n\t\tif (char === \"\\\\\" && quote === '\"' && command[index + 1] === '\"') {\n\t\t\tcurrent += '\"';\n\t\t\tindex++;\n\t\t\tcontinue;\n\t\t}\n\t\tif (quote) {\n\t\t\tif (char === quote) quote = undefined;\n\t\t\telse current += char;\n\t\t\tcontinue;\n\t\t}\n\t\tif (char === '\"' || char === \"'\") {\n\t\t\tquote = char;\n\t\t\tstarted = true;\n\t\t\tcontinue;\n\t\t}\n\t\tif (char === \" \" || char === \"\\t\") {\n\t\t\tif (started) tokens.push(current);\n\t\t\tcurrent = \"\";\n\t\t\tstarted = false;\n\t\t\tcontinue;\n\t\t}\n\t\tcurrent += char;\n\t\tstarted = true;\n\t}\n\n\tif (started) tokens.push(current);\n\treturn tokens;\n}\n\nexport async function editInExternalEditor(options: ExternalEditorOptions): Promise<ExternalEditorResult> {\n\tconst directory = mkdtempSync(join(tmpdir(), `${APP_NAME}-editor-`));\n\tconst filePath = join(directory, \"prompt.md\");\n\ttry {\n\t\twriteFileSync(filePath, options.content, \"utf-8\");\n\t\tconst [editor, ...editorArgs] = parseEditorCommand(options.command);\n\t\tif (!editor) {\n\t\t\treturn { status: \"failed\" };\n\t\t}\n\t\tprocess.stdout.write(\n\t\t\t`Launching external editor: ${options.command}\\n${APP_NAME} will resume when the editor exits.\\n`,\n\t\t);\n\n\t\t// Do not use spawnSync here. On Windows, synchronous child_process calls can keep\n\t\t// Node/libuv's console input read active after the parent pauses stdin, racing\n\t\t// vim/nvim for the console input buffer until Ctrl+C cancels the pending read.\n\t\tconst exitCode = await new Promise<number | null>((resolve) => {\n\t\t\tconst useShell = process.platform === \"win32\";\n\t\t\t// With shell:true the arguments are concatenated into a command line, so a\n\t\t\t// path containing a space has to carry its own quotes to survive.\n\t\t\tconst quoteForShell = (value: string) => (useShell && /[\\s&|<>^]/.test(value) ? `\"${value}\"` : value);\n\t\t\tconst child = spawn(quoteForShell(editor), [...editorArgs, filePath].map(quoteForShell), {\n\t\t\t\tstdio: \"inherit\",\n\t\t\t\tshell: useShell,\n\t\t\t});\n\t\t\tchild.on(\"error\", () => resolve(null));\n\t\t\tchild.on(\"close\", (code) => resolve(code));\n\t\t});\n\n\t\tif (exitCode !== 0) {\n\t\t\treturn { status: \"failed\" };\n\t\t}\n\n\t\treturn { status: \"complete\", content: stripBom(readFileSync(filePath, \"utf-8\")).replace(/\\n$/, \"\") };\n\t} finally {\n\t\ttry {\n\t\t\trmSync(directory, { recursive: true, force: true });\n\t\t} catch {\n\t\t\t// Cleanup is best effort.\n\t\t}\n\t}\n}\n"]}