Skip to content

Commit 51e9e82

Browse files
committed
Support "Show Razor" commands in cohosting
1 parent 46b28d2 commit 51e9e82

File tree

5 files changed

+124
-4
lines changed

5 files changed

+124
-4
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,13 +1896,13 @@
18961896
"command": "extension.showRazorCSharpWindow",
18971897
"title": "%command.extension.showRazorCSharpWindow%",
18981898
"category": "Razor",
1899-
"enablement": "isWorkspaceTrusted && razor.mode == 'lsp'"
1899+
"enablement": "isWorkspaceTrusted"
19001900
},
19011901
{
19021902
"command": "extension.showRazorHtmlWindow",
19031903
"title": "%command.extension.showRazorHtmlWindow%",
19041904
"category": "Razor",
1905-
"enablement": "isWorkspaceTrusted && razor.mode == 'lsp'"
1905+
"enablement": "isWorkspaceTrusted"
19061906
},
19071907
{
19081908
"command": "razor.reportIssue",
@@ -5631,4 +5631,4 @@
56315631
}
56325632
}
56335633
}
5634-
}
5634+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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 { TextDocumentIdentifier } from 'vscode-languageserver-types';
7+
import { GeneratedDocumentKind } from './generatedDocumentKind';
8+
9+
export class DocumentContentsRequest {
10+
constructor(public readonly textDocument: TextDocumentIdentifier, public readonly kind: GeneratedDocumentKind) {}
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
export enum GeneratedDocumentKind {
7+
CSharp,
8+
Html,
9+
Formatting,
10+
}

src/lsptoolshost/razor/razorEndpoints.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import { ReportIssueCommand } from '../../razor/src/diagnostics/reportIssueComma
5353
import { HtmlDocument } from './htmlDocument';
5454
import { HtmlForwardedRequest } from './htmlForwardedRequest';
5555
import { BlazorDebugConfigurationProvider } from '../../razor/src/blazorDebug/blazorDebugConfigurationProvider';
56+
import { ShowGeneratedDocumentCommand } from './showGeneratedDocumentCommand';
5657

5758
export function registerRazorEndpoints(
5859
context: vscode.ExtensionContext,
@@ -70,6 +71,7 @@ export function registerRazorEndpoints(
7071
registerCohostingEndpoints();
7172

7273
context.subscriptions.push(BlazorDebugConfigurationProvider.register(razorLogger, vscode));
74+
context.subscriptions.push(ShowGeneratedDocumentCommand.register(roslynLanguageServer));
7375
} else {
7476
vscode.commands.executeCommand('setContext', 'razor.mode', 'lsp');
7577
registerNonCohostingEndpoints();
@@ -82,7 +84,13 @@ export function registerRazorEndpoints(
8284
//
8385
function registerCohostingEndpoints() {
8486
const documentManager = new HtmlDocumentManager(platformInfo, roslynLanguageServer, razorLogger);
85-
const reportIssueCommand = new ReportIssueCommand(vscode, undefined, documentManager, razorLogger);
87+
const reportIssueCommand = new ReportIssueCommand(
88+
vscode,
89+
undefined,
90+
documentManager,
91+
roslynLanguageServer,
92+
razorLogger
93+
);
8694
context.subscriptions.push(documentManager.register());
8795
context.subscriptions.push(reportIssueCommand.register());
8896

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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, '&lt;').replace(/</g, '&gt;')}</pre>
88+
</body>
89+
</html>`;
90+
}
91+
}

0 commit comments

Comments
 (0)