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:
Christian Klotz
2026-07-22 16:49:01 +03:00
committed by GitHub
parent 7b52cef2e6
commit 75e6123aba
7 changed files with 162 additions and 99 deletions
@@ -137,6 +137,7 @@ import { TreeSelectorComponent } from "./components/tree-selector.ts";
import { TrustSelectorComponent } from "./components/trust-selector.ts";
import { UserMessageComponent } from "./components/user-message.ts";
import { UserMessageSelectorComponent } from "./components/user-message-selector.ts";
import { editInExternalEditor } from "./external-editor.ts";
import { getModelSearchText } from "./model-search.ts";
import {
getAvailableThemes,
@@ -2577,7 +2578,7 @@ export class InteractiveMode {
this.defaultEditor.onAction("app.model.select", () => this.showModelSelector());
this.defaultEditor.onAction("app.tools.expand", () => this.toggleToolOutputExpansion());
this.defaultEditor.onAction("app.thinking.toggle", () => this.toggleThinkingBlockVisibility());
this.defaultEditor.onAction("app.editor.external", () => this.openExternalEditor());
this.defaultEditor.onAction("app.editor.external", () => void this.handleOpenExternalEditor());
this.defaultEditor.onAction("app.message.copy", () => void this.handleCopyCommand());
this.defaultEditor.onAction("app.message.followUp", () => this.handleFollowUp());
this.defaultEditor.onAction("app.message.dequeue", () => this.handleDequeue());
@@ -3809,57 +3810,20 @@ export class InteractiveMode {
this.showStatus(`Thinking blocks: ${this.hideThinkingBlock ? "hidden" : "visible"}`);
}
private async openExternalEditor(): Promise<void> {
private async handleOpenExternalEditor(): Promise<void> {
const editorCmd = this.settingsManager.getExternalEditorCommand();
if (!editorCmd) {
this.showWarning("No editor configured. Set externalEditor in settings.json or $VISUAL/$EDITOR.");
return;
}
const currentText = this.editor.getExpandedText?.() ?? this.editor.getText();
const tmpFile = path.join(os.tmpdir(), `pi-editor-${Date.now()}.pi.md`);
const content = this.editor.getExpandedText?.() ?? this.editor.getText();
this.ui.stop();
try {
// Write current content to temp file
fs.writeFileSync(tmpFile, currentText, "utf-8");
// Stop TUI to release terminal
this.ui.stop();
// Split by space to support editor arguments (e.g., "code --wait")
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 ui.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: editorCmd,
content,
});
// On successful exit (status 0), replace editor 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);
}
// On non-zero exit, keep original text (no action needed)
} finally {
// Clean up temp file
try {
fs.unlinkSync(tmpFile);
} catch {
// Ignore cleanup errors
}
// Restart TUI
this.ui.start();
// Force full re-render since external editor uses alternate screen
this.ui.requestRender(true);
}
}