Skip to content

Commit 44017fe

Browse files
committed
try to fix some issues with visualization
1 parent dcba988 commit 44017fe

5 files changed

Lines changed: 76 additions & 49 deletions

File tree

media/programflow-visualization/webview.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ window.addEventListener("message", (event) => {
3636
/**
3737
* Updates the Visualization in the Webview, with the given BackendTraceElem.
3838
*
39-
* @param traceElem A BackendTraceElem with 3 fields (line, stack, heap)
39+
* @param traceElem A FrontendTraceElem
4040
*/
4141
function updateVisualization(traceElem) {
4242
const data = `
@@ -56,10 +56,10 @@ function updateVisualization(traceElem) {
5656
</div>
5757
<div class="row">
5858
<div class="column floating-left floating-left-content" id="frames">
59-
${traceElem[1]}
59+
${traceElem.stackHTML}
6060
</div>
6161
<div class="column floating-right floating-right-content" id="objects">
62-
${traceElem[2]}
62+
${traceElem.heapHTML}
6363
</div>
6464
</div>
6565
`;
@@ -78,17 +78,17 @@ function updateVisualization(traceElem) {
7878
}
7979

8080
const stdoutLog = document.getElementById("stdout-log");
81-
stdoutLog.innerHTML = traceElem[4];
81+
stdoutLog.innerHTML = traceElem.outputState;
8282
stdoutLog.scrollTo(0, stdoutLog.scrollHeight);
8383
}
8484

8585
/**
8686
* Updates the indendation for heap elements, if a other heap element references it.
8787
*
88-
* @param traceElem A BackendTraceElem with 3 fields (line, stack, heap)
88+
* @param traceElem A FrontendTraceElem
8989
*/
9090
function updateIntend(traceElem) {
91-
const heapTags = traceElem[2].match(/(?<=startPointer)[0-9]+/g);
91+
const heapTags = traceElem.heapHTML.match(/(?<=startPointer)[0-9]+/g);
9292
if (heapTags) {
9393
heapTags.forEach((tag) => {
9494
const element = document.getElementById("objectItem" + tag);
@@ -117,7 +117,7 @@ function updateRefArrows(traceElem) {
117117
return new LinkerLine({
118118
parent: document.getElementById("viz"),
119119
start: tag.elem1,
120-
end: tag.elem2,
120+
end: tag.elem2,
121121
size: 2,
122122
path: "magnet",
123123
startSocket: "right",
@@ -139,9 +139,9 @@ function updateRefArrows(traceElem) {
139139
* @returns A list with all ids that have either a start or end pointer id in the html
140140
*/
141141
function getCurrentTags(traceElem) {
142-
const stackTags = traceElem[1].match(/(?<=id=")(.+)Pointer[0-9]+/g);
143-
const heapTags = traceElem[2].match(/(?<=startPointer)[0-9]+/g);
144-
const uniqueId = traceElem[2].match(/(?<=)\d+(?=startPointer)/g);
142+
const stackTags = traceElem.stackHTML.match(/(?<=id=")(.+)Pointer[0-9]+/g);
143+
const heapTags = traceElem.heapHTML.match(/(?<=startPointer)[0-9]+/g);
144+
const uniqueId = traceElem.heapHTML.match(/(?<=)\d+(?=startPointer)/g);
145145

146146
if (!stackTags) {
147147
return;

src/programflow-visualization/frontend/HTMLGenerator.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,13 @@ export class HTMLGenerator {
5050
if (traceElement.traceback !== undefined) {
5151
output += `<span class="traceback-text">${traceElement.traceback}</span>`;
5252
}
53-
return [traceElement.line, frameItems, objectItems, traceElement.filePath, output];
53+
return {
54+
lineNumber: traceElement.line,
55+
stackHTML: frameItems,
56+
heapHTML: objectItems,
57+
filename: traceElement.filePath,
58+
outputState: output
59+
};
5460
}
5561

5662
private objectItem(name: string, value: HeapValue): string {

src/programflow-visualization/frontend/visualization_panel.ts

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ export class VisualizationPanel {
109109
this._trace.push((new HTMLGenerator()).generateHTML(backendTraceElem));
110110
await this.postMessagesToWebview('updateButtons', 'updateContent');
111111
if (firstElement) {
112-
this.updateLineHighlight();
112+
await this.updateLineHighlight();
113113
}
114114
});
115115
this._tracePort?.on('close', async () => {
@@ -195,45 +195,58 @@ export class VisualizationPanel {
195195
}
196196

197197
private async updateLineHighlight(remove: boolean = false) {
198-
if (this._trace.length === 0) {
199-
this._outChannel.appendLine("updateLineHighlight: no trace available, aborting");
200-
return;
201-
}
202-
const traceFile = this._trace[this._traceIndex][3]!;
203-
let editor: vscode.TextEditor | undefined = vscode.window.visibleTextEditors.filter(
204-
editor => path.basename(editor.document.uri.path) === path.basename(traceFile)
205-
)[0];
198+
try {
199+
if (this._trace.length === 0) {
200+
this._outChannel.appendLine("updateLineHighlight: no trace available, aborting");
201+
return;
202+
}
203+
const traceFile = this._trace[this._traceIndex].filename;
204+
this._outChannel.appendLine(
205+
`updateLineHighlight: traceFile=${traceFile}, traceIndex=${this._traceIndex}, remove=${remove}`);
206206

207-
const openPath = vscode.Uri.parse(traceFile);
208-
if (!editor || editor.document.uri.path !== openPath.path) {
209-
// How can it be that editor is not null/undefined, but the path does not match?
210-
await vscode.commands.executeCommand('workbench.action.focusFirstEditorGroup');
211-
const document = await vscode.workspace.openTextDocument(openPath);
212-
editor = await vscode.window.showTextDocument(document);
213-
}
207+
// Use vscode.Uri.file() for proper file path to URI conversion
208+
const openPath = vscode.Uri.file(traceFile);
214209

215-
const traceLine = this._trace[this._traceIndex][0];
216-
if (remove) {
217-
this._outChannel.appendLine(
218-
"updateLineHighlight: removing highlighting in " + editor.document.fileName);
219-
editor.setDecorations(nextLineExecuteHighlightType, []);
220-
} else if (editor.document.lineCount < traceLine) {
221-
// How can it be that traceLine is out of range?
222-
this._outChannel.appendLine(
223-
"updateLineHighlight: removing highlighting in " + editor.document.fileName +
224-
"(out of range)");
225-
editor.setDecorations(nextLineExecuteHighlightType, []);
226-
} else {
227-
const hlLine = traceLine - 1; // why - 1
228-
if (hlLine > -1) {
210+
// Find editor by full normalized path, not just basename
211+
let editor: vscode.TextEditor | undefined = vscode.window.visibleTextEditors.find(
212+
editor => editor.document.uri.fsPath === openPath.fsPath
213+
);
214+
215+
if (!editor && remove) {
216+
return;
217+
} else if (!editor){
218+
this._outChannel.appendLine(`updateLineHighlight: editor not found, opening document: ${openPath.fsPath}`);
219+
await vscode.commands.executeCommand('workbench.action.focusFirstEditorGroup');
220+
const document = await vscode.workspace.openTextDocument(openPath);
221+
editor = await vscode.window.showTextDocument(document, { preserveFocus: false });
222+
// Give the editor time to fully initialize
223+
await new Promise(resolve => setTimeout(resolve, 100));
224+
if (!editor) {
225+
this._outChannel.appendLine(`updateLineHighlight: failed to get editor after opening document`);
226+
return;
227+
}
228+
}
229+
230+
const traceLine = this._trace[this._traceIndex].lineNumber;
231+
const lineNo = traceLine - 1; // zero-based indexing in vscode
232+
if (remove) {
233+
this._outChannel.appendLine(
234+
"updateLineHighlight: removing highlighting in " + editor.document.fileName);
235+
editor.setDecorations(nextLineExecuteHighlightType, []);
236+
} else if (lineNo < 0 || lineNo >= editor.document.lineCount) {
229237
this._outChannel.appendLine(
230-
"updateLineHighlight: highlighting line " + hlLine + " in " + editor.document.fileName);
231-
this.setEditorDecorations(editor, nextLineExecuteHighlightType, hlLine);
238+
"updateLineHighlight: traceLine " + traceLine + " out of range (doc has " +
239+
editor.document.lineCount + " lines) in " + editor.document.fileName);
240+
editor.setDecorations(nextLineExecuteHighlightType, []);
232241
} else {
233-
// how can this happen?
234242
this._outChannel.appendLine(
235-
"updateLineHighlight: cannot highlight line " + hlLine + " in " +
236-
editor.document.fileName + "(out of range)");
243+
"updateLineHighlight: highlighting line " + traceLine + " in " + editor.document.fileName);
244+
this.setEditorDecorations(editor, nextLineExecuteHighlightType, lineNo);
245+
}
246+
} catch (error) {
247+
this._outChannel.appendLine(`updateLineHighlight: ERROR - ${error}`);
248+
if (error instanceof Error) {
249+
this._outChannel.appendLine(`Stack: ${error.stack}`);
237250
}
238251
}
239252
}

src/programflow-visualization/types.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,15 @@ type Failure = { errorMessage: string };
77

88
// State Types for the Frontend
99
type FrontendTrace = Array<FrontendTraceElem>;
10-
// FIXME: what are this array elements? Why isn't there a type with named fields?
11-
type FrontendTraceElem = [number, string, string, string, string];
10+
11+
type FrontendTraceElem = {
12+
lineNumber: number, // 1-based
13+
stackHTML: string,
14+
heapHTML: string,
15+
filename: string,
16+
outputState: string,
17+
};
18+
1219
// ############################################################################################
1320
// State Types for the Backend
1421
type PartialBackendTrace = {

vscode-test/type-test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ class Point:
1212
x: int
1313
y: int
1414

15-
p = Point()
15+
p = Point(1, 2)
16+
print(p)

0 commit comments

Comments
 (0)