From 2ae0ab2a96463d29c785ed3040db9f51280ed6c6 Mon Sep 17 00:00:00 2001 From: yaad96 Date: Sat, 2 Mar 2024 15:46:28 -0500 Subject: [PATCH] Doi Processing class added and visitedFiles functionality implemented --- src/DoiProcessing.ts | 77 ++++++++++++++++++++++++++++++++++++++++ src/FileChangeManager.ts | 29 +++++++++++++-- src/extension.ts | 30 +++++++--------- 3 files changed, 116 insertions(+), 20 deletions(-) create mode 100644 src/DoiProcessing.ts diff --git a/src/DoiProcessing.ts b/src/DoiProcessing.ts new file mode 100644 index 0000000..8e64303 --- /dev/null +++ b/src/DoiProcessing.ts @@ -0,0 +1,77 @@ +import * as vscode from 'vscode'; +import WebSocket from 'ws'; +import { exec } from 'child_process'; +import { promisify } from 'util'; +import * as fs from 'fs'; +import * as path from 'path'; +import { readFile } from 'fs/promises'; +import { WebSocketConstants } from './WebSocketConstants'; +import { buildFolderHierarchy } from './utilites'; + +export class DoiProcessing{ + private ws: WebSocket | null; + private projectPath:string; + private static instance: DoiProcessing|null = null; + + private timedVisitedFiles: { timeStamp: string, filePath: string }[] = []; + + public constructor(projectPath:string,ws:WebSocket|null){ + this.projectPath=projectPath; + this.ws = ws; + this.monitorWorkSpaceBehavior(); + + } + + public static getInstance(projectPath:string = "", ws: WebSocket | null = null): DoiProcessing { + if (!DoiProcessing.instance) { + DoiProcessing.instance = new DoiProcessing(projectPath,ws); + } + return DoiProcessing.instance; + } + + public updateProjectWs(projectPath: string, ws: WebSocket): void { + this.projectPath = projectPath; + this.ws = ws; + this.monitorWorkSpaceBehavior(); + //this.learningDRPath = path.join(this.currentProjectPath, Constants.LEARNING_DR_DIRECTORY); + + } + + private monitorWorkSpaceBehavior(){ + // Listening for file opening events in VSCode + vscode.workspace.onDidOpenTextDocument(this.newVisitedFile.bind(this)); + + } + + public getVisitedFiles(): { timeStamp: string, filePath: string }[] { + // Directly return the array of visited files + return this.timedVisitedFiles; + } + + + private newVisitedFile(document: vscode.TextDocument): void { + const filePath = document.uri.fsPath; + this.updateVisitedFiles(filePath); + // Assuming updateSearchHistory() functionality is handled elsewhere or omitted + //this.currentFilePath = filePath; + } + + private updateVisitedFiles(newFilePath: string): void { + const currentTime = new Date().getTime(); + const timeStamp = currentTime.toString(); + + if (this.timedVisitedFiles.length > 0) { + const lastVisitedFile = this.timedVisitedFiles[this.timedVisitedFiles.length - 1]; + const lastTimeStamp = parseInt(lastVisitedFile.timeStamp); + + // If the new visit occurs within 1 second of the last, remove the last entry + if (currentTime - lastTimeStamp < 1000) { + this.timedVisitedFiles.pop(); + } + } + + this.timedVisitedFiles.push({ timeStamp, filePath: newFilePath }); + } + + +} \ No newline at end of file diff --git a/src/FileChangeManager.ts b/src/FileChangeManager.ts index 5ff8f8f..11e5311 100644 --- a/src/FileChangeManager.ts +++ b/src/FileChangeManager.ts @@ -10,6 +10,7 @@ import { buildFolderHierarchy } from './utilites'; //import { MessageProcessor } from './MessageProcessor'; import { FollowAndAuthorRulesProcessor } from './FollowAndAuthorRulesProcessor'; import { MiningRulesProcessor } from './MiningRulesProcessor'; +import { DoiProcessing } from './DoiProcessing'; const readFileAsync = promisify(fs.readFile); @@ -72,11 +73,35 @@ export class FileChangeManager { // Handle or log the error appropriately } } + try { + const ins = DoiProcessing.getInstance(); + if (!ins) { + // If getInstance() returned null, we create a new instance + // But since our getInstance never returns null (creates a new instance if null), + // This check is just for demonstrating a similar approach to catching NullPointerException in Java. + throw new Error('Instance is null'); // Simulating a scenario to create a new instance + } + ins.updateProjectWs(this.projectPath, this.ws); + } catch (error) { + if (error instanceof Error && error.message === 'Instance is null') { + // This block is for handling the specific error thrown above, + // simulating the catch for NullPointerException in Java. + // In practical TypeScript, this pattern is rarely needed due to the dynamic nature of JS and the way we handle nulls/undefined values. + new DoiProcessing(projectPath, this.ws); // Assuming this will set the instance internally or perform necessary actions + } else { + console.error("An unexpected error occurred:", error); + // Handle or log the error appropriately + } + } } } + /*public void checkChangedProject(){ + + }*/ + public static getInstance(projectPath:string,ws:WebSocket): FileChangeManager { if (!FileChangeManager.instance) { FileChangeManager.instance = new FileChangeManager(projectPath,ws); @@ -84,9 +109,7 @@ export class FileChangeManager { return FileChangeManager.instance; } - public setWebSocket(ws: WebSocket) { - this.ws = ws; - } + private watchWorkspaceChanges() { diff --git a/src/extension.ts b/src/extension.ts index 35e151d..30acf1f 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -9,6 +9,7 @@ import { WebSocketConstants } from './WebSocketConstants'; import * as path from 'path'; import { FollowAndAuthorRulesProcessor } from './FollowAndAuthorRulesProcessor'; import { MiningRulesProcessor } from './MiningRulesProcessor'; +import { DoiProcessing } from './DoiProcessing'; @@ -30,7 +31,7 @@ export function activate(context: vscode.ExtensionContext) { server.on('connection', (ws) => { console.log('Client connected'); - //fileChangeManager.setWebSocket(ws); + (async () => { // Immediately Invoked Function Expression (IIFE) for async if (vscode.workspace.workspaceFolders) { @@ -137,31 +138,26 @@ export function activate(context: vscode.ExtensionContext) { text: word }; - /*ws.send(MessageProcessor.encodeData({ - command: WebSocketConstants.SEND_ELEMENT_INFO_FOR_MINE_RULES, - data: minigDataInfo - }));*/ ws.send(JSON.stringify({ command: WebSocketConstants.SEND_ELEMENT_INFO_FOR_MINE_RULES, data: minigDataInfo })); + const doiProcessing = DoiProcessing.getInstance(); + + const doiData = { + recentVisitedFiles:doiProcessing.getVisitedFiles(), + recentSearches:[], + recentElements:[] + }; - /* doi processing has to be imitated - + ws.send(JSON.stringify({ + command:WebSocketConstants.SEND_DOI_INFORMATION, + data:doiData + })); - DoiProcessing doiClass = DoiProcessing.getInstance(); -FileChangeManager.getInstance().sendMessage(MessageProcessor.encodeData(new Object[]{ -WebSocketConstants.SEND_DOI_INFORMATION, MessageProcessor.encodeDoiInformation( -new Object[]{doiClass.getVisitedFiles(), doiClass.getSearchHistory(), -doiClass.getVisitedElements()} -)}).toString()); -*/ - /*ws.send(MessageProcessor.encodeData({ - command: WebSocketConstants.SEND_REQUEST_MINE_RULES_FOR_ELEMENT, - data: "" - }));*/ ws.send(JSON.stringify({ command: WebSocketConstants.SEND_REQUEST_MINE_RULES_FOR_ELEMENT, data: ""