fix(coding-agent): speed up external editor launch (#6903)
* fix(coding-agent): speed up external editor launch Fixes #6774 * refactor(coding-agent): clarify external editor handling * refactor(coding-agent): simplify external editor result * refactor(coding-agent): model external editor lifecycle * fix(coding-agent): rename external editor test fake * fix(coding-agent): secure external editor temp files * fix(coding-agent): simplify external editor temp path * fix(coding-agent): restore system temp editor files
This commit is contained in:
@@ -3,10 +3,6 @@
|
||||
* Supports Ctrl+G for external editor.
|
||||
*/
|
||||
|
||||
import { spawn } from "node:child_process";
|
||||
import * as fs from "node:fs";
|
||||
import * as os from "node:os";
|
||||
import * as path from "node:path";
|
||||
import {
|
||||
Container,
|
||||
Editor,
|
||||
@@ -18,6 +14,7 @@ import {
|
||||
type TUI,
|
||||
} from "@earendil-works/pi-tui";
|
||||
import type { KeybindingsManager } from "../../../core/keybindings.ts";
|
||||
import { editInExternalEditor } from "../external-editor.ts";
|
||||
import { getEditorTheme, theme } from "../theme/theme.ts";
|
||||
import { DynamicBorder } from "./dynamic-border.ts";
|
||||
import { keyHint } from "./keybinding-hints.ts";
|
||||
@@ -28,7 +25,7 @@ export class ExtensionEditorComponent extends Container implements Focusable {
|
||||
private onCancelCallback: () => void;
|
||||
private tui: TUI;
|
||||
private keybindings: KeybindingsManager;
|
||||
private externalEditorCommand: string | undefined;
|
||||
private externalEditorCommand: string;
|
||||
|
||||
private _focused = false;
|
||||
get focused(): boolean {
|
||||
@@ -53,7 +50,11 @@ export class ExtensionEditorComponent extends Container implements Focusable {
|
||||
|
||||
this.tui = tui;
|
||||
this.keybindings = keybindings;
|
||||
this.externalEditorCommand = externalEditorCommand;
|
||||
this.externalEditorCommand =
|
||||
externalEditorCommand ||
|
||||
process.env.VISUAL ||
|
||||
process.env.EDITOR ||
|
||||
(process.platform === "win32" ? "notepad" : "nano");
|
||||
this.onSubmitCallback = onSubmit;
|
||||
this.onCancelCallback = onCancel;
|
||||
|
||||
@@ -79,14 +80,13 @@ export class ExtensionEditorComponent extends Container implements Focusable {
|
||||
this.addChild(new Spacer(1));
|
||||
|
||||
// Add hint
|
||||
const hasExternalEditor = !!this.getExternalEditorCommand();
|
||||
const hint =
|
||||
keyHint("tui.select.confirm", "submit") +
|
||||
" " +
|
||||
keyHint("tui.input.newLine", "newline") +
|
||||
" " +
|
||||
keyHint("tui.select.cancel", "cancel") +
|
||||
(hasExternalEditor ? ` ${keyHint("app.editor.external", "external editor")}` : "");
|
||||
` ${keyHint("app.editor.external", "external editor")}`;
|
||||
this.addChild(new Text(hint, 1, 0));
|
||||
|
||||
this.addChild(new Spacer(1));
|
||||
@@ -105,7 +105,7 @@ export class ExtensionEditorComponent extends Container implements Focusable {
|
||||
|
||||
// External editor (app keybinding)
|
||||
if (this.keybindings.matches(keyData, "app.editor.external")) {
|
||||
this.openExternalEditor();
|
||||
void this.handleOpenExternalEditor();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -113,54 +113,19 @@ export class ExtensionEditorComponent extends Container implements Focusable {
|
||||
this.editor.handleInput(keyData);
|
||||
}
|
||||
|
||||
private getExternalEditorCommand(): string | undefined {
|
||||
const editorCmd = this.externalEditorCommand || process.env.VISUAL || process.env.EDITOR;
|
||||
if (editorCmd) {
|
||||
return editorCmd;
|
||||
}
|
||||
return process.platform === "win32" ? "notepad" : "nano";
|
||||
}
|
||||
|
||||
private async openExternalEditor(): Promise<void> {
|
||||
const editorCmd = this.getExternalEditorCommand();
|
||||
if (!editorCmd) {
|
||||
return;
|
||||
}
|
||||
|
||||
const currentText = this.editor.getText();
|
||||
const tmpFile = path.join(os.tmpdir(), `pi-extension-editor-${Date.now()}.md`);
|
||||
|
||||
private async handleOpenExternalEditor(): Promise<void> {
|
||||
const content = this.editor.getText();
|
||||
this.tui.stop();
|
||||
try {
|
||||
fs.writeFileSync(tmpFile, currentText, "utf-8");
|
||||
this.tui.stop();
|
||||
|
||||
const [editor, ...editorArgs] = editorCmd.split(" ");
|
||||
process.stdout.write(`Launching external editor: ${editorCmd}\nPi will resume when the editor exits.\n`);
|
||||
|
||||
// Do not use spawnSync here. On Windows, synchronous child_process calls can keep
|
||||
// Node/libuv's console input read active after tui.stop() pauses stdin, racing
|
||||
// vim/nvim for the console input buffer until Ctrl+C cancels the pending read.
|
||||
const status = await new Promise<number | null>((resolve) => {
|
||||
const child = spawn(editor, [...editorArgs, tmpFile], {
|
||||
stdio: "inherit",
|
||||
shell: process.platform === "win32",
|
||||
});
|
||||
child.on("error", () => resolve(null));
|
||||
child.on("close", (code) => resolve(code));
|
||||
const result = await editInExternalEditor({
|
||||
command: this.externalEditorCommand,
|
||||
content,
|
||||
});
|
||||
|
||||
if (status === 0) {
|
||||
const newContent = fs.readFileSync(tmpFile, "utf-8").replace(/\n$/, "");
|
||||
this.editor.setText(newContent);
|
||||
if (result.status === "complete") {
|
||||
this.editor.setText(result.content);
|
||||
}
|
||||
} finally {
|
||||
try {
|
||||
fs.unlinkSync(tmpFile);
|
||||
} catch {
|
||||
// Ignore cleanup errors
|
||||
}
|
||||
this.tui.start();
|
||||
// Force full re-render since external editor uses alternate screen
|
||||
this.tui.requestRender(true);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user