MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / editFileInEditor

Function editFileInEditor

source code/utils/promptEditor.ts:33–103  ·  view source on GitHub ↗
(filePath: string)

Source from the content-addressed store, hash-verified

31
32// sync IO: called from sync context (React components, sync command handlers)
33export function editFileInEditor(filePath: string): EditorResult {
34 const fs = getFsImplementation()
35 const inkInstance = instances.get(process.stdout)
36 if (!inkInstance) {
37 throw new Error('Ink instance not found - cannot pause rendering')
38 }
39
40 const editor = getExternalEditor()
41 if (!editor) {
42 return { content: null }
43 }
44
45 try {
46 fs.statSync(filePath)
47 } catch {
48 return { content: null }
49 }
50
51 const useAlternateScreen = !isGuiEditor(editor)
52
53 if (useAlternateScreen) {
54 // Terminal editors (vi, nano, etc.) take over the terminal. Delegate to
55 // Ink's alt-screen-aware handoff so fullscreen mode (where <AlternateScreen>
56 // already entered alt screen) doesn't get knocked back to the main buffer
57 // by a hardcoded ?1049l. enterAlternateScreen() internally calls pause()
58 // and suspendStdin(); exitAlternateScreen() undoes both and resets frame
59 // state so the next render writes from scratch.
60 inkInstance.enterAlternateScreen()
61 } else {
62 // GUI editors (code, subl, etc.) open in a separate window — just pause
63 // Ink and release stdin while they're open.
64 inkInstance.pause()
65 inkInstance.suspendStdin()
66 }
67
68 try {
69 // Use override command if available, otherwise use the editor as-is
70 const editorCommand = EDITOR_OVERRIDES[editor] ?? editor
71 execSync_DEPRECATED(`${editorCommand} "${filePath}"`, {
72 stdio: 'inherit',
73 })
74
75 // Read the edited content
76 const editedContent = fs.readFileSync(filePath, { encoding: 'utf-8' })
77 return { content: editedContent }
78 } catch (err) {
79 if (
80 typeof err === 'object' &&
81 err !== null &&
82 'status' in err &&
83 typeof (err as { status: unknown }).status === 'number'
84 ) {
85 const status = (err as { status: number }).status
86 if (status !== 0) {
87 const editorName = toIDEDisplayName(editor)
88 return {
89 content: null,
90 error: `${editorName} exited with code ${status}`,

Callers 7

handleKeyDownFunction · 0.85
AgentEditorFunction · 0.85
ConfirmStepWrapperFunction · 0.85
editPromptInEditorFunction · 0.85
callFunction · 0.85
handleSelectMemoryFileFunction · 0.85
callFunction · 0.85

Calls 11

getFsImplementationFunction · 0.85
isGuiEditorFunction · 0.85
execSync_DEPRECATEDFunction · 0.85
toIDEDisplayNameFunction · 0.85
enterAlternateScreenMethod · 0.80
pauseMethod · 0.80
suspendStdinMethod · 0.80
exitAlternateScreenMethod · 0.80
resumeStdinMethod · 0.80
resumeMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected