MCPcopy Index your code
hub / github.com/codeaashu/claude-code / editFileInEditor

Function editFileInEditor

src/utils/promptEditor.ts:31–101  ·  view source on GitHub ↗
(filePath: string)

Source from the content-addressed store, hash-verified

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

Tested by

no test coverage detected