Add Zed Editor to Eidtor List (#1372)

This commit is contained in:
Scott Densmore 2025-06-23 23:32:09 -07:00 committed by GitHub
parent 9f5a625730
commit 324715ee8b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 144 additions and 110 deletions

View File

@ -17,6 +17,7 @@ export interface EditorDisplay {
} }
export const EDITOR_DISPLAY_NAMES: Record<EditorType, string> = { export const EDITOR_DISPLAY_NAMES: Record<EditorType, string> = {
zed: 'Zed',
vscode: 'VS Code', vscode: 'VS Code',
windsurf: 'Windsurf', windsurf: 'Windsurf',
cursor: 'Cursor', cursor: 'Cursor',
@ -27,7 +28,13 @@ class EditorSettingsManager {
private readonly availableEditors: EditorDisplay[]; private readonly availableEditors: EditorDisplay[];
constructor() { constructor() {
const editorTypes: EditorType[] = ['vscode', 'windsurf', 'cursor', 'vim']; const editorTypes: EditorType[] = [
'zed',
'vscode',
'windsurf',
'cursor',
'vim',
];
this.availableEditors = [ this.availableEditors = [
{ {
name: 'None', name: 'None',

View File

@ -59,6 +59,7 @@ describe('editor utils', () => {
{ editor: 'windsurf', command: 'windsurf', win32Command: 'windsurf' }, { editor: 'windsurf', command: 'windsurf', win32Command: 'windsurf' },
{ editor: 'cursor', command: 'cursor', win32Command: 'cursor' }, { editor: 'cursor', command: 'cursor', win32Command: 'cursor' },
{ editor: 'vim', command: 'vim', win32Command: 'vim' }, { editor: 'vim', command: 'vim', win32Command: 'vim' },
{ editor: 'zed', command: 'zed', win32Command: 'zed' },
]; ];
for (const { editor, command, win32Command } of testCases) { for (const { editor, command, win32Command } of testCases) {
@ -105,29 +106,36 @@ describe('editor utils', () => {
}); });
describe('getDiffCommand', () => { describe('getDiffCommand', () => {
it('should return the correct command for vscode', () => { const guiEditors: Array<{
const command = getDiffCommand('old.txt', 'new.txt', 'vscode'); editor: EditorType;
expect(command).toEqual({ command: string;
command: 'code', win32Command: string;
}> = [
{ editor: 'vscode', command: 'code', win32Command: 'code.cmd' },
{ editor: 'windsurf', command: 'windsurf', win32Command: 'windsurf' },
{ editor: 'cursor', command: 'cursor', win32Command: 'cursor' },
{ editor: 'zed', command: 'zed', win32Command: 'zed' },
];
for (const { editor, command, win32Command } of guiEditors) {
it(`should return the correct command for ${editor} on non-windows`, () => {
Object.defineProperty(process, 'platform', { value: 'linux' });
const diffCommand = getDiffCommand('old.txt', 'new.txt', editor);
expect(diffCommand).toEqual({
command,
args: ['--wait', '--diff', 'old.txt', 'new.txt'], args: ['--wait', '--diff', 'old.txt', 'new.txt'],
}); });
}); });
it('should return the correct command for windsurf', () => { it(`should return the correct command for ${editor} on windows`, () => {
const command = getDiffCommand('old.txt', 'new.txt', 'windsurf'); Object.defineProperty(process, 'platform', { value: 'win32' });
expect(command).toEqual({ const diffCommand = getDiffCommand('old.txt', 'new.txt', editor);
command: 'windsurf', expect(diffCommand).toEqual({
args: ['--wait', '--diff', 'old.txt', 'new.txt'], command: win32Command,
});
});
it('should return the correct command for cursor', () => {
const command = getDiffCommand('old.txt', 'new.txt', 'cursor');
expect(command).toEqual({
command: 'cursor',
args: ['--wait', '--diff', 'old.txt', 'new.txt'], args: ['--wait', '--diff', 'old.txt', 'new.txt'],
}); });
}); });
}
it('should return the correct command for vim', () => { it('should return the correct command for vim', () => {
const command = getDiffCommand('old.txt', 'new.txt', 'vim'); const command = getDiffCommand('old.txt', 'new.txt', 'vim');
@ -163,7 +171,9 @@ describe('editor utils', () => {
}); });
describe('openDiff', () => { describe('openDiff', () => {
it('should call spawn for vscode', async () => { const spawnEditors: EditorType[] = ['vscode', 'windsurf', 'cursor', 'zed'];
for (const editor of spawnEditors) {
it(`should call spawn for ${editor}`, async () => {
const mockSpawn = { const mockSpawn = {
on: vi.fn((event, cb) => { on: vi.fn((event, cb) => {
if (event === 'close') { if (event === 'close') {
@ -172,17 +182,26 @@ describe('editor utils', () => {
}), }),
}; };
(spawn as Mock).mockReturnValue(mockSpawn); (spawn as Mock).mockReturnValue(mockSpawn);
await openDiff('old.txt', 'new.txt', 'vscode'); await openDiff('old.txt', 'new.txt', editor);
const diffCommand = getDiffCommand('old.txt', 'new.txt', editor)!;
expect(spawn).toHaveBeenCalledWith( expect(spawn).toHaveBeenCalledWith(
'code', diffCommand.command,
['--wait', '--diff', 'old.txt', 'new.txt'], diffCommand.args,
{ stdio: 'inherit' }, {
stdio: 'inherit',
},
);
expect(mockSpawn.on).toHaveBeenCalledWith(
'close',
expect.any(Function),
);
expect(mockSpawn.on).toHaveBeenCalledWith(
'error',
expect.any(Function),
); );
expect(mockSpawn.on).toHaveBeenCalledWith('close', expect.any(Function));
expect(mockSpawn.on).toHaveBeenCalledWith('error', expect.any(Function));
}); });
it('should reject if spawn for vscode fails', async () => { it(`should reject if spawn for ${editor} fails`, async () => {
const mockError = new Error('spawn error'); const mockError = new Error('spawn error');
const mockSpawn = { const mockSpawn = {
on: vi.fn((event, cb) => { on: vi.fn((event, cb) => {
@ -192,12 +211,12 @@ describe('editor utils', () => {
}), }),
}; };
(spawn as Mock).mockReturnValue(mockSpawn); (spawn as Mock).mockReturnValue(mockSpawn);
await expect(openDiff('old.txt', 'new.txt', 'vscode')).rejects.toThrow( await expect(openDiff('old.txt', 'new.txt', editor)).rejects.toThrow(
'spawn error', 'spawn error',
); );
}); });
it('should reject if vscode exits with non-zero code', async () => { it(`should reject if ${editor} exits with non-zero code`, async () => {
const mockSpawn = { const mockSpawn = {
on: vi.fn((event, cb) => { on: vi.fn((event, cb) => {
if (event === 'close') { if (event === 'close') {
@ -206,12 +225,13 @@ describe('editor utils', () => {
}), }),
}; };
(spawn as Mock).mockReturnValue(mockSpawn); (spawn as Mock).mockReturnValue(mockSpawn);
await expect(openDiff('old.txt', 'new.txt', 'vscode')).rejects.toThrow( await expect(openDiff('old.txt', 'new.txt', editor)).rejects.toThrow(
'VS Code exited with code 1', `${editor} exited with code 1`,
); );
}); });
}
const execSyncEditors: EditorType[] = ['vim', 'windsurf', 'cursor']; const execSyncEditors: EditorType[] = ['vim'];
for (const editor of execSyncEditors) { for (const editor of execSyncEditors) {
it(`should call execSync for ${editor} on non-windows`, async () => { it(`should call execSync for ${editor} on non-windows`, async () => {
Object.defineProperty(process, 'platform', { value: 'linux' }); Object.defineProperty(process, 'platform', { value: 'linux' });
@ -249,7 +269,7 @@ describe('editor utils', () => {
// @ts-expect-error Testing unsupported editor // @ts-expect-error Testing unsupported editor
await openDiff('old.txt', 'new.txt', 'foobar'); await openDiff('old.txt', 'new.txt', 'foobar');
expect(consoleErrorSpy).toHaveBeenCalledWith( expect(consoleErrorSpy).toHaveBeenCalledWith(
'No diff tool available. Install vim or vscode.', 'No diff tool available. Install a supported editor.',
); );
}); });
}); });
@ -264,7 +284,7 @@ describe('editor utils', () => {
expect(allowEditorTypeInSandbox('vim')).toBe(true); expect(allowEditorTypeInSandbox('vim')).toBe(true);
}); });
const guiEditors: EditorType[] = ['vscode', 'windsurf', 'cursor']; const guiEditors: EditorType[] = ['vscode', 'windsurf', 'cursor', 'zed'];
for (const editor of guiEditors) { for (const editor of guiEditors) {
it(`should not allow ${editor} in sandbox mode`, () => { it(`should not allow ${editor} in sandbox mode`, () => {
process.env.SANDBOX = 'sandbox'; process.env.SANDBOX = 'sandbox';

View File

@ -6,10 +6,10 @@
import { execSync, spawn } from 'child_process'; import { execSync, spawn } from 'child_process';
export type EditorType = 'vscode' | 'windsurf' | 'cursor' | 'vim'; export type EditorType = 'vscode' | 'windsurf' | 'cursor' | 'vim' | 'zed';
function isValidEditorType(editor: string): editor is EditorType { function isValidEditorType(editor: string): editor is EditorType {
return ['vscode', 'windsurf', 'cursor', 'vim'].includes(editor); return ['vscode', 'windsurf', 'cursor', 'vim', 'zed'].includes(editor);
} }
interface DiffCommand { interface DiffCommand {
@ -34,6 +34,7 @@ const editorCommands: Record<EditorType, { win32: string; default: string }> = {
windsurf: { win32: 'windsurf', default: 'windsurf' }, windsurf: { win32: 'windsurf', default: 'windsurf' },
cursor: { win32: 'cursor', default: 'cursor' }, cursor: { win32: 'cursor', default: 'cursor' },
vim: { win32: 'vim', default: 'vim' }, vim: { win32: 'vim', default: 'vim' },
zed: { win32: 'zed', default: 'zed' },
}; };
export function checkHasEditorType(editor: EditorType): boolean { export function checkHasEditorType(editor: EditorType): boolean {
@ -45,7 +46,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 (['vscode', 'windsurf', 'cursor'].includes(editor)) { if (['vscode', 'windsurf', 'cursor', 'zed'].includes(editor)) {
return notUsingSandbox; return notUsingSandbox;
} }
return true; return true;
@ -73,22 +74,18 @@ export function getDiffCommand(
newPath: string, newPath: string,
editor: EditorType, editor: EditorType,
): DiffCommand | null { ): DiffCommand | null {
if (!isValidEditorType(editor)) {
return null;
}
const commandConfig = editorCommands[editor];
const command =
process.platform === 'win32' ? commandConfig.win32 : commandConfig.default;
switch (editor) { switch (editor) {
case 'vscode': case 'vscode':
return {
command: 'code',
args: ['--wait', '--diff', oldPath, newPath],
};
case 'windsurf': case 'windsurf':
return {
command: 'windsurf',
args: ['--wait', '--diff', oldPath, newPath],
};
case 'cursor': case 'cursor':
return { case 'zed':
command: 'cursor', return { command, args: ['--wait', '--diff', oldPath, newPath] };
args: ['--wait', '--diff', oldPath, newPath],
};
case 'vim': case 'vim':
return { return {
command: 'vim', command: 'vim',
@ -134,32 +131,37 @@ export async function openDiff(
): Promise<void> { ): Promise<void> {
const diffCommand = getDiffCommand(oldPath, newPath, editor); const diffCommand = getDiffCommand(oldPath, newPath, editor);
if (!diffCommand) { if (!diffCommand) {
console.error('No diff tool available. Install vim or vscode.'); console.error('No diff tool available. Install a supported editor.');
return; return;
} }
try { try {
if (editor === 'vscode') { switch (editor) {
// Use spawn to avoid blocking the entire process, resolve this function when editor is closed. case 'vscode':
case 'windsurf':
case 'cursor':
case 'zed':
// Use spawn for GUI-based editors to avoid blocking the entire process
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const process = spawn(diffCommand.command, diffCommand.args, { const childProcess = spawn(diffCommand.command, diffCommand.args, {
stdio: 'inherit', stdio: 'inherit',
}); });
process.on('close', (code) => { childProcess.on('close', (code) => {
if (code === 0) { if (code === 0) {
resolve(); resolve();
} else { } else {
reject(new Error(`VS Code exited with code ${code}`)); reject(new Error(`${editor} exited with code ${code}`));
} }
}); });
process.on('error', (error) => { childProcess.on('error', (error) => {
reject(error); reject(error);
}); });
}); });
} else {
// Use execSync for terminal-based editors like vim case 'vim': {
// Use execSync for terminal-based editors
const command = const command =
process.platform === 'win32' process.platform === 'win32'
? `${diffCommand.command} ${diffCommand.args.join(' ')}` ? `${diffCommand.command} ${diffCommand.args.join(' ')}`
@ -168,6 +170,11 @@ export async function openDiff(
stdio: 'inherit', stdio: 'inherit',
encoding: 'utf8', encoding: 'utf8',
}); });
break;
}
default:
throw new Error(`Unsupported editor: ${editor}`);
} }
} catch (error) { } catch (error) {
console.error(error); console.error(error);