import * as vscode from 'vscode'; import * as child_process from 'child_process'; import * as path from 'path'; export function activate(context: vscode.ExtensionContext) { console.log('CNNL Language Support is now active'); // 注册编译命令 const compileCommand = vscode.commands.registerCommand('cnnl.compile', async () => { const editor = vscode.window.activeTextEditor; if (!editor) { vscode.window.showErrorMessage('No active editor'); return; } const document = editor.document; if (document.languageId !== 'cnnl') { vscode.window.showErrorMessage('Current file is not a CNNL file'); return; } // 保存文件 await document.save(); const config = vscode.workspace.getConfiguration('cnnl'); const compilerPath = config.get('compilerPath', 'cnnl'); const enableVerification = config.get('enableVerification', true); const filePath = document.uri.fsPath; const outputDir = path.join(path.dirname(filePath), 'output'); // 构建编译命令 const args = [filePath, '--output', outputDir]; if (enableVerification) { args.push('--verify'); } // 显示输出通道 const outputChannel = vscode.window.createOutputChannel('CNNL Compiler'); outputChannel.show(); outputChannel.appendLine(`Compiling: ${filePath}`); outputChannel.appendLine(`Command: ${compilerPath} ${args.join(' ')}`); // 执行编译 try { const result = child_process.spawnSync(compilerPath, args, { encoding: 'utf8', cwd: path.dirname(filePath) }); if (result.error) { outputChannel.appendLine(`Error: ${result.error.message}`); vscode.window.showErrorMessage(`Compilation failed: ${result.error.message}`); return; } outputChannel.appendLine(result.stdout); if (result.stderr) { outputChannel.appendLine(result.stderr); } if (result.status === 0) { vscode.window.showInformationMessage('Compilation successful!'); } else { vscode.window.showErrorMessage(`Compilation failed with exit code ${result.status}`); } } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); outputChannel.appendLine(`Exception: ${errorMessage}`); vscode.window.showErrorMessage(`Compilation error: ${errorMessage}`); } }); // 注册验证命令 const verifyCommand = vscode.commands.registerCommand('cnnl.verify', async () => { const editor = vscode.window.activeTextEditor; if (!editor) { vscode.window.showErrorMessage('No active editor'); return; } const document = editor.document; if (document.languageId !== 'cnnl') { vscode.window.showErrorMessage('Current file is not a CNNL file'); return; } await document.save(); const config = vscode.workspace.getConfiguration('cnnl'); const compilerPath = config.get('compilerPath', 'cnnl'); const filePath = document.uri.fsPath; const outputDir = path.join(path.dirname(filePath), 'output'); const outputChannel = vscode.window.createOutputChannel('CNNL Verification'); outputChannel.show(); outputChannel.appendLine(`Verifying: ${filePath}`); try { const result = child_process.spawnSync(compilerPath, [filePath, '--output', outputDir, '--verify'], { encoding: 'utf8', cwd: path.dirname(filePath) }); if (result.error) { outputChannel.appendLine(`Error: ${result.error.message}`); vscode.window.showErrorMessage(`Verification failed: ${result.error.message}`); return; } outputChannel.appendLine(result.stdout); if (result.stderr) { outputChannel.appendLine(result.stderr); } if (result.status === 0) { vscode.window.showInformationMessage('Verification successful!'); } else { vscode.window.showErrorMessage(`Verification failed with exit code ${result.status}`); } } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); outputChannel.appendLine(`Exception: ${errorMessage}`); vscode.window.showErrorMessage(`Verification error: ${errorMessage}`); } }); context.subscriptions.push(compileCommand); context.subscriptions.push(verifyCommand); } export function deactivate() { console.log('CNNL Language Support is now deactivated'); }