File size: 6,291 Bytes
9332ccf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { spawn, spawnSync } from 'node:child_process';
import type { ReadStream } from 'node:tty';
import {
  ALL_EDITORS,
  CoreEvent,
  coreEvents,
  type EditorType,
  getEditorCommand,
  getEditorExtraArgs,
  getEditorWaitFlag,
  isGuiEditor,
  isTerminalEditor,
  isValidEditorType,
  resolveEditorTypeFromCommand,
} from '@google/gemini-cli-core';

/**
 * Command name substrings used to guess whether an unknown $VISUAL/$EDITOR
 * value is a GUI editor. This is a fallback for editors not in the registry;
 * registered editors are detected via resolveEditorTypeFromCommand instead.
 */
const HEURISTIC_GUI_COMMANDS = [
  'code',
  'cursor',
  'subl',
  'zed',
  'atom',
  'agy',
] as const;

/**
 * Opens a file in an external editor and waits for it to close.
 * Handles raw mode switching to ensure the editor can interact with the terminal.
 *
 * @param filePath Path to the file to open
 * @param stdin The stdin stream from Ink/Node
 * @param setRawMode Function to toggle raw mode
 * @param preferredEditorType The user's preferred editor from config
 * @param openInNewWindow Whether to open VS Code-family editors in a new window
 */
export async function openFileInEditor(
  filePath: string,
  stdin: ReadStream | null | undefined,
  setRawMode: ((mode: boolean) => void) | undefined,
  preferredEditorType?: EditorType,
  openInNewWindow?: boolean,
): Promise<void> {
  let command: string | undefined = undefined;
  const args = [filePath];
  // Extra args that come before the file path (e.g. -nw for emacsclient)
  const extraArgs: string[] = [];

  if (preferredEditorType) {
    if (!isValidEditorType(preferredEditorType)) {
      coreEvents.emitFeedback(
        'error',
        `Editor '${preferredEditorType}' is not a recognized editor identifier. ` +
          `Supported editors: ${ALL_EDITORS.join(', ')}. ` +
          `Use /editor to select one, or set the $VISUAL or $EDITOR environment variable.`,
      );
      return;
    }
    command = getEditorCommand(preferredEditorType);
    if (isGuiEditor(preferredEditorType)) {
      args.unshift(getEditorWaitFlag(preferredEditorType));
    }
    extraArgs.push(
      ...getEditorExtraArgs(preferredEditorType, {
        newWindow: openInNewWindow,
      }),
    );
  }

  if (!command) {
    const envCommand = process.env['VISUAL'] ?? process.env['EDITOR'];
    if (envCommand) {
      command = envCommand;
      const [envExecutable = ''] = envCommand.split(' ');
      const resolvedType = resolveEditorTypeFromCommand(envExecutable);
      if (resolvedType) {
        if (
          isGuiEditor(resolvedType) &&
          !envCommand.includes('--wait') &&
          !envCommand.includes('-w')
        ) {
          args.unshift(getEditorWaitFlag(resolvedType));
        }
        extraArgs.push(
          ...getEditorExtraArgs(resolvedType, { newWindow: openInNewWindow }),
        );
      } else {
        // Heuristic fallback for commands not in the registry
        const lower = envCommand.toLowerCase();
        const isGui = HEURISTIC_GUI_COMMANDS.some((g) => lower.includes(g));
        if (isGui && !lower.includes('--wait') && !lower.includes('-w')) {
          args.unshift(lower.includes('subl') ? '-w' : '--wait');
        }
      }
    }
  }

  if (!command) {
    command = process.platform === 'win32' ? 'notepad' : 'vi';
  }

  const [executable = '', ...initialArgs] = command.split(' ');

  // Determine if we should use sync or async based on the command/editor type.
  // If we have a preferredEditorType, we can check if it's a terminal editor.
  // Otherwise, we guess based on the command name.
  const terminalEditors = [
    'vi',
    'vim',
    'nvim',
    'emacs',
    'emacsclient',
    'hx',
    'nano',
    'micro',
  ];
  const isTerminal = preferredEditorType
    ? isTerminalEditor(preferredEditorType)
    : terminalEditors.some((te) => executable.toLowerCase().includes(te));

  if (
    isTerminal &&
    (executable.includes('vi') ||
      executable.includes('vim') ||
      executable.includes('nvim'))
  ) {
    // Pass -i NONE to prevent E138 'Can't write viminfo file' errors in restricted environments.
    args.unshift('-i', 'NONE');
  }

  const wasRaw = stdin?.isRaw ?? false;
  setRawMode?.(false);

  try {
    if (isTerminal) {
      const result = spawnSync(
        executable,
        [...initialArgs, ...extraArgs, ...args],
        {
          stdio: 'inherit',
          shell: process.platform === 'win32',
        },
      );
      if (result.error) {
        const spawnErr = result.error as NodeJS.ErrnoException;
        coreEvents.emitFeedback(
          'error',
          spawnErr.code === 'ENOENT'
            ? `Editor command '${executable}' was not found in PATH. Install it or use /editor to choose another editor.`
            : (spawnErr.message ?? String(spawnErr)),
        );
        return;
      }
      if (typeof result.status === 'number' && result.status !== 0) {
        coreEvents.emitFeedback(
          'error',
          `External editor exited with status ${result.status}`,
        );
        return;
      }
    } else {
      await new Promise<void>((resolve) => {
        const child = spawn(
          executable,
          [...initialArgs, ...extraArgs, ...args],
          {
            stdio: 'inherit',
            shell: process.platform === 'win32',
          },
        );

        child.on('error', (err) => {
          const spawnErr = err as NodeJS.ErrnoException;
          resolve();
          coreEvents.emitFeedback(
            'error',
            spawnErr.code === 'ENOENT'
              ? `Editor command '${executable}' was not found in PATH. Install it or use /editor to choose another editor.`
              : (spawnErr.message ?? String(spawnErr)),
          );
        });

        child.on('close', (status) => {
          resolve();
          if (typeof status === 'number' && status !== 0) {
            coreEvents.emitFeedback(
              'error',
              `External editor exited with status ${status}`,
            );
          }
        });
      });
    }
  } finally {
    if (wasRaw) {
      setRawMode?.(true);
    }
    coreEvents.emit(CoreEvent.ExternalEditorClosed);
  }
}