Skip to content

Commit

Permalink
analyzeDatabases is implemented, focusing on snippet is partially imp…
Browse files Browse the repository at this point in the history
…lemented
  • Loading branch information
yaad96 committed Feb 26, 2024
1 parent b8d8d33 commit 0443944
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 139 deletions.
4 changes: 3 additions & 1 deletion src/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ export const Constants = {
TEMP_XML_FILE: "tempResultXmlFile.xml",
TEMP_JAVA_FILE: "tempExprFile.java",
XML_HEADER: "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>",
LEARNING_DR_DIRECTORY:"/LearningDR"
LEARNING_DR_DIRECTORY:"/LearningDR",
SRCML_BIN_PATH: "C:\\Program Files\\srcML 0.9.5\\bin\\srcml"

};
23 changes: 23 additions & 0 deletions src/FileChangeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ export class FileChangeManager {
}

private watchWorkspaceChanges() {

this.handleActiveTextEditorChange();

vscode.workspace.onDidChangeTextDocument(this.handleChangeTextDocument.bind(this));
vscode.workspace.onDidCreateFiles(this.handleCreateFile.bind(this));
vscode.workspace.onDidDeleteFiles(this.handleDeleteFile.bind(this));
Expand Down Expand Up @@ -138,6 +141,26 @@ export class FileChangeManager {
console.error('Failed to generate project hierarchy:', error);
}
}

private handleActiveTextEditorChange() {
vscode.window.onDidChangeActiveTextEditor(editor => {
if (editor) {
const document = editor.document;
if (document.languageId === 'java') { // Adjust the condition based on your requirements
const javaFilePath = document.uri.fsPath;

if(this.ws){
this.ws.send(MessageProcessor.encodeData({
command:WebSocketConstants.SEND_FILE_CHANGE_IN_IDE_MSG,
data: javaFilePath
}));
}

}
}
});
}



