Skip to content

Commit

Permalink
[MPQEditor] Handle invalid VISQ data
Browse files Browse the repository at this point in the history
This commit implements handling of invalid VISQ file.

ONE-vscode-DCO-1.0-Signed-off-by: s.malakhov <[email protected]>
  • Loading branch information
stamalakhov committed May 23, 2023
1 parent 1295a74 commit 7c0db20
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 43 deletions.
36 changes: 8 additions & 28 deletions src/MPQEditor/MPQCircleSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export type MPQSelectionCmdOpenArgs = {
};

export type MPQVisqData = {
visqPath: string;
visqJsonData: string[];
};

export type MPQSelectionCmdCloseArgs = {
Expand Down Expand Up @@ -82,7 +82,7 @@ export class MPQSelectionPanel
MPQSelectionPanel.createOrShow(
context.extensionUri,
args,
"",
[],
handler
);
}
Expand All @@ -109,7 +109,7 @@ export class MPQSelectionPanel
MPQSelectionPanel.createOrShow(
context.extensionUri,
args,
visqData.visqPath,
visqData.visqJsonData,
handler
);
}
Expand All @@ -125,7 +125,7 @@ export class MPQSelectionPanel
public static createOrShow(
extensionUri: vscode.Uri,
args: MPQSelectionCmdOpenArgs,
visqPath: string,
visqJsonData: string[],
handler: MPQSelectionEvent | undefined
) {
let column = args.panel.viewColumn;
Expand Down Expand Up @@ -159,7 +159,7 @@ export class MPQSelectionPanel
panel,
extensionUri,
args,
visqPath,
visqJsonData,
handler
);

Expand Down Expand Up @@ -218,7 +218,7 @@ export class MPQSelectionPanel
panel: vscode.WebviewPanel,
extensionUri: vscode.Uri,
args: MPQSelectionCmdOpenArgs,
visqPath: string,
visqData: string[],
handler: MPQSelectionEvent | undefined
) {
super(extensionUri, panel.webview);
Expand All @@ -230,30 +230,17 @@ export class MPQSelectionPanel
this._modelPath = args.modelPath;
this._mpqEventHandler = handler;
this._lastSelected = args.names;
this._visqData = [];
this._visqData = visqData;

// Listen for when the panel is disposed
// This happens when the user closes the panel or when the panel is closed programmatically
this._panel.onDidDispose(() => this.dispose(), null, this._disposables);

this.initGraphCtrl(this._modelPath, this);
if (visqPath.length < 1) {
if (this._visqData.length < 1) {
this.setMode("selector");
} else {
this.setMode("visqselector");
const visqUri = vscode.Uri.file(visqPath);
vscode.workspace.fs.readFile(visqUri).then((visqData) => {
try {
this._visqData = JSON.parse(visqData.toString());
} catch (error) {
this.onInvalidVISQData(visqPath);
}

// check whether _visqData pretend to be valid
if (!("error" in this._visqData) || !("meta" in this._visqData)) {
this.onInvalidVISQData(visqPath);
}
});
}
if (this._mpqEventHandler) {
this._mpqEventHandler.onOpened(this._ownerPanel);
Expand Down Expand Up @@ -331,11 +318,4 @@ export class MPQSelectionPanel
}
this.setSelection(selections);
}

private onInvalidVISQData(_visqPath: string) {
this._visqData = [];
if (this._mpqEventHandler) {
// TODO
}
}
}
55 changes: 40 additions & 15 deletions src/MPQEditor/MPQEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ export class MPQEditorProvider
if (visqPath.length < 1) {
this.showCircleModelGraph(document, webviewPanel);
} else {
this.showVisqCircleModelGraph(visqPath, document, webviewPanel);
this.processVisqPath(visqPath, document, webviewPanel);
}
} else {
this.closeModelGraphView(document);
Expand All @@ -677,7 +677,7 @@ export class MPQEditorProvider
* @brief Called when it's needed to show visq data in circle-graph
*/
private showVisqCircleModelGraph(
visqPath: string,
visqJsonData: string[],
document: vscode.TextDocument,
webviewPanel: vscode.WebviewPanel
) {
Expand All @@ -689,7 +689,7 @@ export class MPQEditorProvider
};

const visqData: MPQVisqData = {
visqPath: visqPath,
visqJsonData: visqJsonData,
};
vscode.commands.executeCommand(
MPQSelectionPanel.cmdOpenVisq,
Expand All @@ -700,7 +700,7 @@ export class MPQEditorProvider

webviewPanel.webview.postMessage({
type: "VisqFileLoaded",
visqFile: visqPath,
visqFile: this._mpqDataMap[document.uri.toString()].visqPath,
});
}

Expand All @@ -719,10 +719,7 @@ export class MPQEditorProvider
// remove invalid path
this.removeVisqFile(document, webviewPanel);
} else if (path.endsWith(".visq.json")) {
// reload visq
this._mpqDataMap[document.uri.toString()].visqPath = path;
this.closeModelGraphView(document);
this.showVisqCircleModelGraph(path, document, webviewPanel);
this.processVisqPath(path, document, webviewPanel);
}
}

Expand All @@ -741,6 +738,40 @@ export class MPQEditorProvider
}
}

/**
* @brief shows visqPath at selector if it's valid
*/
private processVisqPath(
visqPath: string,
document: vscode.TextDocument,
webviewPanel: vscode.WebviewPanel
): void {
const visqUri = vscode.Uri.file(visqPath);
vscode.workspace.fs.readFile(visqUri).then((visqData) => {
let visqJsonData = null;
let valid = true;
try {
visqJsonData = JSON.parse(visqData.toString());
} catch (error) {
valid = false;
}

// check whether visqData pretend to be valid
valid &&= "error" in visqJsonData && "meta" in visqJsonData;

if (valid) {
const docUri = document.uri.toString();
this._mpqDataMap[docUri].visqPath = visqPath;
// close previous view if any
this.closeModelGraphView(document);
// open new view
this.showVisqCircleModelGraph(visqJsonData, document, webviewPanel);
} else {
Balloon.error("Invalid visq file " + visqPath);
}
});
}

/**
* @brief Called when user requests to load visq file from UI
*/
Expand All @@ -759,13 +790,7 @@ export class MPQEditorProvider
vscode.window.showOpenDialog(dialogOptions).then((fileUri) => {
if (fileUri && fileUri[0]) {
const visqPath = fileUri[0].fsPath.toString();

const docUri = document.uri.toString();
this._mpqDataMap[docUri].visqPath = visqPath;
// close previous view if any
this.closeModelGraphView(document);
// open new view
this.showVisqCircleModelGraph(visqPath, document, webviewPanel);
this.processVisqPath(visqPath, document, webviewPanel);
}
});
}
Expand Down

0 comments on commit 7c0db20

Please sign in to comment.