|
| 1 | +/*--------------------------------------------------------- |
| 2 | + * Copyright (C) Microsoft Corporation. All rights reserved. |
| 3 | + *--------------------------------------------------------*/ |
| 4 | + |
| 5 | +import * as vscode from 'vscode'; |
| 6 | +import { bundlePage } from '../bundlePage'; |
| 7 | +import { ProfileAnnotations } from '../profileAnnotations'; |
| 8 | +import { ProfileCodeLensProvider } from '../profileCodeLensProvider'; |
| 9 | +import { ReadonlyCustomDocument } from '../readonly-custom-document'; |
| 10 | +import { reopenWithEditor } from '../reopenWithEditor'; |
| 11 | +import { buildModel, IProfileModel } from './model'; |
| 12 | +import { IHeapProfileRaw, Message } from './types'; |
| 13 | + |
| 14 | +export class HeapProfileEditorProvider |
| 15 | + implements vscode.CustomEditorProvider<ReadonlyCustomDocument<IProfileModel>> |
| 16 | +{ |
| 17 | + public readonly onDidChangeCustomDocument = new vscode.EventEmitter<never>().event; |
| 18 | + |
| 19 | + constructor( |
| 20 | + private readonly lens: ProfileCodeLensProvider, |
| 21 | + private readonly bundle: vscode.Uri, |
| 22 | + private readonly extraConsts: Record<string, unknown> = {}, |
| 23 | + ) {} |
| 24 | + |
| 25 | + /** |
| 26 | + * @inheritdoc |
| 27 | + */ |
| 28 | + async openCustomDocument(uri: vscode.Uri) { |
| 29 | + const content = await vscode.workspace.fs.readFile(uri); |
| 30 | + const raw: IHeapProfileRaw = JSON.parse(new TextDecoder().decode(content)); |
| 31 | + const document = new ReadonlyCustomDocument(uri, buildModel(raw)); |
| 32 | + |
| 33 | + const annotations = new ProfileAnnotations(); |
| 34 | + // const rootPath = document.userData.rootPath; |
| 35 | + // for (const location of document.userData.locations) { |
| 36 | + // annotations.add(rootPath, location); |
| 37 | + // } |
| 38 | + |
| 39 | + // this.lens.registerLenses(annotations); |
| 40 | + return document; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @inheritdoc |
| 45 | + */ |
| 46 | + public async resolveCustomEditor( |
| 47 | + document: ReadonlyCustomDocument<IProfileModel>, |
| 48 | + webviewPanel: vscode.WebviewPanel, |
| 49 | + ): Promise<void> { |
| 50 | + webviewPanel.webview.onDidReceiveMessage((message: Message) => { |
| 51 | + switch (message.type) { |
| 52 | + case 'openDocument': |
| 53 | + // openLocation({ |
| 54 | + // rootPath: document.userData?.rootPath, |
| 55 | + // viewColumn: message.toSide ? vscode.ViewColumn.Beside : vscode.ViewColumn.Active, |
| 56 | + // callFrame: message.callFrame, |
| 57 | + // location: message.location, |
| 58 | + // }); |
| 59 | + return; |
| 60 | + case 'reopenWith': |
| 61 | + reopenWithEditor(document.uri, message.viewType, message.requireExtension); |
| 62 | + return; |
| 63 | + default: |
| 64 | + console.warn(`Unknown request from webview: ${JSON.stringify(message)}`); |
| 65 | + } |
| 66 | + }); |
| 67 | + |
| 68 | + webviewPanel.webview.options = { enableScripts: true }; |
| 69 | + webviewPanel.webview.html = await bundlePage(webviewPanel.webview.asWebviewUri(this.bundle), { |
| 70 | + MODEL: document.userData, |
| 71 | + ...this.extraConsts, |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @inheritdoc |
| 77 | + */ |
| 78 | + public async saveCustomDocument() { |
| 79 | + // no-op |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @inheritdoc |
| 84 | + */ |
| 85 | + public async revertCustomDocument() { |
| 86 | + // no-op |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @inheritdoc |
| 91 | + */ |
| 92 | + public async backupCustomDocument() { |
| 93 | + return { id: '', delete: () => undefined }; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @inheritdoc |
| 98 | + */ |
| 99 | + public saveCustomDocumentAs( |
| 100 | + document: ReadonlyCustomDocument<IProfileModel>, |
| 101 | + destination: vscode.Uri, |
| 102 | + ) { |
| 103 | + return vscode.workspace.fs.copy(document.uri, destination, { overwrite: true }); |
| 104 | + } |
| 105 | +} |
0 commit comments