private async handleCreateFile(event: vscode.FileCreateEvent) {
Expand Down
150 changes: 92 additions & 58 deletions src/FollowAndAuthorRulesProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { MessageProcessor } from './MessageProcessor';
import { Constants } from './Constants';
import * as fs1 from 'fs';
import { FileChangeManager } from './FileChangeManager';
import { writeToFile,convertToXML } from './utilites';
import { writeToFile, convertToXML, findLineNumber } from './utilites';



Expand Down Expand Up @@ -38,7 +38,7 @@ export class FollowAndAuthorRulesProcessor {
this.currentProjectPath = currentProjectPath;
this.ws = ws;
this.tagTable = [];
this.ruleTable=[]; // Initialize as an empty array
this.ruleTable = []; // Initialize as an empty array
this.loadTagTable();
this.loadRuleTable();
}
Expand Down Expand Up @@ -87,34 +87,68 @@ export class FollowAndAuthorRulesProcessor {
return tagTableData;
}

public async processReceivedMessages(message:string): Promise<void>{
const jsonData= JSON.parse(message.toString());
public async processReceivedMessages(message: string): Promise<void> {
const jsonData = JSON.parse(message.toString());
const command = jsonData.command;

switch (command) {
case WebSocketConstants.RECEIVE_SNIPPET_XML_MSG:
// Handle RECEIVE_SNIPPET_XML_MSG
const xmlString = jsonData.data.xml;


const tempXmlFilePath = path.join(this.currentProjectPath, Constants.TEMP_XML_FILE);
const xmlHeader = Constants.XML_HEADER;

// Write XML to temporary file
fs.writeFile(tempXmlFilePath, xmlHeader + xmlString, { encoding: 'utf8' });

// Open the specified file
//we are getting the full path from the client
const fileUri = vscode.Uri.file(jsonData.data.fileName);
const document = await vscode.workspace.openTextDocument(fileUri);
const editor = await vscode.window.showTextDocument(document);

try {
const positionString = await findLineNumber(tempXmlFilePath);
// Calculate the position based on the XML length

const positionIndex = positionString.length;

// Find the position in the document to highlight
const startPosition = document.positionAt(positionIndex);
const endPosition = new vscode.Position(startPosition.line + 1, 0);

// Move the cursor and highlight the code
editor.selection = new vscode.Selection(startPosition,endPosition);
editor.revealRange(new vscode.Range(startPosition, endPosition), vscode.TextEditorRevealType.InCenter);
} catch (error) {
console.error("An error occurred:");
console.error(error); // Handle the error
}


break;
case WebSocketConstants.RECEIVE_MODIFIED_RULE_MSG:
// Extract ruleID and ruleInfo from jsonData.data
const ruleID = jsonData.data.ruleID;
const ruleInfo = jsonData.data.ruleInfo;
const ruleExists = this.checkRuleExists(ruleID,ruleInfo);
if(ruleExists){
const ruleIndex = this.ruleTable.findIndex(rule=>rule.index === ruleID);
const ruleExists = this.checkRuleExists(ruleID, ruleInfo);
if (ruleExists) {
const ruleIndex = this.ruleTable.findIndex(rule => rule.index === ruleID);
this.ruleTable[ruleIndex] = ruleInfo;
this.updateRuleTableFile();

this.ws?.send(MessageProcessor.encodeData({
command: WebSocketConstants.SEND_UPDATE_RULE_MSG,
data:jsonData.data
data: jsonData.data
}));

}
else{
else {
this.ws?.send(MessageProcessor.encodeData({
command: WebSocketConstants.SEND_FAILED_UPDATE_RULE_MSG,
data:jsonData.data
data: jsonData.data
}));
}
// Update the rule by ruleID with ruleInfo here
Expand All @@ -124,13 +158,13 @@ export class FollowAndAuthorRulesProcessor {
const updateTagID = jsonData.data.tagID;
const updateTagInfo = jsonData.data.tagInfo;
var data = {
ID:jsonData.data.tagInfo.ID,
tagName:jsonData.data.tagInfo.tagName,
detail:jsonData.data.tagInfo.detail
ID: jsonData.data.tagInfo.ID,
tagName: jsonData.data.tagInfo.tagName,
detail: jsonData.data.tagInfo.detail
};
// Update the tag by tagID with tagInfo here
const tagExists = this.checkTagExists(updateTagID,updateTagInfo);
if(tagExists){
const tagExists = this.checkTagExists(updateTagID, updateTagInfo);
if (tagExists) {
const tagIndex = this.tagTable.findIndex(tag => tag.ID === updateTagID);
this.tagTable[tagIndex] = updateTagInfo;
this.updateTagTableFile();
Expand All @@ -141,29 +175,29 @@ export class FollowAndAuthorRulesProcessor {
}));

}
else{
else {
this.ws?.send(MessageProcessor.encodeData({
command: WebSocketConstants.SEND_FAILED_UPDATE_TAG_MSG,
data: data
}));
}

break;
case WebSocketConstants.RECEIVE_CODE_TO_XML_MSG:
// Handle conversion of code to XML and respond back
const plainCode = jsonData.data.codeText;
const tempJavaFilePath = path.join(this.currentProjectPath,Constants.TEMP_JAVA_FILE);
writeToFile(tempJavaFilePath,plainCode);
const tempJavaFilePath = path.join(this.currentProjectPath, Constants.TEMP_JAVA_FILE);
writeToFile(tempJavaFilePath, plainCode);
if (tempJavaFilePath.endsWith('.java')) {


try {
const xmlContent = await convertToXML(tempJavaFilePath); // Adjusted call
this.ws?.send(MessageProcessor.encodeData({
command:WebSocketConstants.SEND_XML_FROM_CODE_MSG,
data:{
xmlText:xmlContent,
messageID:jsonData.data.messageID
command: WebSocketConstants.SEND_XML_FROM_CODE_MSG,
data: {
xmlText: xmlContent,
messageID: jsonData.data.messageID
}
}));

Expand All @@ -182,16 +216,16 @@ export class FollowAndAuthorRulesProcessor {
const newRuleID = jsonData.data.ruleID;
const newRuleInfo = jsonData.data.ruleInfo;

const ruleAlreadyExists = this.checkRuleExists(newRuleID,newRuleInfo);
if(ruleAlreadyExists){
const ruleAlreadyExists = this.checkRuleExists(newRuleID, newRuleInfo);
if (ruleAlreadyExists) {

this.ws?.send(MessageProcessor.encodeData({
command: WebSocketConstants.SEND_FAILED_NEW_RULE_MSG,
data: jsonData.data
}));

}
else{
else {
//console.log("here");
this.ruleTable.push(newRuleInfo);
this.updateRuleTableFile();
Expand All @@ -208,14 +242,14 @@ export class FollowAndAuthorRulesProcessor {
const newTagID = jsonData.data.tagID;
const newTagInfo = jsonData.data.tagInfo;
data = {
ID:jsonData.data.tagInfo.ID,
tagName:jsonData.data.tagInfo.tagName,
detail:jsonData.data.tagInfo.detail
ID: jsonData.data.tagInfo.ID,
tagName: jsonData.data.tagInfo.tagName,
detail: jsonData.data.tagInfo.detail
};


const tagAlreadyExists = this.checkTagExists(newTagID,newTagInfo);
if(tagAlreadyExists){
const tagAlreadyExists = this.checkTagExists(newTagID, newTagInfo);
if (tagAlreadyExists) {


this.ws?.send(MessageProcessor.encodeData({
Expand All @@ -225,7 +259,7 @@ export class FollowAndAuthorRulesProcessor {


}
else{
else {
this.tagTable.push(newTagInfo);
this.updateTagTableFile();
this.ws?.send(MessageProcessor.encodeData({
Expand All @@ -239,55 +273,55 @@ export class FollowAndAuthorRulesProcessor {
default:
console.log(`Unrecognized command: ${command}`);
}

}

private checkRuleExists(newRuleID:string,newRuleInfo:any):boolean{
if(newRuleID !== newRuleInfo.index){
private checkRuleExists(newRuleID: string, newRuleInfo: any): boolean {
if (newRuleID !== newRuleInfo.index) {
console.error("Mismatched IDs");
return true;
}

const ruleExists = this.ruleTable.some(rule=>rule.index === newRuleID);
if(ruleExists){
const ruleExists = this.ruleTable.some(rule => rule.index === newRuleID);
if (ruleExists) {
return true;
}
return false;
}

private checkTagExists(newTagID: string, newTagInfo:Tag): boolean {
private checkTagExists(newTagID: string, newTagInfo: Tag): boolean {


// Ensure the ID in the newTagInfo matches newTagID
if (newTagInfo.ID !== newTagID) {
console.error("Mismatched IDs");
return true;
}

// Check if the tagTable already contains a tag with the newTagID
const tagExists = this.tagTable.some(tag => tag.ID === newTagID);
if (tagExists) {
// Tag already exists, return true
return true;
}
}
return false;
}


private async updateRuleTableFile() {
const ruleTablePath = path.join(this.currentProjectPath,Constants.RULE_TABLE_JSON); // Adjust __dirname to your project's root path as necessary
const ruleTablePath = path.join(this.currentProjectPath, Constants.RULE_TABLE_JSON); // Adjust __dirname to your project's root path as necessary

// Read the existing tag table
fs1.readFile(ruleTablePath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading the file table:', err);
return;
}

// Parse the existing tag table and append new tag info
//const tagTable = JSON.parse(data);


// Write the updated tag table back to the file
fs1.writeFile(ruleTablePath, JSON.stringify(this.ruleTable, null, 2), 'utf8', (err) => {
if (err) {
Expand All @@ -299,22 +333,22 @@ export class FollowAndAuthorRulesProcessor {
});
}



private async updateTagTableFile() {
const tagTablePath = path.join(this.currentProjectPath,Constants.TAG_TABLE_JSON); // Adjust __dirname to your project's root path as necessary
const tagTablePath = path.join(this.currentProjectPath, Constants.TAG_TABLE_JSON); // Adjust __dirname to your project's root path as necessary

// Read the existing tag table
fs1.readFile(tagTablePath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading the tag table:', err);
return;
}

// Parse the existing tag table and append new tag info
//const tagTable = JSON.parse(data);


// Write the updated tag table back to the file
fs1.writeFile(tagTablePath, JSON.stringify(this.tagTable, null, 2), 'utf8', (err) => {
if (err) {
Expand All @@ -326,6 +360,6 @@ export class FollowAndAuthorRulesProcessor {
});
}



}
Loading

0 comments on commit 0443944

Please sign in to comment.