| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import { promises as fs } from 'node:fs'; |
| import * as os from 'node:os'; |
| import * as path from 'node:path'; |
| import { exec } from 'node:child_process'; |
| import { promisify } from 'node:util'; |
| import { isKittyProtocolEnabled } from './kittyProtocolDetector.js'; |
| import { VSCODE_SHIFT_ENTER_SEQUENCE } from './platformConstants.js'; |
|
|
| const execAsync = promisify(exec); |
|
|
| |
| |
| |
| |
| function stripJsonComments(content: string): string { |
| |
| return content.replace(/^\s*\/\/.*$/gm, ''); |
| } |
|
|
| export interface TerminalSetupResult { |
| success: boolean; |
| message: string; |
| requiresRestart?: boolean; |
| } |
|
|
| type SupportedTerminal = 'vscode' | 'cursor' | 'windsurf'; |
|
|
| |
| async function detectTerminal(): Promise<SupportedTerminal | null> { |
| const termProgram = process.env['TERM_PROGRAM']; |
|
|
| |
| |
| if ( |
| process.env['CURSOR_TRACE_ID'] || |
| process.env['VSCODE_GIT_ASKPASS_MAIN']?.toLowerCase().includes('cursor') |
| ) { |
| return 'cursor'; |
| } |
| |
| if ( |
| process.env['VSCODE_GIT_ASKPASS_MAIN']?.toLowerCase().includes('windsurf') |
| ) { |
| return 'windsurf'; |
| } |
| |
| if (termProgram === 'vscode' || process.env['VSCODE_GIT_IPC_HANDLE']) { |
| return 'vscode'; |
| } |
|
|
| |
| if (os.platform() !== 'win32') { |
| try { |
| const { stdout } = await execAsync('ps -o comm= -p $PPID'); |
| const parentName = stdout.trim(); |
|
|
| |
| if (parentName.includes('windsurf') || parentName.includes('Windsurf')) |
| return 'windsurf'; |
| if (parentName.includes('cursor') || parentName.includes('Cursor')) |
| return 'cursor'; |
| if (parentName.includes('code') || parentName.includes('Code')) |
| return 'vscode'; |
| } catch (error) { |
| |
| console.debug('Parent process detection failed:', error); |
| } |
| } |
|
|
| return null; |
| } |
|
|
| |
| async function backupFile(filePath: string): Promise<void> { |
| try { |
| const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); |
| const backupPath = `${filePath}.backup.${timestamp}`; |
| await fs.copyFile(filePath, backupPath); |
| } catch (error) { |
| |
| console.warn(`Failed to create backup of ${filePath}:`, error); |
| } |
| } |
|
|
| |
| function getVSCodeStyleConfigDir(appName: string): string | null { |
| const platform = os.platform(); |
|
|
| if (platform === 'darwin') { |
| return path.join( |
| os.homedir(), |
| 'Library', |
| 'Application Support', |
| appName, |
| 'User', |
| ); |
| } else if (platform === 'win32') { |
| if (!process.env['APPDATA']) { |
| return null; |
| } |
| return path.join(process.env['APPDATA'], appName, 'User'); |
| } else { |
| return path.join(os.homedir(), '.config', appName, 'User'); |
| } |
| } |
|
|
| |
| async function configureVSCodeStyle( |
| terminalName: string, |
| appName: string, |
| ): Promise<TerminalSetupResult> { |
| const configDir = getVSCodeStyleConfigDir(appName); |
|
|
| if (!configDir) { |
| return { |
| success: false, |
| message: `Could not determine ${terminalName} config path on Windows: APPDATA environment variable is not set.`, |
| }; |
| } |
|
|
| const keybindingsFile = path.join(configDir, 'keybindings.json'); |
|
|
| try { |
| await fs.mkdir(configDir, { recursive: true }); |
|
|
| let keybindings: unknown[] = []; |
| try { |
| const content = await fs.readFile(keybindingsFile, 'utf8'); |
| await backupFile(keybindingsFile); |
| try { |
| const cleanContent = stripJsonComments(content); |
| const parsedContent = JSON.parse(cleanContent); |
| if (!Array.isArray(parsedContent)) { |
| return { |
| success: false, |
| message: |
| `${terminalName} keybindings.json exists but is not a valid JSON array. ` + |
| `Please fix the file manually or delete it to allow automatic configuration.\n` + |
| `File: ${keybindingsFile}`, |
| }; |
| } |
| keybindings = parsedContent; |
| } catch (parseError) { |
| return { |
| success: false, |
| message: |
| `Failed to parse ${terminalName} keybindings.json. The file contains invalid JSON.\n` + |
| `Please fix the file manually or delete it to allow automatic configuration.\n` + |
| `File: ${keybindingsFile}\n` + |
| `Error: ${parseError}`, |
| }; |
| } |
| } catch { |
| |
| } |
|
|
| const shiftEnterBinding = { |
| key: 'shift+enter', |
| command: 'workbench.action.terminal.sendSequence', |
| when: 'terminalFocus', |
| args: { text: VSCODE_SHIFT_ENTER_SEQUENCE }, |
| }; |
|
|
| const ctrlEnterBinding = { |
| key: 'ctrl+enter', |
| command: 'workbench.action.terminal.sendSequence', |
| when: 'terminalFocus', |
| args: { text: VSCODE_SHIFT_ENTER_SEQUENCE }, |
| }; |
|
|
| |
| const existingShiftEnter = keybindings.find((kb) => { |
| const binding = kb as { key?: string }; |
| return binding.key === 'shift+enter'; |
| }); |
|
|
| const existingCtrlEnter = keybindings.find((kb) => { |
| const binding = kb as { key?: string }; |
| return binding.key === 'ctrl+enter'; |
| }); |
|
|
| if (existingShiftEnter || existingCtrlEnter) { |
| const messages: string[] = []; |
| if (existingShiftEnter) { |
| messages.push(`- Shift+Enter binding already exists`); |
| } |
| if (existingCtrlEnter) { |
| messages.push(`- Ctrl+Enter binding already exists`); |
| } |
| return { |
| success: false, |
| message: |
| `Existing keybindings detected. Will not modify to avoid conflicts.\n` + |
| messages.join('\n') + |
| '\n' + |
| `Please check and modify manually if needed: ${keybindingsFile}`, |
| }; |
| } |
|
|
| |
| const hasOurShiftEnter = keybindings.some((kb) => { |
| const binding = kb as { |
| command?: string; |
| args?: { text?: string }; |
| key?: string; |
| }; |
| return ( |
| binding.key === 'shift+enter' && |
| binding.command === 'workbench.action.terminal.sendSequence' && |
| binding.args?.text === '\\\r\n' |
| ); |
| }); |
|
|
| const hasOurCtrlEnter = keybindings.some((kb) => { |
| const binding = kb as { |
| command?: string; |
| args?: { text?: string }; |
| key?: string; |
| }; |
| return ( |
| binding.key === 'ctrl+enter' && |
| binding.command === 'workbench.action.terminal.sendSequence' && |
| binding.args?.text === '\\\r\n' |
| ); |
| }); |
|
|
| if (!hasOurShiftEnter || !hasOurCtrlEnter) { |
| if (!hasOurShiftEnter) keybindings.unshift(shiftEnterBinding); |
| if (!hasOurCtrlEnter) keybindings.unshift(ctrlEnterBinding); |
|
|
| await fs.writeFile(keybindingsFile, JSON.stringify(keybindings, null, 4)); |
| return { |
| success: true, |
| message: `Added Shift+Enter and Ctrl+Enter keybindings to ${terminalName}.\nModified: ${keybindingsFile}`, |
| requiresRestart: true, |
| }; |
| } else { |
| return { |
| success: true, |
| message: `${terminalName} keybindings already configured.`, |
| }; |
| } |
| } catch (error) { |
| return { |
| success: false, |
| message: `Failed to configure ${terminalName}.\nFile: ${keybindingsFile}\nError: ${error}`, |
| }; |
| } |
| } |
|
|
| |
|
|
| async function configureVSCode(): Promise<TerminalSetupResult> { |
| return configureVSCodeStyle('VS Code', 'Code'); |
| } |
|
|
| async function configureCursor(): Promise<TerminalSetupResult> { |
| return configureVSCodeStyle('Cursor', 'Cursor'); |
| } |
|
|
| async function configureWindsurf(): Promise<TerminalSetupResult> { |
| return configureVSCodeStyle('Windsurf', 'Windsurf'); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export async function terminalSetup(): Promise<TerminalSetupResult> { |
| |
| if (isKittyProtocolEnabled()) { |
| return { |
| success: true, |
| message: |
| 'Your terminal is already configured for an optimal experience with multiline input (Shift+Enter and Ctrl+Enter).', |
| }; |
| } |
|
|
| const terminal = await detectTerminal(); |
|
|
| if (!terminal) { |
| return { |
| success: false, |
| message: |
| 'Could not detect terminal type. Supported terminals: VS Code, Cursor, and Windsurf.', |
| }; |
| } |
|
|
| switch (terminal) { |
| case 'vscode': |
| return configureVSCode(); |
| case 'cursor': |
| return configureCursor(); |
| case 'windsurf': |
| return configureWindsurf(); |
| default: |
| return { |
| success: false, |
| message: `Terminal "${terminal}" is not supported yet.`, |
| }; |
| } |
| } |
|
|