Skip to content

Commit f5818d3

Browse files
authored
Bump Razor and support "Show Razor" commands in cohosting (#8639)
2 parents ce524fa + 6a897a9 commit f5818d3

File tree

10 files changed

+153
-13
lines changed

10 files changed

+153
-13
lines changed

CHANGELOG.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
* Allow clients to send range ending at the line after the last line in the document(PR: [#80310](https://github.com/dotnet/roslyn/pull/80310))
1313
* Don't show Razor diagnostics in Full Solution Analysis(PR: [#80296](https://github.com/dotnet/roslyn/pull/80296))
1414
* Log project context in which document was found(PR: [#80202](https://github.com/dotnet/roslyn/pull/80202))
15-
* Bump Razor to 10.0.0-preview.25469.5 (PR: [#8642](https://github.com/dotnet/vscode-csharp/pull/8642))
16-
* Support view components in Go To Def (PR: [#12222](https://github.com/dotnet/razor/pull/12222))
17-
* Redirect the older named assembly too (PR: [#12239](https://github.com/dotnet/razor/pull/12239))
15+
* Bump Razor to 10.0.0-preview.25472.6 (PR: [#8639](https://github.com/dotnet/vscode-csharp/pull/8639))
16+
* Support view components in Go To Def (PR: [#12222](https://github.com/dotnet/razor/pull/12222))
17+
* Redirect the older named assembly too (PR: [#12239](https://github.com/dotnet/razor/pull/12239))
18+
* Restore "Show Html/C#" VS Code commands (PR: [#12234](https://github.com/dotnet/razor/pull/12234))
1819

1920
# 2.92.x
2021
* Bump Razor to 10.0.0-preview.25464.2 (PR: [#8628](https://github.com/dotnet/vscode-csharp/pull/8628))

l10n/bundle.l10n.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
"Non Razor file as active document": "Non Razor file as active document",
110110
"Could not determine CSharp content": "Could not determine CSharp content",
111111
"Could not determine Html content": "Could not determine Html content",
112-
"Cohosting is on, client has no access to CSharp content": "Cohosting is on, client has no access to CSharp content",
112+
"Could not determine CSharp content: {0}": "Could not determine CSharp content: {0}",
113113
"A valid dotnet installation could not be found: {0}": "A valid dotnet installation could not be found: {0}",
114114
"Is this a Bug or Feature request?": "Is this a Bug or Feature request?",
115115
"Bug": "Bug",

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"defaults": {
4343
"roslyn": "5.0.0-2.25472.11",
4444
"omniSharp": "1.39.14",
45-
"razor": "10.0.0-preview.25469.5",
45+
"razor": "10.0.0-preview.25472.6",
4646
"razorOmnisharp": "7.0.0-preview.23363.1",
4747
"xamlTools": "17.14.36106.43"
4848
},
@@ -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",
@@ -5632,4 +5632,4 @@
56325632
}
56335633
}
56345634
}
5635-
}
5635+
}
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+
}

src/razor/src/diagnostics/reportIssueCommand.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import { HtmlDocumentManager } from '../../../lsptoolshost/razor/htmlDocumentManager';
7+
import { RoslynLanguageServer } from '../../../lsptoolshost/server/roslynLanguageServer';
78
import { RazorDocumentManager } from '../document/razorDocumentManager';
89
import { RazorLogger } from '../razorLogger';
910
import { api } from '../vscodeAdapter';
@@ -21,10 +22,16 @@ export class ReportIssueCommand {
2122
private readonly vscodeApi: api,
2223
documentManager: RazorDocumentManager | undefined,
2324
cohostingDocumentManager: HtmlDocumentManager | undefined,
25+
roslynLanguageServer: RoslynLanguageServer | undefined,
2426
logger: RazorLogger
2527
) {
2628
this.dataCollectorFactory = new ReportIssueDataCollectorFactory(logger);
27-
this.issueCreator = new ReportIssueCreator(this.vscodeApi, documentManager, cohostingDocumentManager);
29+
this.issueCreator = new ReportIssueCreator(
30+
this.vscodeApi,
31+
documentManager,
32+
cohostingDocumentManager,
33+
roslynLanguageServer
34+
);
2835
this.issuePanel = new ReportIssuePanel(this.dataCollectorFactory, this.issueCreator, logger);
2936
}
3037

src/razor/src/diagnostics/reportIssueCreator.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@ import { IRazorDocumentManager } from '../document/IRazorDocumentManager';
1212
import { razorExtensionId } from '../razorExtensionId';
1313
import { IReportIssueDataCollectionResult } from './IReportIssueDataCollectionResult';
1414
import { HtmlDocumentManager } from '../../../lsptoolshost/razor/htmlDocumentManager';
15+
import { ShowGeneratedDocumentCommand } from '../../../lsptoolshost/razor/showGeneratedDocumentCommand';
16+
import { GeneratedDocumentKind } from '../../../lsptoolshost/razor/generatedDocumentKind';
17+
import { RoslynLanguageServer } from '../../../lsptoolshost/server/roslynLanguageServer';
1518

1619
export class ReportIssueCreator {
1720
constructor(
1821
private readonly vscodeApi: vscodeAdapter.api,
1922
private readonly documentManager?: IRazorDocumentManager,
20-
private readonly cohostingDocumentManager?: HtmlDocumentManager
23+
private readonly cohostingDocumentManager?: HtmlDocumentManager,
24+
private readonly roslynLanguageServer?: RoslynLanguageServer
2125
) {}
2226

2327
public async create(collectionResult: IReportIssueDataCollectionResult) {
@@ -37,7 +41,15 @@ export class ReportIssueCreator {
3741
csharpContent = await this.getProjectedCSharp(razorDocument);
3842
htmlContent = await this.getProjectedHtml(razorDocument);
3943
} else if (this.cohostingDocumentManager) {
40-
csharpContent = vscode.l10n.t('Cohosting is on, client has no access to CSharp content');
44+
try {
45+
csharpContent = await ShowGeneratedDocumentCommand.getGeneratedDocumentContent(
46+
collectionResult.document.uri,
47+
GeneratedDocumentKind.CSharp,
48+
this.roslynLanguageServer!
49+
);
50+
} catch (e: any) {
51+
csharpContent = vscode.l10n.t('Could not determine CSharp content: {0}', e.toString());
52+
}
4153

4254
const htmlDocument = await this.cohostingDocumentManager.getDocument(collectionResult.document.uri);
4355
if (htmlDocument) {

src/razor/src/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export async function activate(
139139
const csharpFeature = new RazorCSharpFeature(documentManager, eventEmitterFactory, logger);
140140
const htmlFeature = new RazorHtmlFeature(documentManager, languageServiceClient, eventEmitterFactory, logger);
141141
const localRegistrations: vscode.Disposable[] = [];
142-
const reportIssueCommand = new ReportIssueCommand(vscodeType, documentManager, undefined, logger);
142+
const reportIssueCommand = new ReportIssueCommand(vscodeType, documentManager, undefined, undefined, logger);
143143
const razorCodeActionRunner = new RazorCodeActionRunner(languageServerClient, logger);
144144
const codeActionsHandler = new CodeActionsHandler(
145145
documentManager,

0 commit comments

Comments
 (0)