|
| 1 | +import * as vscode from "vscode"; |
| 2 | +import { WebviewViewProvider } from "vscode"; |
| 3 | +import { getHtmlForWebview } from "../../common/vscode/webview-html"; |
| 4 | +import { FromMethodModelingMessage } from "../../common/interface-types"; |
| 5 | +import { telemetryListener } from "../../common/vscode/telemetry"; |
| 6 | +import { showAndLogExceptionWithTelemetry } from "../../common/logging/notifications"; |
| 7 | +import { extLogger } from "../../common/logging/vscode/loggers"; |
| 8 | +import { redactableError } from "../../common/errors"; |
| 9 | + |
| 10 | +export class MethodModelingViewProvider implements WebviewViewProvider { |
| 11 | + public static readonly viewType = "codeQLMethodModeling"; |
| 12 | + |
| 13 | + constructor(private readonly context: vscode.ExtensionContext) {} |
| 14 | + |
| 15 | + /** |
| 16 | + * This is called when a view first becomes visible. This may happen when the view is |
| 17 | + * first loaded or when the user hides and then shows a view again. |
| 18 | + */ |
| 19 | + public resolveWebviewView( |
| 20 | + webviewView: vscode.WebviewView, |
| 21 | + _context: vscode.WebviewViewResolveContext, |
| 22 | + _token: vscode.CancellationToken, |
| 23 | + ) { |
| 24 | + webviewView.webview.options = { |
| 25 | + enableScripts: true, |
| 26 | + localResourceRoots: [this.context.extensionUri], |
| 27 | + }; |
| 28 | + |
| 29 | + const html = getHtmlForWebview( |
| 30 | + this.context, |
| 31 | + webviewView.webview, |
| 32 | + "method-modeling", |
| 33 | + { |
| 34 | + allowInlineStyles: true, |
| 35 | + allowWasmEval: false, |
| 36 | + }, |
| 37 | + ); |
| 38 | + |
| 39 | + webviewView.webview.html = html; |
| 40 | + |
| 41 | + webviewView.webview.onDidReceiveMessage(async (msg) => this.onMessage(msg)); |
| 42 | + } |
| 43 | + |
| 44 | + private async onMessage(msg: FromMethodModelingMessage): Promise<void> { |
| 45 | + switch (msg.t) { |
| 46 | + case "telemetry": { |
| 47 | + telemetryListener?.sendUIInteraction(msg.action); |
| 48 | + break; |
| 49 | + } |
| 50 | + case "unhandledError": |
| 51 | + void showAndLogExceptionWithTelemetry( |
| 52 | + extLogger, |
| 53 | + telemetryListener, |
| 54 | + redactableError( |
| 55 | + msg.error, |
| 56 | + )`Unhandled error in method modeling view: ${msg.error.message}`, |
| 57 | + ); |
| 58 | + break; |
| 59 | + } |
| 60 | + } |
| 61 | +} |
0 commit comments