const vscode = require('vscode'); const fs = require('fs'); const path = require('path'); const io = require('socket.io-client'); let debounceTimeout; let vs_socket= null; let config = {}; // 发送数据到 Flask 服务器,并携带 session 作为 Cookie async function sendToServer(data) { try { if (!vs_socket) { return; } // const response = await axios.post('http://localhost:5000/vscode_data', data, { // }); // console.log('Data sent to Flask server:', response.data); vs_socket.emit('message', data) } catch (error) { console.error('Error sending data to Flask server:', error); } } // Reads and parses key-value pairs from the .config file function readConfigFile(folderPath) { const configFilePath = path.join(folderPath, '.config'); if (!fs.existsSync(configFilePath)) { vscode.window.showErrorMessage('No .config file found in the workspace folder.'); return null; } try { const configContent = fs.readFileSync(configFilePath, 'utf-8'); return JSON.parse(configContent); } catch (error) { console.error('Error reading the .config file:', error); return null; } } function establishSocketConnection(){ vscode.window.showInformationMessage('Connecting to server...'); vs_socket = io('ws://59.78.194.131:5000/vscode'); vs_socket.on('connect', () => { vscode.window.showInformationMessage('Connected to server'); vs_socket.emit('login', { config: config }); }); vs_socket.on('disconnect', () => { vscode.window.showInformationMessage('Disconnected from server'); }); vs_socket.on('terminal',(data)=>{ vscode.window.showInformationMessage('Terminal command received'+data); const terminal = vscode.window.createTerminal('New Terminal'); // 在终端中执行命令 terminal.sendText(data.data); // 显示终端 terminal.show(); }) } // 激活插件时调用此函数 function activate(context) { const workspaceFolders = vscode.workspace.workspaceFolders; if (!workspaceFolders) { vscode.window.showErrorMessage('No workspace folder is open.'); return; } const folderPath = workspaceFolders[0].uri.fsPath; config = readConfigFile(folderPath); if (config) { console.log('Config file loaded:', config); establishSocketConnection(); } vscode.window.onDidChangeActiveTextEditor(editor => { if (editor) { const filePath = editor.document.uri.fsPath; console.log(`Current open file: ${filePath}`); sendToServer({ type: 'activeFile', filePath: filePath, config: config, }); } }); // 监听文件内容变化 vscode.workspace.onDidChangeTextDocument(event => { const filePath = event.document.uri.fsPath; const changes = event.contentChanges; // 检查是否有大段文本插入,判断为粘贴操作 changes.forEach(change => { if (change.text.length > 20 && !change.rangeLength) { // 设置长度阈值为20字符,且非删除操作 console.log(`Detected paste action in file ${filePath}. Sending content...`); sendToServer({ type: 'paste', filePath: filePath, content: change.text, config: config, }); } }); // 防抖处理非粘贴的编辑操作 const content = event.document.getText(); debounceSendToServer({ type: 'fileEdit', filePath: filePath, content: content, config: config, }, 5000); }); // 监听工作区的变化 vscode.workspace.onDidChangeWorkspaceFolders(event => { console.log('Workspace folders changed'); const workspaceFolders = vscode.workspace.workspaceFolders; if (workspaceFolders) { workspaceFolders.forEach(folder => { const fileTree = getFileTree(folder.uri); console.log(`File Tree for ${folder.uri.fsPath}:`, fileTree); sendToServer({ type: 'workspaceFolders', fileTree: fileTree, config: config, }); }); } }); if (workspaceFolders) { workspaceFolders.forEach(folder => { const fileTree = getFileTree(folder.uri); console.log(`File Tree for ${folder.uri.fsPath}:`, fileTree); sendToServer({ type: 'workspaceFolders', fileTree: fileTree, config: config, }); }); } } // 缓冲器函数(防抖) function debounceSendToServer(data, delay) { if (debounceTimeout) { clearTimeout(debounceTimeout); } debounceTimeout = setTimeout(() => { sendToServer(data); }, delay); } // 递归获取文件树 function getFileTree(uri) { let result = []; const files = fs.readdirSync(uri.fsPath); files.forEach(file => { const filePath = path.join(uri.fsPath, file); const stat = fs.statSync(filePath); if (stat.isDirectory()) { result.push({ name: file, type: 'folder', children: getFileTree(vscode.Uri.file(filePath)) }); } else { result.push({ name: file, type: 'file' }); } }); return result; } // 禁用插件时调用此函数 function deactivate() {} module.exports = { activate, deactivate };