mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-05-12 12:54:07 -07:00
refactor(editor): use const assertion for editor types with single source of truth (#8604)
This commit is contained in:
@@ -15,6 +15,7 @@ import {
|
|||||||
isNodeError,
|
isNodeError,
|
||||||
parseAndFormatApiError,
|
parseAndFormatApiError,
|
||||||
safeLiteralReplace,
|
safeLiteralReplace,
|
||||||
|
DEFAULT_GUI_EDITOR,
|
||||||
type AnyDeclarativeTool,
|
type AnyDeclarativeTool,
|
||||||
type ToolCall,
|
type ToolCall,
|
||||||
type ToolConfirmationPayload,
|
type ToolConfirmationPayload,
|
||||||
@@ -435,7 +436,7 @@ export class Task {
|
|||||||
outputUpdateHandler: this._schedulerOutputUpdate.bind(this),
|
outputUpdateHandler: this._schedulerOutputUpdate.bind(this),
|
||||||
onAllToolCallsComplete: this._schedulerAllToolCallsComplete.bind(this),
|
onAllToolCallsComplete: this._schedulerAllToolCallsComplete.bind(this),
|
||||||
onToolCallsUpdate: this._schedulerToolCallsUpdate.bind(this),
|
onToolCallsUpdate: this._schedulerToolCallsUpdate.bind(this),
|
||||||
getPreferredEditor: () => 'vscode',
|
getPreferredEditor: () => DEFAULT_GUI_EDITOR,
|
||||||
config: this.config,
|
config: this.config,
|
||||||
});
|
});
|
||||||
return scheduler;
|
return scheduler;
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import {
|
|||||||
modifyWithEditor,
|
modifyWithEditor,
|
||||||
isModifiableDeclarativeTool,
|
isModifiableDeclarativeTool,
|
||||||
} from './modifiable-tool.js';
|
} from './modifiable-tool.js';
|
||||||
import type { EditorType } from '../utils/editor.js';
|
import { DEFAULT_GUI_EDITOR } from '../utils/editor.js';
|
||||||
import fs from 'node:fs';
|
import fs from 'node:fs';
|
||||||
import fsp from 'node:fs/promises';
|
import fsp from 'node:fs/promises';
|
||||||
import os from 'node:os';
|
import os from 'node:os';
|
||||||
@@ -24,9 +24,13 @@ import { debugLogger } from '../utils/debugLogger.js';
|
|||||||
const mockOpenDiff = vi.hoisted(() => vi.fn());
|
const mockOpenDiff = vi.hoisted(() => vi.fn());
|
||||||
const mockCreatePatch = vi.hoisted(() => vi.fn());
|
const mockCreatePatch = vi.hoisted(() => vi.fn());
|
||||||
|
|
||||||
vi.mock('../utils/editor.js', () => ({
|
vi.mock('../utils/editor.js', async (importOriginal) => {
|
||||||
openDiff: mockOpenDiff,
|
const actual = await importOriginal<typeof import('../utils/editor.js')>();
|
||||||
}));
|
return {
|
||||||
|
...actual,
|
||||||
|
openDiff: mockOpenDiff,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
vi.mock('diff', () => ({
|
vi.mock('diff', () => ({
|
||||||
createPatch: mockCreatePatch,
|
createPatch: mockCreatePatch,
|
||||||
@@ -103,7 +107,7 @@ describe('modifyWithEditor', () => {
|
|||||||
const result = await modifyWithEditor(
|
const result = await modifyWithEditor(
|
||||||
mockParams,
|
mockParams,
|
||||||
mockModifyContext,
|
mockModifyContext,
|
||||||
'vscode' as EditorType,
|
DEFAULT_GUI_EDITOR,
|
||||||
abortSignal,
|
abortSignal,
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -169,9 +173,14 @@ describe('modifyWithEditor', () => {
|
|||||||
await modifyWithEditor(
|
await modifyWithEditor(
|
||||||
mockParams,
|
mockParams,
|
||||||
mockModifyContext,
|
mockModifyContext,
|
||||||
'vscode' as EditorType,
|
DEFAULT_GUI_EDITOR,
|
||||||
abortSignal,
|
abortSignal,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const [oldFilePath] = mockOpenDiff.mock.calls[0];
|
||||||
|
const diffDir = path.dirname(oldFilePath);
|
||||||
|
// Temp directory should be cleaned up after modification
|
||||||
|
await expect(fsp.stat(diffDir)).rejects.toThrow();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -184,7 +193,7 @@ describe('modifyWithEditor', () => {
|
|||||||
const result = await modifyWithEditor(
|
const result = await modifyWithEditor(
|
||||||
mockParams,
|
mockParams,
|
||||||
mockModifyContext,
|
mockModifyContext,
|
||||||
'vscode' as EditorType,
|
DEFAULT_GUI_EDITOR,
|
||||||
abortSignal,
|
abortSignal,
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -212,7 +221,7 @@ describe('modifyWithEditor', () => {
|
|||||||
const result = await modifyWithEditor(
|
const result = await modifyWithEditor(
|
||||||
mockParams,
|
mockParams,
|
||||||
mockModifyContext,
|
mockModifyContext,
|
||||||
'vscode' as EditorType,
|
DEFAULT_GUI_EDITOR,
|
||||||
abortSignal,
|
abortSignal,
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -241,7 +250,7 @@ describe('modifyWithEditor', () => {
|
|||||||
await modifyWithEditor(
|
await modifyWithEditor(
|
||||||
mockParams,
|
mockParams,
|
||||||
mockModifyContext,
|
mockModifyContext,
|
||||||
'vscode' as EditorType,
|
DEFAULT_GUI_EDITOR,
|
||||||
abortSignal,
|
abortSignal,
|
||||||
{
|
{
|
||||||
currentContent: overrideCurrent,
|
currentContent: overrideCurrent,
|
||||||
@@ -268,7 +277,7 @@ describe('modifyWithEditor', () => {
|
|||||||
await modifyWithEditor(
|
await modifyWithEditor(
|
||||||
mockParams,
|
mockParams,
|
||||||
mockModifyContext,
|
mockModifyContext,
|
||||||
'vscode' as EditorType,
|
DEFAULT_GUI_EDITOR,
|
||||||
abortSignal,
|
abortSignal,
|
||||||
{
|
{
|
||||||
currentContent: null,
|
currentContent: null,
|
||||||
@@ -298,7 +307,7 @@ describe('modifyWithEditor', () => {
|
|||||||
modifyWithEditor(
|
modifyWithEditor(
|
||||||
mockParams,
|
mockParams,
|
||||||
mockModifyContext,
|
mockModifyContext,
|
||||||
'vscode' as EditorType,
|
DEFAULT_GUI_EDITOR,
|
||||||
abortSignal,
|
abortSignal,
|
||||||
),
|
),
|
||||||
).rejects.toThrow('Editor failed to open');
|
).rejects.toThrow('Editor failed to open');
|
||||||
@@ -327,7 +336,7 @@ describe('modifyWithEditor', () => {
|
|||||||
await modifyWithEditor(
|
await modifyWithEditor(
|
||||||
mockParams,
|
mockParams,
|
||||||
mockModifyContext,
|
mockModifyContext,
|
||||||
'vscode' as EditorType,
|
DEFAULT_GUI_EDITOR,
|
||||||
abortSignal,
|
abortSignal,
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -353,7 +362,7 @@ describe('modifyWithEditor', () => {
|
|||||||
await modifyWithEditor(
|
await modifyWithEditor(
|
||||||
mockParams,
|
mockParams,
|
||||||
mockModifyContext,
|
mockModifyContext,
|
||||||
'vscode' as EditorType,
|
DEFAULT_GUI_EDITOR,
|
||||||
abortSignal,
|
abortSignal,
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -374,7 +383,7 @@ describe('modifyWithEditor', () => {
|
|||||||
await modifyWithEditor(
|
await modifyWithEditor(
|
||||||
mockParams,
|
mockParams,
|
||||||
mockModifyContext,
|
mockModifyContext,
|
||||||
'vscode' as EditorType,
|
DEFAULT_GUI_EDITOR,
|
||||||
abortSignal,
|
abortSignal,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -8,16 +8,36 @@ import { execSync, spawn, spawnSync } from 'node:child_process';
|
|||||||
import { debugLogger } from './debugLogger.js';
|
import { debugLogger } from './debugLogger.js';
|
||||||
import { coreEvents, CoreEvent } from './events.js';
|
import { coreEvents, CoreEvent } from './events.js';
|
||||||
|
|
||||||
export type EditorType =
|
const GUI_EDITORS = [
|
||||||
| 'vscode'
|
'vscode',
|
||||||
| 'vscodium'
|
'vscodium',
|
||||||
| 'windsurf'
|
'windsurf',
|
||||||
| 'cursor'
|
'cursor',
|
||||||
| 'vim'
|
'zed',
|
||||||
| 'neovim'
|
'antigravity',
|
||||||
| 'zed'
|
] as const;
|
||||||
| 'emacs'
|
const TERMINAL_EDITORS = ['vim', 'neovim', 'emacs'] as const;
|
||||||
| 'antigravity';
|
const EDITORS = [...GUI_EDITORS, ...TERMINAL_EDITORS] as const;
|
||||||
|
|
||||||
|
const GUI_EDITORS_SET = new Set<string>(GUI_EDITORS);
|
||||||
|
const TERMINAL_EDITORS_SET = new Set<string>(TERMINAL_EDITORS);
|
||||||
|
const EDITORS_SET = new Set<string>(EDITORS);
|
||||||
|
|
||||||
|
export const DEFAULT_GUI_EDITOR: GuiEditorType = 'vscode';
|
||||||
|
|
||||||
|
export type GuiEditorType = (typeof GUI_EDITORS)[number];
|
||||||
|
export type TerminalEditorType = (typeof TERMINAL_EDITORS)[number];
|
||||||
|
export type EditorType = (typeof EDITORS)[number];
|
||||||
|
|
||||||
|
export function isGuiEditor(editor: EditorType): editor is GuiEditorType {
|
||||||
|
return GUI_EDITORS_SET.has(editor);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isTerminalEditor(
|
||||||
|
editor: EditorType,
|
||||||
|
): editor is TerminalEditorType {
|
||||||
|
return TERMINAL_EDITORS_SET.has(editor);
|
||||||
|
}
|
||||||
|
|
||||||
export const EDITOR_DISPLAY_NAMES: Record<EditorType, string> = {
|
export const EDITOR_DISPLAY_NAMES: Record<EditorType, string> = {
|
||||||
vscode: 'VS Code',
|
vscode: 'VS Code',
|
||||||
@@ -36,17 +56,7 @@ export function getEditorDisplayName(editor: EditorType): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function isValidEditorType(editor: string): editor is EditorType {
|
function isValidEditorType(editor: string): editor is EditorType {
|
||||||
return [
|
return EDITORS_SET.has(editor);
|
||||||
'vscode',
|
|
||||||
'vscodium',
|
|
||||||
'windsurf',
|
|
||||||
'cursor',
|
|
||||||
'vim',
|
|
||||||
'neovim',
|
|
||||||
'zed',
|
|
||||||
'emacs',
|
|
||||||
'antigravity',
|
|
||||||
].includes(editor);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface DiffCommand {
|
interface DiffCommand {
|
||||||
@@ -94,11 +104,7 @@ export function checkHasEditorType(editor: EditorType): boolean {
|
|||||||
|
|
||||||
export function allowEditorTypeInSandbox(editor: EditorType): boolean {
|
export function allowEditorTypeInSandbox(editor: EditorType): boolean {
|
||||||
const notUsingSandbox = !process.env['SANDBOX'];
|
const notUsingSandbox = !process.env['SANDBOX'];
|
||||||
if (
|
if (isGuiEditor(editor)) {
|
||||||
['vscode', 'vscodium', 'windsurf', 'cursor', 'zed', 'antigravity'].includes(
|
|
||||||
editor,
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
return notUsingSandbox;
|
return notUsingSandbox;
|
||||||
}
|
}
|
||||||
// For terminal-based editors like vim and emacs, allow in sandbox.
|
// For terminal-based editors like vim and emacs, allow in sandbox.
|
||||||
@@ -197,9 +203,7 @@ export async function openDiff(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const isTerminalEditor = ['vim', 'emacs', 'neovim'].includes(editor);
|
if (isTerminalEditor(editor)) {
|
||||||
|
|
||||||
if (isTerminalEditor) {
|
|
||||||
try {
|
try {
|
||||||
const result = spawnSync(diffCommand.command, diffCommand.args, {
|
const result = spawnSync(diffCommand.command, diffCommand.args, {
|
||||||
stdio: 'inherit',
|
stdio: 'inherit',
|
||||||
|
|||||||
Reference in New Issue
Block a user