|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as vscode from 'vscode'; |
| 7 | +import { RazorLanguage } from '../../razor/src/razorLanguage'; |
| 8 | +import { getUriPath } from '../../razor/src/uriPaths'; |
| 9 | +import { RoslynLanguageServer } from '../server/roslynLanguageServer'; |
| 10 | +import { DocumentContentsRequest } from './documentContentsRequest'; |
| 11 | +import { CancellationToken, RequestType, TextDocumentIdentifier } from 'vscode-languageserver-protocol'; |
| 12 | +import { UriConverter } from '../utils/uriConverter'; |
| 13 | +import { GeneratedDocumentKind } from './generatedDocumentKind'; |
| 14 | + |
| 15 | +export class ShowGeneratedDocumentCommand { |
| 16 | + private static requestType = new RequestType<DocumentContentsRequest, string, void>( |
| 17 | + 'razor/generatedDocumentContents' |
| 18 | + ); |
| 19 | + |
| 20 | + public static register(roslynLanguageServer: RoslynLanguageServer) { |
| 21 | + return vscode.Disposable.from( |
| 22 | + vscode.commands.registerCommand('extension.showRazorCSharpWindow', async () => |
| 23 | + this.show(roslynLanguageServer, GeneratedDocumentKind.CSharp, '.g.cs') |
| 24 | + ), |
| 25 | + vscode.commands.registerCommand('extension.showRazorHtmlWindow', async () => |
| 26 | + this.show(roslynLanguageServer, GeneratedDocumentKind.Html, '.g.html') |
| 27 | + ) |
| 28 | + ); |
| 29 | + } |
| 30 | + |
| 31 | + private static async show( |
| 32 | + roslynLanguageServer: RoslynLanguageServer, |
| 33 | + kind: GeneratedDocumentKind, |
| 34 | + extension: string |
| 35 | + ) { |
| 36 | + const uri = await this.getActiveDocumentUri(); |
| 37 | + if (!uri) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + const title = `${getUriPath(uri)}${extension}`; |
| 42 | + const panel = vscode.window.createWebviewPanel('razorGeneratedDocument', title, vscode.ViewColumn.Two, { |
| 43 | + enableScripts: false, |
| 44 | + // Disallow any remote sources |
| 45 | + localResourceRoots: [], |
| 46 | + }); |
| 47 | + |
| 48 | + const content = await this.getGeneratedDocumentContent(uri, kind, roslynLanguageServer); |
| 49 | + |
| 50 | + panel.webview.html = await this.getWebViewContent(content, title); |
| 51 | + } |
| 52 | + |
| 53 | + private static async getActiveDocumentUri() { |
| 54 | + if (!vscode.window.activeTextEditor) { |
| 55 | + return null; |
| 56 | + } |
| 57 | + |
| 58 | + if (vscode.window.activeTextEditor.document.languageId !== RazorLanguage.id) { |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + return vscode.window.activeTextEditor.document.uri; |
| 63 | + } |
| 64 | + |
| 65 | + public static async getGeneratedDocumentContent( |
| 66 | + uri: vscode.Uri, |
| 67 | + kind: GeneratedDocumentKind, |
| 68 | + roslynLanguageServer: RoslynLanguageServer |
| 69 | + ) { |
| 70 | + return roslynLanguageServer.sendRequest( |
| 71 | + ShowGeneratedDocumentCommand.requestType, |
| 72 | + new DocumentContentsRequest(TextDocumentIdentifier.create(UriConverter.serialize(uri)), kind), |
| 73 | + CancellationToken.None |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + private static async getWebViewContent(content: string, title: string) { |
| 78 | + return `<!DOCTYPE html> |
| 79 | +<html lang="en"> |
| 80 | +<head> |
| 81 | + <meta charset="UTF-8"> |
| 82 | + <meta http-equiv="Content-Security-Policy"> |
| 83 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 84 | + <title>${title}</title> |
| 85 | +</head> |
| 86 | +<body> |
| 87 | + <pre>${content.replace(/</g, '<').replace(/</g, '>')}</pre> |
| 88 | +</body> |
| 89 | +</html>`; |
| 90 | + } |
| 91 | +} |
0 commit comments