From 8dfe773c5cc66df8c4534e895d5c3ed5084d1826 Mon Sep 17 00:00:00 2001 From: yaad96 Date: Wed, 17 Jul 2024 13:24:08 -0400 Subject: [PATCH] Tasks done till feedbacks of 15th July --- src/FollowAndAuthorRulesProcessor.ts | 155 ++++++++++++++++++--------- src/WebSocketConstants.ts | 4 +- src/utilites.ts | 40 +++++++ 3 files changed, 147 insertions(+), 52 deletions(-) diff --git a/src/FollowAndAuthorRulesProcessor.ts b/src/FollowAndAuthorRulesProcessor.ts index 5457640..3d3fe26 100644 --- a/src/FollowAndAuthorRulesProcessor.ts +++ b/src/FollowAndAuthorRulesProcessor.ts @@ -7,7 +7,7 @@ import { WebSocketConstants } from './WebSocketConstants'; import { Constants } from './Constants'; import * as fs1 from 'fs'; import { FileChangeManager } from './FileChangeManager'; -import { writeToFile, convertToXML, findLineNumber } from './utilites'; +import { writeToFile, convertToXML, findFileAndReadContent } from './utilites'; @@ -26,6 +26,8 @@ export class FollowAndAuthorRulesProcessor { private tagTable: Tag[]; private currentProjectPath: string; public readonly wsMessages: string[] = [ + WebSocketConstants.RECEIVE_EDIT_FIX, + WebSocketConstants.SEND_CONTENT_FOR_EDIT_FIX, WebSocketConstants.RECEIVE_LLM_MODIFIED_FILE_CONTENT, WebSocketConstants.RECEIVE_CONVERTED_JAVA_SNIPPET_MSG, WebSocketConstants.RECEIVE_LLM_SNIPPET_MSG, @@ -90,37 +92,88 @@ export class FollowAndAuthorRulesProcessor { return tagTableData; } + + + public async processReceivedMessages(message: string): Promise { const jsonData = JSON.parse(message.toString()); const command = jsonData.command; switch (command) { + case WebSocketConstants.RECEIVE_EDIT_FIX: + console.log("ASDASDASD"); + console.log(jsonData); + const filePathOfSuggestedFix = jsonData.data.filePathOfSuggestedFix; + + findFileAndReadContent(filePathOfSuggestedFix) + .then(content => { + if (content) { + // Process the file content as needed + console.log('File content:', content); + + this.ws?.send(JSON.stringify({ + command: WebSocketConstants.SEND_CONTENT_FOR_EDIT_FIX, + data: content + })); + } + }) + .catch(err => { + console.error('Error reading file content:', err); + }); + + break; + + case WebSocketConstants.RECEIVE_LLM_MODIFIED_FILE_CONTENT: console.log("COME"); - const localFilePath = jsonData.filePath; - const modifiedContent = jsonData.modifiedFileContent; - - - // Read local file content - const localFileUri = vscode.Uri.file(localFilePath); - const localDocument = await vscode.workspace.openTextDocument(localFileUri); - - // Create a virtual document for the modified content - const modifiedUri = localFileUri.with({ scheme: 'untitled', path: localFilePath + '-modified' }); - - await vscode.workspace.openTextDocument(modifiedUri).then(localDocument => { - const edit = new vscode.WorkspaceEdit(); - edit.insert(modifiedUri, new vscode.Position(0, 0), modifiedContent); - return vscode.workspace.applyEdit(edit).then(success => { - if (success) { - vscode.commands.executeCommand('vscode.diff', localFileUri, modifiedUri, 'Local ↔ Modified'); + console.log(jsonData); + const localFilePath = jsonData.data.filePath; + const modifiedContent = jsonData.data.modifiedFileContent; + const explanation = jsonData.data.explanation; + const violatedCode = jsonData.data.violatedCode; + + // Function to escape special characters in a string for use in a regular expression + const escapeRegExp = (string: string): string => { + return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string + }; + + // Read the file content + fs1.readFile(localFilePath, 'utf8', (err, data) => { + if (err) { + console.error('Error reading the file:', err); + return; + } + + // Escape the violatedCode for use in a regular expression + const escapedViolatedCode = escapeRegExp(violatedCode); + + // Create a regular expression to find the violated code, with the 's' flag for dotAll mode + const regex = new RegExp(escapedViolatedCode, 'gs'); + + // Replace the violated code with the modified content + const newContent = data.replace(regex, modifiedContent); + + // Format the explanation as a comment + //const comment = `/*\n${explanation}\n*/\n\n`; + + const comment = ``; + + // Prepare the final content with the explanation + const finalContent = `${comment}${newContent}`; + + // Write the updated content back to the file + fs1.writeFile(localFilePath, finalContent, 'utf8', (err) => { + if (err) { + console.error('Error writing to the file:', err); } else { - vscode.window.showErrorMessage('Could not display the diff.'); + console.log('File updated successfully.'); } }); }); break; + + case WebSocketConstants.RECEIVE_CONVERTED_JAVA_SNIPPET_MSG: console.log("ASDAD222aaaa"); try { @@ -128,41 +181,41 @@ export class FollowAndAuthorRulesProcessor { const fileName = jsonData.data.fileName; const convertedJava = jsonData.data.convertedJava; const lastLineSnippet = convertedJava.trim().split('\n').pop().trim(); - + const openPath = vscode.Uri.file(fileName); vscode.workspace.openTextDocument(openPath).then(doc => { - vscode.window.showTextDocument(doc).then(editor => { - const text = doc.getText(); - const lines = text.split('\n'); - let lineIndex = -1; - - // Find the line containing the lastLineSnippet - for (let i = 0; i < lines.length; i++) { - if (lines[i].includes(lastLineSnippet)) { - lineIndex = i; - break; - } - } - - if (lineIndex !== -1) { - const startPos = new vscode.Position(lineIndex, 0); - const endPos = new vscode.Position(lineIndex, lines[lineIndex].length); - const range = new vscode.Range(startPos, endPos); - - editor.selection = new vscode.Selection(startPos, endPos); - editor.revealRange(range, vscode.TextEditorRevealType.InCenter); - - // Optionally highlight the line - const decoration = vscode.window.createTextEditorDecorationType({ - backgroundColor: 'rgba(255,255,0,0.3)' - }); - editor.setDecorations(decoration, [range]); - } - }); + vscode.window.showTextDocument(doc).then(editor => { + const text = doc.getText(); + const lines = text.split('\n'); + let lineIndex = -1; + + // Find the line containing the lastLineSnippet + for (let i = 0; i < lines.length; i++) { + if (lines[i].includes(lastLineSnippet)) { + lineIndex = i; + break; + } + } + + if (lineIndex !== -1) { + const startPos = new vscode.Position(lineIndex, 0); + const endPos = new vscode.Position(lineIndex, lines[lineIndex].length); + const range = new vscode.Range(startPos, endPos); + + editor.selection = new vscode.Selection(startPos, endPos); + editor.revealRange(range, vscode.TextEditorRevealType.InCenter); + + // Optionally highlight the line + const decoration = vscode.window.createTextEditorDecorationType({ + backgroundColor: 'rgba(255,255,0,0.3)' + }); + editor.setDecorations(decoration, [range]); + } + }); }); - }catch (error) { + } catch (error) { vscode.window.showErrorMessage('Failed to process the message: ' + error); - } + } break; case WebSocketConstants.RECEIVE_LLM_SNIPPET_MSG: @@ -220,7 +273,7 @@ export class FollowAndAuthorRulesProcessor { break; - + case WebSocketConstants.RECEIVE_MODIFIED_RULE_MSG: // Extract ruleID and ruleInfo from jsonData.data const ruleID = jsonData.data.ruleID; diff --git a/src/WebSocketConstants.ts b/src/WebSocketConstants.ts index 4cbeab9..a319a5f 100644 --- a/src/WebSocketConstants.ts +++ b/src/WebSocketConstants.ts @@ -1,7 +1,9 @@ export class WebSocketConstants { static readonly MESSAGE_KEY_COMMAND: string = "command"; static readonly MESSAGE_KEY_DATA: string = "data"; - + + static readonly RECEIVE_EDIT_FIX:string = "SEND_INFO_FOR_EDIT_FIX"; + static readonly SEND_CONTENT_FOR_EDIT_FIX:string = "RECEIVE_CONTENT_FOR_EDIT_FIX"; static readonly RECEIVE_LLM_MODIFIED_FILE_CONTENT:string = "LLM_MODIFIED_FILE_CONTENT"; static readonly RECEIVE_CONVERTED_JAVA_SNIPPET_MSG:string = "CONVERTED_JAVA_SNIPPET"; static readonly RECEIVE_LLM_SNIPPET_MSG:string= "LLM_SNIPPET"; diff --git a/src/utilites.ts b/src/utilites.ts index 5d56dbb..cd383ab 100644 --- a/src/utilites.ts +++ b/src/utilites.ts @@ -7,6 +7,46 @@ import * as os from 'os'; import * as fs from 'fs'; + + + +export async function findFileAndReadContent(fileName: string) { + const workspaceFolder = vscode.workspace.workspaceFolders?.[0]; + if (!workspaceFolder) { + vscode.window.showErrorMessage('No workspace folder open.'); + return; + } + + const folderUri = workspaceFolder.uri; + const fileContent = await findFileRecursively(folderUri, fileName); + if (fileContent) { + return fileContent; + } + + vscode.window.showErrorMessage(`File ${fileName} not found in workspace.`); +} + +async function findFileRecursively(folderUri: vscode.Uri, fileName: string): Promise { + const directoryEntries = await vscode.workspace.fs.readDirectory(folderUri); + + for (const [name, type] of directoryEntries) { + const entryUri = vscode.Uri.joinPath(folderUri, name); + + if (type === vscode.FileType.File && name === fileName) { + const fileContent = await vscode.workspace.fs.readFile(entryUri); + return Buffer.from(fileContent).toString('utf8'); + } else if (type === vscode.FileType.Directory) { + const fileContent = await findFileRecursively(entryUri, fileName); + if (fileContent) { + return fileContent; + } + } + } + + return undefined; +} + + export function debounce void>(func: T, wait: number): (...args: Parameters) => void { let timeout: NodeJS.Timeout | null; return function (...args: Parameters) {