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
3 changed files with 144 additions and 110 deletions

View File

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