Skip to content

Commit

Permalink
Added recentlyVisitedElements in DOIProcessing
Browse files Browse the repository at this point in the history
  • Loading branch information
yaad96 committed Mar 3, 2024
1 parent 557fb8b commit 5ab86bf
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
44 changes: 44 additions & 0 deletions src/DoiProcessing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class DoiProcessing{
private static instance: DoiProcessing|null = null;

private timedVisitedFiles: { timeStamp: string, filePath: string }[] = [];
private timedCaretPositions: { timeStamp: string, filePath: string, startOffset:string, endOffset: string }[] = [];

public constructor(projectPath:string,ws:WebSocket|null){
this.projectPath=projectPath;
Expand All @@ -40,6 +41,7 @@ export class DoiProcessing{
private monitorWorkSpaceBehavior(){
// Listening for file opening events in VSCode
vscode.workspace.onDidOpenTextDocument(this.newVisitedFile.bind(this));
vscode.window.onDidChangeTextEditorSelection(this.newCaretPosition.bind(this));

}

Expand All @@ -48,6 +50,48 @@ export class DoiProcessing{
return this.timedVisitedFiles;
}

public getVisitedElements():{timeStamp:string,filePath:string,startOffset:string,endOffset:string}[]{
return this.timedCaretPositions;
}

private newCaretPosition(event: vscode.TextEditorSelectionChangeEvent): void {
const activeEditor = vscode.window.activeTextEditor;
if (!activeEditor) {
return;
}

const filePath = activeEditor.document.uri.fsPath;
const selection = event.selections[0]; // If handling multiple selections, you may need to adjust this
const startOffset = activeEditor.document.offsetAt(selection.start);
const endOffset = activeEditor.document.offsetAt(selection.end);

this.updateCaretPositions(filePath, startOffset, endOffset);
}

private updateCaretPositions(filePath: string, startOffset: number, endOffset: number): void {
const currentTime = new Date().getTime().toString();
//const currentTimeInString = new Date().getTime().toString();
if(this.timedCaretPositions.length>1){
const currentTimeInNumber = parseFloat(currentTime);
const lastTime = parseFloat(this.timedCaretPositions[this.timedCaretPositions.length-1].timeStamp);
if(currentTimeInNumber-lastTime<1000){
this.timedCaretPositions.pop();
}

if(this.timedCaretPositions.length===100){
this.timedCaretPositions.pop();
}
}

const startOffsetAsString: string = String(startOffset);
const endOffsetAsString: string = String(endOffset);
this.timedCaretPositions.push({ timeStamp: currentTime, filePath: filePath.replace(/\\/g, '/'), startOffset:startOffsetAsString, endOffset:endOffsetAsString });

// Optionally, send this information over WebSocket or process it further

}



private newVisitedFile(document: vscode.TextDocument): void {
const filePath = document.uri.fsPath;
Expand Down
4 changes: 2 additions & 2 deletions src/FileChangeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ export class FileChangeManager {
console.error(`Error in srcML output for file ${inputFilePath}:`, stderr);
reject(new Error(stderr));
} else {
console.log(`Converted to XML: ${inputFilePath}`);
//console.log(`Converted to XML: ${inputFilePath}`);
resolve(stdout); // stdout contains the XML content
}
});
Expand Down Expand Up @@ -337,7 +337,7 @@ export class FileChangeManager {
console.error(`Error sending XML for file ${filePath}:`, error);
reject(error); // Stop sending if an error occurs
} else {
console.log(`Sent XML for file ${filePath}`);
//console.log(`Sent XML for file ${filePath}`);
resolve();
}
});
Expand Down
5 changes: 2 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { DoiProcessing } from './DoiProcessing';

//const readFileAsync = promisify(fs.readFile);

const port = 8887;
const port = 9000;



Expand Down Expand Up @@ -149,8 +149,7 @@ export function activate(context: vscode.ExtensionContext) {

const doiData = {
recentVisitedFiles:doiProcessing.getVisitedFiles(),
recentSearches:[],
recentElements:[]
recentElements:doiProcessing.getVisitedElements()
};

ws.send(JSON.stringify({
Expand Down

0 comments on commit 5ab86bf

Please sign in to comment.